Skip to content

Commit

Permalink
v1.2.1 fix #9 issue with sequence completion
Browse files Browse the repository at this point in the history
  • Loading branch information
reececomo committed Apr 26, 2024
1 parent 690de91 commit 5dc6516
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 44 deletions.
5 changes: 4 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@
"warn",
"stroustrup"
],
"curly": "warn",
"curly": [
"warn",
"multi-line"
],
"semi": [
"warn",
"always"
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pixijs-actions",
"version": "1.2.0",
"version": "1.2.1",
"author": "Reece Como <[email protected]>",
"authors": [
"Reece Como <[email protected]>",
Expand Down
46 changes: 46 additions & 0 deletions src/__tests__/Action.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,30 @@ describe('Action Chaining', () => {
expect((action as any).actions.length).toBe(2);
expect((action as any)._squashedActions().length).toBe(11);
});

it('completes regression case', () => {
const otherNodeA = new Container();
const otherNodeB = new Container();

let myRunCompleted = false;

const action = Action.sequence([
Action.group([
Action.waitForDuration(0.25),
Action.run(() => otherNodeA.run(Action.moveByX(2, 1.0))),
Action.run(() => otherNodeB.run(Action.moveByX(2, 1.0))),
]),
Action.run(() => myRunCompleted = true),
]);

const myNode = new Container();
myNode.run(action);

simulateTime(0.28);

expect(myNode.hasActions()).toBe(false);
expect(myRunCompleted).toBe(true);
});
});

describe('group()', () => {
Expand Down Expand Up @@ -269,6 +293,28 @@ describe('Action Chaining', () => {

myNode.removeAllActions();
});

it('should work with waitForDuration()', () => {
const otherNodeA = new Container();
const otherNodeB = new Container();

const action = Action.group([
Action.waitForDuration(0.25),
Action.run(() => otherNodeA.run(Action.moveByX(2, 1.0))),
Action.run(() => otherNodeB.run(Action.moveByX(2, 1.0))),
]);

expect(action.duration).toBeCloseTo(0.25);
expect(action.scaledDuration).toBeCloseTo(0.25);

const myNode = new Container();
myNode.runWithKey(action, 'myTriggerAction');

simulateTime(0.3);

expect(myNode.action('myTriggerAction')).toBeUndefined();
expect(myNode.hasActions()).toBe(false);
});
});

describe('repeat()', () => {
Expand Down
16 changes: 7 additions & 9 deletions src/actions/chainable/GroupAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,19 @@ export class GroupAction extends Action {
ticker: IActionTicker,
deltaTime: number,
): void {
let allDone = true;
const relativeTimeDelta = ticker.scaledDuration === Infinity
const relativeDeltaTime = ticker.scaledDuration === Infinity
? deltaTime * this.speed
: dt * ticker.scaledDuration;

let allDone = true;
for (const childTicker of ticker.data.childTickers as IActionTicker[]) {
if (!childTicker.isDone) {
allDone = false;
childTicker.tick(relativeTimeDelta);
}
}
if (childTicker.isDone) continue;

if (allDone) {
ticker.isDone = true;
allDone = false;
childTicker.tick(relativeDeltaTime);
}

if (allDone) ticker.isDone = true;
}

protected onTickerDidReset(ticker: IActionTicker): any {
Expand Down
8 changes: 4 additions & 4 deletions src/actions/chainable/RepeatAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,18 @@ export class RepeatAction extends Action {
deltaTime: number
): void {
const childTicker: IActionTicker = ticker.data.childTicker;
let remainingTimeDelta = deltaTime * this.speed;
let remainingDeltaTime = deltaTime * this.speed;

remainingTimeDelta = childTicker.tick(remainingTimeDelta);
remainingDeltaTime = childTicker.tick(remainingDeltaTime);

if (remainingTimeDelta > 0 || childTicker.scaledDuration === 0) {
if (remainingDeltaTime > 0 || childTicker.scaledDuration === 0) {
if (++ticker.data.n >= this.repeats) {
ticker.isDone = true;
return;
}

childTicker.reset();
remainingTimeDelta = childTicker.tick(remainingTimeDelta);
remainingDeltaTime = childTicker.tick(remainingDeltaTime);
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/actions/chainable/RepeatForeverAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ export class RepeatForeverAction extends Action {
deltaTime: number
): void {
const childTicker: IActionTicker = ticker.data.childTicker;
let remainingTimeDelta = deltaTime * ticker.speed;
let remainingDeltaTime = deltaTime * ticker.speed;

remainingTimeDelta = childTicker.tick(remainingTimeDelta);
remainingDeltaTime = childTicker.tick(remainingDeltaTime);

if (remainingTimeDelta > 0) {
if (remainingDeltaTime > 0) {
childTicker.reset();
remainingTimeDelta = childTicker.tick(remainingTimeDelta);
remainingDeltaTime = childTicker.tick(remainingDeltaTime);
}
}

Expand Down
23 changes: 6 additions & 17 deletions src/actions/chainable/SequenceAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,32 +32,21 @@ export class SequenceAction extends Action {
ticker: IActionTicker,
deltaTime: number,
): void {
let allDone = true;
let remainingTimeDelta = ticker.scaledDuration === Infinity
let remainingDeltaTime = ticker.scaledDuration === Infinity
? deltaTime * this.speed
: dt * ticker.scaledDuration;

for (const childTicker of ticker.data.childTickers as IActionTicker[]) {
if (!childTicker.isDone) {
if (childTicker.isDone) continue;

if (remainingTimeDelta > 0 || childTicker.scaledDuration === 0) {
remainingTimeDelta = childTicker.tick(remainingTimeDelta);
}
else {
allDone = false;
break;
}
remainingDeltaTime = childTicker.tick(remainingDeltaTime);

if (remainingTimeDelta < 0) {
allDone = false;
break;
}
if (remainingDeltaTime < 0) {
return; // Cannot continue to next: Current action not completed yet.
}
}

if (allDone) {
ticker.isDone = true;
}
ticker.isDone = true;
}

protected onTickerDidReset(ticker: IActionTicker): any {
Expand Down
1 change: 1 addition & 0 deletions src/lib/Action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { TimingMode, TimingModeFn } from '../TimingMode';
import { IActionTicker } from './IActionTicker';

export abstract class Action {
public log = false;

// ----- Default Timing Modes: -----

Expand Down
16 changes: 8 additions & 8 deletions src/lib/ActionTicker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Action } from "./Action";
import { TimingModeFn } from "../TimingMode";

const EPSILON = 0.0000000001;
const EPSILON_ONE = 1 - EPSILON;
const EPSILON_1 = 1 - EPSILON;

/**
* An internal utility class that runs (or "ticks") stateless
Expand Down Expand Up @@ -240,11 +240,11 @@ export class ActionTicker {
return;
}

const scaledTimeDelta = deltaTime * this.speed;
const scaledDeltaTime = deltaTime * this.speed;

// Instantaneous actions:
if (this.scaledDuration === 0) {
// Instantaneous action.
(action as any).onTick(this.target, 1.0, 1.0, this, scaledTimeDelta);
(action as any).onTick(this.target, 1.0, 1.0, this, scaledDeltaTime);
this.isDone = true;

// Remove completed action.
Expand All @@ -253,18 +253,18 @@ export class ActionTicker {
return deltaTime; // relinquish the full time.
}

if (deltaTime === 0) {
if (deltaTime === 0 && this.timeDistance < EPSILON_1) {
return -1; // Early exit, no progress.
}

const b = this.easedTimeDistance;
this._elapsed += scaledTimeDelta;
this._elapsed += scaledDeltaTime;
const t = this.easedTimeDistance;
const dt = t - b;

(action as any).onTick(this.target, t, dt, this, scaledTimeDelta);
(action as any).onTick(this.target, t, dt, this, scaledDeltaTime);

if (this.isDone || (this.autoComplete && this.timeDistance >= EPSILON_ONE)) {
if (this.isDone || (this.autoComplete && this.timeDistance >= EPSILON_1)) {
this.isDone = true;

// Remove completed action.
Expand Down

0 comments on commit 5dc6516

Please sign in to comment.