Skip to content

Commit

Permalink
add TankColor
Browse files Browse the repository at this point in the history
  • Loading branch information
newagebegins committed Jun 20, 2012
1 parent ead43c6 commit 7b73738
Show file tree
Hide file tree
Showing 65 changed files with 201 additions and 67 deletions.
1 change: 1 addition & 0 deletions BattleCity.html
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
<script type="text/javascript" src="src/Lives.js"></script>
<script type="text/javascript" src="src/BaseExplosionFactory.js"></script>
<script type="text/javascript" src="src/BaseExplosion.js"></script>
<script type="text/javascript" src="src/TankColor.js"></script>

<script type="text/javascript" src="lib/Stats.js"></script>
<script type="text/javascript" src="src/FPSCounter.js"></script>
Expand Down
2 changes: 2 additions & 0 deletions SpecRunner.html
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
<script type="text/javascript" src="spec/LivesSpec.js"></script>
<script type="text/javascript" src="spec/BaseExplosionFactorySpec.js"></script>
<script type="text/javascript" src="spec/BaseExplosionSpec.js"></script>
<script type="text/javascript" src="spec/TankColorSpec.js"></script>

<!-- source files -->
<script type="text/javascript" src="src/Utils.js"></script>
Expand Down Expand Up @@ -122,6 +123,7 @@
<script type="text/javascript" src="src/Lives.js"></script>
<script type="text/javascript" src="src/BaseExplosionFactory.js"></script>
<script type="text/javascript" src="src/BaseExplosion.js"></script>
<script type="text/javascript" src="src/TankColor.js"></script>

<script type="text/javascript">
(function() {
Expand Down
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
70 changes: 70 additions & 0 deletions spec/TankColorSpec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
describe("TankColor", function () {
describe("#update", function () {
var color;

beforeEach(function () {
color = new TankColor();
color.setColors([[0,1],[0,2],[1,2],[0,0]]);
});

it("no hits", function () {
expect(color.getColor()).toEqual(0);
color.update();
expect(color.getColor()).toEqual(1);
color.update();
expect(color.getColor()).toEqual(0);
color.update();
expect(color.getColor()).toEqual(1);
});

it("one hit", function () {
color.hit();
expect(color.getColor()).toEqual(0);
color.update();
expect(color.getColor()).toEqual(2);
color.update();
expect(color.getColor()).toEqual(0);
color.update();
expect(color.getColor()).toEqual(2);
});

it("two hits", function () {
color.hit();
color.hit();
expect(color.getColor()).toEqual(1);
color.update();
expect(color.getColor()).toEqual(2);
color.update();
expect(color.getColor()).toEqual(1);
color.update();
expect(color.getColor()).toEqual(2);
});

it("three hits", function () {
color.hit();
color.hit();
color.hit();
expect(color.getColor()).toEqual(0);
color.update();
expect(color.getColor()).toEqual(0);
color.update();
expect(color.getColor()).toEqual(0);
color.update();
expect(color.getColor()).toEqual(0);
});

it("four hits", function () {
color.hit();
color.hit();
color.hit();
color.hit();
expect(color.getColor()).toEqual(0);
color.update();
expect(color.getColor()).toEqual(0);
color.update();
expect(color.getColor()).toEqual(0);
color.update();
expect(color.getColor()).toEqual(0);
});
});
});
16 changes: 16 additions & 0 deletions spec/TankSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,14 @@ describe("Tank", function () {
tank.hit();
expect(tank.destroy).toHaveBeenCalled();
});

it("color", function () {
var color = new TankColor();
tank.setColor(color);
spyOn(color, 'hit');
tank.hit();
expect(color.hit).toHaveBeenCalled();
});
});

describe("#stateAppearingEnd", function () {
Expand Down Expand Up @@ -460,6 +468,14 @@ describe("Tank", function () {
expect(tank.getUpgradeLevel()).toEqual(3);
});
});

it("#updateColor", function () {
var color = new TankColor();
tank.setColor(color);
spyOn(color, 'update');
tank.updateColor();
expect(color.update).toHaveBeenCalled();
});
});

describe("Tank", function () {
Expand Down
14 changes: 10 additions & 4 deletions spec/TankStateNormalSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,24 +49,24 @@ describe("TankStateNormal", function () {
describe("#getImage", function () {
it("RIGHT", function () {
tank.setDirection(Sprite.Direction.RIGHT);
expect(state.getImage()).toEqual('tank_player1_right_1');
expect(state.getImage()).toEqual('tank_player1_right_c0_t1');
});
it("LEFT", function () {
tank.toNormalSpeed();
tank.setDirection(Sprite.Direction.LEFT);
state.update();
expect(state.getImage()).toEqual('tank_player1_left_2');
expect(state.getImage()).toEqual('tank_player1_left_c0_t2');
});
it("flashed", function () {
tank.setDirection(Sprite.Direction.RIGHT);
tank.startFlashing();
state.setFlashed(true);
expect(state.getImage()).toEqual('tank_player1_right_1_f');
expect(state.getImage()).toEqual('tank_player1_right_c0_t1_f');
});
it("upgrade 1", function () {
tank.setDirection(Sprite.Direction.RIGHT);
tank.upgrade();
expect(state.getImage()).toEqual('tank_player1_right_1_s1');
expect(state.getImage()).toEqual('tank_player1_right_c0_t1_s1');
});
});

Expand All @@ -87,6 +87,12 @@ describe("TankStateNormal", function () {
expect(state.updateTrackAnimation).not.toHaveBeenCalled();
expect(state.updateFlash).toHaveBeenCalled();
});

it("normal - with color", function () {
spyOn(tank, 'updateColor');
state.update();
expect(tank.updateColor).toHaveBeenCalled();
});
});

it("#canMove", function () {
Expand Down
124 changes: 62 additions & 62 deletions src/ImageManager.js
Original file line number Diff line number Diff line change
@@ -1,67 +1,67 @@
var ImageManager = (function() {
var images = {
tank_player1_down_1: null,
tank_player1_down_2: null,
tank_player1_left_1: null,
tank_player1_left_2: null,
tank_player1_right_1: null,
tank_player1_right_2: null,
tank_player1_up_1: null,
tank_player1_up_2: null,

tank_player1_down_1_s1: null,
tank_player1_down_2_s1: null,
tank_player1_left_1_s1: null,
tank_player1_left_2_s1: null,
tank_player1_right_1_s1: null,
tank_player1_right_2_s1: null,
tank_player1_up_1_s1: null,
tank_player1_up_2_s1: null,

tank_player1_down_1_s2: null,
tank_player1_down_2_s2: null,
tank_player1_left_1_s2: null,
tank_player1_left_2_s2: null,
tank_player1_right_1_s2: null,
tank_player1_right_2_s2: null,
tank_player1_up_1_s2: null,
tank_player1_up_2_s2: null,

tank_player1_down_1_s3: null,
tank_player1_down_2_s3: null,
tank_player1_left_1_s3: null,
tank_player1_left_2_s3: null,
tank_player1_right_1_s3: null,
tank_player1_right_2_s3: null,
tank_player1_up_1_s3: null,
tank_player1_up_2_s3: null,

tank_basic_down_1: null,
tank_basic_down_2: null,
tank_basic_left_1: null,
tank_basic_left_2: null,
tank_basic_right_1: null,
tank_basic_right_2: null,
tank_basic_up_1: null,
tank_basic_up_2: null,

tank_basic_down_1_f: null,
tank_basic_down_2_f: null,
tank_basic_left_1_f: null,
tank_basic_left_2_f: null,
tank_basic_right_1_f: null,
tank_basic_right_2_f: null,
tank_basic_up_1_f: null,
tank_basic_up_2_f: null,

tank_fast_down_1: null,
tank_fast_down_2: null,
tank_fast_left_1: null,
tank_fast_left_2: null,
tank_fast_right_1: null,
tank_fast_right_2: null,
tank_fast_up_1: null,
tank_fast_up_2: null,
tank_player1_down_c0_t1: null,
tank_player1_down_c0_t2: null,
tank_player1_left_c0_t1: null,
tank_player1_left_c0_t2: null,
tank_player1_right_c0_t1: null,
tank_player1_right_c0_t2: null,
tank_player1_up_c0_t1: null,
tank_player1_up_c0_t2: null,

tank_player1_down_c0_t1_s1: null,
tank_player1_down_c0_t2_s1: null,
tank_player1_left_c0_t1_s1: null,
tank_player1_left_c0_t2_s1: null,
tank_player1_right_c0_t1_s1: null,
tank_player1_right_c0_t2_s1: null,
tank_player1_up_c0_t1_s1: null,
tank_player1_up_c0_t2_s1: null,

tank_player1_down_c0_t1_s2: null,
tank_player1_down_c0_t2_s2: null,
tank_player1_left_c0_t1_s2: null,
tank_player1_left_c0_t2_s2: null,
tank_player1_right_c0_t1_s2: null,
tank_player1_right_c0_t2_s2: null,
tank_player1_up_c0_t1_s2: null,
tank_player1_up_c0_t2_s2: null,

tank_player1_down_c0_t1_s3: null,
tank_player1_down_c0_t2_s3: null,
tank_player1_left_c0_t1_s3: null,
tank_player1_left_c0_t2_s3: null,
tank_player1_right_c0_t1_s3: null,
tank_player1_right_c0_t2_s3: null,
tank_player1_up_c0_t1_s3: null,
tank_player1_up_c0_t2_s3: null,

tank_basic_down_c0_t1: null,
tank_basic_down_c0_t2: null,
tank_basic_left_c0_t1: null,
tank_basic_left_c0_t2: null,
tank_basic_right_c0_t1: null,
tank_basic_right_c0_t2: null,
tank_basic_up_c0_t1: null,
tank_basic_up_c0_t2: null,

tank_basic_down_c0_t1_f: null,
tank_basic_down_c0_t2_f: null,
tank_basic_left_c0_t1_f: null,
tank_basic_left_c0_t2_f: null,
tank_basic_right_c0_t1_f: null,
tank_basic_right_c0_t2_f: null,
tank_basic_up_c0_t1_f: null,
tank_basic_up_c0_t2_f: null,

tank_fast_down_c0_t1: null,
tank_fast_down_c0_t2: null,
tank_fast_left_c0_t1: null,
tank_fast_left_c0_t2: null,
tank_fast_right_c0_t1: null,
tank_fast_right_c0_t2: null,
tank_fast_up_c0_t1: null,
tank_fast_up_c0_t2: null,

appear_1: null,
appear_2: null,
Expand Down
14 changes: 14 additions & 0 deletions src/Tank.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ function Tank(eventManager) {
this._flashing = false;
this._collisionResolvingMoveLimit = 10;
this._upgradeLevel = 0;
this._color = new TankColor();

this._hitLimit = 1;
this._hit = 0;
Expand Down Expand Up @@ -137,6 +138,18 @@ Tank.prototype.updateHook = function () {
this._state.update();
};

Tank.prototype.updateColor = function () {
this._color.update();
};

Tank.prototype.setColor = function (color) {
this._color = color;
};

Tank.prototype.getColorValue = function () {
return this._color.getColor();
};

Tank.prototype.notify = function (event) {
if (event.name == Bullet.Event.DESTROYED && event.tank == this) {
this._bullets--;
Expand Down Expand Up @@ -201,6 +214,7 @@ Tank.prototype.getEventManager = function () {

Tank.prototype.hit = function () {
this._hit++;
this._color.hit();
if (this._hit == this._hitLimit) {
this.destroy();
}
Expand Down
24 changes: 24 additions & 0 deletions src/TankColor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
function TankColor() {
this._colors = [[0,0]];
this._hit = 0;
this._color = 0;
}

TankColor.prototype.setColors = function (colors) {
this._colors = colors;
};

TankColor.prototype.getColor = function () {
return this._colors[this._hit][this._color];
};

TankColor.prototype.update = function () {
this._color = this._color == 0 ? 1 : 0;
};

TankColor.prototype.hit = function () {
this._hit++;
if (this._hit >= this._colors.length) {
this._hit = this._colors.length - 1;
}
};
3 changes: 2 additions & 1 deletion src/TankStateNormal.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ function TankStateNormal(tank) {
}

TankStateNormal.prototype.getImage = function () {
var image = 'tank_' + this._tank.getType() + '_' + this._tank.getDirection() + '_' + this._trackAnimation.getFrame();
var image = 'tank_' + this._tank.getType() + '_' + this._tank.getDirection() + '_c' + this._tank.getColorValue() + '_t' + this._trackAnimation.getFrame();
if (this._tank.isFlashing() && this._flashed) {
image += '_f';
}
Expand All @@ -23,6 +23,7 @@ TankStateNormal.prototype.update = function () {
this.updateTrackAnimation();
}
this.updateFlash();
this._tank.updateColor();
};

TankStateNormal.prototype.updateTrackAnimation = function () {
Expand Down

0 comments on commit 7b73738

Please sign in to comment.