-
-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathComponent.js
172 lines (146 loc) · 4.78 KB
/
Component.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
import { $storeSize, createStore, resetStoreFor, resizeStore } from './Storage.js'
import { $queries, queryAddEntity, queryRemoveEntity, queryCheckEntity, commitRemovals } from './Query.js'
import { $bitflag, $size } from './World.js'
import { $entityMasks, getDefaultSize, eidToWorld, $entityComponents, getGlobalSize, $entitySparseSet } from './Entity.js'
export const $componentMap = Symbol('componentMap')
export const components = []
export const resizeComponents = (size) => {
components.forEach(component => resizeStore(component, size))
}
/**
* Defines a new component store.
*
* @param {object} schema
* @returns {object}
*/
export const defineComponent = (schema, size) => {
const component = createStore(schema, size || getGlobalSize())
if (schema && Object.keys(schema).length) components.push(component)
return component
}
export const incrementBitflag = (world) => {
world[$bitflag] *= 2
if (world[$bitflag] >= 2**31) {
world[$bitflag] = 1
world[$entityMasks].push(new Uint32Array(world[$size]))
}
}
/**
* Registers a component with a world.
*
* @param {World} world
* @param {Component} component
*/
export const registerComponent = (world, component) => {
if (!component) throw new Error(`bitECS - Cannot register null or undefined component`)
const queries = new Set()
const notQueries = new Set()
const changedQueries = new Set()
world[$queries].forEach(q => {
if (q.allComponents.includes(component)) {
queries.add(q)
}
})
world[$componentMap].set(component, {
generationId: world[$entityMasks].length - 1,
bitflag: world[$bitflag],
store: component,
queries,
notQueries,
changedQueries,
})
incrementBitflag(world)
}
/**
* Registers multiple components with a world.
*
* @param {World} world
* @param {Component} components
*/
export const registerComponents = (world, components) => {
components.forEach(c => registerComponent(world, c))
}
/**
* Checks if an entity has a component.
*
* @param {World} world
* @param {Component} component
* @param {number} eid
* @returns {boolean}
*/
export const hasComponent = (world, component, eid) => {
const registeredComponent = world[$componentMap].get(component)
if (!registeredComponent) return false
const { generationId, bitflag } = registeredComponent
const mask = world[$entityMasks][generationId][eid]
return (mask & bitflag) === bitflag
}
/**
* Adds a component to an entity
*
* @param {World} world
* @param {Component} component
* @param {number} eid
* @param {boolean} [reset=false]
*/
export const addComponent = (world, component, eid, reset=false) => {
if (eid === undefined) throw new Error('bitECS - entity is undefined.')
if (!world[$entitySparseSet].has(eid)) throw new Error('bitECS - entity does not exist in the world.')
if (!world[$componentMap].has(component)) registerComponent(world, component)
if (hasComponent(world, component, eid)) return
const c = world[$componentMap].get(component)
const { generationId, bitflag, queries, notQueries } = c
// Add bitflag to entity bitmask
world[$entityMasks][generationId][eid] |= bitflag
// todo: archetype graph
queries.forEach(q => {
// remove this entity from toRemove if it exists in this query
q.toRemove.remove(eid)
const match = queryCheckEntity(world, q, eid)
if (match) {
q.exited.remove(eid)
queryAddEntity(q, eid)
}
if (!match) {
q.entered.remove(eid)
queryRemoveEntity(world, q, eid)
}
})
world[$entityComponents].get(eid).add(component)
// Zero out each property value
if (reset) resetStoreFor(component, eid)
}
/**
* Removes a component from an entity and resets component state unless otherwise specified.
*
* @param {World} world
* @param {Component} component
* @param {number} eid
* @param {boolean} [reset=true]
*/
export const removeComponent = (world, component, eid, reset=true) => {
if (eid === undefined) throw new Error('bitECS - entity is undefined.')
if (!world[$entitySparseSet].has(eid)) throw new Error('bitECS - entity does not exist in the world.')
if (!hasComponent(world, component, eid)) return
const c = world[$componentMap].get(component)
const { generationId, bitflag, queries } = c
// Remove flag from entity bitmask
world[$entityMasks][generationId][eid] &= ~bitflag
// todo: archetype graph
queries.forEach(q => {
// remove this entity from toRemove if it exists in this query
q.toRemove.remove(eid)
const match = queryCheckEntity(world, q, eid)
if (match) {
q.exited.remove(eid)
queryAddEntity(q, eid)
}
if (!match) {
q.entered.remove(eid)
queryRemoveEntity(world, q, eid)
}
})
world[$entityComponents].get(eid).delete(component)
// Zero out each property value
if (reset) resetStoreFor(component, eid)
}