Skip to content

Commit 49bd94d

Browse files
committed
unit movement bug fixes
1 parent 7381447 commit 49bd94d

File tree

2 files changed

+44
-59
lines changed

2 files changed

+44
-59
lines changed

src/controllers/game/grid/controller.ts

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -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 {

src/controllers/game/unit/controller.ts

Lines changed: 30 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ export class UnitController extends BaseController {
7878
};
7979

8080
private _onUnitMove = (cell: GridCell): void => {
81-
this._moveUnit2(this._selectedUnit, cell);
81+
this._moveUnit(this._selectedUnit, cell);
8282
};
8383

8484
private _onUnitAttack = (defendingUnit: BaseUnit): void => {
@@ -93,72 +93,56 @@ export class UnitController extends BaseController {
9393
// ---------------------------------------
9494

9595
private _moveUnit(unit: BaseUnit, targetCell: GridCell): void {
96-
let x = targetCell.x, y = targetCell.y;
97-
let skipXAnimation = x === unit.x;
98-
99-
let movementX = this._ctrl.game.add.tween(unit.spr)
100-
.to({ isoX: x * this._ctrl.config.cellSize }, Math.abs(unit.x - x)*150, Phaser.Easing.Linear.None);
101-
102-
movementX.onComplete.add(() => unit.setYPosition(y));
103-
104-
var movementY = this._ctrl.game.add.tween(unit.spr)
105-
.to({ isoY: y * this._ctrl.config.cellSize }, Math.abs(unit.y - y)*150, Phaser.Easing.Linear.None);
106-
107-
movementY.onComplete.add(() => this._ctrl.dispatch(GameEvent.UnitMoveCompleted, unit));
108-
109-
unit.setXPosition(x);
110-
111-
if(skipXAnimation) {
112-
unit.setYPosition(y)
113-
movementY.start();
114-
} else {
115-
movementX.chain(movementY).start();
116-
}
117-
}
118-
119-
private _moveUnit2(unit: BaseUnit, targetCell: GridCell): void {
12096
let path = targetCell.pathFromActiveCell;
12197

122-
let tweens: Tween = [];
98+
let tween = this._ctrl.game.add.tween(unit.spr);
12399
let xx = unit.x;
124-
let yy = unit.y
100+
let yy = unit.y;
101+
102+
let xPos = [unit.x];
103+
let yPos = [unit.y];
125104

126105
for(let i in path) {
127106
let currentTween;
128107

129108
switch(path[i]) {
130109
case 'up':
131-
currentTween = this._ctrl.game.add.tween(unit.spr)
132-
.to({ isoY: (--yy) * this._ctrl.config.cellSize }, 150, Phaser.Easing.Linear.None);
133-
currentTween.onStart.add(() => unit.setYPosition(unit.y - 1));
110+
yPos.push(--yy);
111+
xPos.push(xx);
112+
tween.to({ isoY: (yy) * this._ctrl.config.cellSize }, 150, Phaser.Easing.Linear.None);
134113
break;
135114
case 'down':
136-
currentTween = this._ctrl.game.add.tween(unit.spr)
137-
.to({ isoY: (++yy) * this._ctrl.config.cellSize }, 150, Phaser.Easing.Linear.None);
138-
currentTween.onStart.add(() => unit.setYPosition(unit.y + 1));
115+
yPos.push(++yy);
116+
xPos.push(xx);
117+
tween.to({ isoY: (yy) * this._ctrl.config.cellSize }, 150, Phaser.Easing.Linear.None);
139118
break;
140119
case 'left':
141-
currentTween = this._ctrl.game.add.tween(unit.spr)
142-
.to({ isoX: (--xx) * this._ctrl.config.cellSize }, 150, Phaser.Easing.Linear.None);
143-
currentTween.onStart.add(() => unit.setXPosition(unit.x - 1));
120+
xPos.push(--xx);
121+
yPos.push(yy);
122+
tween.to({ isoX: (xx) * this._ctrl.config.cellSize }, 150, Phaser.Easing.Linear.None);
144123
break;
145124
case 'right':
146-
currentTween = this._ctrl.game.add.tween(unit.spr)
147-
.to({ isoX: (++xx) * this._ctrl.config.cellSize }, 150, Phaser.Easing.Linear.None);
148-
currentTween.onStart.add(() => unit.setXPosition(unit.x + 1));
125+
xPos.push(++xx);
126+
yPos.push(yy);
127+
tween.to({ isoX: (xx) * this._ctrl.config.cellSize }, 150, Phaser.Easing.Linear.None);
149128
break;
150129
default:
151130
console.error('Invalid path');
152131
}
153-
if(tweens.length) {
154-
tweens[tweens.length-1].chain(currentTween);
155-
}
156-
157-
tweens.push(currentTween);
158132
}
159133

160-
tweens[tweens.length - 1].onComplete.add(() => this._ctrl.dispatch(GameEvent.UnitMoveCompleted, unit));
161-
tweens[0].start();
134+
let posIndex = 1;
135+
tween.onChildComplete.add((a,b) => {
136+
if(posIndex >= xPos.length) return;
137+
posIndex++;
138+
console.log('x', a.x, xPos[posIndex]);
139+
unit.setXPosition(xPos[posIndex]);
140+
unit.setYPosition(yPos[posIndex]);
141+
});
142+
unit.setXPosition(xPos[1]);
143+
unit.setYPosition(yPos[1]);
144+
tween.onComplete.add(() => this._ctrl.dispatch(GameEvent.UnitMoveCompleted, unit));
145+
tween.start();
162146
}
163147

164148
private _handleCombat(attackingUnit: BaseUnit, defendingUnit: BaseUnit): void {

0 commit comments

Comments
 (0)