-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathECS.lua
218 lines (192 loc) · 5.93 KB
/
ECS.lua
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
--[[
The big changes :
• entities are divided into batches. The player is his own batch with 1 single plaer,
but many other players
• components are also divided in batches. Positions are run in one file, same goes for rigid bodies
etc
]]
local ECS = {}
ECS._entities_path = "entities/"
ECS._components_path = "components/"
ECS._entity_names = {
world = "world.lua",
player = "player.lua",
npcs = "npcs.lua",
bullets = "bullets.lua",
pushpellets = "pushpellets.lua",
}
ECS._component_names = {
position = "position.lua",
body = "body.lua"
}
ECS.components = {}
ECS.entities = {}
ECS.entities_to_destroy = {}
ECS.components_to_destroy = {}
ECS.components_draw_order = {}
ECS.entities_draw_order = {}
-- The components table stores components as classified by tables.
-- As such, each batch of components of the same type are updated together in the same script which is
-- The script that is used to load them
function ECS:initialize(reload)
-- COMPONENTS MUST BE INITIALIZED BEFORE ENTITIES. BECAUSE SOME ENTITIES WILL CREATE THEM JUST AS LOADED
print((reload and "Rel" or "L").."oading components :")
if reload then
self.components = {}
self.entities = {}
self.entities_to_destroy = {}
self.components_to_destroy = {}
self.components_draw_order = {}
self.entities_draw_order = {}
end
for k,v in pairs(self._component_names) do
print("\tLoaded : "..k.." "..v);
self.components[k] = love.filesystem.load(self._components_path..v)()
if self.components[k].draw then
self.components_draw_order[#self.components_draw_order+1] = k
self.components[k].__draw_order = self.components[k].__draw_order or 9999
end
end
print((reload and "Rel" or "L").."oading entities :")
for k,v in pairs(self._entity_names) do
print("\tLoaded : "..k.." "..v);
self.entities[k] = love.filesystem.load(self._entities_path..v)()
if self.entities[k].draw then
self.entities_draw_order[#self.entities_draw_order+1] = k
self.entities[k].__draw_order = self.entities[k].__draw_order or 9999
end
end
table.sort(self.components_draw_order, function(a, b) return self.components[a].__draw_order < self.components[b].__draw_order end)
table.sort(self.entities_draw_order, function(a, b) return self.entities[a].__draw_order < self.entities[b].__draw_order end)
end
local function sort_by_layer(a, b)
return a.layer < b.layer
end
--
-- REMARK : ENTITY WORKS IS SUPPLIED WITH AN INFO TABLE
-- COMPONENTS ARE SUPPLIED THE ... WAY.
--
function ECS:new_entity(type, info)
-- Due to the change on how entities work, there is really no direct way to set up
-- which entity to draw before the other
-- Should basically return
return self.entities[type]:add(info);
end
function ECS:new_component(type, ...) -- returns the ID of the component as well as any other extra information (ex : reference of the table of the component), this information must be stored by the entity so that it can
-- use it (ex : positions or such) and so that the entity class destroy it once it destroys an entity
return self.components[type]:add(...);
end
function ECS:queue_entity_destroy(type, id) -- This can be called from a component such as health to indicate that entity has no more health
for i,v in ipairs(self.entities_to_destroy) do
if v[1] == type and v[2] == id then
return; -- if already pushed to be deleted, why delete it twice? Just return
end
end
self.entities_to_destroy[#self.entities_to_destroy+1] = {type=type, id=id}
end
function ECS:queue_component_destroy(type, id) -- This is called to destroy a component
for i,v in ipairs(self.components_to_destroy) do
if v[1] == type and v[2] == id then
return; -- if already pushed to be deleted, why delete it twice? Just return
end
end
self.components_to_destroy[#self.components_to_destroy+1] = {type=type, id=id}
end
--
--> Events
--
function ECS:draw()
for i,v in ipairs(self.components_draw_order) do
self.components[v]:draw()
end
for i,v in ipairs(self.entities_draw_order) do
self.entities[v]:draw()
end
end
function ECS:pre_update(dt)
for k,v in pairs(self.components) do
if v.pre_update then
v:pre_update(dt)
end
end
for k,v in pairs(self.entities) do
if v.pre_update then
v:pre_update(dt)
end
end
for k=#self.entities_to_destroy, 1, -1 do
local v = self.entities_to_destroy[k];
self.entities[v.type]:destroy(v.id);
table.remove(self.entities_to_destroy, k);
end
for k=#self.components_to_destroy, 1, -1 do
local v = self.components_to_destroy[k];
self.components[v.type]:destroy(v.id);
table.remove(self.components_to_destroy, k);
end
end
function ECS:update(dt)
-- Do pre_update
self:pre_update(dt);
-- then do update
for k,v in pairs(self.components) do
if v.update then
v:update(dt)
end
end
for k,v in pairs(self.entities) do
if v.update then
v:update(dt)
end
end
-- entities are destroyed at pre_update
end
function ECS:keypressed(const, scancode, isrepeat)
for k,v in pairs(self.components) do
if v.keypressed then
v:keypressed(const, scancode, isrepeat)
end
end
for k,v in pairs(self.entities) do
if v.keypressed then
v:keypressed(const, scancode, isrepeat)
end
end
end
function ECS:keyreleased(const, scancode, isrepeat)
for k,v in pairs(self.components) do
if v.keyreleased then
v:keyreleased(const, scancode, isrepeat)
end
end
for k,v in pairs(self.entities) do
if v.keyreleased then
v:keyreleased(const, scancode, isrepeat)
end
end
end
function ECS:mousepressed(x, y, button, istouch)
for k,v in pairs(self.components) do
if v.mousepressed then
v:mousepressed(x, y, button, istouch)
end
end
for k,v in pairs(self.entities) do
if v.mousepressed then
v:mousepressed(x, y, button, istouch)
end
end
end
function ECS:mousereleased(x, y, button, istouch)
for k,v in pairs(self.components) do
if v.mousereleased then
v:mousereleased(x, y, button, istouch)
end
end
for k,v in pairs(self.entities) do
if v.mousereleased then
v:mousereleased(x, y, button, istouch)
end
end
end
return ECS