summaryrefslogtreecommitdiff
path: root/objects/door.lua 20-22-03-209.lua
diff options
context:
space:
mode:
authorEltrac <hi@guhub.cn>2025-07-20 14:47:04 +0800
committerEltrac <hi@guhub.cn>2025-07-20 14:47:04 +0800
commite1eb0472e10644105dd31ed364a6bafac273bc75 (patch)
tree9795e8475b53fe35fd077f5e6ab1757f702885b9 /objects/door.lua 20-22-03-209.lua
parent8fe17e64276891fcabb085a88551795f6632097d (diff)
dump: changes from a long time ago which I cannot recall
Diffstat (limited to 'objects/door.lua 20-22-03-209.lua')
-rw-r--r--objects/door.lua 20-22-03-209.lua69
1 files changed, 69 insertions, 0 deletions
diff --git a/objects/door.lua 20-22-03-209.lua b/objects/door.lua 20-22-03-209.lua
new file mode 100644
index 0000000..6df6c26
--- /dev/null
+++ b/objects/door.lua 20-22-03-209.lua
@@ -0,0 +1,69 @@
+require "data.directions"
+local physics = require "logic.physics"
+
+local Door = {}
+Door.__index = Door
+
+local function calculateDoorLocation(obj)
+ local roomX, roomY = obj.within:getX(), obj.within:getY()
+ local roomW, roomH = obj.within:getWidth(), obj.within:getHeight()
+ local loc, size, thickness = obj.location, obj.size, obj.thickness
+
+ local positions = {
+ [Direction.LEFT] = { x = roomX, y = (love.graphics.getHeight() - size) / 2, w = thickness, h = size },
+ [Direction.RIGHT] = { x = roomX + roomW, y = (love.graphics.getHeight() - size) / 2, w = thickness, h = size },
+ [Direction.TOP] = { x = (love.graphics.getWidth() - thickness) / 2, y = roomY, w = size, h = thickness },
+ [Direction.BOTTOM] = { x = (love.graphics.getWidth() - thickness) / 2, y = roomY + roomH, w = size, h = thickness }
+ }
+
+ local pos = positions[loc]
+ obj.x, obj.y, obj.w, obj.h = pos.x, pos.y, pos.w, pos.h
+end
+
+function Door:new(location, within, to)
+ local obj = {
+ location = location,
+ size = config.graphics.doorSize,
+ thickness = config.graphics.doorThickness,
+
+ within = within,
+ to = to,
+ }
+
+ setmetatable(obj, Door)
+
+ calculateDoorLocation(obj)
+ G.BodyLifeCycleManager:create(obj, { obj.w, obj.h }, "static")
+
+ return obj
+end
+
+function Door:setTo(room)
+ self.to = room
+end
+
+function Door:onContact(other)
+ local map = require "logic.map"
+ if other.objectType == "player" then
+ print "player in contact with door"
+ if not G.currentRoom.isCleared then return end
+
+ map.switchRoom(self.to)
+
+ -- update player position after entering a new room
+ local doorH, doorW, playerR = self.h, self.w, G.player.physicsShape:getRadius()
+ local roomX, roomY, roomW, roomH = G.currentRoom:getX(), G.currentRoom:getY(), G.currentRoom:getWidth(), G.currentRoom:getHeight()
+
+ local positions = {
+ [Direction.LEFT] = { "x", roomX + roomW - playerR - doorW },
+ [Direction.RIGHT] = { "x", roomX + playerR + doorW },
+ [Direction.TOP] = { "y", roomY + roomH - playerR - doorH },
+ [Direction.BOTTOM] = { "y", roomY + playerR + doorH }
+ }
+
+ local axis, pos = positions[self.location][1], positions[self.location][2]
+ G.BodyLifeCycleManager:setPosition(G.player, axis, pos)
+ end
+end
+
+return Door