Skip to content

Commit

Permalink
add Lives, LivesView
Browse files Browse the repository at this point in the history
  • Loading branch information
newagebegins committed Jun 19, 2012
1 parent 018344e commit 4f18ddf
Show file tree
Hide file tree
Showing 10 changed files with 94 additions and 1 deletion.
2 changes: 2 additions & 0 deletions BattleCity.html
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
<script type="text/javascript" src="src/ShovelHandler.js"></script>
<script type="text/javascript" src="src/Pause.js"></script>
<script type="text/javascript" src="src/PauseListener.js"></script>
<script type="text/javascript" src="src/Lives.js"></script>

<script type="text/javascript" src="lib/Stats.js"></script>
<script type="text/javascript" src="src/FPSCounter.js"></script>
Expand All @@ -76,6 +77,7 @@
<script type="text/javascript" src="src/Gamefield.js"></script>
<script type="text/javascript" src="src/Level.js"></script>
<script type="text/javascript" src="src/EnemyFactoryView.js"></script>
<script type="text/javascript" src="src/LivesView.js"></script>

<script type="text/javascript">
$(function () {
Expand Down
2 changes: 2 additions & 0 deletions SpecRunner.html
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
<script type="text/javascript" src="spec/ShovelHandlerSpec.js"></script>
<script type="text/javascript" src="spec/PauseSpec.js"></script>
<script type="text/javascript" src="spec/PauseListenerSpec.js"></script>
<script type="text/javascript" src="spec/LivesSpec.js"></script>

<!-- source files -->
<script type="text/javascript" src="src/Utils.js"></script>
Expand Down Expand Up @@ -116,6 +117,7 @@
<script type="text/javascript" src="src/ShovelHandler.js"></script>
<script type="text/javascript" src="src/Pause.js"></script>
<script type="text/javascript" src="src/PauseListener.js"></script>
<script type="text/javascript" src="src/Lives.js"></script>

<script type="text/javascript">
(function() {
Expand Down
Binary file added images/lives.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/roman_one.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
38 changes: 38 additions & 0 deletions spec/LivesSpec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
describe("Lives", function () {
it("should subscribe", function () {
var eventManager = new EventManager();
spyOn(eventManager, 'addSubscriber');
var lives = new Lives(eventManager);
expect(eventManager.addSubscriber).toHaveBeenCalledWith(lives, [Tank.Event.PLAYER_DESTROYED]);
});

describe("#notify", function () {
it("Tank.Event.PLAYER_DESTROYED", function () {
var eventManager = new EventManager();
var lives = new Lives(eventManager);
var tank = new Tank(eventManager);
spyOn(lives, 'take');
lives.notify({'name': Tank.Event.PLAYER_DESTROYED, 'tank': tank});
expect(lives.take).toHaveBeenCalled();
});
});

it("#take", function () {
var eventManager = new EventManager();
spyOn(eventManager, 'fireEvent');
var EVENT = {'name': Lives.Event.END};
var lives = new Lives(eventManager);
lives.setCount(2);

expect(lives.getCount()).toEqual(2);
lives.take();
expect(lives.getCount()).toEqual(1);
lives.take();
expect(lives.getCount()).toEqual(0);

expect(eventManager.fireEvent).not.toHaveBeenCalledWith(EVENT);
lives.take();
expect(lives.getCount()).toEqual(0);
expect(eventManager.fireEvent).toHaveBeenCalledWith(EVENT);
});
});
2 changes: 1 addition & 1 deletion src/EnemyFactoryView.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ function EnemyFactoryView(enemyFactory) {

EnemyFactoryView.prototype.draw = function (ctx) {
for (var i = 0; i < this._enemyFactory.getEnemiesToCreateCount(); ++i) {
var x = 466 + Globals.TILE_SIZE * (i % 2);
var x = 465 + Globals.TILE_SIZE * (i % 2);
var y = 34 + Globals.TILE_SIZE * Math.floor(i / 2);
ctx.drawImage(ImageManager.getImage('enemy'), x, y);
}
Expand Down
2 changes: 2 additions & 0 deletions src/ImageManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ var ImageManager = (function() {

base: null,
enemy: null,
lives: null,
roman_one: null,

points_100: null,
points_500: null,
Expand Down
4 changes: 4 additions & 0 deletions src/Level.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ function Level(eventManager) {

this._pause = new Pause(eventManager);

var lives = new Lives(eventManager);
this._livesView = new LivesView(lives);

var map = "Base(224,400);BrickWall(208,416);BrickWall(208,400);BrickWall(208,384);BrickWall(224,384);BrickWall(240,384);BrickWall(256,384);BrickWall(256,400);BrickWall(256,416);BrickWall(64,368);BrickWall(80,368);BrickWall(64,384);BrickWall(80,384);BrickWall(64,336);BrickWall(80,336);BrickWall(64,352);BrickWall(80,352);BrickWall(64,304);BrickWall(80,304);BrickWall(64,320);BrickWall(80,320);BrickWall(128,304);BrickWall(144,304);BrickWall(128,320);BrickWall(144,320);BrickWall(128,336);BrickWall(144,336);BrickWall(128,352);BrickWall(144,352);BrickWall(128,368);BrickWall(144,368);BrickWall(128,384);BrickWall(144,384);BrickWall(192,304);BrickWall(208,304);BrickWall(192,320);BrickWall(208,320);BrickWall(192,272);BrickWall(208,272);BrickWall(192,288);BrickWall(208,288);BrickWall(224,272);BrickWall(240,272);BrickWall(224,288);BrickWall(240,288);BrickWall(256,272);BrickWall(272,272);BrickWall(256,288);BrickWall(272,288);BrickWall(256,304);BrickWall(272,304);BrickWall(256,320);BrickWall(272,320);BrickWall(320,368);BrickWall(336,368);BrickWall(320,384);BrickWall(336,384);BrickWall(320,336);BrickWall(336,336);BrickWall(320,352);BrickWall(336,352);BrickWall(320,304);BrickWall(336,304);BrickWall(320,320);BrickWall(336,320);BrickWall(384,304);BrickWall(400,304);BrickWall(384,320);BrickWall(400,320);BrickWall(384,336);BrickWall(400,336);BrickWall(384,352);BrickWall(400,352);BrickWall(384,368);BrickWall(400,368);BrickWall(384,384);BrickWall(400,384);BrickWall(192,336);BrickWall(208,336);BrickWall(256,336);BrickWall(272,336);BrickWall(128,288);BrickWall(144,288);BrickWall(64,288);BrickWall(80,288);BrickWall(320,288);BrickWall(336,288);BrickWall(384,288);BrickWall(400,288);BrickWall(256,256);BrickWall(272,256);BrickWall(192,256);BrickWall(208,256);SteelWall(32,240);SteelWall(48,240);SteelWall(416,240);SteelWall(432,240);BrickWall(352,240);BrickWall(368,240);BrickWall(320,240);BrickWall(336,240);BrickWall(96,240);BrickWall(112,240);BrickWall(128,240);BrickWall(144,240);BrickWall(32,224);BrickWall(48,224);BrickWall(96,224);BrickWall(112,224);BrickWall(128,224);BrickWall(144,224);BrickWall(320,224);BrickWall(336,224);BrickWall(352,224);BrickWall(368,224);BrickWall(416,224);BrickWall(432,224);BrickWall(192,208);BrickWall(208,208);BrickWall(256,208);BrickWall(272,208);BrickWall(256,192);BrickWall(272,192);BrickWall(192,192);BrickWall(208,192);BrickWall(128,176);BrickWall(144,176);BrickWall(64,176);BrickWall(80,176);BrickWall(320,176);BrickWall(336,176);BrickWall(384,176);BrickWall(400,176);BrickWall(256,144);BrickWall(272,144);BrickWall(192,144);BrickWall(208,144);BrickWall(128,144);BrickWall(144,144);BrickWall(128,160);BrickWall(144,160);BrickWall(64,144);BrickWall(80,144);BrickWall(64,160);BrickWall(80,160);BrickWall(64,112);BrickWall(80,112);BrickWall(64,128);BrickWall(80,128);BrickWall(64,80);BrickWall(80,80);BrickWall(64,96);BrickWall(80,96);BrickWall(64,48);BrickWall(80,48);BrickWall(64,64);BrickWall(80,64);BrickWall(128,48);BrickWall(144,48);BrickWall(128,64);BrickWall(144,64);BrickWall(128,80);BrickWall(144,80);BrickWall(128,96);BrickWall(144,96);BrickWall(128,112);BrickWall(144,112);BrickWall(128,128);BrickWall(144,128);BrickWall(192,112);BrickWall(208,112);BrickWall(192,128);BrickWall(208,128);BrickWall(192,80);BrickWall(208,80);BrickWall(192,96);BrickWall(208,96);BrickWall(192,48);BrickWall(208,48);BrickWall(192,64);BrickWall(208,64);BrickWall(256,48);BrickWall(272,48);BrickWall(256,64);BrickWall(272,64);BrickWall(256,80);BrickWall(272,80);BrickWall(256,96);BrickWall(272,96);BrickWall(256,112);BrickWall(272,112);BrickWall(256,128);BrickWall(272,128);BrickWall(320,144);BrickWall(336,144);BrickWall(320,160);BrickWall(336,160);BrickWall(320,112);BrickWall(336,112);BrickWall(320,128);BrickWall(336,128);BrickWall(320,80);BrickWall(336,80);BrickWall(320,96);BrickWall(336,96);BrickWall(320,48);BrickWall(336,48);BrickWall(320,64);BrickWall(336,64);BrickWall(384,48);BrickWall(400,48);BrickWall(384,64);BrickWall(400,64);BrickWall(384,80);BrickWall(400,80);BrickWall(384,96);BrickWall(400,96);BrickWall(384,112);BrickWall(400,112);BrickWall(384,128);BrickWall(400,128);BrickWall(384,144);BrickWall(400,144);BrickWall(384,160);BrickWall(400,160);SteelWall(224,112);SteelWall(240,112);SteelWall(224,128);SteelWall(240,128)";
var serializer = new SpriteSerializer(eventManager);
serializer.unserializeSprites(map);
Expand All @@ -77,6 +80,7 @@ Level.prototype.draw = function (ctx) {
Gamefield.prototype.draw.call(this, ctx);
this._enemyFactoryView.draw(ctx);
this._pause.draw(ctx);
this._livesView.draw(ctx);
};

Level.prototype._createPowerUpFactory = function () {
Expand Down
30 changes: 30 additions & 0 deletions src/Lives.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
function Lives(eventManager) {
this._eventManager = eventManager;
this._eventManager.addSubscriber(this, [Tank.Event.PLAYER_DESTROYED]);
this._count = 2;
}

Lives.Event = {};
Lives.Event.END = 'Lives.Event.END';

Lives.prototype.notify = function (event) {
if (event.name == Tank.Event.PLAYER_DESTROYED) {
this.take();
}
};

Lives.prototype.setCount = function (count) {
this._count = count;
};

Lives.prototype.getCount = function () {
return this._count;
};

Lives.prototype.take = function () {
if (this._count == 0) {
this._eventManager.fireEvent({'name': Lives.Event.END});
return;
}
this._count--;
};
15 changes: 15 additions & 0 deletions src/LivesView.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
function LivesView(lives) {
this._lives = lives;
}

LivesView.prototype.draw = function (ctx) {
ctx.fillStyle = "#000000";
ctx.font = "16px prstart"

ctx.drawImage(ImageManager.getImage('roman_one'), 468, 256);

ctx.fillText("P", 482, 286 - 16);
ctx.fillText(this._lives.getCount(), 482, 286);

ctx.drawImage(ImageManager.getImage('lives'), 465, 272);
};

0 comments on commit 4f18ddf

Please sign in to comment.