forked from newagebegins/BattleCity
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add PlayerTankFactory, PlayerTankControllerFactory
- Loading branch information
1 parent
183d5bd
commit fd05261
Showing
11 changed files
with
144 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
describe("PlayerTankControllerFactory", function () { | ||
it("should subscribe", function () { | ||
var eventManager = new EventManager(); | ||
spyOn(eventManager, 'addSubscriber'); | ||
var factory = new PlayerTankControllerFactory(eventManager); | ||
expect(eventManager.addSubscriber).toHaveBeenCalledWith(factory, [PlayerTankFactory.Event.PLAYER_TANK_CREATED]); | ||
}); | ||
|
||
describe("#notify", function () { | ||
it("PlayerTankFactory.Event.PLAYER_TANK_CREATED", function () { | ||
var eventManager = new EventManager(); | ||
var factory = new PlayerTankControllerFactory(eventManager); | ||
spyOn(factory, 'create'); | ||
var tank = new Tank(eventManager); | ||
factory.notify({'name': PlayerTankFactory.Event.PLAYER_TANK_CREATED, 'tank': tank}); | ||
expect(factory.create).toHaveBeenCalledWith(tank); | ||
}); | ||
}); | ||
|
||
it("#create", function () { | ||
var eventManager = new EventManager(); | ||
var factory = new PlayerTankControllerFactory(eventManager); | ||
var tank = new Tank(eventManager); | ||
var controller = factory.create(tank); | ||
expect(controller instanceof TankController).toBeTruthy(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
describe("PlayerTankFactory", function () { | ||
it("should subscribe", function () { | ||
var eventManager = new EventManager(); | ||
spyOn(eventManager, 'addSubscriber'); | ||
var factory = new PlayerTankFactory(eventManager); | ||
expect(eventManager.addSubscriber).toHaveBeenCalledWith(factory, [TankExplosion.Event.DESTROYED]); | ||
}); | ||
|
||
describe("#notify", function () { | ||
it("TankExplosion.Event.DESTROYED", function () { | ||
var eventManager = new EventManager(); | ||
var factory = new PlayerTankFactory(eventManager); | ||
spyOn(factory, 'create'); | ||
var tank = new Tank(eventManager); | ||
var explosion = new TankExplosion(eventManager, tank); | ||
factory.notify({'name': TankExplosion.Event.DESTROYED, 'explosion': explosion}); | ||
expect(factory.create).toHaveBeenCalled(); | ||
}); | ||
}); | ||
|
||
it("#create", function () { | ||
var eventManager = new EventManager(); | ||
spyOn(eventManager, 'fireEvent'); | ||
var factory = new PlayerTankFactory(eventManager); | ||
factory.setAppearPosition(new Point(1,2)); | ||
|
||
var tank = new Tank(eventManager); | ||
tank.setPosition(new Point(1,2)); | ||
tank.setState(new TankStateAppearing(tank)); | ||
|
||
var product = factory.create(); | ||
expect(product).toEqual(tank); | ||
expect(eventManager.fireEvent).toHaveBeenCalledWith({'name': PlayerTankFactory.Event.PLAYER_TANK_CREATED, 'tank': tank}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,14 @@ | ||
function AITankControllerFactory(eventManager) { | ||
this._eventManager = eventManager; | ||
this._eventManager.addSubscriber(this, [Tank.Event.CREATED]); | ||
this._eventManager.addSubscriber(this, [EnemyFactory.Event.ENEMY_CREATED]); | ||
} | ||
|
||
AITankControllerFactory.prototype.createController = function (tank) { | ||
return new AITankController(tank, new Random()); | ||
}; | ||
|
||
AITankControllerFactory.prototype.notify = function (event) { | ||
if (event.name == Tank.Event.CREATED) { | ||
this.createController(event.tank); | ||
if (event.name == EnemyFactory.Event.ENEMY_CREATED) { | ||
this.createController(event.enemy); | ||
} | ||
}; | ||
|
||
AITankControllerFactory.prototype.createController = function (tank) { | ||
return new AITankController(tank, new Random()); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
function PlayerTankControllerFactory(eventManager) { | ||
this._eventManager = eventManager; | ||
this._eventManager.addSubscriber(this, [PlayerTankFactory.Event.PLAYER_TANK_CREATED]); | ||
} | ||
|
||
PlayerTankControllerFactory.prototype.notify = function (event) { | ||
if (event.name == PlayerTankFactory.Event.PLAYER_TANK_CREATED) { | ||
this.create(event.tank); | ||
} | ||
}; | ||
|
||
PlayerTankControllerFactory.prototype.create = function (tank) { | ||
var controller = new TankController(this._eventManager, tank); | ||
return controller; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
function PlayerTankFactory(eventManager) { | ||
this._eventManager = eventManager; | ||
this._eventManager.addSubscriber(this, [TankExplosion.Event.DESTROYED]); | ||
this._appearPosition = new Point(0, 0); | ||
} | ||
|
||
PlayerTankFactory.Event = {}; | ||
PlayerTankFactory.Event.PLAYER_TANK_CREATED = 'PlayerTankFactory.Event.PLAYER_TANK_CREATED'; | ||
|
||
PlayerTankFactory.prototype.notify = function (event) { | ||
if (this._tankExplosionDestroyed(event)) { | ||
this.create(); | ||
} | ||
}; | ||
|
||
PlayerTankFactory.prototype.setAppearPosition = function (position) { | ||
this._appearPosition = position; | ||
}; | ||
|
||
PlayerTankFactory.prototype.create = function () { | ||
var tank = new Tank(this._eventManager); | ||
tank.setPosition(this._appearPosition); | ||
tank.setState(new TankStateAppearing(tank)); | ||
this._eventManager.fireEvent({'name': PlayerTankFactory.Event.PLAYER_TANK_CREATED, 'tank': tank}); | ||
return tank; | ||
}; | ||
|
||
PlayerTankFactory.prototype._tankExplosionDestroyed = function (event) { | ||
if (event.name != TankExplosion.Event.DESTROYED) { | ||
return false; | ||
} | ||
var tank = event.explosion.getTank(); | ||
if (!tank.isPlayer()) { | ||
return false; | ||
} | ||
return true; | ||
}; |