1
0
Fork 0
spacetac/src/ui/battle/LogProcessor.ts

324 lines
10 KiB
TypeScript
Raw Normal View History

2017-02-09 00:00:35 +00:00
module TS.SpaceTac.UI {
/**
* Processor of events coming from the battle log
*
* This will bind to the battle log to receive new events, and update
* the battle view accordingly.
*
* It is also possible to go back/forward in time.
*/
2014-12-31 00:00:00 +00:00
export class LogProcessor {
// Link to the battle view
private view: BattleView
2014-12-31 00:00:00 +00:00
// Link to the battle
private battle: Battle
2014-12-31 00:00:00 +00:00
// Link to the battle log
private log: BattleLog
2014-12-31 00:00:00 +00:00
// Subscription identifier
private subscription: any = null
2014-12-31 00:00:00 +00:00
// Delay before processing next events
private delayed = false
// Processing queue, when delay is active
private queue: BaseBattleEvent[] = []
// Forward events to other subscribers
private forwarding: LogSubscriber[] = []
// Current position in the battle log
private cursor = -1;
2014-12-31 00:00:00 +00:00
constructor(view: BattleView) {
this.view = view;
this.battle = view.battle;
this.log = view.battle.log;
view.inputs.bindCheat("PageUp", "Step backward", () => {
this.stepBackward();
});
view.inputs.bindCheat("PageDown", "Step forward", () => {
this.stepForward();
});
view.inputs.bindCheat("Home", "Jump to beginning", () => {
this.jumpToStart();
});
view.inputs.bindCheat("End", "Jump to end", () => {
this.jumpToEnd();
});
2017-02-21 22:38:31 +00:00
}
2014-12-31 00:00:00 +00:00
2017-02-21 22:38:31 +00:00
/**
* Start log processing
*/
start() {
this.subscription = this.log.subscribe(event => this.processBattleEvent(event));
2017-05-30 18:23:35 +00:00
this.cursor = this.log.events.length - 1;
this.battle.getBootstrapEvents().forEach(event => this.processBattleEvent(event));
}
/**
* Make a step backward in time
*/
stepBackward() {
2017-05-30 18:23:35 +00:00
if (!this.atStart()) {
this.cursor -= 1;
2017-05-30 18:23:35 +00:00
this.processBattleEvent(this.log.events[this.cursor + 1].getReverse());
}
}
/**
* Make a step forward in time
*/
stepForward() {
2017-05-30 18:23:35 +00:00
if (!this.atEnd()) {
this.cursor += 1;
this.processBattleEvent(this.log.events[this.cursor]);
}
}
/**
* Jump to the start of the log
*
* This will rewind all applied event
*/
jumpToStart() {
2017-05-30 18:23:35 +00:00
while (!this.atStart()) {
this.stepBackward();
}
}
/**
* Jump to the end of the log
*
* This will apply all remaining event
*/
jumpToEnd() {
2017-05-30 18:23:35 +00:00
while (!this.atEnd()) {
this.stepForward();
}
}
/**
* Check if we are currently at the start of the log
*/
atStart(): boolean {
return this.cursor < 0;
}
/**
* Check if we are currently at the end of the log
*/
atEnd(): boolean {
return this.cursor >= this.log.events.length - 1;
}
/**
* Check if we need a player or AI to interact at this point
*/
getPlayerNeeded(): Player | null {
if (this.atEnd()) {
return this.battle.playing_ship ? this.battle.playing_ship.getPlayer() : null;
} else {
return null;
}
}
/**
* Register a sub-subscriber.
*
* The difference with registering directly to the BattleLog is that events may be delayed
* for animations.
*
* The callback may return the duration it needs to display the change.
*/
register(callback: (event: BaseBattleEvent) => number) {
this.forwarding.push(event => {
let duration = callback(event);
if (duration) {
this.delayNextEvents(duration);
}
});
2014-12-31 00:00:00 +00:00
}
/**
* Register a sub-subscriber, to receive events for a specific ship
*/
registerForShip(ship: Ship, callback: (event: BaseLogShipEvent) => number) {
this.register(event => {
if (event instanceof BaseLogShipEvent && event.ship === ship) {
return callback(event);
} else {
return 0;
}
});
}
/**
* Introduce a delay in event processing
*/
delayNextEvents(duration: number) {
if (duration > 0 && !this.view.gameui.headless) {
this.delayed = true;
2017-02-16 22:59:41 +00:00
this.view.timer.schedule(duration, () => this.processQueued());
}
}
/**
* Process the events queued due to a delay
*/
processQueued() {
let events = acopy(this.queue);
this.queue = [];
this.delayed = false;
events.forEach(event => this.processBattleEvent(event));
}
/**
* Process a single event
*/
processBattleEvent(event: BaseBattleEvent) {
if (this.delayed) {
this.queue.push(event);
return;
}
2014-12-31 00:00:00 +00:00
console.log("Battle event", event);
this.forwarding.forEach(subscriber => subscriber(event));
2017-02-09 00:00:35 +00:00
if (event instanceof ShipChangeEvent) {
2017-02-08 18:54:02 +00:00
this.processShipChangeEvent(event);
2017-02-09 00:00:35 +00:00
} else if (event instanceof DeathEvent) {
2017-02-08 18:54:02 +00:00
this.processDeathEvent(event);
2017-02-09 00:00:35 +00:00
} else if (event instanceof FireEvent) {
2017-02-08 18:54:02 +00:00
this.processFireEvent(event);
2017-02-09 00:00:35 +00:00
} else if (event instanceof DamageEvent) {
2017-02-08 18:54:02 +00:00
this.processDamageEvent(event);
2017-02-09 00:00:35 +00:00
} else if (event instanceof EndBattleEvent) {
2017-02-08 18:54:02 +00:00
this.processEndBattleEvent(event);
2017-02-09 00:00:35 +00:00
} else if (event instanceof DroneDeployedEvent) {
2017-02-08 18:54:02 +00:00
this.processDroneDeployedEvent(event);
2017-02-09 00:00:35 +00:00
} else if (event instanceof DroneDestroyedEvent) {
2017-02-08 18:54:02 +00:00
this.processDroneDestroyedEvent(event);
} else if (event instanceof DroneAppliedEvent) {
this.processDroneAppliedEvent(event);
2014-12-31 00:00:00 +00:00
}
2017-05-30 18:23:35 +00:00
// FIXME temporary fix for cursor not being forwarded
let cursor = this.log.events.indexOf(event);
if (cursor >= 0) {
this.cursor = cursor;
}
// Transfer control to the needed player
let player = this.getPlayerNeeded();
if (player) {
if (this.battle.playing_ship && !this.battle.playing_ship.alive) {
this.view.setInteractionEnabled(false);
this.battle.advanceToNextShip();
this.delayNextEvents(200);
} else if (player === this.view.player) {
this.view.setInteractionEnabled(true);
} else {
this.view.playAI();
}
} else {
this.view.setInteractionEnabled(false);
}
2014-12-31 00:00:00 +00:00
}
// Destroy the log processor
destroy() {
if (this.subscription) {
this.log.unsubscribe(this.subscription);
this.subscription = null;
this.queue = [];
2014-12-31 00:00:00 +00:00
}
}
2015-02-03 00:00:00 +00:00
// Playing ship changed
2017-02-09 00:00:35 +00:00
private processShipChangeEvent(event: ShipChangeEvent): void {
2017-03-15 00:24:56 +00:00
this.view.arena.setShipPlaying(event.new_ship);
this.view.ship_list.setPlaying(event.new_ship);
2017-05-30 18:23:35 +00:00
this.view.gameui.audio.playOnce("battle-ship-change");
2015-02-03 00:00:00 +00:00
}
// Damage to ship
2017-02-09 00:00:35 +00:00
private processDamageEvent(event: DamageEvent): void {
var item = this.view.ship_list.findItem(event.ship);
if (item) {
item.setDamageHit();
}
2015-02-03 00:00:00 +00:00
}
2015-02-18 00:00:00 +00:00
// A ship died
2017-02-09 00:00:35 +00:00
private processDeathEvent(event: DeathEvent): void {
2015-02-23 00:00:00 +00:00
if (this.view.ship_hovered === event.ship) {
this.view.setShipHovered(null);
}
this.view.arena.markAsDead(event.ship);
this.view.ship_list.markAsDead(event.ship);
2017-02-15 22:34:27 +00:00
if (!event.initial) {
this.delayNextEvents(1000);
}
2015-02-18 00:00:00 +00:00
}
2015-02-20 00:00:00 +00:00
// Weapon used
2017-02-09 00:00:35 +00:00
private processFireEvent(event: FireEvent): void {
var source = Target.newFromShip(event.ship);
var destination = event.target;
2015-02-20 00:00:00 +00:00
2017-02-15 16:41:24 +00:00
var effect = new WeaponEffect(this.view.arena, source, destination, event.weapon);
let duration = effect.start();
this.delayNextEvents(duration);
2015-02-20 00:00:00 +00:00
}
// Battle ended (victory or defeat)
2017-02-09 00:00:35 +00:00
private processEndBattleEvent(event: EndBattleEvent): void {
2017-03-12 23:32:41 +00:00
this.view.endBattle();
this.destroy();
}
2017-02-08 18:54:02 +00:00
// New drone deployed
2017-02-09 00:00:35 +00:00
private processDroneDeployedEvent(event: DroneDeployedEvent): void {
2017-02-15 22:34:27 +00:00
let duration = this.view.arena.addDrone(event.drone, !event.initial);
2017-05-17 16:21:14 +00:00
if (duration) {
this.view.gameui.audio.playOnce("battle-drone-deploy");
}
this.delayNextEvents(duration);
2017-02-08 18:54:02 +00:00
}
// Drone destroyed
2017-02-09 00:00:35 +00:00
private processDroneDestroyedEvent(event: DroneDestroyedEvent): void {
2017-02-08 18:54:02 +00:00
this.view.arena.removeDrone(event.drone);
2017-05-17 16:21:14 +00:00
if (!event.initial) {
this.view.gameui.audio.playOnce("battle-drone-destroy");
this.delayNextEvents(1000);
}
2017-02-08 18:54:02 +00:00
}
// Drone applied
private processDroneAppliedEvent(event: DroneAppliedEvent): void {
let drone = this.view.arena.findDrone(event.drone);
if (drone) {
let duration = drone.setApplied();
2017-05-17 16:21:14 +00:00
if (duration) {
this.view.gameui.audio.playOnce("battle-drone-activate");
}
this.delayNextEvents(duration);
}
}
2014-12-31 00:00:00 +00:00
}
2015-01-07 00:00:00 +00:00
}