diff options
| author | Eltrac <hi@guhub.cn> | 2025-03-13 00:29:22 +0800 |
|---|---|---|
| committer | Eltrac <hi@guhub.cn> | 2025-03-13 00:29:22 +0800 |
| commit | a46017a963e2f14894463ae5525429cc7612c99c (patch) | |
| tree | 8717758879ab62557f40b475f501d065c8c6a891 /objects | |
| parent | 8feb250231335a90d358f0ebdd53c4f29df60831 (diff) | |
refactor: move room switch trigger to door contact
due to the requirement for adding doors and door bodies before initializing body lifecycle manager, move lifecycle manager to game initialization and create more bodies with this manager
Diffstat (limited to 'objects')
| -rw-r--r-- | objects/Door.lua | 48 | ||||
| -rw-r--r-- | objects/Player.lua | 44 | ||||
| -rw-r--r-- | objects/room.lua | 10 |
3 files changed, 60 insertions, 42 deletions
diff --git a/objects/Door.lua b/objects/Door.lua index 81c7b4c..089a589 100644 --- a/objects/Door.lua +++ b/objects/Door.lua @@ -1,5 +1,5 @@ -local Body = require "objects.Body" require "data.directions" +local physics = require "logic.physics" local Door = {} Door.__index = Door @@ -9,15 +9,15 @@ local function calculateDoorLocation(obj) local roomW, roomH = obj.within:getWidth(), obj.within:getHeight() local loc, size, thickness = obj.location, obj.size, obj.thickness - if loc == Direction.LEFT or loc == Direction.RIGHT then - obj.x = (loc == Direction.LEFT) and (roomX - thickness / 2) or (roomX + roomW - thickness / 2) - obj.y = (love.graphics.getHeight() - size) / 2 - obj.body = Body:new("rectangle", thickness, size) - elseif loc == Direction.TOP or loc == Direction.BOTTOM then - obj.x = (love.graphics.getWidth() - size) / 2 - obj.y = (loc == Direction.TOP) and (roomY - thickness / 2) or (roomY + roomH - thickness / 2) - obj.body = Body:new("rectangle", size, thickness) - end + 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) @@ -28,15 +28,12 @@ function Door:new(location, within, to) within = within, to = to, - - x = nil, -- location and size is calculated afterwards - y = nil, - body = nil, } setmetatable(obj, Door) calculateDoorLocation(obj) + G.BodyLifeCycleManager:create(obj, { obj.w, obj.h }, "static") return obj end @@ -45,4 +42,27 @@ function Door:setTo(room) self.to = room end +function Door:onContact(other) + local map = require "logic.map" + if other.objectType == "player" then + 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 diff --git a/objects/Player.lua b/objects/Player.lua index e53192d..51de42d 100644 --- a/objects/Player.lua +++ b/objects/Player.lua @@ -119,28 +119,28 @@ function Player:update(dt) self.frameTimer:update(dt) -- player enters door - for _, door in pairs(G.currentRoom.doors) do - local entersDoor = door.body:collide(self.body, door.x, door.y, self.x, self.y) and - G.currentRoom.isCleared - if door ~= false and entersDoor then - map.switchRoom(door.to) - - -- update player position after entering a new room - local doorH, doorW, playerR = door.body.h, door.body.w, self.body.r - local roomX, roomY, roomW, roomH = G.currentRoom:getX(), G.currentRoom:getY(), G.currentRoom:getWidth(), G.currentRoom:getHeight() - if door.location == Direction.LEFT then - self:setX(roomX + roomW - playerR - doorW) - elseif door.location == Direction.RIGHT then - self:setX(roomX + playerR + doorW) - elseif door.location == Direction.TOP then - self:setY(roomY + roomH - playerR - doorH) - elseif door.location == Direction.BOTTOM then - self:setY(roomY + playerR + doorH) - end - - break - end - end + -- for _, door in pairs(G.currentRoom.doors) do + -- local entersDoor = door.body:collide(self.body, door.x, door.y, self.x, self.y) and + -- G.currentRoom.isCleared + -- if door ~= false and entersDoor then + -- map.switchRoom(door.to) + + -- -- update player position after entering a new room + -- local doorH, doorW, playerR = door.body.h, door.body.w, self.body.r + -- local roomX, roomY, roomW, roomH = G.currentRoom:getX(), G.currentRoom:getY(), G.currentRoom:getWidth(), G.currentRoom:getHeight() + -- if door.location == Direction.LEFT then + -- self:setX(roomX + roomW - playerR - doorW) + -- elseif door.location == Direction.RIGHT then + -- self:setX(roomX + playerR + doorW) + -- elseif door.location == Direction.TOP then + -- self:setY(roomY + roomH - playerR - doorH) + -- elseif door.location == Direction.BOTTOM then + -- self:setY(roomY + playerR + doorH) + -- end + + -- break + -- end + -- end end return Player diff --git a/objects/room.lua b/objects/room.lua index 3415660..3894fef 100644 --- a/objects/room.lua +++ b/objects/room.lua @@ -195,7 +195,7 @@ function Room:initEnemies() if not enemy.x or not enemy.y then enemy.x, enemy.y = self:getLocation(enemy.presetLocation or "random", offset) end - physics.bodifyObject(G.world, enemy) + G.BodyLifeCycleManager:create(enemy) end end @@ -206,7 +206,7 @@ function Room:initObstacles() obs.x, obs.y = self:getLocation(obs.presetLocation or "random", offset) end - physics.bodifyObject(G.world, obs, { obs.body.w, obs.body.h }, "static") + G.BodyLifeCycleManager:create(obs, { obs.body.w, obs.body.h }, "static") end end @@ -225,11 +225,9 @@ function Room:addWalls() for _, b in ipairs(borders) do local wall = { objectType = "wall", - body = love.physics.newBody(G.world, b.x, b.y, "static"), - shape = love.physics.newRectangleShape(b.w, b.h), - fixture = nil + x = b.x, y = b.y } - wall.fixture = love.physics.newFixture(wall.body, wall.shape) + G.BodyLifeCycleManager:create(wall, { b.w, b.h }, "static") table.insert(walls, wall) end |
