-
Notifications
You must be signed in to change notification settings - Fork 0
/
ecs.js
333 lines (313 loc) · 8.87 KB
/
ecs.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/**
*
* @param {float} angle angle in degrees
* @returns {float} angle in radians
*/
const deg2rad = (angle) => angle * 3.1415926 / 180
const rad2deg = (angle) => angle * 180 / 3.1415926
class Vec2 {
/**
* New 2d vector with x and y component
* @param {float} x
* @param {float} y
*/
constructor(x=0, y=0) {
this.x = x; this.y = y
}
toString() {
return `[${this.x.toFixed(2)} ${this.y.toFixed(2)}]`
}
/**
* Vector addition
* @param {Vec2} other
* @returns {Vec2}
*/
add(other) {
return new Vec2(this.x + other.x, this.y + other.y)
}
/**
* Vector subtraction
* @param {Vec2} other
* @returns {Vec2}
*/
sub(other) {
return new Vec2(this.x - other.x, this.y - other.y)
}
/**
* Vector-scalar product
* @param {number} scalar
* @returns Vec2
*/
mul_scalar(scalar) {
return new Vec2(scalar * this.x, scalar * this.y)
}
/**
* Multiplication
* @param {Number} other can be Scalar
* @returns {Vec2}
*/
mul(other) {
if (!isNaN(other)) return this.mul_scalar(other)
}
/**
* Division
* @param {*} scalar scalar value
* @returns {Vec2}
*/
div(scalar) {
return new Vec2(this.x / scalar, this.y / scalar)
}
/**
* Magnitude of vector
* @returns {number} magnitude
*/
magnitude() {
return Math.sqrt(this.x * this.x + this.y * this.y)
}
/**
* Calculate unit vector (norm)
* @returns {Vec2}
*/
unit() {
const magnitude = this.magnitude()
return new Vec2(this.x / magnitude, this.y / magnitude)
}
/**
* Calculate angle of vector, with other vector being (0,1)
* @returns {Number} angle in degrees
*/
angle() {
return rad2deg(Math.atan2(this.x, this.y))
}
/**
* Rotate a vector
* @param {number} angle in degrees
* @returns
*/
rotate_deg(angle) {
const magnitude = this.magnitude()
return new Vec2(
// Math.cos(deg2rad(angle)) - Math.sin(deg2rad(angle)) * magnitude,
// Math.sin(deg2rad(angle)) + Math.cos(deg2rad(angle)) * magnitude
Math.cos(deg2rad(angle)) * magnitude,
Math.sin(deg2rad(angle)) * magnitude
)
}
/**
* Distance between two vectors
* @param {Vec2} a Vector one
* @param {Vec2} b Vector two
* @returns {Number} distance
*/
static dist(a, b) {
return new Vec2(b.x - a.x, b.y - a.y).magnitude()
}
/**
* Calculates the dot product between two vectors
* @param {Vec2} a Vector one
* @param {Vec2} b Vector two
* @returns {Number} dot product
*/
static dot(a, b) {
return a.x * b.x + a.y * b.y
}
/**
* Calculates the cosine between two vectors
* @param {Vec2} a Vector one
* @param {Vec2} b Vector two
* @returns {Number} cosine
*/
static cos(a, b) {
return Vec2.dot(a, b) / (a.magnitude() * b.magnitude())
}
}
class Mat3x3 {
constructor(mat) {
this.mat = mat
}
/**
* Create a translation matrix
* @param {number} x x-displacement
* @param {number} y y-displacement
* @returns {Mat3x3}
*/
static translation(x, y) {
return new Mat3x3([
[1, 0, x],
[0, 1, y],
[0, 0, 1]
])
}
/**
* Create a rotation matrix
* @param {number} angle angle in degrees
* @returns {Mat3x3}
*/
static rotation(angle) {
angle = deg2rad(angle)
return new Mat3x3([
[Math.cos(angle), -Math.sin(angle), 0],
[Math.sin(angle), Math.cos(angle), 0],
[ 0, 0, 1]
])
}
/**
* Matrix-Vector product (for homogenous coordinate system)
* @param {*} vec
* @returns
*/
mul_vec2(vec) {
return new Vec2(
this.mat[0][0] * vec.x + this.mat[0][1] * vec.y + this.mat[0][2],
this.mat[1][0] * vec.x + this.mat[1][1] * vec.y + this.mat[1][2])
}
/**
* Matrix-matrix product
* @param {Mat3x3} mat other matrix
* @returns {Mat3x3} result
*/
mul_mat3x3(mat) {
return new Mat3x3([
// Cheat sheet: https://commons.wikimedia.org/wiki/File:3x3-Matrix-Multiplication.png
// Col 0 Col 1 Col 2
[this.mat[0][0] * mat.mat[0][0] + this.mat[0][1] * mat.mat[1][0] + this.mat[0][2] * mat.mat[2][0], this.mat[0][0] * mat.mat[0][1] + this.mat[0][1] * mat.mat[1][1] + this.mat[0][2] * mat.mat[2][1], this.mat[0][0] * mat.mat[0][2] + this.mat[0][1] * mat.mat[1][2] + this.mat[0][2] * mat.mat[2][2]],
[this.mat[1][0] * mat.mat[0][0] + this.mat[1][1] * mat.mat[1][0] + this.mat[1][2] * mat.mat[2][0], this.mat[1][0] * mat.mat[0][1] + this.mat[1][1] * mat.mat[1][1] + this.mat[1][2] * mat.mat[2][1], this.mat[1][0] * mat.mat[0][2] + this.mat[1][1] * mat.mat[1][2] + this.mat[1][2] * mat.mat[2][2]],
[this.mat[2][0] * mat.mat[0][0] + this.mat[2][1] * mat.mat[1][0] + this.mat[2][2] * mat.mat[2][0], this.mat[2][0] * mat.mat[0][1] + this.mat[2][1] * mat.mat[1][1] + this.mat[2][2] * mat.mat[2][1], this.mat[2][0] * mat.mat[0][2] + this.mat[2][1] * mat.mat[1][2] + this.mat[2][2] * mat.mat[2][2]]
])
}
/**
* General multiplication
* @param {Vec2 | Mat3x3} other
* @returns {Vec2 | Mat3x3}
*/
mul(other) {
if (other instanceof Vec2) return this.mul_vec2(other)
else if (other instanceof Mat3x3) return this.mul_mat3x3(other)
}
}
class Component {
/**
* Construct component type
* @param {string} name Name of component
* @param {object} initialState Initial State, which gets used for creation of every new entity
*/
constructor(name, initialState) {
this.new = () => { return Object.assign({}, initialState) }
this.name = name
}
}
class Entity {
/**
* Construct entity
* @param {Array<Component>} initialComponents Components which the entity will hold. They will be constructed by calling the `.new()` method
*/
constructor(initialComponents) {
this.components = {}
for (let component of initialComponents) {
this.components[component.name] = component.new()
}
}
}
class System {
/**
* @param {Array<Component>} hookComponents Components which system gets hooked in.
*/
constructor(hookComponents) {
this.hookComponents = hookComponents
}
onEntity() {
alert("you need to implement the System.onEntity method!")
}
}
class ECS {
/**
* Construct ECS which holds and manages systems, entities
* @param {Object} globals Potential global settings every system can access
*/
constructor(globals) {
this.systems = []
this.entities = []
this.running = true
this.lastEntityId = 0
this.startTime = new Date().getTime()
this.deltaTime = 0
this.globals = globals
}
/**
* Add and initialize systems
* @param {Array<System>} systems
*/
addSystems(systems) {
// initialize systems
this.systems = [...this.systems, ...systems.map(s => new s(this))]
}
/**
* @param {Array<Entity>} entities
*/
addEntities(entities) {
for (const e of entities) {
e.id = this.lastEntityId++
this.entities.push(e)
}
this._assembleEntities()
}
/**
* Assemble all entities for systems in array, so system can access all entites with `this.entities`
*/
_assembleEntities() {
for (const system of this.systems) {
const componentRequirements = system.hookComponents.map(comp => comp.name)
system.entities = this.entities.filter(e => componentRequirements.every(name => Object.keys(e.components).includes(name)))
}
}
/**
* Executes one tick. This consists of
* - loop through all systems, execute `beforeTick` function
* - loop through all entities, see if system is hooked to current entity
* - if entity is hooked, execute `onEntity` function
*/
tick() {
const newTime = new Date().getTime()
this.deltaTime = newTime - this.startTime
this.startTime = newTime
// Iterate through all systems
for (const system of this.systems) {
system.beforeTick(this, system)
const systemComponentNames = system.hookComponents.map(comp => comp.name)
// Iterate through all entities
for (const entity of this.entities) {
const componentNames = Object.keys(entity.components)
const match = systemComponentNames.every(name => componentNames.includes(name))
if (match) {
system.onEntity(this, entity)
}
}
}
}
/**
* Continuous running loop
*/
run() {
const loopFunc = () => {
this.tick()
if (this.running) window.requestAnimationFrame(loopFunc)
}
if (this.running) window.requestAnimationFrame(loopFunc)
}
/**
* Stop running loop
*/
pause() {
if (this.running) {
this.running = false
} else {
const newTime = new Date().getTime()
this.deltaTime = newTime - this.startTime
this.startTime = newTime
this.running = true
}
}
}
Math.random_between = (min, max) => {
return Math.random() * (max - min) + min;
}