Replace AnimationHandle.pause with speed

This commit is contained in:
yushijinhun 2019-04-21 00:07:20 +08:00
parent 5992d3e68d
commit 0103e81b8e
No known key found for this signature in database
GPG Key ID: 5BC167F73EA558E4
2 changed files with 7 additions and 25 deletions

View File

@ -56,7 +56,7 @@ Three.js powered Minecraft skin viewer.
// Set the speed of an animation // Set the speed of an animation
run.speed = 3; run.speed = 3;
// Pause single animation // Pause single animation
run.paused = true; run.speed = 0;
// Pause all animations! // Pause all animations!
skinViewer.animationSpeed = 0; skinViewer.animationSpeed = 0;
</script> </script>

View File

@ -19,7 +19,6 @@ export function invokeAnimation(animation: Animation, player: PlayerObject, time
// This interface is used to control animations // This interface is used to control animations
export interface AnimationHandle { export interface AnimationHandle {
paused: boolean;
speed: number; speed: number;
readonly animation: Animation; readonly animation: Animation;
@ -28,41 +27,24 @@ export interface AnimationHandle {
} }
class AnimationWrapper implements AnimationHandle, IAnimation { class AnimationWrapper implements AnimationHandle, IAnimation {
public paused = false;
public speed: number = 1.0; public speed: number = 1.0;
public readonly animation: Animation; public readonly animation: Animation;
private _paused = false; private lastTime = 0;
private _lastChange: number | null = null; private progress = 0;
private _speed: number = 1.0;
private _lastChangeX: number | null = null;
constructor(animation: Animation) { constructor(animation: Animation) {
this.animation = animation; this.animation = animation;
} }
play(player: PlayerObject, time: number) { play(player: PlayerObject, time: number) {
if (this._lastChange === null) { this.progress += (time - this.lastTime) * this.speed;
this._lastChange = time; this.lastTime = time;
this._lastChangeX = 0; invokeAnimation(this.animation, player, this.progress);
} else if (this.paused !== this._paused || this.speed !== this._speed) {
const dt = time - this._lastChange;
if (this._paused === false) {
this._lastChangeX! += dt * this._speed;
}
this._paused = this.paused;
this._speed = this.speed;
this._lastChange = time;
}
if (this.paused === false) {
const dt = time - this._lastChange;
const x = this._lastChangeX! + this.speed * dt;
invokeAnimation(this.animation, player, x);
}
} }
reset() { reset() {
this._lastChange = null; this.progress = 0;
} }
remove() { remove() {