@@ -5,16 +5,12 @@ import {InputController, InputEvent} from "../../input/controller";
55import { BaseController } from "../../base" ;
66import Group = Phaser . Group ;
77import { BaseUnit } from "../../../game_objects/units/base" ;
8- import { GameConfig , MapLayout } from "../../../config" ;
98import Game = Phaser . Game ;
109import { 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 ( )
1613export 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 ( ) {
0 commit comments