Skip to content

Commit

Permalink
fixed errors when wall crawlers and wall avoiders weren't near any walls
Browse files Browse the repository at this point in the history
  • Loading branch information
Evan Wallace committed Jan 10, 2011
1 parent d310509 commit 72be85a
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 21 deletions.
2 changes: 2 additions & 0 deletions closure_compiler/rapt.externs.js
Expand Up @@ -5,4 +5,6 @@ Player.prototype.leftKey;
Player.prototype.rightKey;
Player.prototype.crouchKey;

function GameState() {}

GameState.prototype.killKey;
8 changes: 5 additions & 3 deletions game/src/entities/wallavoider.js
Expand Up @@ -46,9 +46,11 @@ WallAvoider.prototype.move = function(seconds) {
return this.accelerate(new Vector(0, 0), seconds);
}
this.acceleration = targetDelta.unit();
var closestPointDelta = ref_worldPoint.ref.sub(this.getCenter());
var wallAvoidance = closestPointDelta.mul(-1 / (closestPointDist * closestPointDist));
this.acceleration.inplaceAdd(wallAvoidance);
if (closestPointDist < Number.POSITIVE_INFINITY) {
var closestPointDelta = ref_worldPoint.ref.sub(this.getCenter());
var wallAvoidance = closestPointDelta.mul(-1 / (closestPointDist * closestPointDist));
this.acceleration.inplaceAdd(wallAvoidance);
}
this.acceleration.normalize();
this.acceleration.inplaceMul(WALL_AVOIDER_ACCEL);

Expand Down
39 changes: 21 additions & 18 deletions game/src/entities/wallcrawler.js
Expand Up @@ -39,27 +39,30 @@ function WallCrawler(center, direction) {
WallCrawler.prototype.move = function(seconds) {
var ref_shapePoint = {};
var ref_worldPoint = {};
CollisionDetector.closestToEntityWorld(this, 2, ref_shapePoint, ref_worldPoint, gameState.world);
var closestPointDist = CollisionDetector.closestToEntityWorld(this, 2, ref_shapePoint, ref_worldPoint, gameState.world);

var delta = this.getCenter().sub(ref_worldPoint.ref);
// Make sure it doesn't get too far away or get stuck in corners
var flip = delta.flip();
if (closestPointDist < Number.POSITIVE_INFINITY) {
var delta = this.getCenter().sub(ref_worldPoint.ref);
// Make sure it doesn't get too far away or get stuck in corners
var flip = delta.flip();

if (this.firstTick) {
if (this.velocity.dot(flip) < 0) this.clockwise = true;
else this.clockwise = false;
this.firstTick = false;
if (this.firstTick) {
if (this.velocity.dot(flip) < 0) this.clockwise = true;
else this.clockwise = false;
this.firstTick = false;
}
if (delta.lengthSquared() > (WALL_CRAWLER_RADIUS * WALL_CRAWLER_RADIUS * 1.1)) {
// Pull the crawler towards the wall
if (this.clockwise) this.velocity = flip.mul(-1).sub(delta.mul(PULL_FACTOR));
else this.velocity = flip.sub(delta.mul(PULL_FACTOR));
} else {
// Push the crawler away from the wall
if (this.clockwise) this.velocity = flip.mul(-1).add(delta.mul(PUSH_FACTOR));
else this.velocity = flip.add(delta.mul(PUSH_FACTOR));
}
this.velocity.normalize();
}
if (delta.lengthSquared() > (WALL_CRAWLER_RADIUS * WALL_CRAWLER_RADIUS * 1.1)) {
// Pull the crawler towards the wall
if (this.clockwise) this.velocity = flip.mul(-1).sub(delta.mul(PULL_FACTOR));
else this.velocity = flip.sub(delta.mul(PULL_FACTOR));
} else {
// Push the crawler away from the wall
if (this.clockwise) this.velocity = flip.mul(-1).add(delta.mul(PUSH_FACTOR));
else this.velocity = flip.add(delta.mul(PUSH_FACTOR));
}
this.velocity.normalize();

return this.velocity.mul(WALL_CRAWLER_SPEED * seconds);
};

Expand Down

0 comments on commit 72be85a

Please sign in to comment.