Skip to content

Commit

Permalink
Erase destroyed entites after a safe time has elapsed
Browse files Browse the repository at this point in the history
  • Loading branch information
qlkzy committed Dec 16, 2013
1 parent c4df364 commit c49616d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
*~
*.sublime-*
.DS_Store
.ackrc

10 changes: 10 additions & 0 deletions colliderwrapper.lua
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
local class = require 'middleclass.middleclass'
local HC = require 'HardonCollider'

require 'events.eraseevent'

ColliderWrapper = class('ColliderWrapper')

-- number of ticks of slack/buffer to allow when erasing an event
-- theoretically, this should not be necessary, but using it guarantees
-- that we will not encounter hard-to-reproduce bugs due to off-by-one
-- errors on the order of milliseconds
ColliderWrapper.static.erasureSlack = 10

function ColliderWrapper:initialize(eventLog)
self.collider = HC(100, function(...) self:onCollide(...) end)
self.hitboxes = {}
Expand All @@ -25,10 +33,12 @@ function ColliderWrapper:onCollide(dt, hitbox1, hitbox2, dx, dy)

if entity1:hit(entity2, dx, dy) then
self.eventLog:insert(DestroyEvent(id1), self.time)
self.eventLog:insert(EraseEvent(id1), self.time + constants.jumpTime + self.class.erasureSlack)
end

if entity2:hit(entity1, -dx, -dy) then
self.eventLog:insert(DestroyEvent(id2), self.time)
self.eventLog:insert(EraseEvent(id2), self.time + constants.jumpTime + self.class.erasureSlack)
end
end

Expand Down
12 changes: 12 additions & 0 deletions events/eraseevent.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
local class = require 'middleclass.middleclass'

require 'events.event'
require 'utils'

EraseEvent = class('events.EraseEvent', EntityEvent)


function EraseEvent:safeApply(state, collider)
collider:remove(state[self.entityId].hitbox)
state[self.entityId] = nil
end

0 comments on commit c49616d

Please sign in to comment.