-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathentity.lua
More file actions
79 lines (65 loc) · 1.76 KB
/
Copy pathentity.lua
File metadata and controls
79 lines (65 loc) · 1.76 KB
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
pico-8 cartridge // http://www.pico-8.com
version 18
__lua__
function getentity()
local entity = {}
entity.__index = entity
setmetatable(entity, {
__call = function(klass, ...)
local self = setmetatable({}, klass)
self:_init(...)
return self
end,
})
function entity:_init(id, type)
self.id = id
self.type = type
self.components = {} -- Use a table/map
self.eventManager = nil
self.alive = false
end
function entity:set_type(type_newval)
self.type = type_newval
end
function entity:get_type()
return self.type
end
function entity:get(cmp_type)
--debug
if debug then
logger:debug("get " .. cmp_type .. " from entity ")
end
return self.components[cmp_type] -- Direct access, O(1)
end
function entity:add(cmp)
local cmp_type = cmp:get_type()
if not self.components[cmp_type] then
self.components[cmp_type] = cmp
if debug then
logger:debug("added "..cmp_type.." to entity ")
end
else
if debug then
logger:debug("Aready added "..cmp_type.." to entity ")
end
end
end
function entity:remove(cmp)
--debug
if debug then
logger:debug("delete " .. cmp:get_type() .. " from entity " )
end
local cmp_type = cmp:get_type()
self.components[cmp_type] = nil -- Remove from the table
end
function entity:get_components()
return self.components
end
function entity:set_id(newval)
self.id = newval
end
function entity:get_id()
return self.id
end
return entity
end