@@ -39,7 +39,9 @@ export class GridController extends BaseController {
3939 // The last parameter is the group you want to add it to (just like game.add.sprite)
4040 let tileSpr = this . _ctrl . game . add . isoSprite ( xx * this . _ctrl . config . cellSize , yy * this . _ctrl . config . cellSize , 0 , 'tile' , 0 , this . isoGridGroup ) ;
4141 tileSpr . anchor . set ( 0.5 , 0 ) ;
42- this . cells . push ( new GridCell ( tileSpr , xx , yy ) ) ;
42+ let newCell = new GridCell ( tileSpr , xx , yy ) ;
43+ if ( Math . random ( ) < .3 ) { newCell . spr . tint = 0x555555 ; newCell . isObstacle = true ; }
44+ this . cells . push ( newCell ) ;
4345 }
4446 }
4547 }
@@ -174,8 +176,9 @@ export class GridController extends BaseController {
174176 } ) ;
175177 }
176178
177- private _highlightCellsInRange ( cell : GridCell , range : number , isAttack : boolean = false , previousCell : GridCell = null , depth : number = 0 , originalCell = null ) {
178- if ( ! range || ! cell || cell . isObstacle )
179+ private _highlightCellsInRange ( cell : GridCell , range : number , isAttack : boolean = false , previousCell : GridCell = null ) {
180+ if ( ! range || ! cell || cell . isObstacle
181+ || ( cell . active && ! ! previousCell ) ) //if we looped back to the original cell, just stop
179182 return ;
180183
181184 if ( isAttack || ! this . _getUnitAt ( cell ) ) {
@@ -190,27 +193,25 @@ export class GridController extends BaseController {
190193
191194 if ( ! previousCell ) {
192195 this . cells . forEach ( c => c . pathFromActiveCell = [ ] ) ;
193- originalCell = cell ;
194196 }
195197 else {
196- let distanceTravelled = Math . abs ( cell . x - originalCell . x ) + Math . abs ( cell . y - originalCell . y ) ;
197- if ( distanceTravelled - 1 === previousCell . pathFromActiveCell . length && previousCell . pathFromActiveCell . length === depth - 1 ) {
198- direction = cell . x === previousCell . x
199- ? cell . y - previousCell . y > 0 ? 'down' : 'up'
200- : cell . x - previousCell . x > 0 ? 'right' : 'left' ;
198+ direction = cell . x === previousCell . x
199+ ? cell . y - previousCell . y > 0 ? 'down' : 'up'
200+ : cell . x - previousCell . x > 0 ? 'right' : 'left' ;
201201
202+ if ( cell . pathFromActiveCell . length === 0 || previousCell . pathFromActiveCell . length <= cell . pathFromActiveCell . length ) {
202203 cell . pathFromActiveCell = previousCell . pathFromActiveCell . concat ( [ direction ] ) ;
203204 }
204205 }
205206
206207 if ( direction !== 'left' )
207- this . _highlightCellsInRange ( this . cells . find ( c => c . x === cell . x + 1 && c . y === cell . y ) , range - 1 , isAttack , cell , depth + 1 , originalCell ) ;
208+ this . _highlightCellsInRange ( this . cells . find ( c => c . x === cell . x + 1 && c . y === cell . y ) , range - 1 , isAttack , cell ) ;
208209 if ( direction !== 'right' )
209- this . _highlightCellsInRange ( this . cells . find ( c => c . x === cell . x - 1 && c . y === cell . y ) , range - 1 , isAttack , cell , depth + 1 , originalCell ) ;
210+ this . _highlightCellsInRange ( this . cells . find ( c => c . x === cell . x - 1 && c . y === cell . y ) , range - 1 , isAttack , cell ) ;
210211 if ( direction !== 'up' )
211- this . _highlightCellsInRange ( this . cells . find ( c => c . x === cell . x && c . y === cell . y + 1 ) , range - 1 , isAttack , cell , depth + 1 , originalCell ) ;
212+ this . _highlightCellsInRange ( this . cells . find ( c => c . x === cell . x && c . y === cell . y + 1 ) , range - 1 , isAttack , cell ) ;
212213 if ( direction !== 'down' )
213- this . _highlightCellsInRange ( this . cells . find ( c => c . x === cell . x && c . y === cell . y - 1 ) , range - 1 , isAttack , cell , depth + 1 , originalCell ) ;
214+ this . _highlightCellsInRange ( this . cells . find ( c => c . x === cell . x && c . y === cell . y - 1 ) , range - 1 , isAttack , cell ) ;
214215 }
215216
216217 private _getUnitAt ( cell : GridCell ) : BaseUnit {
0 commit comments