Skip to content

Commit

Permalink
when setting an anim clip's time property, also update its eventCursor (
Browse files Browse the repository at this point in the history
  • Loading branch information
ellthompson authored Oct 25, 2022
1 parent 7ae1102 commit 7850feb
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
15 changes: 10 additions & 5 deletions src/framework/anim/evaluator/anim-clip.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,7 @@ class AnimClip {
this._blendWeight = 1.0; // blend weight 0..1
this._blendOrder = 0.0; // blend order relative to other clips
this._eventHandler = eventHandler;
this._eventCursor = 0;
// move the event cursor to an event that should fire after the starting time
while (this._track.events[this._eventCursor] && this._track.events[this._eventCursor].time < this.time) {
this._eventCursor++;
}
this.alignCursorToCurrentTime();
}

set name(name) {
Expand All @@ -58,6 +54,7 @@ class AnimClip {

set time(time) {
this._time = time;
this.alignCursorToCurrentTime();
}

get time() {
Expand Down Expand Up @@ -104,6 +101,14 @@ class AnimClip {
return this._eventCursor;
}

alignCursorToCurrentTime() {
this._eventCursor = 0;
// move the event cursor to the event that should fire after the current time
while (this._track.events[this._eventCursor] && this._track.events[this._eventCursor].time < this.time) {
this._eventCursor++;
}
}

activeEventsForFrame(frameStartTime, frameEndTime) {
if (frameStartTime === 0) {
this.eventCursor = 0;
Expand Down
23 changes: 22 additions & 1 deletion test/framework/anim/evaluator/anim-clip.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { AnimTrack } from '../../../../src/framework/anim/evaluator/anim-track.j
import { AnimData } from '../../../../src/framework/anim/evaluator/anim-data.js';
import { AnimCurve } from '../../../../src/framework/anim/evaluator/anim-curve.js';
import { AnimClip } from '../../../../src/framework/anim/evaluator/anim-clip.js';
import { AnimEvents } from '../../../../src/framework/anim/evaluator/anim-events.js';
import { INTERPOLATION_LINEAR } from '../../../../src/framework/anim/constants.js';
import { expect } from 'chai';

Expand All @@ -12,7 +13,12 @@ describe('AnimClip', function () {
const curves = [new AnimCurve(['path/to/entity'], 0, 0, INTERPOLATION_LINEAR)];
const inputs = [new AnimData(1, [0, 1, 2])];
const outputs = [new AnimData(3, [0, 0, 0, 1, 2, 3, 2, 4, 6])];
const animTrack = new AnimTrack('track', 2, inputs, outputs, curves);
const animEvents = new AnimEvents([
{ name: 'event1', time: 0.5 },
{ name: 'event2', time: 1.0 },
{ name: 'event3', time: 1.5 }
]);
const animTrack = new AnimTrack('track', 2, inputs, outputs, curves, animEvents);
animClip = new AnimClip(animTrack, 0, 1, true, false);
});

Expand All @@ -25,6 +31,7 @@ describe('AnimClip', function () {
expect(animClip.snapshot._name).to.equal('trackSnapshot');
expect(animClip.time).to.equal(0);
expect(animClip.loop).to.equal(false);
expect(animClip.eventCursor).to.equal(0);
});

});
Expand Down Expand Up @@ -93,4 +100,18 @@ describe('AnimClip', function () {

});

describe('#time', function () {

it('updates the clips eventCursor property', function () {
expect(animClip.eventCursor).to.equal(0);
animClip.time = 1.1;
expect(animClip.eventCursor).to.equal(2);
animClip.time = 0.6;
expect(animClip.eventCursor).to.equal(1);
animClip.time = 0.1;
expect(animClip.eventCursor).to.equal(0);
});

});

});

0 comments on commit 7850feb

Please sign in to comment.