Skip to content

Commit

Permalink
Changes to the name interface and couple other things:
Browse files Browse the repository at this point in the history
* Made sure entities added and removed before World:start is called.
* Updated .gitignore for ammo-assets.
  • Loading branch information
Michael Ebens committed Oct 21, 2012
1 parent 93abae6 commit 439f311
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 15 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
_*
.DS_STORE
.DS_Store
assets/
tweens/
physics/
input/
Expand Down
5 changes: 2 additions & 3 deletions core/Entity.lua
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,8 @@ function Entity._mt:__newindex(key, value)
if self._name == value then return end

if self._world then
local prev = self._name
self._name = value
self._world:_setName(self, prev)
if self._name then self._world.names[self._name] = nil end
self._world.names[value] = self
else
self._name = value
end
Expand Down
24 changes: 13 additions & 11 deletions core/World.lua
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,15 @@ function World:initialize()
self._add = {}
self._remove = {}
self._classCounts = {}
self.names = {}
self._names = setmetatable({}, { __newindex = function(s, k, v) print(k, v); rawset(s, k, v) end })

-- interface to _names
self.names = setmetatable({}, { __index = self._names, __newindex = function(_, name, entity)
if self._names[name] then self._names[name]._name = nil end
self._names[name] = entity
--print(name, entity, self._names)
if entity then entity._name = name end
end })

self:applyAccessors()
self.camera = nil -- set off the default behaviour
Expand Down Expand Up @@ -162,8 +170,8 @@ function World:_updateLists()
v._removalQueued = false
v._world = nil
if v.class then self._classCounts[v.class.name] = self._classCounts[v.class.name] - 1 end
if v.layer then self._layers[v._layer]:remove(v) end
if v.name then self.names[v.name] = nil end
if v._layer then self._layers[v._layer]:remove(v) end
if v._name then self._names[v._name] = nil end
end

-- add
Expand All @@ -172,8 +180,8 @@ function World:_updateLists()
v._additionQueued = false
v._world = self
if v.class then self._classCounts[v.class.name] = (self._classCounts[v.class.name] or 0) + 1 end
if v.layer then self:_setLayer(v) end
if v.name then self:_setName(v) end
if v._layer then self:_setLayer(v) end
if v._name then self.names[v._name] = v end
if v.added then v:added() end
end

Expand All @@ -187,9 +195,3 @@ function World:_setLayer(e, prev)
if not self._layers[e.layer] then self:addLayer(e.layer) end
self._layers[e.layer]:unshift(e)
end

function World:_setName(e, prev)
assert(not self.names[e.name], "An entity already has the name \"" .. e.name .. "\" in this world.")
if prev then self.names[prev] = nil end
self.names[e.name] = e
end
6 changes: 5 additions & 1 deletion core/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,11 @@ function ammo.update(dt)
if ammo._world then ammo._world:stop() end
ammo._world = ammo._goto
ammo._goto = nil
if ammo._world then ammo._world:start() end

if ammo._world then
ammo._world:_updateLists() -- make sure all entities are added (or removed) beforehand
ammo._world:start()
end
end
end

Expand Down

0 comments on commit 439f311

Please sign in to comment.