summaryrefslogtreecommitdiff
path: root/honey/ecs.lua
blob: 4eee34354412b5be26be23eecb0137dee6450338 (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
local module = {}
setmetatable(module, {__index=_G})
setfenv(1, module)


--===== filters =====--


Filter = {}

-- base filter creation
function Filter.new(_, operation, tbl)
	local self = {}
	self.keys = {}
	self.filters = {}
	self.op = operation

	for _, v in ipairs(tbl) do
		if type(v) == "function" then
			table.insert(self.filters, v)
		elseif type(v) == "string" then
			table.insert(self.keys, v)
		end
	end

	return function(entity, _self) 
		local entity = _self or entity -- able to call as . or :
		return Filter.check(self, entity)
	end
end
setmetatable(Filter, {__call=Filter.new})


-- base filter checking
function Filter.check(self, entity)
	local funcname = "check" .. self.op
	return Filter[funcname](self, entity)
end


-- AND filter (all keys and subfilters must match)
function Filter.AND(tbl)
	return Filter("AND", tbl)
end
function Filter.checkAND(self, entity)
	for _, subfilter in ipairs(self.filters) do
		if not subfilter(entity) then return false end
	end
	for _, key in ipairs(self.keys) do
		if entity[key] == nil then return false end
	end
	return true
end


-- OR filter (at least one key or subfilter must match)
function Filter.OR(tbl)
	return Filter("OR", tbl)
end
function Filter.checkOR(self, entity)
	for _, subfilter in ipairs(self.filters) do
		if subfilter(entity) then return true end
	end
	for _, key in ipairs(self.keys) do
		if entity[key] ~= nil then return true end
	end
	return false
end


-- NAND filter (at least one key or subfilter must NOT match)
function Filter.NAND(tbl)
	return Filter("NAND", tbl)
end
function Filter.checkNAND(self, entity)
	for _, subfilter in ipairs(self.filters) do
		if not subfilter(entity) then return true end
	end
	for _, key in ipairs(self.keys) do
		if entity[key] == nil then return true end
	end
	return false
end


-- NOR filter (no keys or subfilters may match)
function Filter.NOR(tbl)
	return Filter("NOR", tbl)
end
function Filter.checkNOR(self, entity)
	for _, subfilter in ipairs(self.filters) do
		if subfilter(entity) then return false end
	end
	for _, key in ipairs(self.keys) do
		if entity[key] ~= nil then return false end
	end
	return true
end


--===== levels =====--

Level = {}
Level.__index = Level

function Level.new(_)
	local self = {}
	self.systems = {}
	self.entities = {}
	self.nextId = 1
	setmetatable(self, Level)
	return self
end
setmetatable(Level, {__call=Level.new})


local function systemLt(a, b)
	return (a.priority or 50) < (b.priority or 50)
end
function Level.addSystem(self, system)
	assert(system.update, "systems must have an 'update' key")
	assert(system.filter, "systems must have a 'filter' key")
	system.entities = {}
	table.insert(self.systems, system)
	table.sort(self.systems, systemLt)
end


local function addEntityToSystem(system, id, entity)
	-- check if entity is already present
	if system.entities[id] then return end

	if system.onAddEntity then
		system.onAddEntity(id, entity)
	end
	system.entities[id] = true
end


local function removeEntityFromSystem(system, id, entity)
	-- check if entity is already absent
	if not system.entities[id] then return end

	if system.onRemoveEntity then
		system.onRemoveEntity(id, entity)
	end
	system.entities[id] = nil
end


function Level.addEntity(self, entity)
	table.insert(self.entities, entity)
	local id = self.nextId
	self.nextId = id + 1

	for _, system in ipairs(self.systems) do
		if system.filter(entity) then
			addEntityToSystem(system, id, entity)
		end
	end

	return id
end


function Level.reconfigureEntity(self, id)
	local entity = self.entities[id]
	
	for _, system in ipairs(self.systems) do
		if system.filter(entity) then
			addEntityToSystem(system, id, entity)
		else
			removeEntityFromSystem(system, id, entity)
		end
	end
end


function Level.removeEntity(self, id)
	local entity = self.entities[id]
	for _, system in ipairs(self.systems) do
		removeEntityFromSystem(system, id, entity)
	end
	self.entities[id] = nil
end


function Level.reconfigureAllEntities(self)
	for id in ipairs(self.entities) do
		self:reconfigureEntity(id)
	end
end


function Level.update(self, dt)
	for _, system in ipairs(self.systems) do
		for id in pairs(system.entities) do
			local entity = self.entities[id]
			system.update(entity, dt)
		end
	end
end


return module