Skip to content

Commit

Permalink
actually, define the whole scheduleAnimations loop on maze itself sin…
Browse files Browse the repository at this point in the history
…ce it doesn't actually have anything to do with animations
  • Loading branch information
Hamms committed Feb 13, 2018
1 parent ae271d4 commit 4f5be83
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 73 deletions.
58 changes: 0 additions & 58 deletions apps/src/maze/animationsController.js
Original file line number Diff line number Diff line change
Expand Up @@ -518,64 +518,6 @@ module.exports = class AnimationsController {
}, delay);
}

/**
* Perform our animations, either all of them or those of a single step
*/
scheduleAnimations(singleStep, onAnimationEnd) {
timeoutList.clearTimeouts();

var timePerAction = this.maze.stepSpeed * this.maze.scale.stepSpeed *
this.maze.skin.movePegmanAnimationSpeedScale;
// get a flat list of actions we want to schedule
var actions = this.maze.executionInfo.getActions(singleStep);

this.scheduleSingleAnimation_(0, actions, singleStep, timePerAction, onAnimationEnd);
}

/**
* schedule animations in sequence
* The reason we do this recursively instead of iteratively is that we want to
* ensure that we finish scheduling action1 before starting to schedule
* action2. Otherwise we get into trouble when stepSpeed is 0.
*/
scheduleSingleAnimation_(index, actions, singleStep, timePerAction, onAnimationEnd) {
if (index >= actions.length) {
this.finishAnimations_(singleStep, onAnimationEnd);
return;
}

this.maze.animateAction(actions[index], singleStep, timePerAction);

var command = actions[index] && actions[index].command;
var timeModifier = (this.maze.skin.actionSpeedScale && this.maze.skin.actionSpeedScale[command]) || 1;
var timeForThisAction = Math.round(timePerAction * timeModifier);

timeoutList.setTimeout(() => {
this.scheduleSingleAnimation_(index + 1, actions, singleStep, timePerAction, onAnimationEnd);
}, timeForThisAction);
}

/**
* Once animations are complete, we want to reenable the step button if we
* have steps left, otherwise we're done with this execution.
*/
finishAnimations_(singleStep, onAnimationEnd) {
var stepsRemaining = this.maze.executionInfo.stepsRemaining();
var stepButton = document.getElementById('stepButton');

// allow time for additional pause if we're completely done
var waitTime = (stepsRemaining ? 0 : 1000);

// run after all animations
timeoutList.setTimeout(() => {
if (stepsRemaining) {
stepButton.removeAttribute('disabled');
} else {
onAnimationEnd(singleStep);
}
}, waitTime);
}

stopIdling() {
// Removing the idle animation and replace with pegman sprite
if (this.maze.skin.idlePegmanAnimation) {
Expand Down
79 changes: 64 additions & 15 deletions apps/src/maze/maze.js
Original file line number Diff line number Diff line change
Expand Up @@ -356,21 +356,70 @@ module.exports = class Maze {
};

scheduleAnimations_(singleStep) {
this.animationsController.scheduleAnimations(singleStep, () => {
this.animating_ = false;
if (studioApp().isUsingBlockly()) {
// reenable toolbox
Blockly.mainBlockSpaceEditor.setEnableToolbox(true);
}
// If stepping and we failed, we want to retain highlighting until
// clicking reset. Otherwise we can clear highlighting/disabled
// blocks now
if (!singleStep || this.result === ResultType.SUCCESS) {
this.reenableCachedBlockStates_();
studioApp().clearHighlighting();
timeoutList.clearTimeouts();

var timePerAction = this.stepSpeed * this.scale.stepSpeed *
this.skin.movePegmanAnimationSpeedScale;
// get a flat list of actions we want to schedule
var actions = this.executionInfo.getActions(singleStep);

this.scheduleSingleAnimation_(0, actions, singleStep, timePerAction);
}

/**
* schedule animations in sequence
* The reason we do this recursively instead of iteratively is that we want to
* ensure that we finish scheduling action1 before starting to schedule
* action2. Otherwise we get into trouble when stepSpeed is 0.
*/
scheduleSingleAnimation_(index, actions, singleStep, timePerAction) {
if (index >= actions.length) {
this.finishAnimations_(singleStep);
return;
}

this.animateAction_(actions[index], singleStep, timePerAction);

var command = actions[index] && actions[index].command;
var timeModifier = (this.skin.actionSpeedScale && this.skin.actionSpeedScale[command]) || 1;
var timeForThisAction = Math.round(timePerAction * timeModifier);

timeoutList.setTimeout(() => {
this.scheduleSingleAnimation_(index + 1, actions, singleStep, timePerAction);
}, timeForThisAction);
}

/**
* Once animations are complete, we want to reenable the step button if we
* have steps left, otherwise we're done with this execution.
*/
finishAnimations_(singleStep) {
var stepsRemaining = this.executionInfo.stepsRemaining();
var stepButton = document.getElementById('stepButton');

// allow time for additional pause if we're completely done
var waitTime = (stepsRemaining ? 0 : 1000);

// run after all animations
timeoutList.setTimeout(() => {
if (stepsRemaining) {
stepButton.removeAttribute('disabled');
} else {
this.animating_ = false;
if (studioApp().isUsingBlockly()) {
// reenable toolbox
Blockly.mainBlockSpaceEditor.setEnableToolbox(true);
}
// If stepping and we failed, we want to retain highlighting until
// clicking reset. Otherwise we can clear highlighting/disabled
// blocks now
if (!singleStep || this.result === ResultType.SUCCESS) {
this.reenableCachedBlockStates_();
studioApp().clearHighlighting();
}
this.displayFeedback_();
}
this.displayFeedback_();
});
}, waitTime);
}

/**
Expand Down Expand Up @@ -799,7 +848,7 @@ module.exports = class Maze {
* @param {boolean} spotlightBlocks Whether or not we should highlight entire blocks
* @param {integer} timePerStep How much time we have allocated before the next step
*/
animateAction(action, spotlightBlocks, timePerStep) {
animateAction_(action, spotlightBlocks, timePerStep) {
if (action.blockId) {
studioApp().highlight(String(action.blockId), spotlightBlocks);
}
Expand Down

0 comments on commit 4f5be83

Please sign in to comment.