Skip to content

Commit

Permalink
make tanks collide with water
Browse files Browse the repository at this point in the history
  • Loading branch information
newagebegins committed Jun 24, 2012
1 parent 47820f3 commit 2a11337
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
10 changes: 10 additions & 0 deletions spec/TankSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,16 @@ describe("Tank", function () {
expect(tank.resolveCollisionWithSprite).toHaveBeenCalledWith(base);
});

it("should resolve collision when collides with water", function () {
spyOn(tank, 'resolveCollisionWithSprite');
var water = new Water(eventManager);
tank.notify({
'name': CollisionDetector.Event.COLLISION,
'initiator': tank,
'sprite': water});
expect(tank.resolveCollisionWithSprite).toHaveBeenCalledWith(water);
});

describe("collision with a tank", function () {
it("normal tank", function () {
spyOn(tank, 'resolveCollisionWithSprite');
Expand Down
8 changes: 7 additions & 1 deletion src/Tank.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ Tank.prototype.notify = function (event) {
if (event.name == Bullet.Event.DESTROYED && event.tank == this) {
this._bullets--;
}
else if (this._wallCollision(event) || this._tankCollision(event) || this._baseCollision(event)) {
else if (this._wallCollision(event) || this._tankCollision(event) || this._baseCollision(event) || this._waterCollision(event)) {
this.resolveCollisionWithSprite(event.sprite);
}
else if (this._bulletCollision(event) && this.canBeDestroyed()) {
Expand Down Expand Up @@ -402,6 +402,12 @@ Tank.prototype._baseCollision = function (event) {
event.sprite instanceof Base;
};

Tank.prototype._waterCollision = function (event) {
return event.name == CollisionDetector.Event.COLLISION &&
event.initiator === this &&
event.sprite instanceof Water;
};

Tank.prototype._tankCollision = function (event) {
return event.name == CollisionDetector.Event.COLLISION &&
event.initiator === this &&
Expand Down
2 changes: 2 additions & 0 deletions src/Water.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
function Water(eventManager) {
Sprite.call(this, eventManager);
this._animation = new Animation([1,2], 30, true);
this._w = Globals.UNIT_SIZE;
this._h = Globals.UNIT_SIZE;
}

Water.subclass(Sprite);
Expand Down

0 comments on commit 2a11337

Please sign in to comment.