Skip to content

Commit

Permalink
walk isPaused and speed together
Browse files Browse the repository at this point in the history
  • Loading branch information
reececomo committed Apr 26, 2024
1 parent 5aab8dd commit b85c644
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 47 deletions.
16 changes: 2 additions & 14 deletions src/DisplayObject.mixin.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { _ as Action } from "./Action";
import { ActionTicker } from "./lib/ActionTicker";
import { getSpeed } from "./lib/utils/displayobject";

//
// ----- DisplayObject Mixin: -----
Expand Down Expand Up @@ -28,19 +27,8 @@ export function registerDisplayObjectMixin(displayObject: any): void {
ActionTicker.runAction(key, this, action);
};

prototype.runAsPromise = function (
action: Action,
timeoutBufferMs: number = 100
): Promise<void> {
const node = this;
return new Promise(function (resolve, reject) {
const timeLimitMs = timeoutBufferMs + (getSpeed(node) * action.duration * 1_000);
const timeoutCheck = setTimeout(() => reject('Took too long to complete.'), timeLimitMs);
node.run(action, () => {
clearTimeout(timeoutCheck);
resolve();
});
});
prototype.runAsPromise = function (action: Action): Promise<void> {
return new Promise(resolve => this.run(action, () => resolve()));
};

prototype.action = function (forKey: string): Action | undefined {
Expand Down
26 changes: 21 additions & 5 deletions src/lib/ActionTicker.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Action } from "./Action";
import { TimingModeFn } from "../TimingMode";
import { getIsPaused, getSpeed } from "./utils/displayobject";

const EPSILON = 0.0000000001;
const EPSILON_ONE = 1 - EPSILON;
Expand Down Expand Up @@ -33,19 +32,19 @@ export class ActionTicker {
const deltaTime = deltaTimeMs * 0.001;

for (const [target, tickers] of this._tickers.entries()) {
if (getIsPaused(target)) {
const [isPaused, speed] = _getTargetState(target);

if (isPaused || speed <= 0) {
continue;
}

const targetSpeed = getSpeed(target);

for (const actionTicker of tickers.values()) {
if (categoryMask !== undefined && (categoryMask & actionTicker.action.categoryMask) === 0) {
continue;
}

try {
actionTicker.tick(deltaTime * targetSpeed);
actionTicker.tick(deltaTime * speed);
}
catch (error) {
// Isolate individual action errors.
Expand Down Expand Up @@ -301,3 +300,20 @@ export class ActionTicker {
(this.action as any).onTickerDidReset(this);
}
}

/**
* Get the global action processing state of a descendent target.
*/
function _getTargetState(target: TargetNode): [isPaused: boolean, speed: number] {
let leaf = target;
let isPaused = leaf.isPaused;
let speed = leaf.speed;

while (!isPaused && leaf.parent != null) {
isPaused = leaf.parent.isPaused;
speed *= leaf.parent.speed;
leaf = leaf.parent;
}

return [isPaused, speed];
}
28 changes: 0 additions & 28 deletions src/lib/utils/displayobject.ts

This file was deleted.

0 comments on commit b85c644

Please sign in to comment.