Skip to content

Commit dbb4920

Browse files
authored
Merge pull request #41 from code-dot-org/support-multiple-pegmen
Support Multiple Pegman (part 1)
2 parents eff74f4 + 075c7b1 commit dbb4920

File tree

10 files changed

+244
-103
lines changed

10 files changed

+244
-103
lines changed

src/animationsController.js

Lines changed: 51 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ module.exports = class AnimationsController {
114114
this.scheduleTurn(this.maze.startDirection);
115115
}, danceTime + 150);
116116
} else {
117-
this.displayPegman(this.maze.pegmanX, this.maze.pegmanY, tiles.directionToFrame(this.maze.pegmanD));
117+
this.displayPegman(this.maze.getPegmanX(), this.maze.getPegmanY(), tiles.directionToFrame(this.maze.getPegmanD()));
118118

119119
const finishIcon = document.getElementById('finish');
120120
if (finishIcon) {
@@ -267,15 +267,17 @@ 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) {
273-
var startX = this.maze.pegmanX;
274-
var startY = this.maze.pegmanY;
275-
var direction = this.maze.pegmanD;
274+
scheduleMove(endX, endY, timeForAnimation, id) {
275+
const startX = this.maze.getPegmanX(id);
276+
const startY = this.maze.getPegmanY(id);
277+
const direction = this.maze.getPegmanD(id);
276278

277-
var deltaX = (endX - startX);
278-
var deltaY = (endY - startY);
279+
const deltaX = (endX - startX);
280+
const deltaY = (endY - startY);
279281
var numFrames;
280282
var timePerFrame;
281283

@@ -336,17 +338,19 @@ 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) {
345+
scheduleTurn(endDirection, id) {
342346
var numFrames = 4;
343-
var startDirection = this.maze.pegmanD;
347+
var startDirection = this.maze.getPegmanD(id);
344348
var deltaDirection = endDirection - startDirection;
345349
utils.range(1, numFrames).forEach((frame) => {
346350
timeoutList.setTimeout(() => {
347351
this.displayPegman(
348-
this.maze.pegmanX,
349-
this.maze.pegmanY,
352+
this.maze.getPegmanX(id),
353+
this.maze.getPegmanY(id),
350354
tiles.directionToFrame(startDirection + deltaDirection * frame / numFrames));
351355
}, this.maze.stepSpeed * (frame - 1));
352356
});
@@ -392,8 +396,10 @@ module.exports = class AnimationsController {
392396
}
393397
}
394398

395-
scheduleWallHit(targetX, targetY, deltaX, deltaY, frame) {
399+
scheduleWallHit(targetX, targetY, deltaX, deltaY, frame, id) {
396400
// Play the animation of hitting the wall
401+
const pegmanX = this.maze.getPegmanX(id);
402+
const pegmanY = this.maze.getPegmanY(id);
397403
if (this.maze.skin.hittingWallAnimation) {
398404
var wallAnimationIcon = document.getElementById('wallAnimation');
399405
var numFrames = this.maze.skin.hittingWallAnimationFrameNumber || 0;
@@ -410,8 +416,8 @@ module.exports = class AnimationsController {
410416
// animate our sprite sheet
411417
var timePerFrame = 100;
412418
this.scheduleSheetedMovement_({
413-
x: this.maze.pegmanX,
414-
y: this.maze.pegmanY
419+
x: pegmanX,
420+
y: pegmanY
415421
}, {
416422
x: deltaX,
417423
y: deltaY
@@ -424,10 +430,10 @@ module.exports = class AnimationsController {
424430
// active our gif
425431
timeoutList.setTimeout(() => {
426432
wallAnimationIcon.setAttribute('x',
427-
this.maze.SQUARE_SIZE * (this.maze.pegmanX + 0.5 + deltaX * 0.5) -
433+
this.maze.SQUARE_SIZE * (pegmanX + 0.5 + deltaX * 0.5) -
428434
wallAnimationIcon.getAttribute('width') / 2);
429435
wallAnimationIcon.setAttribute('y',
430-
this.maze.SQUARE_SIZE * (this.maze.pegmanY + 1 + deltaY * 0.5) -
436+
this.maze.SQUARE_SIZE * (pegmanY + 1 + deltaY * 0.5) -
431437
wallAnimationIcon.getAttribute('height'));
432438
wallAnimationIcon.setAttribute('visibility', 'visible');
433439
wallAnimationIcon.setAttributeNS(
@@ -437,14 +443,14 @@ module.exports = class AnimationsController {
437443
}
438444
}
439445
timeoutList.setTimeout(() => {
440-
this.displayPegman(this.maze.pegmanX, this.maze.pegmanY, frame);
446+
this.displayPegman(pegmanX, pegmanY, frame, id);
441447
}, this.maze.stepSpeed);
442448
timeoutList.setTimeout(() => {
443-
this.displayPegman(this.maze.pegmanX + deltaX / 4, this.maze.pegmanY + deltaY / 4,
444-
frame);
449+
this.displayPegman(pegmanX + deltaX / 4, pegmanY + deltaY / 4,
450+
frame, id);
445451
}, this.maze.stepSpeed * 2);
446452
timeoutList.setTimeout(() => {
447-
this.displayPegman(this.maze.pegmanX, this.maze.pegmanY, frame);
453+
this.displayPegman(pegmanX, pegmanY, frame, id);
448454
}, this.maze.stepSpeed * 3);
449455

450456
if (this.maze.skin.wallPegmanAnimation) {
@@ -453,24 +459,24 @@ module.exports = class AnimationsController {
453459
pegmanIcon.setAttribute('visibility', 'hidden');
454460
this.updatePegmanAnimation_({
455461
idStr: 'wall',
456-
row: this.maze.pegmanY,
457-
col: this.maze.pegmanX,
458-
direction: this.maze.pegmanD
462+
row: pegmanY,
463+
col: pegmanX,
464+
direction: this.maze.getPegmanD(id)
459465
});
460466
}, this.maze.stepSpeed * 4);
461467
}
462468
}
463469

464-
scheduleObstacleHit(targetX, targetY, deltaX, deltaY, frame) {
470+
scheduleObstacleHit(targetX, targetY, deltaX, deltaY, frame, id) {
465471
// Play the animation
466472
var obsId = targetX + this.maze.map.COLS * targetY;
467473
var obsIcon = document.getElementById('obstacle' + obsId);
468474
obsIcon.setAttributeNS(
469475
'http://www.w3.org/1999/xlink', 'xlink:href',
470476
this.maze.skin.obstacleAnimation);
471477
timeoutList.setTimeout(() => {
472-
this.displayPegman(this.maze.pegmanX + deltaX / 2,
473-
this.maze.pegmanY + deltaY / 2,
478+
this.displayPegman(this.maze.getPegmanX(id) + deltaX / 2,
479+
this.maze.getPegmanY(id)+ deltaY / 2,
474480
frame);
475481
}, this.maze.stepSpeed);
476482

@@ -538,9 +544,13 @@ module.exports = class AnimationsController {
538544
* @param {boolean} victoryDance This is a victory dance after completing the
539545
* puzzle (vs. dancing on load).
540546
* @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.
541549
*/
542-
scheduleDance(victoryDance, timeAlloted) {
550+
scheduleDance(victoryDance, timeAlloted, id) {
543551
const finishIcon = document.getElementById('finish');
552+
const pegmanX = this.maze.getPegmanX(id);
553+
const pegmanY = this.maze.getPegmanY(id);
544554

545555
// Some skins (like scrat) have custom celebration animations we want to
546556
// suport
@@ -550,7 +560,7 @@ module.exports = class AnimationsController {
550560
}
551561
const numFrames = this.maze.skin.celebratePegmanRow;
552562
const timePerFrame = timeAlloted / numFrames;
553-
const start = { x: this.maze.pegmanX, y: this.maze.pegmanY };
563+
const start = { x: pegmanX, y: pegmanY };
554564

555565
this.scheduleSheetedMovement_(
556566
{ x: start.x, y: start.y },
@@ -564,8 +574,8 @@ module.exports = class AnimationsController {
564574
return;
565575
}
566576

567-
var originalFrame = tiles.directionToFrame(this.maze.pegmanD);
568-
this.displayPegman(this.maze.pegmanX, this.maze.pegmanY, 16);
577+
var originalFrame = tiles.directionToFrame(this.maze.getPegmanD(id));
578+
this.displayPegman(pegmanX, pegmanY, 16, id);
569579

570580
// If victoryDance === true, play the goal animation, else reset it
571581
if (victoryDance && finishIcon) {
@@ -575,21 +585,21 @@ module.exports = class AnimationsController {
575585

576586
var danceSpeed = timeAlloted / 5;
577587
timeoutList.setTimeout(() => {
578-
this.displayPegman(this.maze.pegmanX, this.maze.pegmanY, 18);
588+
this.displayPegman(pegmanX, pegmanY, 18, id);
579589
}, danceSpeed);
580590
timeoutList.setTimeout(() => {
581-
this.displayPegman(this.maze.pegmanX, this.maze.pegmanY, 20);
591+
this.displayPegman(pegmanX, pegmanY, 20, id);
582592
}, danceSpeed * 2);
583593
timeoutList.setTimeout(() => {
584-
this.displayPegman(this.maze.pegmanX, this.maze.pegmanY, 18);
594+
this.displayPegman(pegmanX, pegmanY, 18, id);
585595
}, danceSpeed * 3);
586596
timeoutList.setTimeout(() => {
587-
this.displayPegman(this.maze.pegmanX, this.maze.pegmanY, 20);
597+
this.displayPegman(pegmanX, pegmanY, 20, id);
588598
}, danceSpeed * 4);
589599

590600
timeoutList.setTimeout(() => {
591601
if (!victoryDance || this.maze.skin.turnAfterVictory) {
592-
this.displayPegman(this.maze.pegmanX, this.maze.pegmanY, originalFrame);
602+
this.displayPegman(pegmanX, pegmanY, originalFrame, id);
593603
}
594604

595605
if (victoryDance && this.maze.skin.transparentTileEnding) {
@@ -640,9 +650,11 @@ module.exports = class AnimationsController {
640650
* Display Pegman at the specified location, facing the specified direction.
641651
* @param {number} x Horizontal grid (or fraction thereof).
642652
* @param {number} y Vertical grid (or fraction thereof).
643-
* @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.
644656
*/
645-
displayPegman(x, y, frame) {
657+
displayPegman(x, y, frame, id) {
646658
var pegmanIcon = document.getElementById('pegman');
647659
var clipRect = document.getElementById('clipRect');
648660
displayPegman(this.maze.skin, pegmanIcon, clipRect, x, y, frame);

src/bee.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -278,8 +278,8 @@ module.exports = class Bee extends Gatherer {
278278
* @return {boolean} whether or not this attempt was successful
279279
*/
280280
tryGetNectar() {
281-
const col = this.maze_.pegmanX;
282-
const row = this.maze_.pegmanY;
281+
const col = this.maze_.getPegmanX();
282+
const row = this.maze_.getPegmanY();
283283

284284
// Make sure we're at a flower.
285285
if (!this.isFlower(row, col)) {
@@ -309,8 +309,8 @@ module.exports = class Bee extends Gatherer {
309309
* @return {boolean} whether or not this attempt was successful
310310
*/
311311
tryMakeHoney() {
312-
const col = this.maze_.pegmanX;
313-
const row = this.maze_.pegmanY;
312+
const col = this.maze_.getPegmanX();
313+
const row = this.maze_.getPegmanY();
314314

315315
if (!this.isHive(row, col)) {
316316
this.emit('notAtHive');
@@ -326,8 +326,8 @@ module.exports = class Bee extends Gatherer {
326326
}
327327

328328
nectarRemaining(userCheck=false) {
329-
const col = this.maze_.pegmanX;
330-
const row = this.maze_.pegmanY;
329+
const col = this.maze_.getPegmanX();
330+
const row = this.maze_.getPegmanY();
331331

332332
if (userCheck) {
333333
this.userChecks_[row][col].checkedForNectar = true;
@@ -337,8 +337,8 @@ module.exports = class Bee extends Gatherer {
337337
}
338338

339339
honeyAvailable() {
340-
const col = this.maze_.pegmanX;
341-
const row = this.maze_.pegmanY;
340+
const col = this.maze_.getPegmanX();
341+
const row = this.maze_.getPegmanY();
342342

343343
return this.hiveRemainingCapacity(row, col);
344344
}
@@ -354,8 +354,8 @@ module.exports = class Bee extends Gatherer {
354354
* @throws Will throw an error if the current cell has no nectar.
355355
*/
356356
animateGetNectar() {
357-
const col = this.maze_.pegmanX;
358-
const row = this.maze_.pegmanY;
357+
const col = this.maze_.getPegmanX();
358+
const row = this.maze_.getPegmanY();
359359

360360
if (this.getValue(row, col) <= 0) {
361361
throw new Error("Shouldn't be able to end up with a nectar animation if " +
@@ -378,8 +378,8 @@ module.exports = class Bee extends Gatherer {
378378
* @throws Will throw an error if the current cell is not a hive.
379379
*/
380380
animateMakeHoney() {
381-
const col = this.maze_.pegmanX;
382-
const row = this.maze_.pegmanY;
381+
const col = this.maze_.getPegmanX();
382+
const row = this.maze_.getPegmanY();
383383

384384
if (!this.isHive(row, col)) {
385385
throw new Error("Shouldn't be able to end up with a honey animation if " +

src/harvester.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ module.exports = class Harvester extends Gatherer {
4242
}
4343

4444
hasCrop(crop) {
45-
const col = this.maze_.pegmanX;
46-
const row = this.maze_.pegmanY;
45+
const col = this.maze_.getPegmanX();
46+
const row = this.maze_.getPegmanY();
4747

4848
const cell = this.getCell(row, col);
4949

@@ -63,8 +63,8 @@ module.exports = class Harvester extends Gatherer {
6363
}
6464

6565
atCrop(crop) {
66-
const col = this.maze_.pegmanX;
67-
const row = this.maze_.pegmanY;
66+
const col = this.maze_.getPegmanX();
67+
const row = this.maze_.getPegmanY();
6868

6969
const cell = this.getCell(row, col);
7070

@@ -100,8 +100,8 @@ module.exports = class Harvester extends Gatherer {
100100
* @return {boolean} whether or not this attempt was successful
101101
*/
102102
tryGetCrop(crop) {
103-
const col = this.maze_.pegmanX;
104-
const row = this.maze_.pegmanY;
103+
const col = this.maze_.getPegmanX();
104+
const row = this.maze_.getPegmanY();
105105

106106
const cell = this.getCell(row, col);
107107

@@ -143,8 +143,8 @@ module.exports = class Harvester extends Gatherer {
143143
* available to harvest.
144144
*/
145145
animateGetCrop(crop) {
146-
const col = this.maze_.pegmanX;
147-
const row = this.maze_.pegmanY;
146+
const col = this.maze_.getPegmanX();
147+
const row = this.maze_.getPegmanY();
148148

149149
const cell = this.getCell(row, col);
150150

0 commit comments

Comments
 (0)