-
Notifications
You must be signed in to change notification settings - Fork 0
/
Entity.js
executable file
·199 lines (164 loc) · 3.94 KB
/
Entity.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
// parts pattern of funtional inheritance
// basic plumbing for gameObjects
var Entity = function(sub) {
var that = sub || {};
var updateFuncs;
if( that.updateFuncs === undefined ) {
updateFuncs = [];
}
else {
updateFuncs = that.updateFuncs;
}
var addUpdateFunc = function (f) {
updateFuncs.push(f);
};
var update = function () {
updateFuncs.forEach(function (f) {
f();
});
};
that.update = update;
that.addUpdateFunc = addUpdateFunc;
that.updateFuncs = updateFuncs;
return that;
};
// renders to the screen
var Renderable = function (mesh, sub) {
var Mesh, geometry, material, active = false;
var that = sub || {};
Entity(that);
if(mesh instanceof THREE.Mesh) {
Mesh = mesh;
geometry = Mesh.geometry;
material = Mesh.material;
Nomads.scene.add(Mesh);
}
that.addUpdateFunc(function () {
});
var toggleActive = function () {
that.active = !that.active;
if(that.active === false) {
Nomads.scene.remove(Mesh);
}
else {
Nomads.scene.add(Mesh);
}
return that.active;
}
that.Mesh = Mesh;
that.geometry = geometry;
that.material = material;
that.active = active;
that.toggleActive = toggleActive;
// init
toggleActive();
return that;
}
// is selectable
var Selectable = function(sub) {
var selected = false;
var that = sub || {};
Entity(that);
that.addUpdateFunc(function() {
if (that.material === undefined) {
return;
}
if(selected) {
that.material.emissive.setHex(0xff0000);
}
else {
that.material.emissive.setHex(0x000000);
}
});
var select = function() {
selected = true;
}
var deselect = function() {
selected = false;
}
that.select = select;
that.deselect = deselect;
return that;
};
// can accept orders and move in the world
var Moveable = function(sub) {
var that = sub || {};
Entity(that);
var moveSpeed = 1.0;
var turnSpeed = 0.2;
var moving = false;
var turning = false;
var target;
var turnAxis = new THREE.Vector3( 0, 1, 0 );;
var previousLook = new THREE.Vector3(1);
var logCount = 0;
if(that.Mesh !== undefined) {
target = new THREE.Vector3().copy(that.Mesh.position);
}
else {
target = new THREE.Vector3();
}
var move = function (loc) {
if(that.Mesh === undefined) {
return;
}
target = loc;
}
var turnAngle = function (from, to) {
var fromCopy = new THREE.Vector3().copy(from).normalize();
var toCopy = new THREE.Vector3().copy(to).normalize();
var angleFrom = Math.atan2(fromCopy.z, fromCopy.x);
var angleTo = Math.atan2(toCopy.z, toCopy.x);
var diff = Math.abs(angleFrom - angleTo);
if(diff > Math.PI) {
if(angleFrom < angleTo) {
angleFrom += 2 * Math.PI;
}
else {
angleTo += 2 * Math.PI;
}
}
var signedDiff = angleFrom - angleTo;
if(signedDiff > turnSpeed) {
// origin is CCW from the target
// turn CW
turning = true;
that.Mesh.rotateOnAxis(turnAxis, turnSpeed);
}
else if(signedDiff < -turnSpeed) {
// origin is CW from target
// turn CCW
turning = true;
that.Mesh.rotateOnAxis(turnAxis, -turnSpeed);
}
}
that.addUpdateFunc(function() {
if(that.Mesh === undefined) {
return;
}
var threshold = moveSpeed*moveSpeed;
var pos = that.Mesh.position;
var moveDir = new THREE.Vector3().subVectors(target,pos);
previousLook = that.Mesh.localToWorld(new THREE.Vector3(1));
previousLook = previousLook.sub(that.Mesh.position);
previousLook.setY(0);
turnAngle(previousLook, moveDir);
if(moveDir.lengthSq() > threshold) {
moving = true;
// move toward target
pos.add(moveDir.normalize().multiplyScalar(moveSpeed));
}
else
{
moving = false;
}
});
that.move = move
that.moving = moving;
that.moveSpeed = moveSpeed;
};
// plays animations and keeps animation state
var Animated = function(sub) {
var that = sub || {};
Entity(that);
};