forked from JosephShering/adorbs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadorbs.lua
93 lines (81 loc) · 2.69 KB
/
adorbs.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
local engine = {}
local system = {}
local entity = {}
local state = {
entities = {},
systems = {}
}
-- System
function system.create(name, components, processFunc, status)
state.systems[name] = {
status = status ~= nil and status or 'running',
components = components,
process = processFunc
}
end
function system.pause(name)
state.systems[name].status = 'paused'
end
-- end System
-- Engine
function engine.state()
return state
end
function engine.process()
for _, system in pairs(state.systems) do
for entityName, entity in pairs(state.entities) do
local pluckedEntityComponents = {}
local matchRequirements = true
for _, requiredSystemComponentName in ipairs(system.components) do
if entity.components[requiredSystemComponentName] ~= nil then
pluckedEntityComponents[#pluckedEntityComponents +1] = entity.components[requiredSystemComponentName]
else
matchRequirements = false
break
end
end
if matchRequirements and #pluckedEntityComponents > 0 and system.status == 'running' then
system.process(love.timer.getDelta(), unpack(pluckedEntityComponents))
end
end
end
end
function engine.draw()
for _, system in pairs(state.systems) do
for entityName, entity in pairs(state.entities) do
local pluckedEntityComponents = {}
for _, requiredSystemComponentName in ipairs(system.components) do
if entity.components[requiredSystemComponentName] ~= nil then
pluckedEntityComponents[#pluckedEntityComponents +1] = entity.components[requiredSystemComponentName]
end
end
if #pluckedEntityComponents > 0 and system.status == 'running' and system.draw ~= nil then
system.draw(love.timer.getDelta(), unpack(pluckedEntityComponents))
end
end
end
end
-- Entity
function entity.create(name, components, isActive)
local newEntity = {
status = 'init',
active = isActive,
components = {}
}
if components ~= nil then
-- Allows the passing of either a string or another table
for componentName, component in pairs(components) do
if type(componentName) == "string" and type(component) == "table" then
newEntity.components[componentName] = component
else
newEntity.components[component] = {}
end
end
end
state.entities[name] = newEntity
return state.entities[name]
end
-- end Entity
return function()
return engine, system, entity
end