-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDelayedAction.ts
64 lines (53 loc) · 1.54 KB
/
DelayedAction.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import { Action, IAction } from './Action';
import {
RuleRegistry,
instance as ruleRegistryInstance,
} from '@civ-clone/core-rule/RuleRegistry';
import {
Turn,
instance as turnInstance,
} from '@civ-clone/core-turn-based-game/Turn';
import Busy from './Rules/Busy';
import Criterion from '@civ-clone/core-rule/Criterion';
import Effect from '@civ-clone/core-rule/Effect';
import Moved from './Rules/Moved';
import Tile from '@civ-clone/core-world/Tile';
import Unit from './Unit';
export interface IDelayedAction extends IAction {
perform(turns: number, action: (...args: any[]) => void): void;
}
export class DelayedAction extends Action implements IDelayedAction {
#turn: Turn;
constructor(
from: Tile,
to: Tile,
unit: Unit,
ruleRegistry: RuleRegistry = ruleRegistryInstance,
turn: Turn = turnInstance
) {
super(from, to, unit, ruleRegistry);
this.#turn = turn;
}
perform(
turns: number,
action: (...args: any[]) => void = () => {},
BusyRule: typeof Busy = Busy
): void {
const endTurn: number = this.#turn.value() + turns;
this.unit().setActive(false);
this.unit().moves().set(0);
this.unit().setBusy(
new BusyRule(
new Criterion((): boolean => this.#turn.value() === endTurn),
new Effect((...args: any[]): void => {
const unit: Unit = this.unit();
unit.setActive();
unit.setBusy();
action(...args);
this.ruleRegistry().process(Moved, this.unit(), this);
})
)
);
}
}
export default DelayedAction;