diff options
| -rw-r--r-- | tilemap.lua | 8 | ||||
| -rw-r--r-- | world.lua | 40 |
2 files changed, 48 insertions, 0 deletions
diff --git a/tilemap.lua b/tilemap.lua index 72a186e..f809598 100644 --- a/tilemap.lua +++ b/tilemap.lua @@ -58,6 +58,7 @@ function Tilemap:new(mapPath, tilesets) self.entitiesSpikes = {} self.entitiesTextTriggers = {} self.entitiesPickups = {} + self.entityDoor = nil self.layerBackground = nil self.layerDecorationBackground = nil self.layerDecorationForeground = nil @@ -146,6 +147,9 @@ function Tilemap:new(mapPath, tilesets) table.insert(self.entitiesLiquidSurfaces, LiquidSurface:new(entity)) elseif name == "pickups" then table.insert(self.entitiesPickups, Pickup.new(entity)) + elseif name == "door" then + entity.isDoor = true + self.entityDoor = entity end end end @@ -175,6 +179,10 @@ function Tilemap:getEntitiesSpikes() return self.entitiesSpikes end +function Tilemap:getEntityDoor() + return self.entityDoor +end + function Tilemap:getEntitiesTextTriggers() return self.entitiesTextTriggers end @@ -29,6 +29,8 @@ function World:new() self.refractionCanvas = nil self.liquidShader = nil self.activeSplashes = {} + self.entityDoor = nil + self.doorOpened = false self.camera = nil self.groundContacts = {} self.waterContacts = {} @@ -168,6 +170,14 @@ function World:load(mapPath, tilesets) table.insert(self.entities, pickup) end + self.entityDoor = self.tilemap:getEntityDoor() + if self.entityDoor then + self.entityDoor:enablePhysics(self.physicsWorld, "static") + self.entityDoor.fixture:setSensor(true) + self.entityDoor.fixture:setUserData(self.entityDoor) + table.insert(self.entities, self.entityDoor) + end + local ok, shader = pcall(love.graphics.newShader, "shaders/liquid.glsl") self.liquidShader = ok and shader or nil if not self.liquidShader then @@ -225,6 +235,32 @@ function World:handlePickup(pickup, player) end end +function World:handleDoor(door, player) + if not (door and player and door.isDoor and self:_isPlayerLike(player)) then return end + if self.doorOpened then return end + + local keyCount = player.pickups.key or 0 + local needed = 3 + + if keyCount >= needed then + self.doorOpened = true + self.playerTextbox:show( + "The door opens! You escaped!", + { player.x + player.width / 2, player.y - 20, "center" }, + "write", + { life = 5, fontSize = 9, centeredText = true, wrapToFit = true } + ) + else + local remaining = needed - keyCount + self.playerTextbox:show( + "Locked. You need " .. remaining .. " more key" .. (remaining > 1 and "s" or "") .. ".", + { player.x + player.width / 2, player.y - 20, "center" }, + "write", + { life = 3, fontSize = 9, centeredText = true, wrapToFit = true } + ) + end +end + function World:_onBeginContact(udA, udB, nx, ny, contact) if type(udA) == "table" and udA.isTextTrigger then self:handleTextTrigger(udA, udB) @@ -234,6 +270,10 @@ function World:_onBeginContact(udA, udB, nx, ny, contact) self:handlePickup(udA, udB) elseif type(udB) == "table" and udB.isPickup then self:handlePickup(udB, udA) + elseif type(udA) == "table" and udA.isDoor and self:_isPlayerLike(udB) then + self:handleDoor(udA, udB) + elseif type(udB) == "table" and udB.isDoor and self:_isPlayerLike(udA) then + self:handleDoor(udB, udA) end if udA == "ground" and self:_isTrackedEntity(udB) then |
