skinview3d/src/animation.ts

142 lines
4.0 KiB
TypeScript
Raw Normal View History

2018-07-21 14:52:02 +02:00
import { PlayerObject } from "./model";
2018-07-17 20:49:00 +02:00
export function invokeAnimation(animation: Animation, player: PlayerObject, time: number) {
2018-08-17 06:15:02 +02:00
if (animation instanceof Function) {
2018-01-06 15:01:12 +01:00
animation(player, time);
} else {
2018-08-17 06:15:02 +02:00
// must be IAnimation here
animation.play(player, time);
2018-01-06 15:01:12 +01:00
}
}
2018-08-01 15:51:52 +02:00
export interface IAnimation {
2018-07-17 20:49:00 +02:00
play(player: PlayerObject, time: number): void;
}
2018-08-06 12:15:18 +02:00
export type AnimationFn = (player: PlayerObject, time: number) => void;
2018-08-01 15:51:52 +02:00
export type Animation = AnimationFn | IAnimation;
2018-08-01 15:51:52 +02:00
export class AnimationHandle implements IAnimation {
2018-07-21 14:52:02 +02:00
public paused = false;
public speed: number = 1.0;
2018-07-17 20:49:00 +02:00
2018-07-21 14:52:02 +02:00
private animation: Animation;
2018-07-17 20:49:00 +02:00
private _paused = false;
2018-08-01 12:01:58 +02:00
private _lastChange: number | null = null;
2018-07-17 20:49:00 +02:00
private _speed: number = 1.0;
2018-08-01 12:01:58 +02:00
private _lastChangeX: number | null = null;
2018-07-17 20:49:00 +02:00
constructor(animation: Animation) {
this.animation = animation;
2018-01-06 15:01:12 +01:00
}
2018-07-17 20:49:00 +02:00
play(player: PlayerObject, time: number) {
2018-01-06 15:01:12 +01:00
if (this._lastChange === null) {
this._lastChange = time;
this._lastChangeX = 0;
} else if (this.paused !== this._paused || this.speed !== this._speed) {
2018-07-21 14:52:02 +02:00
const dt = time - this._lastChange;
2018-01-06 15:01:12 +01:00
if (this._paused === false) {
2018-08-01 12:01:58 +02:00
this._lastChangeX! += dt * this._speed;
2018-01-06 15:01:12 +01:00
}
this._paused = this.paused;
this._speed = this.speed;
this._lastChange = time;
}
if (this.paused === false) {
2018-07-21 14:52:02 +02:00
const dt = time - this._lastChange;
2018-08-01 12:01:58 +02:00
const x = this._lastChangeX! + this.speed * dt;
2018-01-06 15:01:12 +01:00
invokeAnimation(this.animation, player, x);
}
}
2018-07-17 20:49:00 +02:00
reset() {
2018-01-06 15:01:12 +01:00
this._lastChange = null;
}
2018-07-17 20:49:00 +02:00
remove(animHandle: AnimationHandle) {
// stub get's overriden
2018-07-17 20:49:00 +02:00
}
2018-01-06 15:01:12 +01:00
}
export class CompositeAnimation {
2018-07-17 20:49:00 +02:00
handles: Set<AnimationHandle>;
2018-01-06 15:01:12 +01:00
constructor() {
this.handles = new Set();
}
add(animation: Animation) {
const handle = new AnimationHandle(animation);
handle.remove = () => this.handles.delete(handle);
this.handles.add(handle);
return handle;
2018-01-06 15:01:12 +01:00
}
play(player: PlayerObject, time: number) {
2018-01-06 15:01:12 +01:00
this.handles.forEach(handle => handle.play(player, time));
}
}
2018-08-17 06:15:02 +02:00
export const WalkingAnimation: Animation = (player, time) => {
2018-07-21 14:52:02 +02:00
const skin = player.skin;
2017-10-01 14:00:45 +02:00
// Multiply by animation's natural speed
time *= 8;
// Leg swing
2018-07-17 20:49:00 +02:00
skin.leftLeg.rotation.x = Math.sin(time) * 0.5;
skin.rightLeg.rotation.x = Math.sin(time + Math.PI) * 0.5;
2017-10-01 14:00:45 +02:00
// Arm swing
2018-07-17 20:49:00 +02:00
skin.leftArm.rotation.x = Math.sin(time + Math.PI) * 0.5;
skin.rightArm.rotation.x = Math.sin(time) * 0.5;
2018-07-21 14:52:02 +02:00
const basicArmRotationZ = Math.PI * 0.02;
2018-07-17 20:49:00 +02:00
skin.leftArm.rotation.z = Math.cos(time) * 0.03 + basicArmRotationZ;
skin.rightArm.rotation.z = Math.cos(time + Math.PI) * 0.03 - basicArmRotationZ;
// Head shaking with different frequency & amplitude
skin.head.rotation.y = Math.sin(time / 4) * 0.2;
skin.head.rotation.x = Math.sin(time / 5) * 0.1;
// Always add an angle for cape around the x axis
2018-07-21 14:52:02 +02:00
const basicCapeRotationX = Math.PI * 0.06;
player.cape.rotation.x = Math.sin(time / 1.5) * 0.06 + basicCapeRotationX;
};
2018-08-17 06:15:02 +02:00
export const RunningAnimation: Animation = (player, time) => {
2018-07-21 14:52:02 +02:00
const skin = player.skin;
time *= 15;
// Leg swing with larger amplitude
2018-07-17 20:49:00 +02:00
skin.leftLeg.rotation.x = Math.cos(time + Math.PI) * 1.3;
skin.rightLeg.rotation.x = Math.cos(time) * 1.3;
// Arm swing
2018-07-17 20:49:00 +02:00
skin.leftArm.rotation.x = Math.cos(time) * 1.5;
skin.rightArm.rotation.x = Math.cos(time + Math.PI) * 1.5;
2018-07-21 14:52:02 +02:00
const basicArmRotationZ = Math.PI * 0.1;
2018-07-17 20:49:00 +02:00
skin.leftArm.rotation.z = Math.cos(time) * 0.1 + basicArmRotationZ;
skin.rightArm.rotation.z = Math.cos(time + Math.PI) * 0.1 - basicArmRotationZ;
// Jumping
player.position.y = Math.cos(time * 2);
// Dodging when running
player.position.x = Math.cos(time) * 0.15;
// Slightly tilting when running
player.rotation.z = Math.cos(time + Math.PI) * 0.01;
// Apply higher swing frequency, lower amplitude,
// and greater basic rotation around x axis,
// to cape when running.
2018-07-21 14:52:02 +02:00
const basicCapeRotationX = Math.PI * 0.3;
player.cape.rotation.x = Math.sin(time * 2) * 0.1 + basicCapeRotationX;
// What about head shaking?
// You shouldn't glance right and left when running dude :P
};
2018-08-17 06:15:02 +02:00
export const RotatingAnimation: Animation = (player, time) => {
player.rotation.y = time;
2017-10-01 14:00:45 +02:00
};