-
Notifications
You must be signed in to change notification settings - Fork 8
/
Battler.ts
204 lines (171 loc) · 5.79 KB
/
Battler.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import {action, computed, observable, reaction} from 'mobx';
import {Stats} from './Stats';
import {Character} from './Character';
import {Skill} from './Skill';
import {list, object, serializable} from 'serializr';
import {cmp, contains, moveItem} from '../../lib/Helpers';
import {randomizeItem} from '../../lib/Helpers';
import {CharacterStatus} from './CharacterStatus';
type AllyOrEnemy = Character;
export class Battler<
TAlly extends AllyOrEnemy = AllyOrEnemy,
TEnemy extends AllyOrEnemy = AllyOrEnemy
> {
@serializable @observable turn: number = 0;
@serializable @observable turnActorIndex: number = 0;
@serializable(list(object(Character))) @observable enemies: TEnemy[] = [];
@serializable(list(object(Character))) @observable deceasedEnemies: TEnemy[] = [];
// No need to serialize since it's automated by quest behavior
@observable public allies: TAlly[] = null;
@observable private enemySource: TEnemy[] = null;
@observable public inBattle: boolean = false;
@computed get turnActorOrder (): AllyOrEnemy[] {
return [...this.allies, ...this.enemies]
.sort((a, b) => cmp(a.stats.speed, b.stats.speed));
}
@computed get turnActor () {
return this.turnActorOrder[this.turnActorIndex];
}
@computed get canHeroAct () {
return contains(this.allies, this.turnActor);
}
@computed get canEnemyAct () {
return contains(this.enemies, this.turnActor);
}
getActorIndex (actor: AllyOrEnemy) {
return this.turnActorOrder.indexOf(actor);
}
@action
startBattle (enemySource: TEnemy []) {
if (this.inBattle) {
console.warn('Should not initiate a new battle while already in one');
this.endBattle();
}
// Move monsters from their source to the battle
// NOTE this is to avoid duplicates when serializing
this.enemySource = enemySource;
this.enemySource.filter((e) => e.isAlive)
.forEach((enemy) => moveItem(enemy, this.enemySource, this.enemies));
this.inBattle = this.enemies.length > 0;
if (this.inBattle) {
console.log('Starting battle', this.allies, 'vs', this.enemies);
}
}
@action
endBattle () {
if (!this.inBattle) {
console.warn('Attempting to end a battle when not in one');
return;
}
console.log('Ending battle');
const killedAllEnemies = this.enemies.length === 0;
// Heal/Revive and return enemies to their source
while (this.enemies.length) {
const enemy = this.enemies.pop();
enemy.resetMutableStats();
console.log('Healing undefeated enemy', enemy);
this.enemySource.push(enemy);
}
while (this.deceasedEnemies.length) {
const enemy = this.deceasedEnemies.pop();
if (!killedAllEnemies) {
enemy.resetMutableStats();
console.log('Reviving defeated enemy', enemy);
}
this.enemySource.push(enemy);
}
this.turn = 0;
this.turnActorIndex = 0;
this.enemySource = null;
this.inBattle = false;
}
performTurnAction (skill?: Skill, targets: AllyOrEnemy[] = []) {
if (skill) {
console.log(this.turnActor.name, 'used', skill.info.name, 'on', targets.map((t) => t.name).join(', '));
targets.map((target) =>
this.emitMemento(target, this.turnActor.useSkill(skill, target))
);
}
this.turnActorIndex += 1;
}
passTurnAction (reason = '') {
const reasonSuffix = reason ? ' (' + reason + ')' : undefined;
console.log(this.turnActor.name, 'passed', reasonSuffix);
this.turnActorIndex += 1;
}
gotoNextTurn () {
this.turnActorIndex = 0;
this.turn += 1;
}
processTurn () {
console.log('Finishing turn', this.turn);
[...this.allies, ...this.enemies].forEach((c) =>
this.emitMemento(c, c.processTurn())
);
}
emitMemento (target: Character, memento: Stats) {
const mementoString = memento.nonNeutral
.map((stat) => stat.info.shortName + ' ' + stat.toString())
.join(', ');
if (mementoString) {
console.info(target.name, 'processed', mementoString);
}
}
initialize (allies: TAlly[]): (() => void)[] {
this.allies = allies;
return [
// Process turn as soon as it changes
reaction(
() => this.turn,
() => this.processTurn()
),
// Battle victory
reaction(
() => this.enemies.length === 0 && this.deceasedEnemies.length > 0,
(shouldEnd) => shouldEnd && this.endBattle(),
true
),
// After an action or death we may want to chance turn
reaction(
() => this.turnActorIndex >= (this.turnActorOrder.length - 1),
(isEndOfTurn) => isEndOfTurn && this.gotoNextTurn(),
true
),
// Move deceased enemies to their own list
reaction(
() => this.enemies.filter((m) => m.isDead),
(dead) => dead.forEach((m) => moveItem(m, this.enemies, this.deceasedEnemies)),
true
),
// Turn automation
reaction(
() => [this.turn, this.turnActorIndex, this.inBattle],
([turn, idx, inBattle]) => {
if (!inBattle || !this.turnActor) {
return;
}
if (this.turnActor.dots.get(CharacterStatus.Stun)) {
this.passTurnAction('stunned');
return;
}
if (!this.canEnemyAct) {
return;
}
// Enemy AI
const usableSkills = this.turnActor.selectedSkills.filter((s) =>
s.info.canUse(this.turnActor, this.enemies, this.allies)
);
const skill = randomizeItem(usableSkills);
if (skill) {
const targets = skill.info.target.select(this.enemies, this.allies);
this.performTurnAction(skill, targets);
} else {
// TODO move towards closest applicable position
this.passTurnAction('no usable skills');
}
},
true
)
];
}
}