Skip to content

Commit eb10ed4

Browse files
committed
move unit and grid creation responsibilities into MapBuilder
1 parent 6315077 commit eb10ed4

File tree

8 files changed

+169
-164
lines changed

8 files changed

+169
-164
lines changed

src/controllers/game/controller.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,29 @@ import Game = Phaser.Game;
44
import {GameConfig} from "../../config";
55
import Signal = Phaser.Signal;
66
import {BaseController} from "../base";
7+
import {IMapBuilder} from "../../services/map_builder/interface";
78

89
@injectable()
910
export class GameController extends BaseController {
1011
private _cache: {[key:string]: any} = {};
1112

12-
constructor(private _game: Game, @inject('config')private _config: GameConfig) {
13+
constructor(
14+
private _game: Game,
15+
@inject('config') private _config: GameConfig,
16+
@inject('IMapBuilder') private _mapBuilder: IMapBuilder
17+
) {
1318
super();
1419
}
1520

1621
init() {
17-
// for(let i in [0,1,2,3,4, 5, 6, 7, 8]) {
18-
// this.subscribe(parseInt(i), _ => console.log(GameEvent[parseInt(i)], _));
19-
// }
20-
this._game.iso.simpleSort(this._game['isoGridGroup']);
22+
for(let i in [0,1,2,3,4, 5, 6, 7, 8]) {
23+
this.subscribe(parseInt(i), _ => console.log(GameEvent[parseInt(i)], _));
24+
}
25+
26+
this._mapBuilder.load('demo');
2127
}
2228

2329
update() {
24-
this._game.iso.simpleSort(this._game['isoUnitsGroup']);
2530
}
2631

2732
render() {

src/controllers/game/grid/controller.ts

Lines changed: 9 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,12 @@ import {InputController, InputEvent} from "../../input/controller";
55
import {BaseController} from "../../base";
66
import Group = Phaser.Group;
77
import {BaseUnit} from "../../../game_objects/units/base";
8-
import {GameConfig, MapLayout} from "../../../config";
98
import Game = Phaser.Game;
109
import {GridCell} from "../../../game_objects/grid/grid_cell";
11-
import {WaterCell} from "../../../game_objects/grid/water";
12-
import {BridgeCell} from "../../../game_objects/grid/bridge";
13-
import {MountainCell} from "../../../game_objects/grid/mountain";
10+
import {IMapBuilder} from "../../../services/map_builder/interface";
1411

1512
@injectable()
1613
export class GridController extends BaseController {
17-
isoGridGroup: Group;
1814
cells: GridCell[];
1915

2016
private _activeCell: GridCell;
@@ -24,54 +20,25 @@ export class GridController extends BaseController {
2420

2521
constructor(
2622
private _game: Game,
27-
private _ctrl: GameController,
23+
public _ctrl: GameController,
2824
private _input: InputController,
29-
@inject('config') private _config: GameConfig
25+
@inject('IMapBuilder') private _mapBuilder: IMapBuilder
3026
) {
3127
super();
32-
this.cells = [];
33-
_ctrl.set('cells', this.cells);
3428
}
3529

3630
init(): void {
3731
this._input.subscribe(InputEvent.Tap, this._onTap);
3832
this._ctrl.subscribe(GameEvent.UnitMoveActionSelected, this._onMoveActionSelected);
3933
this._ctrl.subscribe(GameEvent.UnitAttackActionSelected, this._onAttackActionSelected);
4034
this._ctrl.subscribe(GameEvent.CancelAction, this._onCancelAction);
41-
this._ctrl.subscribe(GameEvent.UnitMove, () => this._canActivateCells = false);
42-
this._ctrl.subscribe(GameEvent.UnitMoveCompleted, () => this._canActivateCells = true);
43-
44-
this.isoGridGroup = this._game['isoGridGroup'];
45-
46-
let mapName = 'demo'; //TODO: move this into a separate loadMap fn or mapBuilder
47-
48-
let mapLayout: string[][] = this._config.maps.find(x => x.name === mapName).layout;
49-
50-
for (let xx = 0; xx < mapLayout[0].length; xx++) {
51-
for (let yy = 0; yy < mapLayout.length; yy++) {
52-
// Create a tile using the new game.add.isoSprite factory method at the specified position.
53-
// The last parameter is the group you want to add it to (just like game.add.sprite)
54-
let tileSpr = this._game.add.isoSprite(xx * this._config.cellSize, yy * this._config.cellSize, 0, 'tile', 0, this.isoGridGroup);
55-
tileSpr.anchor.set(0.5, 0);
56-
57-
let newCell: GridCell;
58-
59-
//TODO: move to map builder or use a type map
60-
if (mapLayout[yy][xx] === 'W') {
61-
newCell = new WaterCell(tileSpr, xx, yy);
62-
let waterAnimation = this._game.add.tween(newCell.spr).to({isoZ: -5}, 800, Phaser.Easing.Sinusoidal.InOut, false, 0, 0, true).loop(true);
63-
setTimeout(() => waterAnimation.start(), Math.random() * 1000);
64-
} else if (mapLayout[yy][xx] === 'M') {
65-
newCell = new MountainCell(tileSpr, xx, yy);
66-
} else if (mapLayout[yy][xx] === 'B') {
67-
newCell = new BridgeCell(tileSpr, xx, yy);
68-
} else {
69-
newCell = new GridCell(tileSpr, xx, yy);
70-
}
35+
this._ctrl.subscribe(GameEvent.UnitMove, (): void => {this._canActivateCells = false;}); // returning false will cancel the event.
36+
this._ctrl.subscribe(GameEvent.UnitMoveCompleted, (): void => {this._canActivateCells = true;});
7137

72-
this.cells.push(newCell);
73-
}
74-
}
38+
this.cells = this._mapBuilder.buildGrid();
39+
this._ctrl.set('cells', this.cells);
40+
41+
this._game.iso.simpleSort(this._game['isoGridGroup']);
7542
}
7643

7744
update() {

src/controllers/game/unit/controller.ts

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@ import {injectable, inject} from "inversify";
33
import Game = Phaser.Game;
44
import {BaseUnit} from "../../../game_objects/units/base";
55
import {GameController, GameEvent} from "../controller";
6-
import {InputController} from "../../input/controller";
76
import {GridCell} from "../../../game_objects/grid/grid_cell";
87
import {BaseController} from "../../base";
98
import Tween = Phaser.Tween;
109
import {GameConfig} from "../../../config";
11-
import {ArmyBuilder} from "../../../services/army_builder";
10+
import {IMapBuilder} from "../../../services/map_builder/interface";
1211

1312

1413
@injectable()
@@ -18,10 +17,9 @@ export class UnitController extends BaseController {
1817

1918
constructor(
2019
private _game: Game,
21-
private _ctrl: GameController,
22-
private _input: InputController,
20+
public _ctrl: GameController,
2321
@inject('config') private _config: GameConfig,
24-
private _armyBuilder: ArmyBuilder
22+
@inject('IMapBuilder') private _mapBuilder: IMapBuilder
2523
) {
2624
super();
2725
}
@@ -34,11 +32,13 @@ export class UnitController extends BaseController {
3432
this._ctrl.subscribe(GameEvent.UnitMove, this._onUnitMove);
3533
this._ctrl.subscribe(GameEvent.UnitAttack, this._onUnitAttack);
3634

37-
this.units = this._armyBuilder.build();
35+
this.units = this._mapBuilder.buildUnits();
3836
this._ctrl.set('units', this.units);
3937
}
4038

41-
update() { }
39+
update() {
40+
this._game.iso.simpleSort(this._game['isoUnitsGroup']);
41+
}
4242

4343
render() {
4444
if(!!this._selectedUnit) {
@@ -66,10 +66,13 @@ export class UnitController extends BaseController {
6666
this._selectedUnit = null;
6767
};
6868

69-
private _onUnitMove = (cell: GridCell): void => {
70-
this._moveUnit(this._selectedUnit, cell);
69+
private _onUnitMove = (destinationCell: GridCell): void => {
70+
if(!destinationCell)
71+
return;
72+
73+
this._moveUnit(this._selectedUnit, destinationCell)
7174
};
72-
75+
7376
private _onUnitAttack = (defendingUnit: BaseUnit): void => {
7477
if (!this._selectedUnit)
7578
console.error('Cannot attack, no unit selected!', defendingUnit);
@@ -134,7 +137,7 @@ export class UnitController extends BaseController {
134137
private _handleCombat(attackingUnit: BaseUnit, defendingUnit: BaseUnit): void {
135138
let damage = attackingUnit.stats.attack - defendingUnit.stats.armor;
136139

137-
//face each other
140+
// make units face each other when attacking
138141
attackingUnit.pointToUnit(defendingUnit);
139142
defendingUnit.pointToUnit(attackingUnit);
140143

@@ -162,14 +165,6 @@ export class UnitController extends BaseController {
162165
});
163166
}
164167

165-
//TODO: delete me at some point
166-
// private _createUnit(unit: Unit): BaseUnit {
167-
// let spr = this._game.add.isoSprite(unit.x*this._config.cellSize, unit.y*this._config.cellSize, 0, unit.asset, 0);
168-
// let unitObj = new (this.unitTypeMap[unit.name])(unit, spr);
169-
//
170-
// this.units.push(unitObj);
171-
// return unitObj;
172-
// }
173168
//TODO: we need some kind of TurnExecutor to run the turn in sequence.
174169
// for the team in action phase, execute the moves and actions one at a time
175170
}

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ class TbsGame {
4444

4545
//game settings
4646
this.game.iso.anchor.setTo(0.5, 0.2);
47+
//TODO: move to controller preload()
4748
this.game['isoGridGroup'] = this.game.add.group();
4849
this.game['isoUnitsGroup'] = this.game.add.group();
4950
}

src/inversify.config.ts

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,31 @@ import {GameController} from "./controllers/game/controller";
88
import {InputController} from "./controllers/input/controller";
99
import {BaseController} from "./controllers/base";
1010
import {GameConfig, getConfig} from "./config";
11+
import {IMapBuilder} from "./services/map_builder/interface";
12+
import {MapBuilder} from "./services/map_builder/service";
1113
import Factory = interfaces.Factory;
12-
import {ArmyBuilder} from "./services/army_builder";
1314

1415
let container = new Container();
1516

1617
container.bind<GameConfig>('config').toConstantValue(getConfig());
1718

18-
container.bind<UnitController>(UnitController).toSelf().inSingletonScope();
19-
container.bind<GridController>(GridController).toSelf().inSingletonScope();
20-
container.bind<ContextMenuController>(ContextMenuController).toSelf().inSingletonScope();
21-
container.bind<GameController>(GameController).toSelf().inSingletonScope();
2219
container.bind<InputController>(InputController).toSelf().inSingletonScope();
20+
container.bind<GameController>(GameController).toSelf().inSingletonScope();
21+
container.bind<ContextMenuController>(ContextMenuController).toSelf().inSingletonScope();
22+
container.bind<GridController>(GridController).toSelf().inSingletonScope();
23+
container.bind<UnitController>(UnitController).toSelf().inSingletonScope();
2324

24-
container.bind<ArmyBuilder>(ArmyBuilder).toSelf().inSingletonScope();
25+
container.bind<IMapBuilder>('IMapBuilder').to(MapBuilder).inSingletonScope();
2526

2627
container.bind<Factory<BaseController[]>>('controllers').toFactory<BaseController[]>((context: interfaces.Context) => {
2728
return () => [
28-
context.container.get<UnitController>(UnitController),
29-
context.container.get<GridController>(GridController),
30-
context.container.get<ContextMenuController>(ContextMenuController),
31-
context.container.get<GameController>(GameController),
32-
context.container.get<InputController>(InputController)
33-
]
29+
// the order of the controllers here determines the order in which init(), update(), etc... runs for the entire game
30+
context.container.get<InputController>(InputController),
31+
context.container.get<GameController>(GameController),
32+
context.container.get<ContextMenuController>(ContextMenuController),
33+
context.container.get<GridController>(GridController),
34+
context.container.get<UnitController>(UnitController),
35+
];
3436
});
3537

3638
export default container;

src/services/army_builder.ts

Lines changed: 0 additions & 85 deletions
This file was deleted.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import {GridCell} from "../../game_objects/grid/grid_cell";
2+
import {BaseUnit} from "../../game_objects/units/base";
3+
4+
export interface IMapBuilder {
5+
load(mapName: string): void;
6+
buildUnits(): BaseUnit[];
7+
buildGrid(): GridCell[];
8+
}

0 commit comments

Comments
 (0)