summaryrefslogtreecommitdiff
path: root/honey/ecs.lua
blob: 69feb12ec18cd3ad2753ff4f8df667e820d3d3e7 (plain)
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
math.randomseed(os.time())

local module = {}
setmetatable(module, {__index=_G})
setfenv(1, module)


--===== EntityDb =====--


-- EntityDb is a database of entities and their associated components
-- it should be quite efficient to query for all entities with a given component, and reasonably
-- efficient to query for all components of a given entity


EntityDb = {}
EntityDb.__index = EntityDb


function EntityDb.new(_)
	local self = {
		entities = {},
		components = {},
	}
	setmetatable(self, EntityDb)
	return self
end
setmetatable(EntityDb, {__call=EntityDb.new})


-- check if a given entity id is legitimate
function EntityDb.checkIsValid(self, id)
	if not self.entities[id] then
		error(string.format("invalid entity id: %s", tostring(id)))
	end
end


local random = math.random
local function uuid()
	local template ='xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'
	return string.gsub(template, '[xy]', function (c)
		local v = (c == 'x') and random(0, 0xf) or random(8, 0xb)
		return string.format('%x', v)
	end)
end


-- create a new entity
function EntityDb.createEntity(self, id)
	local id = id or uuid() -- allow inserting entities at preset ids for loading
	self.entities[id] = true
	return id
end


-- add a component to an entity
function EntityDb.addComponent(self, id, name, value)
	self:checkIsValid(id)
	
	-- create the relevant component table if it doesn't exist
	if not self.components[name] then
		self.components[name] = { count=0, data={} }
	end

	local component = self.components[name]
	component.data[id] = value
	component.count = component.count + 1
end


-- get all entities with a given component
function EntityDb.queryComponent(self, name)
	return self.components[name].data
end


-- get all components associated with an entity
function EntityDb.queryEntity(self, id)
	self:checkIsValid(id)
	local query = {}
	for name, component in pairs(self.components) do
		query[name] = component.data[id]
	end
	return query
end


-- get a specific component from an entity
function EntityDb.getComponent(self, id, name)
	self:checkIsValid(id)
	return self.components[name].data[id]
end


-- remove a component from an entity
function EntityDb.removeComponent(self, id, name)
	self:checkIsValid(id)
	local component = self.components[name]
	if component.data[id] ~= nil then
		component.data[id] = nil
		component.count = component.count - 1
		if component.count == 0 then
			self.components[name] = nil
		end
	end
end


-- remove an entity from the db
function EntityDb.deleteEntity(self, id)
	self:checkIsValid(id)
	for name in pairs(self.components) do
		self:removeComponent(id, name)
	end
	self.entities[id] = nil
end


--===== SystemDb =====--

SystemDb = {}
SystemDb.__index = SystemDb


function SystemDb.new(_, entityDb)
	local self = {
		systems = {},
		sorted = {},
		entityDb = entityDb,
	}
	setmetatable(self, SystemDb)
	return self
end
setmetatable(SystemDb, {__call=SystemDb.new})


local function systemId()
	local template = "xx:xx:xx"
	return string.gsub(template, "x", function(c)
		return string.format("%x", random(0, 0xf))
	end)
end


function SystemDb.addSystem(self, systemFunc, params)
	local system
	if type(systemFunc) == "table" then
		system = systemFunc
	else
		local params = params or {}
		params.db = self.entityDb
		system = systemFunc(params)
	end

	local id = systemId()
	self.systems[id] = system
	table.insert(self.sorted, id)
	self:sort()
	return id
end


function SystemDb.sort(self)
	table.sort(self.sorted, function(a, b) return (self.systems[a].priority or 100) < (self.systems[b].priority or 100) end)
end


function SystemDb.update(self, dt)
	for _, system in ipairs(self.sorted) do
		self.systems[system]:update(dt)
	end
end


function SystemDb.removeSystem(self, id)
	self.systems[id] = nil
	for i=#self.sorted,1,-1 do
		if self.sorted[i] == id then table.remove(self.sorted, i) end
	end
end


--===== Database =====--

Database = {}
Database.__index = Database

function Database.new(_)
	local self = {}
	self.entityDb = EntityDb()
	self.systemDb = SystemDb(self.entityDb)
	setmetatable(self, Database)
	return self
end
setmetatable(Database, {__call=Database.new})


function Database.isValidEntity(self, id)
	return self.entityDb:checkIsValid(id)
end
function Database.createEntity(self, id)
	return self.entityDb:createEntity(id)
end
function Database.addComponent(self, id, name, value)
	return self.entityDb:addComponent(id, name, value)
end
function Database.queryComponent(self, name)
	return self.entityDb:queryComponent(name)
end
function Database.queryEntity(self, id)
	return self.entityDb:queryEntity(id)
end
function Database.getComponent(self, id, name)
	return self.entityDb:getComponent(id, name)
end
function Database.removeComponent(self, id, name)
	return self.entityDb:removeComponent(id, name)
end
function Database.deleteEntity(self, id)
	return self.entityDb.deleteEntity(id)
end


function Database.addSystem(self, func, params)
	return self.systemDb:addSystem(func, params)
end
function Database.update(self, dt)
	return self.systemDb:update(dt)
end
function Database.removeSystem(self, id)
	return self.systemDb:removeSystem(id)
end


return module