Turn AnimationHandle into an interface

This commit is contained in:
yushijinhun 2018-08-17 12:46:04 +08:00
parent e78138b856
commit c067541cc6
No known key found for this signature in database
GPG Key ID: 5BC167F73EA558E4
2 changed files with 29 additions and 15 deletions

View File

@ -1,5 +1,13 @@
import { PlayerObject } from "./model"; import { PlayerObject } from "./model";
export interface IAnimation {
play(player: PlayerObject, time: number): void;
}
export type AnimationFn = (player: PlayerObject, time: number) => void;
export type Animation = AnimationFn | IAnimation;
export function invokeAnimation(animation: Animation, player: PlayerObject, time: number) { export function invokeAnimation(animation: Animation, player: PlayerObject, time: number) {
if (animation instanceof Function) { if (animation instanceof Function) {
animation(player, time); animation(player, time);
@ -9,18 +17,21 @@ export function invokeAnimation(animation: Animation, player: PlayerObject, time
} }
} }
export interface IAnimation { // This interface is used to control animations
play(player: PlayerObject, time: number): void; export interface AnimationHandle {
paused: boolean;
speed: number;
readonly animation: Animation;
reset(): void;
remove(): void;
} }
export type AnimationFn = (player: PlayerObject, time: number) => void; class AnimationWrapper implements AnimationHandle, IAnimation {
export type Animation = AnimationFn | IAnimation;
export class AnimationHandle implements IAnimation {
public paused = false; public paused = false;
public speed: number = 1.0; public speed: number = 1.0;
public readonly animation: Animation;
private animation: Animation;
private _paused = false; private _paused = false;
private _lastChange: number | null = null; private _lastChange: number | null = null;
private _speed: number = 1.0; private _speed: number = 1.0;
@ -54,24 +65,22 @@ export class AnimationHandle implements IAnimation {
this._lastChange = null; this._lastChange = null;
} }
remove(animHandle: AnimationHandle) { remove() {
// stub get's overriden // stub get's overriden
} }
} }
export class CompositeAnimation { export class CompositeAnimation implements IAnimation {
handles: Set<AnimationHandle>; readonly handles: Set<AnimationHandle & IAnimation> = new Set();
constructor() { add(animation: Animation): AnimationHandle {
this.handles = new Set(); const handle = new AnimationWrapper(animation);
}
add(animation: Animation) {
const handle = new AnimationHandle(animation);
handle.remove = () => this.handles.delete(handle); handle.remove = () => this.handles.delete(handle);
this.handles.add(handle); this.handles.add(handle);
return handle; return handle;
} }
play(player: PlayerObject, time: number) { play(player: PlayerObject, time: number) {
this.handles.forEach(handle => handle.play(player, time)); this.handles.forEach(handle => handle.play(player, time));
} }

View File

@ -15,8 +15,13 @@ export {
} from "./orbit_controls"; } from "./orbit_controls";
export { export {
IAnimation,
AnimationFn,
Animation,
invokeAnimation, invokeAnimation,
AnimationHandle,
CompositeAnimation, CompositeAnimation,
WalkingAnimation, WalkingAnimation,
RunningAnimation, RunningAnimation,
RotatingAnimation RotatingAnimation