Skip to content

Commit

Permalink
Merge pull request #1 from Squarific/patch-3
Browse files Browse the repository at this point in the history
Patch 3
  • Loading branch information
Filip Smets authored and Filip Smets committed Jan 24, 2013
2 parents 31a4765 + fb169e7 commit 9210919
Show file tree
Hide file tree
Showing 5 changed files with 207 additions and 17 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ Corner clipping isn't supported, so being too close to a node will lead to its r

The way Javascript handles floating point numbers makes it difficult to have exact integers, something that the collision system relies on. This was solved by "rounding" the numbers when close to an integer, but it's not a very elegant solution.

Having no 3D acceleration the rendering speed relies on the computer's speed, as jumping is dependant on the rendering speed it tends to be extremely fast on powerful computers and slow otherwise. This was necessary to guarantee jumping height so that one could jump up one block.

Chrome has some trouble rendering in plain color, specifically it's the `context.fill()` function that doesn't work when called too much it seems.

Firefox doesn't let the browser lock the pointer unless it's in full screen mode, this program should never run in full screen, unless on a super computer.
Expand Down
142 changes: 142 additions & 0 deletions js/TouchControl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
SQUARIFIC = {framework: {}};
SQUARIFIC.framework.TouchControl = function (elem, settings) {
/* Settings:
{
clickStyle: cssStyleDeclaration, //Style during the touching of the joystick
pretendArrowKeys: boolean, //Should it simulate keypresses of the arrows
}
*/
"use strict";
var callbackList = [],
self = this,
originalStyle = elem.style,
originalX = 0,
originalY = 0,
fakeKeyspressed = [],
multiple = 45,
angleKeys = [
{angle: 0, keyCodes: [39]},
{angle: 45, keyCodes: [39, 40]},
{angle: 90, keyCodes: [40]},
{angle: 135, keyCodes: [40, 37]},
{angle: 180, keyCodes: [37]},
{angle: -180, keyCodes: [37]},
{angle: -135, keyCodes: [37, 38]},
{angle: -90, keyCodes: [38]},
{angle: -45, keyCodes: [38, 39]}
]; //Angle is in degrees and should be a multiple of the var "multiple",x-axis to the right = 0 left = 180, y-axis down = 90 up = -90, one angle can occur multiple times, e.g. {angle: 30, keyCodes: [1]}, {angle: 30, keyCodes: [2]} will fire 1 and 2, {angle: 30, keyCodes: [1, 1]} will fire 1 twice, -180 and 180 should both be added;
if (!settings) {
settings = {};
}
if (!elem) {
throw "Joystick Control: No element provided! Provided:" + elem;
}
settings.pretendArrowKeys = true; //Remove once non-pretend is implemented
if (!elem.style.position) {
elem.style.position = "relative";
}
this.on = function (name, callback) {
callbackList.sort(function (a, b) {return a.id - b.id;}); //To get a unique id we need the highest id last
if (callbackList.length < 1) {
var next = 0;
} else {
var next = callbackList[callbackList.length - 1].id + 1;
}
callbackList.push({id: next, name: name, cb: callback});
return next;
};
this.removeOn = function (id) {
var i;
for (i = 0; i < callbackList.length; i++) {
if (callbackList[i].id === id) {
callbackList.splice(id);
return true;
}
}
return false;
};
this.cb = function (name, arg) {
var i;
for (i = 0; i < callbackList.length; i++) {
if (callbackList[i].name === name && typeof callbackList[i].cb == "function") {
callbackList[i].cb(arg);
}
}
};
this.removeNonFakedKeys = function (keys) {
for (var i = 0; i < fakeKeyspressed.length; i++) {
if (!self.inArray(fakeKeyspressed[i], keys)) {
self.cb("pretendKeyup", {keyCode: fakeKeyspressed[i]});
}
}
};
this.inArray = function (el, arr) {
if (!arr) {
return false;
}
for (var i = 0; i < arr.length; i++) {
if (arr[i] === el) {
return true;
}
}
return false;
};
this.handleTouchStart = function (event) {
if (event.changedTouches[0].target == elem) {
originalStyle = elem.style;
if (!originalStyle.top) {
originalStyle.top = "";
}
if (!originalStyle.left) {
originalStyle.left = "";
}
if (settings.clickStyle) {
elem.style = settings.clickStyle;
}
originalX = event.changedTouches[0].clientX;
originalY = event.changedTouches[0].clientY;
elem.style.left = event.changedTouches[0].clientX - event.changedTouches[0].target.style.width.slice(0, -2) / 2 + "px";
elem.style.top = event.changedTouches[0].clientY - event.changedTouches[0].target.style.height.slice(0, -2) / 2 + "px";
event.preventDefault();
}
};
this.handleTouchStop = function (event) {
if (event.changedTouches[0].target == elem) {
elem.style = originalStyle;
self.removeNonFakedKeys();
event.preventDefault();
}
};
this.handleTouchMove = function (event) {
if (event.changedTouches[0].target == elem) {
var i, k, keys = [], angle,
deltaX = event.changedTouches[0].clientX - originalX,
deltaY = event.changedTouches[0].clientY - originalY;
elem.style.left = event.changedTouches[0].clientX - event.changedTouches[0].target.style.width.slice(0, -2) / 2 + "px";
elem.style.top = event.changedTouches[0].clientY - event.changedTouches[0].target.style.height.slice(0, -2) / 2 + "px";
event.preventDefault();
if (settings.pretendArrowKeys) {
angle = multiple * Math.round((Math.atan2(deltaY, deltaX) * 180 / Math.PI) / multiple);
for (i = 0; i < angleKeys.length; i++) {
if (angleKeys[i].angle === angle) {
for (k = 0; k < angleKeys[i].keyCodes.length; k++) {
keys.push(angleKeys[i].keyCodes[k]);
}
}
}
for (i = 0; i < keys.length; i++) {
if (!self.inArray(keys[i], fakeKeyspressed)) {
fakeKeyspressed.push(keys[i]);
}
self.cb("pretendKeydown", {keyCode: keys[i]});
}
self.removeNonFakedKeys(keys);
} else {
//Planned for later
}
}
};
elem.addEventListener("touchstart", self.handleTouchStart);
elem.addEventListener("touchend", self.handleTouchStop);
elem.addEventListener("touchmove", self.handleTouchMove);
};
44 changes: 34 additions & 10 deletions js/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,13 @@ function Player(world)
this.size = 0.3;
this.speed = 5;
this.rSpeed = 2.5;
this.fallSpeed = 0.1;
this.jumpSpeed = 0.6;
this.velocity = 0;
this.fallSpeed = 8;
this.jumpSpeed = 8;
this.acceleration = 21;
this.gravity = true;
this.collision = true;
this.firstUpdate = true;
this.lastUpdate = new Date().getTime();
this.rotationMatrix = [];
this.keys = {};
Expand All @@ -51,6 +54,17 @@ function Player(world)
{
player.onKeyEvent(event.keyCode, false);
}
this.joystick = new SQUARIFIC.framework.TouchControl(document.getElementById("joystick"), {pretendArrowKeys: true});
this.joystick.on("pretendKeydown",
function (event) {
player.onKeyEvent(event.keyCode, true);
}
);
this.joystick.on("pretendKeyup"
function (event) {
player.onKeyEvent(event.keyCode, false);
}
);

this.spawn();
}
Expand Down Expand Up @@ -130,6 +144,7 @@ Player.prototype.update = function()
if(!this.gravity)
{
this.delta.y = 0;
this.velocity = 0;
}

// get player movement deltas with key input
Expand All @@ -156,7 +171,7 @@ Player.prototype.update = function()
// [space]
if(this.keys[32] && this.gravity && !this.delta.y)
{
this.delta.y += this.jumpSpeed;
this.velocity = this.jumpSpeed;
}
// [pg up]
if(this.keys[33] && !this.gravity)
Expand All @@ -170,20 +185,27 @@ Player.prototype.update = function()
}

// gravity and terminal velocity
if(this.gravity && this.delta.y > -2)
if(this.gravity && this.velocity > -this.fallSpeed)
{
this.delta.y -= this.fallSpeed;
this.velocity -= this.acceleration * this.elapsed;
}
else if(this.gravity)
if(this.gravity && this.velocity < -this.fallSpeed)
{
this.delta.y = -2;
this.velocity = -this.acceleration;
}

this.delta.y = this.velocity * this.elapsed;

if (this.firstUpdate) {
// collision detection doesn't seem to work on the first update
this.delta.y = 0;
this.firstUpdate = false;
}

if(this.collision)
{
this.collisionDetection();
}

// apply movement
this.position.x += this.delta.x;
this.position.y += this.delta.y;
Expand Down Expand Up @@ -287,13 +309,15 @@ Player.prototype.collisionDetection = function()
if(this.position.y+this.height+0.2+this.delta.y >= node.y && this.position.y < node.y)
{
this.delta.y = -0.01;
this.velocity = 0;
this.position.y = node.y-this.height-0.2;
}

// down on the floor
if(this.position.y+this.delta.y <= node.y+1)
{
this.delta.y = 0;
this.velocity = 0;
this.position.y = node.y+1;
}
}
Expand All @@ -316,4 +340,4 @@ Player.prototype.nodeCollision = function(node)
return true;
}
return false;
}
}
19 changes: 15 additions & 4 deletions play.htm
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@
<script type="text/javascript" src="js/node.js"></script>
<script type="text/javascript" src="js/world.js"></script>
<script type="text/javascript" src="js/render.js"></script>
<script type="text/javascript" src="js/TouchControl.js"></script>
<script type="text/javascript">
toggleJoystick = function (elem) {
if (elem.style.display === "none") {
elem.style.display = "block";
} else {
elem.style.display = "none;
}
}
</script>

</head>
<body>
Expand All @@ -38,6 +48,7 @@ <h2>Options</h2>
<li><u>P</u>erformace graph: <input type="checkbox" onchange="renderer.graph = this.checked;" accesskey="p"></li>
<li><u>M</u>ap: <input type="checkbox" onchange="renderer.map = this.checked;" accesskey="m"></li>
<li><input type="button" value="Lock pointer" onclick="renderer.lockPointer();"></li>
<li><input type="button" value="Joystick" onclick="toggleJoystick(document.getElementById('joystick'))"></li>
<li>Focal length:<br>
<input type="range" min="100" max="1000" value="500" onchange="renderer.focalLength = this.value">
</li>
Expand All @@ -46,8 +57,8 @@ <h2>Options</h2>
</li>
<li><u>R</u>ender mode:<br>
<select onchange="renderer.renderMode = this.value;" accesskey="r">
<option value="0" selected>Plain color</option>
<option value="1">Textured</option>
<option value="0">Plain color</option>
<option value="1" selected>Textured</option>
</select>
</li>
<li><u>T</u>ype:<br>
Expand Down Expand Up @@ -129,7 +140,7 @@ <h2>Options</h2>
</div>

<canvas id="canvas" tabindex="1"></canvas>

<joystick id="joystick"></joystick>
<script type="text/javascript">

var canvas = document.getElementById("canvas");
Expand All @@ -142,4 +153,4 @@ <h2>Options</h2>
</script>

</body>
</html>
</html>
17 changes: 16 additions & 1 deletion style/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,21 @@ canvas
background: -webkit-gradient(linear, 0% 0%, 0% 100%, from(#7BACFF), to(#ffffff));
}

joystick
{
display:block;
height: 50px;
width: 50px;
border-radius: 50%;
}

#joystick
{
position: fixed;
left: 100px;
bottom: 100px;
}

#controls
{
position: absolute;
Expand All @@ -68,4 +83,4 @@ form
ul
{
padding-left: 2em;
}
}

0 comments on commit 9210919

Please sign in to comment.