| 1 | export class EcsWorld |
| 2 | { |
| 3 | constructor () |
| 4 | { |
| 5 | this.nextHandle = 1; |
| 6 | this.nextEntityId = 1; |
| 7 | this.freeHandles = []; |
| 8 | this.alive = new Set(); |
| 9 | this.entityIdByHandle = new Map(); |
| 10 | this.handleByEntityId = new Map(); |
| 11 | this.stores = new Map(); |
| 12 | } |
| 13 | |
| 14 | spawn (entityId = null) |
| 15 | { |
| 16 | const id = entityId == null ? this.nextEntityId : Number(entityId); |
| 17 | if (!Number.isInteger(id) || id < 1) |
| 18 | { |
| 19 | const error = new Error("Entity id must be a positive integer."); |
| 20 | error.userFacing = true; |
| 21 | throw error; |
| 22 | } |
| 23 | if (this.handleByEntityId.has(id)) |
| 24 | { |
| 25 | const error = new Error(`Entity ${id} already exists.`); |
| 26 | error.userFacing = true; |
| 27 | throw error; |
| 28 | } |
| 29 | if (id >= this.nextEntityId) |
| 30 | { this.nextEntityId = id + 1; } |
| 31 | |
| 32 | const handle = this.freeHandles.pop() ?? this.nextHandle++; |
| 33 | this.alive.add(handle); |
| 34 | this.entityIdByHandle.set(handle, id); |
| 35 | this.handleByEntityId.set(id, handle); |
| 36 | return handle; |
| 37 | } |
| 38 | |
| 39 | entity () |
| 40 | { |
| 41 | return this.spawn(); |
| 42 | } |
| 43 | |
| 44 | adopt (entityId) |
| 45 | { |
| 46 | return this.spawn(entityId); |
| 47 | } |
| 48 | |
| 49 | handleOf (entityId) |
| 50 | { |
| 51 | return this.handleByEntityId.get(entityId) ?? null; |
| 52 | } |
| 53 | |
| 54 | entityIdOf (handle) |
| 55 | { |
| 56 | return this.entityIdByHandle.get(handle) ?? null; |
| 57 | } |
| 58 | |
| 59 | hasEntity (entityId) |
| 60 | { |
| 61 | return this.handleByEntityId.has(entityId); |
| 62 | } |
| 63 | |
| 64 | isAlive (handle) |
| 65 | { |
| 66 | return this.alive.has(handle); |
| 67 | } |
| 68 | |
| 69 | destroy (handle) |
| 70 | { |
| 71 | if (!this.alive.has(handle)) |
| 72 | { return; } |
| 73 | const entityId = this.entityIdByHandle.get(handle); |
| 74 | this.alive.delete(handle); |
| 75 | this.entityIdByHandle.delete(handle); |
| 76 | if (entityId != null) |
| 77 | { this.handleByEntityId.delete(entityId); } |
| 78 | for (const store of this.stores.values()) |
| 79 | { store.delete(handle); } |
| 80 | this.freeHandles.push(handle); |
| 81 | } |
| 82 | |
| 83 | destroyEntity (entityId) |
| 84 | { |
| 85 | const handle = this.handleOf(entityId); |
| 86 | if (handle != null) |
| 87 | { this.destroy(handle); } |
| 88 | } |
| 89 | |
| 90 | add (handle, name, data) |
| 91 | { |
| 92 | let store = this.stores.get(name); |
| 93 | if (!store) |
| 94 | { |
| 95 | store = new Map(); |
| 96 | this.stores.set(name, store); |
| 97 | } |
| 98 | store.set(handle, data); |
| 99 | return data; |
| 100 | } |
| 101 | |
| 102 | get (handle, name) |
| 103 | { |
| 104 | return this.stores.get(name)?.get(handle); |
| 105 | } |
| 106 | |
| 107 | has (handle, name) |
| 108 | { |
| 109 | return this.stores.get(name)?.has(handle) ?? false; |
| 110 | } |
| 111 | |
| 112 | remove (handle, name) |
| 113 | { |
| 114 | this.stores.get(name)?.delete(handle); |
| 115 | } |
| 116 | |
| 117 | componentsOf (handle) |
| 118 | { |
| 119 | const components = {}; |
| 120 | for (const [name, store] of this.stores) |
| 121 | { |
| 122 | if (store.has(handle)) |
| 123 | { components[name] = store.get(handle); } |
| 124 | } |
| 125 | return components; |
| 126 | } |
| 127 | |
| 128 | query (...names) |
| 129 | { |
| 130 | const primary = this.stores.get(names[0]); |
| 131 | if (!primary) |
| 132 | { return []; } |
| 133 | |
| 134 | const results = []; |
| 135 | for (const [handle, first] of primary) |
| 136 | { |
| 137 | if (!this.alive.has(handle)) |
| 138 | { continue; } |
| 139 | |
| 140 | const record = { |
| 141 | handle, |
| 142 | entityId: this.entityIdByHandle.get(handle), |
| 143 | [names[0]]: first |
| 144 | }; |
| 145 | let matched = true; |
| 146 | for (let i = 1; i < names.length; i++) |
| 147 | { |
| 148 | const component = this.get(handle, names[i]); |
| 149 | if (!component) |
| 150 | { |
| 151 | matched = false; |
| 152 | break; |
| 153 | } |
| 154 | record[names[i]] = component; |
| 155 | } |
| 156 | if (matched) |
| 157 | { results.push(record); } |
| 158 | } |
| 159 | return results; |
| 160 | } |
| 161 | } |