Skip to content

Commit

Permalink
Ball avoid empty cells
Browse files Browse the repository at this point in the history
  • Loading branch information
ajlopez committed Sep 16, 2012
1 parent c32713e commit a557fe8
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
3 changes: 3 additions & 0 deletions lib/gameserver.js
Expand Up @@ -37,6 +37,9 @@ var gameserver = (function() {
}

World.prototype.getCell = function(x, y) {
if (!this.cells[x])
return null;

return this.cells[x][y];
}

Expand Down
12 changes: 7 additions & 5 deletions samples/simplegame/html/index.html
Expand Up @@ -35,13 +35,15 @@ <h1>Simple Game</h1>

var newx = this.x + this.dx;
var newy = this.y + this.dy;

if (newx < 0 || newx > width - 1) {

var cell = world.getCell(Math.floor(newx), Math.floor(newy));

if (newx < 0 || newx > width || cell == null || cell.base != 'green_cell') {
this.dx = -this.dx;
newx = this.x + this.dx;
}

if (newy < 0 || newy > height - 1) {
if (newy < 0 || newy > height || cell == null || cell.base != 'green_cell') {
this.dy = -this.dy;
newy = this.y + this.dy;
}
Expand Down Expand Up @@ -181,7 +183,7 @@ <h1>Simple Game</h1>
var nballs = 20;

for (var k = 0; k < nballs; k++)
balls.push(new Ball(Math.floor(Math.random() * world.width), Math.floor(Math.random() * world.height), (k%2) ? 0.05 : 0, (k%2) ? 0 : 0.05));
balls.push(new Ball(Math.floor(Math.random() * world.width) + 0.5, Math.floor(Math.random() * world.height) + 0.5, (k%2) ? 0.05 : 0, (k%2) ? 0 : 0.05));

drawScene();

Expand Down Expand Up @@ -240,7 +242,7 @@ <h1>Simple Game</h1>
var ball = balls[n];
var ballposition = isoworld.getCellCenterPosition(ball.x, ball.y);
var posx = canvaswidth/2 + ballposition.x - centerposition.x;
var posy = canvasheight/2 + ballposition.y - centerposition.y;
var posy = canvasheight/2 + ballposition.y - centerposition.y - cellheight/2;
context.arc(posx, posy-10, 10, 0, Math.PI*2, true);
}

Expand Down

0 comments on commit a557fe8

Please sign in to comment.