Skip to content

Commit 79b804f

Browse files
committed
clean up ids
1 parent 956b7f8 commit 79b804f

File tree

4 files changed

+52
-36
lines changed

4 files changed

+52
-36
lines changed

src/animationsController.js

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -267,9 +267,11 @@ module.exports = class AnimationsController {
267267
/**
268268
* Schedule the animations for a move from the current position
269269
* @param {number} endX X coordinate of the target position
270-
* @param {number} endY Y coordinate of the target position
270+
* @param {number} endY Y coordinate of the target position
271+
* @param {string} id Optional id of pegman. If no id is provided,
272+
* will schedule move for default pegman.
271273
*/
272-
scheduleMove(endX, endY, timeForAnimation, id = null) {
274+
scheduleMove(endX, endY, timeForAnimation, id) {
273275
var startX = this.maze.getPegmanX(id);
274276
var startY = this.maze.getPegmanY(id);
275277
var direction = this.maze.getPegmanD(id);
@@ -336,9 +338,11 @@ module.exports = class AnimationsController {
336338

337339
/**
338340
* Schedule the animations for a turn from the current direction
339-
* @param {number} endDirection The direction we're turning to
341+
* @param {number} endDirection The direction we're turning to
342+
* @param {string} id Optional id of pegman. If no id is provided,
343+
* will schedule turn for default pegman.
340344
*/
341-
scheduleTurn(endDirection, id = null) {
345+
scheduleTurn(endDirection, id) {
342346
var numFrames = 4;
343347
var startDirection = this.maze.getPegmanD(id);
344348
var deltaDirection = endDirection - startDirection;
@@ -392,7 +396,7 @@ module.exports = class AnimationsController {
392396
}
393397
}
394398

395-
scheduleWallHit(targetX, targetY, deltaX, deltaY, frame, id = null) {
399+
scheduleWallHit(targetX, targetY, deltaX, deltaY, frame, id) {
396400
// Play the animation of hitting the wall
397401
const pegmanX = this.maze.getPegmanX(id);
398402
const pegmanY = this.maze.getPegmanY(id);
@@ -463,7 +467,7 @@ module.exports = class AnimationsController {
463467
}
464468
}
465469

466-
scheduleObstacleHit(targetX, targetY, deltaX, deltaY, frame, id = null) {
470+
scheduleObstacleHit(targetX, targetY, deltaX, deltaY, frame, id) {
467471
// Play the animation
468472
var obsId = targetX + this.maze.map.COLS * targetY;
469473
var obsIcon = document.getElementById('obstacle' + obsId);
@@ -540,8 +544,10 @@ module.exports = class AnimationsController {
540544
* @param {boolean} victoryDance This is a victory dance after completing the
541545
* puzzle (vs. dancing on load).
542546
* @param {integer} timeAlloted How much time we have for our animations
547+
* @param {string} id Optional id of pegman. If no id is provided, will schedule
548+
* dance for default pegman.
543549
*/
544-
scheduleDance(victoryDance, timeAlloted, id = null) {
550+
scheduleDance(victoryDance, timeAlloted, id) {
545551
const finishIcon = document.getElementById('finish');
546552
const pegmanX = this.maze.getPegmanX(id);
547553
const pegmanY = this.maze.getPegmanY(id);
@@ -644,9 +650,11 @@ module.exports = class AnimationsController {
644650
* Display Pegman at the specified location, facing the specified direction.
645651
* @param {number} x Horizontal grid (or fraction thereof).
646652
* @param {number} y Vertical grid (or fraction thereof).
647-
* @param {number} frame Direction (0 - 15) or dance (16 - 17).
653+
* @param {number} frame Direction (0 - 15) or dance (16 - 17). *
654+
* @param {string} id Optional id of pegman. If no id is provided,
655+
* will display default pegman.
648656
*/
649-
displayPegman(x, y, frame, id = null) {
657+
displayPegman(x, y, frame, id) {
650658
var pegmanIcon = document.getElementById('pegman');
651659
var clipRect = document.getElementById('clipRect');
652660
displayPegman(this.maze.skin, pegmanIcon, clipRect, x, y, frame);

src/mazeController.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ module.exports = class MazeController {
231231
this.animationsController.scheduleDance(true, timePerStep);
232232
}
233233

234-
animatedMove(direction, timeForMove, id = null) {
234+
animatedMove(direction, timeForMove, id) {
235235
var positionChange = tiles.directionToDxDy(direction);
236236
var newX = this.getPegmanX(id) + positionChange.dx;
237237
var newY = this.getPegmanY(id) + positionChange.dy;
@@ -241,13 +241,13 @@ module.exports = class MazeController {
241241
this.setPegmanY(newY, id);
242242
}
243243

244-
animatedTurn(direction, id = null) {
244+
animatedTurn(direction, id) {
245245
var newDirection = this.getPegmanD(id) + direction;
246246
this.animationsController.scheduleTurn(newDirection, id);
247247
this.setPegmanD(tiles.constrainDirection4(newDirection), id);
248248
}
249249

250-
animatedFail(forward, id = null) {
250+
animatedFail(forward, id) {
251251
var dxDy = tiles.directionToDxDy(this.getPegmanD(id));
252252
var deltaX = dxDy.dx;
253253
var deltaY = dxDy.dy;
@@ -303,7 +303,7 @@ module.exports = class MazeController {
303303
* in the specified direction.
304304
* @param {!Direction} direction Direction (0 - 3).
305305
*/
306-
animatedLook(direction, id = null) {
306+
animatedLook(direction, id) {
307307
var x = this.getPegmanX(id);
308308
var y = this.getPegmanY(id);
309309
switch (direction) {
@@ -361,32 +361,32 @@ module.exports = class MazeController {
361361
});
362362
}
363363

364-
getPegmanX(id = null) {
364+
getPegmanX(id) {
365365
const pegman = this.pegmanController.getPegman(id);
366366
return pegman && pegman.getX();
367367
}
368368

369-
getPegmanY(id = null) {
369+
getPegmanY(id) {
370370
const pegman = this.pegmanController.getPegman(id);
371371
return pegman && pegman.getY();
372372
}
373373

374-
getPegmanD(id = null) {
374+
getPegmanD(id) {
375375
const pegman = this.pegmanController.getPegman(id);
376376
return pegman && pegman.getDirection();
377377
}
378378

379-
setPegmanX(x, id = null) {
379+
setPegmanX(x, id) {
380380
const pegman = this.pegmanController.getOrCreatePegman(id);
381381
pegman.setX(x);
382382
}
383383

384-
setPegmanY(y, id = null) {
384+
setPegmanY(y, id) {
385385
const pegman = this.pegmanController.getOrCreatePegman(id);
386386
pegman.setY(y);
387387
}
388388

389-
setPegmanD(d, id = null) {
389+
setPegmanD(d, id) {
390390
const pegman = this.pegmanController.getOrCreatePegman(id);
391391
pegman.setDirection(d);
392392
}

src/pegman.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
module.exports = class Pegman {
2-
constructor(x = null, y = null, direction = null, id = null, isVisible = true) {
2+
constructor(id, x = null, y = null, direction = null, isVisible = true) {
3+
if (id == null) {
4+
throw new Error('Pegman id cannot be null or undefined');
5+
}
36
this.x = x;
47
this.y = y;
58
this.direction = direction;

src/pegmanController.js

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,42 @@
11
const Pegman = require('./pegman');
22

3+
const DEFAULT_ID = 'defaultPegman';
4+
35
module.exports = class PegmanController {
6+
47
constructor() {
58
this.pegmen = {};
6-
this.firstPegman = null;
79
}
810

9-
getOrCreatePegman(id = null) {
11+
getOrCreatePegman(id) {
12+
// if id is null or undefined, set to default value.
13+
if(id == undefined) {
14+
id = DEFAULT_ID;
15+
}
1016
let pegman = this.getPegman(id);
1117
if (!pegman) {
12-
pegman = new Pegman(null, null, null, id);
18+
pegman = new Pegman(id);
1319
this.addPegman(pegman);
1420
}
1521
return pegman;
1622
}
1723

18-
getPegman(id = null) {
19-
if (id == null) {
20-
return this.firstPegman;
21-
} else {
22-
return this.pegmen[id];
24+
getPegman(id) {
25+
// if id is null or undefined, set to default value.
26+
if(id == undefined) {
27+
id = DEFAULT_ID;
2328
}
29+
return this.pegmen[id];
2430
}
2531

2632
addPegman(pegman) {
27-
// if this is the first pegman added, store as firstPegman
28-
if (this.firstPegman === null) {
29-
this.firstPegman = pegman;
30-
}
31-
// if the pegman has an id, put it in this.pegmen
32-
// pegmen without ids cannot be accessed via this.pegmen.
33-
if (pegman.id != null) {
34-
this.pegmen[pegman.id] = pegman;
33+
if (this.pegmen[pegman.id]) {
34+
throw new Error(`Pegman with id ${pegman.id} already exists.`);
3535
}
36+
this.pegmen[pegman.id] = pegman;
37+
}
38+
39+
isDefaultPegman(id) {
40+
return id == undefined || id === DEFAULT_ID;
3641
}
3742
}

0 commit comments

Comments
 (0)