-
Notifications
You must be signed in to change notification settings - Fork 2
/
world.lua
137 lines (110 loc) · 2.89 KB
/
world.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
local Global = require("lecs.global")
local TinyECS = require("lecs.tiny_ecs")
---@class World
---@field private _world table
---@field private _eid number
---@field private _singletonEntity table<string, Entity>
---@field private _changedEntityCache Entity
local World = class("World")
function World:ctor()
self._world = TinyECS.world()
self._eid = 0
self._singletonEntity = {}
self._changedEntityCache = nil
end
--region Public
---@public
---@param dt number
function World:Update(dt)
if self._changedEntityCache then
self._changedEntityCache = nil
end
self._world:update(dt)
end
---@public
---@param system System
function World:AddSystem(system)
self._world:addSystem(system._system)
system._world = self
end
---@public
---@param system System
function World:RemoveSystem(system)
self._world:removeSystem(system._system)
system._world = nil
end
---@public
---@return Entity
function World:CreateEntity()
local e = Global:GetEntity(self, self:GetEid())
self:AddEntity(e)
return e
end
---@public
---@param name string SingletonEntity Identifier
---@return Entity
function World:CreateSingletonEntity(name)
if self._singletonEntity[name] then
return self._singletonEntity[name]
end
local e = Global:GetEntity(self, self:GetEid(), name)
self:AddEntity(e)
return e
end
---@public
---@param entity Entity
function World:RemoveEntity(entity)
if entity._singletonName then
self:removeSingletonEntity(entity)
end
self._world:removeEntity(entity)
if self._changedEntityCache == entity then
self._changedEntityCache = nil
end
Global:RecycleEntity(entity)
end
---@public
---@param singletonName string
---@return Entity
function World:GetSingletonEntity(singletonName)
return self._singletonEntity[singletonName]
end
--endregion
--region Private
---@private
function World:GetEid()
self._eid = self._eid + 1
return self._eid
end
---@private
---@param entity Entity
function World:AddEntity(entity)
if entity ~= self._changedEntityCache then
self._changedEntityCache = entity
self._world:addEntity(entity)
end
end
---@private
---@param entity Entity
function World:AddSingletonEntity(entity)
if self._singletonEntity[entity._singletonName] then
Log.e(string.format("Singleton entity %s already in this world"), entity._singletonName)
return
end
self._singletonEntity[entity._singletonName] = entity
end
---@private
---@param entity Entity | string
function World:RemoveSingletonEntity(entity)
local singletonName = entity
if not type(entity) == "string" then
singletonName = entity._singletonName
end
if not self._singletonEntity[singletonName] then
Log.e(string.format("Entity %s isn't a singleton"), singletonName)
return
end
self._singletonEntity[singletonName] = nil
end
--endregion
return World