forked from NeuralMMO/client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanimation.js
175 lines (148 loc) · 4.38 KB
/
animation.js
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import * as textsprite from "./textsprite.js";
export {Move, Damage, Melee, Range, Mage}
//We dont exactly have animation tracks for this project
class ProceduralAnimation {
constructor() {
this.clock = new THREE.Clock()
this.elapsedTime = 0.0;
this.delta = 0.0;
this.setup()
this.scheduler = setTimeout(this.update.bind(this), 1000*tick/nAnim);
}
update() {
this.delta = this.clock.getDelta();
var time = this.elapsedTime + this.delta;
this.elapsedTime = Math.min(time, tick);
this.step(this.delta, this.elapsedTime);
if (this.elapsedTime < tick) {
this.scheduler = setTimeout(this.update.bind(this), 1000*tick/nAnim);
}
else {
this.finish();
}
}
//Abstract
step(delta, elapsedTime) {
throw new Error(
'Must override abstract step method of ProceduralAnimation');
}
//Optional call before animation
setup() {
}
//Optional call upon animation termination
finish() {
}
cancel() {
this.finish();
clearTimeout(this.scheduler);
}
}
class Move extends ProceduralAnimation {
constructor(ent, targ) {
super();
this.pos = ent.obj.position.clone();
this.targ = ent.coords(targ);
this.isTarget = false;
this.ent = ent;
}
step(delta, elapsedTime) {
var moveFrac = elapsedTime / tick;
var x = this.pos.x + moveFrac * (this.targ.x - this.pos.x);
var y = this.pos.y + moveFrac * (this.targ.y - this.pos.y);
var z = this.pos.z + moveFrac * (this.targ.z - this.pos.z);
var pos = new THREE.Vector3(x, y, z);
this.ent.obj.position.copy(pos);
if (this.isTarget) {
engine.camera.position.copy(pos);
engine.controls.target.copy(pos);//this.ent.obj.position);
engine.controls.update();
}
}
}
class Damage extends ProceduralAnimation {
constructor(ent, damage) {
super();
this.dmg = textsprite.makeTextSprite(damage, "16", '#ff0000');
this.dmg.scale.set(0.007, 0.017, 1);
this.height = 128
this.dmg.position.y = this.height
this.ent = ent;
ent.obj.add(this.dmg)
}
step(delta, elapsedTime) {
var moveFrac = elapsedTime / tick;
this.dmg.position.y = this.height+32*moveFrac;
}
finish() {
this.ent.obj.remove(this.dmg);
}
}
class Attack extends ProceduralAnimation {
constructor(scene, orig, targ) {
super();
this.orig = orig.obj;
this.targ = targ.obj;
this.scene = scene;
var attkMatl = new THREE.MeshBasicMaterial( {
color: this.color} );
var attkMesh = new THREE.Mesh(this.attkGeom, attkMatl);
this.attk = attkMesh
this.attk.position.x = this.orig.x;
this.attk.position.y = 128;
this.attk.position.z = this.orig.z;
scene.add(this.attk);
}
step(delta, elapsedTime) {
var moveFrac = elapsedTime / tick;
var x = this.orig.position.x + moveFrac * (this.targ.position.x - this.orig.position.x) + 16;
var y = 96;
var z = this.orig.position.z + moveFrac * (this.targ.position.z - this.orig.position.z) + 16;
var pos = new THREE.Vector3(x, y, z)
this.attk.position.copy(pos);
}
finish() {
this.scene.remove(this.attk);
}
}
class Melee extends Attack{
setup() {
this.attkGeom = new THREE.BoxGeometry(10, 10, 10);
this.color = '#ff0000';
}
}
class Range extends Attack{
setup() {
this.attkGeom = new THREE.OctahedronGeometry(10);
this.color = '#00ff00';
}
}
class Mage extends Attack{
setup() {
this.attkGeom = new THREE.IcosahedronGeometry(10);
this.color = '#0000ff';
}
}
class StatBar extends THREE.Object3D {
constructor(color, width, height) {
super();
this.valBar = this.initSprite(color);
this.valBar.center = new THREE.Vector2(1, 0);
this.redBar = this.initSprite(0xff0000);
this.redBar.center = new THREE.Vector2(0, 0);
this.offset = 64;
this.height = height;
this.width = width;
this.update(width);
}
initSprite(hexColor) {
var material = new THREE.SpriteMaterial({
color: hexColor, sizeAttenuation: false});
var sprite = new THREE.Sprite(material)
this.add(sprite)
return sprite
}
update(val) {
this.valBar.scale.set(val, this.height, 1);
this.redBar.scale.set(this.width - val, this.height, 1);
}
}