1
0
Fork 0

Added battle log system (WIP)

This commit is contained in:
Michaël Lemaire 2014-12-31 01:00:00 +01:00 committed by Michaël Lemaire
parent 006d95ae6b
commit 4efaafafd9
9 changed files with 173 additions and 2 deletions

View file

@ -1,6 +1,9 @@
module SpaceTac.Game {
// A turn-based battle between fleets
export class Battle {
// Log of all battle events
log: BattleLog;
// List of fleets engaged in battle
fleets: Fleet[];
@ -13,6 +16,7 @@ module SpaceTac.Game {
// Create a battle between two fleets
constructor(fleet1: Fleet, fleet2: Fleet) {
this.log = new BattleLog();
this.fleets = [fleet1, fleet2];
this.play_order = [];
this.playing_ship_index = null;
@ -64,7 +68,9 @@ module SpaceTac.Game {
// End the current ship turn, passing control to the next one in play order
// If at the end of the play order, next turn will start automatically
// Member 'play_order' must be defined before calling this function
advanceToNextShip(): void {
advanceToNextShip(log: boolean=true): void {
var previous_ship = this.playing_ship;
if (this.play_order.length == 0) {
this.playing_ship_index = null;
this.playing_ship = null;
@ -79,14 +85,28 @@ module SpaceTac.Game {
}
this.playing_ship = this.play_order[this.playing_ship_index];
}
if (log) {
this.log.add(new Events.ShipChangeEvent(previous_ship, this.playing_ship));
}
}
// Start the battle
// This will call all necessary initialization steps (initiative, placement...)
// This will not add any event to the battle log
start(): void {
this.placeShips();
this.throwInitiative();
this.advanceToNextShip();
this.advanceToNextShip(false);
}
// Force an injection of events in the battle log to simulate the initial state
// For instance, this may be called after 'start', to use the log subscription system
// to initialize a battle UI
injectInitialEvents(): void {
// TODO Simulate initial ship placement
// Simulate game turn
this.log.add(new Events.ShipChangeEvent(this.playing_ship, this.playing_ship));
}
// Create a quick random battle, for testing purposes

View file

@ -0,0 +1,19 @@
module SpaceTac.Game {
// Log of a battle
// This keeps track of all events in a battle
// It also allows to register a callback to receive these events
export class BattleLog {
// Full list of battle events
events: Events.BaseLogEvent[];
// Create an initially empty log
constructor() {
this.events = [];
}
// Add a battle event to the log
add(event: Events.BaseLogEvent) {
this.events.push(event);
}
}
}

View file

@ -0,0 +1,29 @@
module SpaceTac.Game {
// Target for a capability
// This could be a location in space, or a ship
export class Target {
// Coordinates of the target
x: number;
y: number;
// If the target is a ship, this attribute will be set
ship: Ship;
// Standard constructor
constructor(x: number, y: number, ship: Ship) {
this.x = x;
this.y = y;
this.ship = ship;
}
// Constructor to target a single ship
static newFromShip(ship: Ship): Target {
return new Target(ship.arena_x, ship.arena_y, ship);
}
// Constructor to target a location in space
static newFromLocation(x: number, y: number): Target {
return new Target(x, y, null);
}
}
}

View file

@ -0,0 +1,19 @@
module SpaceTac.Game.Events {
// Base class for a BattleLog event
export class BaseLogEvent {
// Code of the event (its type)
code: string;
// The ship causing the event (the one whose turn it is to play)
ship: Ship;
// Target of the event
target: Target;
constructor(code: string, ship: Ship = null, target: Target = null) {
this.code = code;
this.ship = ship;
this.target = target;
}
}
}

View file

@ -0,0 +1,8 @@
module SpaceTac.Game.Events {
// Battle event, when a ship turn ended, and advanced to a new one
export class ShipChangeEvent extends BaseLogEvent {
constructor(ship: Ship, new_ship: Ship) {
super("ship_change", ship, new_ship ? Target.newFromShip(new_ship) : null);
}
}
}

View file

@ -0,0 +1,40 @@
module SpaceTac.Specs {
// Check a single game log event
function checkEvent(got: Game.Events.BaseLogEvent, ship: Game.Ship, code: string,
target_ship: Game.Ship = null, target_x: number = null, target_y: number = null): void {
if (target_ship) {
if (target_x === null) {
target_x = target_ship.arena_x;
}
if (target_y === null) {
target_y = target_ship.arena_y;
}
}
expect(got.ship).toBe(ship);
expect(got.code).toEqual(code);
expect(got.target.ship).toBe(target_ship);
if (target_x === null) {
expect(got.target.x).toBeNull();
} else {
expect(got.target.x).toBeCloseTo(target_x, 0.000001);
}
if (target_y === null) {
expect(got.target.y).toBeNull();
} else {
expect(got.target.y).toBeCloseTo(target_y, 0.000001);
}
}
describe("BattleLog", function () {
it("logs ship change events", function () {
var battle = Game.Battle.newQuickRandom();
expect(battle.log.events.length).toBe(0);
battle.advanceToNextShip();
expect(battle.log.events.length).toBe(1);
checkEvent(battle.log.events[0], battle.play_order[0], "ship_change", battle.play_order[1]);
});
});
}

View file

@ -14,10 +14,14 @@ module SpaceTac.View {
// Battleground container
arena: Phaser.Group;
// Targetting mode (null if we're not in this mode)
targetting: Targetting;
// Init the view, binding it to a specific battle
init(player, battle) {
this.player = player;
this.battle = battle;
this.targetting = null;
}
// Create view graphics
@ -56,5 +60,17 @@ module SpaceTac.View {
this.arena.destroy();
this.arena = null;
}
// Enter targetting mode
// While in this mode, the Targetting object will receive hover and click events, and handle them
enterTargettingMode(): Targetting {
this.targetting = new Targetting(this);
return this.targetting;
}
// Exit targetting mode
exitTargettingMode(): void {
this.targetting = null;
}
}
}

View file

@ -0,0 +1,12 @@
module SpaceTac.View {
// Targetting system
// Allows to pick a target for a capability
export class Targetting {
// Access to the parent battle view
private battleview: BattleView;
constructor(battleview: BattleView) {
this.battleview = battleview;
}
}
}

View file

@ -0,0 +1,8 @@
module SpaceTac.View.Widgets {
// Icon to activate a ship capability (move, fire...)
export class CapabilityIcon extends Phaser.Button {
constructor(battleview: BattleView, x: number, y:number, code: string) {
super(battleview.game, x, y, 'capability-' + code);
}
}
}