Added death status and death event when hull reaches 0
This commit is contained in:
parent
f7d97ef831
commit
2c73a62893
|
@ -12,6 +12,9 @@ module SpaceTac.Game {
|
|||
// Current level
|
||||
level: number;
|
||||
|
||||
// Flag indicating if the ship is alive
|
||||
alive: boolean;
|
||||
|
||||
// Position in the arena
|
||||
arena_x: number;
|
||||
arena_y: number;
|
||||
|
@ -34,7 +37,7 @@ module SpaceTac.Game {
|
|||
// Number of hull points (once it reaches 0, the ship is dead)
|
||||
hull: Attribute;
|
||||
|
||||
// Number of shield points (a shield wan absorb some damage to protect the hull)
|
||||
// Number of shield points (a shield can absorb some damage to protect the hull)
|
||||
shield: Attribute;
|
||||
|
||||
// List of slots, able to contain equipment
|
||||
|
@ -48,6 +51,7 @@ module SpaceTac.Game {
|
|||
this.attributes = new AttributeCollection();
|
||||
this.fleet = fleet || new Fleet();
|
||||
this.name = name;
|
||||
this.alive = true;
|
||||
this.initiative = this.newAttribute(AttributeCode.Initiative);
|
||||
this.initiative.setMaximal(1);
|
||||
this.ap_current = this.newAttribute(AttributeCode.AP);
|
||||
|
@ -66,6 +70,13 @@ module SpaceTac.Game {
|
|||
}
|
||||
}
|
||||
|
||||
// Returns true if the ship is able to play
|
||||
// If *check_ap* is true, ap_current=0 will make this function return false
|
||||
isAbleToPlay(check_ap: boolean = true): boolean {
|
||||
var ap_checked = !check_ap || this.ap_current.current > 0;
|
||||
return this.alive && ap_checked;
|
||||
}
|
||||
|
||||
// Create and register an attribute
|
||||
newAttribute(code: AttributeCode): Attribute {
|
||||
return this.attributes.getRawAttr(code);
|
||||
|
@ -127,6 +138,14 @@ module SpaceTac.Game {
|
|||
return actions;
|
||||
}
|
||||
|
||||
// Add an event to the battle log, if any
|
||||
addBattleEvent(event: BaseLogEvent): void {
|
||||
var battle = this.getBattle();
|
||||
if (battle && battle.log) {
|
||||
battle.log.add(event);
|
||||
}
|
||||
}
|
||||
|
||||
// Set an attribute value
|
||||
// If offset is true, the value will be added to current value
|
||||
// If log is true, an attribute event will be added to the battle log
|
||||
|
@ -140,10 +159,7 @@ module SpaceTac.Game {
|
|||
}
|
||||
|
||||
if (changed && log) {
|
||||
var battle = this.getBattle();
|
||||
if (battle) {
|
||||
battle.log.add(new AttributeChangeEvent(this, attr));
|
||||
}
|
||||
this.addBattleEvent(new AttributeChangeEvent(this, attr));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -193,8 +209,8 @@ module SpaceTac.Game {
|
|||
|
||||
this.setArenaPosition(x, y);
|
||||
|
||||
if (log && this.getBattle()) {
|
||||
this.getBattle().log.add(new MoveEvent(this, x, y));
|
||||
if (log) {
|
||||
this.addBattleEvent(new MoveEvent(this, x, y));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -203,8 +219,16 @@ module SpaceTac.Game {
|
|||
this.setAttribute(this.hull, -hull, true, log);
|
||||
this.setAttribute(this.shield, -shield, true, log);
|
||||
|
||||
if (log && this.getBattle()) {
|
||||
this.getBattle().log.add(new DamageEvent(this, hull, shield));
|
||||
if (log) {
|
||||
this.addBattleEvent(new DamageEvent(this, hull, shield));
|
||||
}
|
||||
|
||||
if (this.hull.current == 0) {
|
||||
// Ship is dead
|
||||
this.alive = false;
|
||||
if (log) {
|
||||
this.addBattleEvent(new DeathEvent(this));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
10
src/scripts/game/events/DeathEvent.ts
Normal file
10
src/scripts/game/events/DeathEvent.ts
Normal file
|
@ -0,0 +1,10 @@
|
|||
module SpaceTac.Game {
|
||||
"use strict";
|
||||
|
||||
// Event logged when a ship is dead
|
||||
export class DeathEvent extends BaseLogEvent {
|
||||
constructor(ship: Ship) {
|
||||
super("death", ship);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -119,5 +119,54 @@ module SpaceTac.Game {
|
|||
expect(ship.shield.current).toEqual(55);
|
||||
expect(battle.log.events.length).toBe(0);
|
||||
});
|
||||
|
||||
it("sets and logs death state", function (){
|
||||
var fleet = new Fleet();
|
||||
var battle = new Battle(fleet);
|
||||
var ship = new Ship(fleet);
|
||||
|
||||
expect(ship.alive).toBe(true);
|
||||
|
||||
ship.hull.set(10);
|
||||
battle.log.clear();
|
||||
ship.addDamage(5, 0);
|
||||
|
||||
expect(ship.alive).toBe(true);
|
||||
expect(battle.log.events.length).toBe(2);
|
||||
expect(battle.log.events[0].code).toEqual("attr");
|
||||
expect(battle.log.events[1].code).toEqual("damage");
|
||||
|
||||
battle.log.clear();
|
||||
ship.addDamage(5, 0);
|
||||
|
||||
expect(ship.alive).toBe(false);
|
||||
expect(battle.log.events.length).toBe(3);
|
||||
expect(battle.log.events[0].code).toEqual("attr");
|
||||
expect(battle.log.events[1].code).toEqual("damage");
|
||||
expect(battle.log.events[2].code).toEqual("death");
|
||||
});
|
||||
|
||||
it("checks if a ship is able to play", function () {
|
||||
var ship = new Ship();
|
||||
|
||||
expect(ship.isAbleToPlay()).toBe(false);
|
||||
expect(ship.isAbleToPlay(false)).toBe(true);
|
||||
|
||||
ship.ap_current.set(5);
|
||||
|
||||
expect(ship.isAbleToPlay()).toBe(true);
|
||||
expect(ship.isAbleToPlay(false)).toBe(true);
|
||||
|
||||
ship.hull.set(10);
|
||||
ship.addDamage(8, 0);
|
||||
|
||||
expect(ship.isAbleToPlay()).toBe(true);
|
||||
expect(ship.isAbleToPlay(false)).toBe(true);
|
||||
|
||||
ship.addDamage(8, 0);
|
||||
|
||||
expect(ship.isAbleToPlay()).toBe(false);
|
||||
expect(ship.isAbleToPlay(false)).toBe(false);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue