summaryrefslogtreecommitdiff
path: root/world.lua
diff options
context:
space:
mode:
Diffstat (limited to 'world.lua')
-rw-r--r--world.lua22
1 files changed, 22 insertions, 0 deletions
diff --git a/world.lua b/world.lua
index 2aff628..861a7da 100644
--- a/world.lua
+++ b/world.lua
@@ -33,6 +33,7 @@ function World:new()
self.contactCounts = {}
self.contactEntity = {}
self.contactKind = {}
+ self.pendingPlayerDeath = nil
self.playerTextbox = Textbox:new()
return self
end
@@ -193,6 +194,10 @@ function World:_isPlayerLike(ud)
return type(ud) == "table" and ud.jump ~= nil
end
+function World:_isHazard(ud)
+ return type(ud) == "table" and (ud.isSpike or ud.isEnemy)
+end
+
function World:_onBeginContact(udA, udB, nx, ny, contact)
if udA == "ground" and self:_isTrackedEntity(udB) then
local kind = self:_normalToContactType(nx, ny)
@@ -239,6 +244,18 @@ function World:_onBeginContact(udA, udB, nx, ny, contact)
self.waterContacts[udA] = (self.waterContacts[udA] or 0) + 1
doWaterSplash(udB, udA)
end
+
+ local playerEntity, hazardNx, hazardNy
+ if self:_isHazard(udA) and self:_isPlayerLike(udB) then
+ playerEntity = udB
+ hazardNx, hazardNy = nx, ny
+ elseif self:_isHazard(udB) and self:_isPlayerLike(udA) then
+ playerEntity = udA
+ hazardNx, hazardNy = -nx, -ny
+ end
+ if playerEntity and not playerEntity.isDead then
+ self.pendingPlayerDeath = { nx = hazardNx, ny = hazardNy }
+ end
end
function World:_onEndContact(udA, udB, contact)
@@ -523,6 +540,11 @@ function World:update(dt)
if not self.physicsWorld then return end
self.physicsWorld:update(PHYSICS_DT)
+ if self.pendingPlayerDeath and self.player then
+ self.player:die(self.pendingPlayerDeath.nx, self.pendingPlayerDeath.ny)
+ self.pendingPlayerDeath = nil
+ end
+
for _, e in ipairs(self.entities) do
if e.body then
e:syncFromPhysicsBody()