-
Notifications
You must be signed in to change notification settings - Fork 2
/
animation.ts
120 lines (91 loc) · 2.11 KB
/
animation.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/// <reference path="utils.ts" />
/// <reference path="geom.ts" />
/// <reference path="task.ts" />
/// <reference path="entity.ts" />
// Animator
// Base class for all animator.
//
class Animator extends Task {
entity: Entity;
constructor(entity: Entity) {
super();
this.entity = entity;
}
}
// Blinker
//
class Blinker extends Entity {
interval: number = 1.0;
target: Entity;
constructor(entity: Entity) {
super(entity.pos);
this.target = entity;
}
onTick() {
super.onTick();
this.pos = this.target.pos;
this.sprites = this.target.sprites;
}
isVisible() {
return (this.isRunning() &&
((this.interval <= 0) ||
(phase(this.getTime(), this.interval) != 0)));
}
}
// Tweener
//
class Tweener extends Animator {
srcpos: Vec2 = null;
dstpos: Vec2 = null;
onStart() {
super.onStart();
this.srcpos = this.entity.pos.copy();
}
onTick() {
super.onTick();
if (this.srcpos !== null && this.dstpos !== null) {
let t = this.getTime() / this.lifetime;
this.entity.pos = this.getPos(t);
}
}
getPos(t: number) {
return this.srcpos.lerp(this.dstpos, t);
}
}
// PolyTweener
//
class PolyTweener extends Tweener {
n: number;
constructor(entity: Entity, n=2) {
super(entity);
this.n = n;
}
}
// PolyTweenerIn
//
class PolyTweenerIn extends PolyTweener {
getPos(t: number) {
t = Math.pow(t, this.n);
return this.srcpos.lerp(this.dstpos, t);
}
}
// PolyTweenerOut
//
class PolyTweenerOut extends PolyTweener {
getPos(t: number) {
t = 1.0 - Math.pow(1.0-t, this.n)
return this.srcpos.lerp(this.dstpos, t);
}
}
// PolyTweenerInOut
//
class PolyTweenerInOut extends PolyTweener {
getPos(t: number) {
if (t < 0.5) {
t = 0.5*Math.pow(2*t, this.n); // in
} else {
t = 0.5*(2.0 - Math.pow(2.0-2*t, this.n)); // out
}
return this.srcpos.lerp(this.dstpos, t);
}
}