Skip to content

Commit d159472

Browse files
committed
code cleanup, ai refactoring
1 parent 354d28f commit d159472

File tree

13 files changed

+83
-89
lines changed

13 files changed

+83
-89
lines changed

src/config.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export function getConfig(): GameConfig {
1616
return {
1717
cellSize: 38,
1818
maps: [{
19-
name: 'demo',
19+
name: 'demo1',
2020
armies: [
2121
['scout', 'scout', 'scout', 'assault', 'assault', 'assault', 'assault'],
2222
['scout', 'tank', 'tank', 'scout']
@@ -54,8 +54,9 @@ export function getConfig(): GameConfig {
5454
['assault', 'assault', 'assault', 'assault', 'assault'],
5555
['scout', 'scout', 'scout', 'assault', 'tank', 'tank']
5656
],
57-
layout: [
58-
['0', '0', 'W', 'W', ' ', ' ', 'W', 'W', ' ', ' ', 'M', '1', '1'],
57+
layout:
58+
[
59+
['0', '0', 'W', 'W', ' ', ' ', 'W', 'W', ' ', ' ', ' ', '1', '1'],
5960

6061
[' ', '0', 'W', 'W', ' ', ' ', ' ', 'W', ' ', 'M', 'M', '1', '1'],
6162

src/controllers/base.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ import Signal = Phaser.Signal;
44

55
@injectable()
66
export abstract class BaseController {
7-
abstract init(): void;
8-
abstract update(): void;
9-
abstract render(): void;
7+
preload(): void { }
8+
create(): void { }
9+
update(): void { }
10+
render(): void { }
1011
}

src/controllers/game/ai/controller.ts

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {GameStateManager, GameEvent} from "../../../services/state/game/service"
77
import {InputStateManager} from "../../../services/state/input/service";
88
import {IArmyCommandStrategyFactory} from "../../../services/army_command_strategy/factory/interface";
99
import {IArmyCommandStrategy} from "../../../services/army_command_strategy/interface";
10+
import {BaseUnit} from "../../../game_objects/units/base";
1011

1112
@injectable()
1213
export class AIController extends BaseController {
@@ -21,32 +22,49 @@ export class AIController extends BaseController {
2122
}
2223

2324
private _playerAis: IArmyCommandStrategy[] = [];
25+
private _numOfPlayers: number;
2426

25-
init() {
26-
this._gameState.subscribe(GameEvent.TurnComplete, playerNum => this._startNextTurn(playerNum));
2727

28-
let totalPlayers = this._gameState.getNumOfPlayers();
28+
create() {
29+
this._gameState.subscribe(GameEvent.TurnStart, this._onTurnStart);
30+
this._gameState.subscribe(GameEvent.UnitMoveActionSelected, this._onMoveActionSelected);
31+
this._gameState.subscribe(GameEvent.UnitMoveCompleted, this._onMoveCompleted);
32+
this._gameState.subscribe(GameEvent.UnitAttackActionSelected, this._onAttackActionSelected);
33+
this._gameState.subscribe(GameEvent.UnitAttackCompleted, this._onAttackCompleted);
34+
this._gameState.subscribe(GameEvent.TurnComplete, this._onTurnComplete);
2935

30-
for(let i =0; i < totalPlayers; i++) {
31-
this._playerAis.push(this._armyStategyFactory.getForPlayer(i));
36+
this._numOfPlayers = this._gameState.getNumOfPlayers();
37+
38+
for(let i = 0; i < this._numOfPlayers; i++) {
39+
this._playerAis.push(this._armyStategyFactory.create(i));
3240
}
3341

3442
this._gameState.dispatch(GameEvent.TurnStart, 0);
3543
}
3644

37-
update() {
38-
39-
}
40-
41-
render() {
45+
private _onTurnStart = (playerNum: number): void => {
46+
this._playerAis[playerNum].selectNextUnit();
47+
};
48+
49+
private _onMoveActionSelected = (unit: BaseUnit): void => {
50+
this._playerAis[unit.belongsToPlayer].moveUnit(unit);
51+
};
4252

43-
}
53+
private _onMoveCompleted = (unit: BaseUnit): void => {
54+
this._playerAis[unit.belongsToPlayer].beginAttack(unit);
55+
};
4456

45-
private _startNextTurn(playerNum: number): void {
46-
let totalPlayers = this._gameState.getNumOfPlayers();
57+
private _onAttackActionSelected = (unit: BaseUnit): void => {
58+
this._playerAis[unit.belongsToPlayer].attackWithUnit(unit);
59+
};
4760

48-
let nextPlayer = playerNum === totalPlayers - 1 ? 0 : playerNum + 1;
61+
private _onAttackCompleted = (unit: BaseUnit): void => {
62+
this._playerAis[unit.belongsToPlayer].selectNextUnit();
63+
};
64+
65+
private _onTurnComplete = (playerNum: number): void => {
66+
let nextPlayer = playerNum === (this._numOfPlayers - 1) ? 0 : (playerNum + 1);
4967

5068
this._gameState.dispatch(GameEvent.TurnStart, nextPlayer);
51-
}
69+
};
5270
}

src/controllers/game/contextmenu/controller.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,12 @@ export class ContextMenuController extends BaseController {
2121
super();
2222
}
2323

24-
init() {
24+
create() {
2525
this._gameState.subscribe(GameEvent.UnitSelected, unit => this._selectedUnit = unit);
2626
this._gameState.subscribe(GameEvent.CancelAction, unit => this._selectedUnit = null);
2727
this._inputState.subscribe(InputEvent.KeyAttack, this._onKeyAttack);
2828
}
2929

30-
update() { }
31-
32-
render() { }
33-
3430
// ------------------------------------
3531
// ---------- EVENT HANDLERS ----------
3632
// ------------------------------------

src/controllers/game/controller.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,13 @@ export class GameController extends BaseController {
1818
super();
1919
}
2020

21-
init() {
21+
create() {
2222
for(let i in [0, 1, 2, 3, 4, 5, 6, 7 ,8 ,9, 10]) {
2323
this._gameState.subscribe(parseInt(i), _ => console.log(GameEvent[parseInt(i)], _));
2424
}
2525

2626
this._mapBuilder.load('demo2');
2727
}
28-
29-
update() {
30-
}
3128

3229
render() {
3330
this._game.debug.text(this._game.time.fps || '--', 2, 14, "#a7aebe");

src/controllers/game/grid/controller.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export class GridController extends BaseController {
2727
super();
2828
}
2929

30-
init(): void {
30+
create(): void {
3131
this._inputState.subscribe(InputEvent.Tap, this._onTap);
3232
this._gameState.subscribe(GameEvent.UnitMoveActionSelected, this._onMoveActionSelected);
3333
this._gameState.subscribe(GameEvent.UnitAttackActionSelected, this._onAttackActionSelected);
@@ -81,8 +81,6 @@ export class GridController extends BaseController {
8181
}
8282
});
8383
}
84-
85-
render(){ }
8684

8785
// ------------------------------------
8886
// ---------- EVENT HANDLERS ----------

src/controllers/game/unit/controller.ts

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,13 @@ export class UnitController extends BaseController {
2525

2626
units: BaseUnit[];
2727

28-
init() {
28+
create() {
2929
this._gameState.subscribe(GameEvent.GridCellActivated, this._onCellActivated);
3030
this._gameState.subscribe(GameEvent.CancelAction, this._onCancelAction);
3131
this._gameState.subscribe(GameEvent.UnitMove, this._onUnitMove);
32+
this._gameState.subscribe(GameEvent.UnitMoveCompleted, this._onUnitMoveCompleted);
3233
this._gameState.subscribe(GameEvent.UnitAttack, this._onUnitAttack);
34+
this._gameState.subscribe(GameEvent.TurnComplete, this._onTurnComplete);
3335

3436
this.units = this._mapBuilder.buildUnits();
3537
this._gameState.units = this.units;
@@ -73,13 +75,21 @@ export class UnitController extends BaseController {
7375
this._moveUnit(this._selectedUnit, destinationCell)
7476
};
7577

78+
private _onUnitMoveCompleted = (unit: BaseUnit): void => {
79+
unit.hasMovedThisTurn = true;
80+
};
81+
7682
private _onUnitAttack = (defendingUnit: BaseUnit): void => {
7783
if (!this._selectedUnit)
7884
console.error('Cannot attack, no unit selected!', defendingUnit);
7985

8086
this._handleCombat(this._selectedUnit, defendingUnit);
8187
};
82-
88+
89+
private _onTurnComplete = (playerNum: number): void => {
90+
this.units.filter(u => u.belongsToPlayer === playerNum).forEach(u => u.hasMovedThisTurn = false);
91+
};
92+
8393
// ---------------------------------------
8494
// ---------- HELPER FUNCTIONS ----------
8595
// ---------------------------------------
@@ -94,27 +104,29 @@ export class UnitController extends BaseController {
94104
let xPos = [unit.x];
95105
let yPos = [unit.y];
96106

107+
let time = 100;
108+
97109
for(let i in path) {
98110
switch(path[i]) {
99111
case 'up':
100112
yPos.push(--yy);
101113
xPos.push(xx);
102-
tween.to({ isoY: (yy) * this._config.cellSize }, 150, Phaser.Easing.Linear.None);
114+
tween.to({ isoY: (yy) * this._config.cellSize }, time, Phaser.Easing.Linear.None);
103115
break;
104116
case 'down':
105117
yPos.push(++yy);
106118
xPos.push(xx);
107-
tween.to({ isoY: (yy) * this._config.cellSize }, 150, Phaser.Easing.Linear.None);
119+
tween.to({ isoY: (yy) * this._config.cellSize }, time, Phaser.Easing.Linear.None);
108120
break;
109121
case 'left':
110122
xPos.push(--xx);
111123
yPos.push(yy);
112-
tween.to({ isoX: (xx) * this._config.cellSize }, 150, Phaser.Easing.Linear.None);
124+
tween.to({ isoX: (xx) * this._config.cellSize }, time, Phaser.Easing.Linear.None);
113125
break;
114126
case 'right':
115127
xPos.push(++xx);
116128
yPos.push(yy);
117-
tween.to({ isoX: (xx) * this._config.cellSize }, 150, Phaser.Easing.Linear.None);
129+
tween.to({ isoX: (xx) * this._config.cellSize }, time, Phaser.Easing.Linear.None);
118130
break;
119131
default:
120132
console.error('Invalid path');

src/controllers/input/controller.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export class InputController extends BaseController {
2020
super();
2121
}
2222

23-
init() {
23+
create() {
2424
// for(let i in [0,1,2,3]) {
2525
// this.subscribe(parseInt(i), _ => console.log(InputEvent[parseInt(i)], _));
2626
// }
@@ -50,8 +50,6 @@ export class InputController extends BaseController {
5050
this._isAttackKeyDown = false;
5151
}
5252
}
53-
54-
render(){}
5553

5654
private _initKey(keyCode: number): Key {
5755
let key = this._game.input.keyboard.addKey(keyCode);

src/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,14 @@ class TbsGame {
5050
//TODO: move to controller preload()
5151
this.game['isoGridGroup'] = this.game.add.group();
5252
this.game['isoUnitsGroup'] = this.game.add.group();
53+
54+
this.controllers.forEach(_ => _.preload());
5355
}
5456

5557
create() {
5658
this.game.stage.backgroundColor = '#aaccff';
5759

58-
this.controllers.forEach(_ => _.init());
60+
this.controllers.forEach(_ => _.create());
5961
}
6062

6163
render() {
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import {BaseUnit} from "../../../game_objects/units/base";
21
import {IArmyCommandStrategy} from "../interface";
32

43
export interface IArmyCommandStrategyFactory {
5-
getForPlayer(playerNum: number): IArmyCommandStrategy;
4+
create(playerNum: number): IArmyCommandStrategy;
65
}

0 commit comments

Comments
 (0)