summaryrefslogtreecommitdiff
path: root/objects
diff options
context:
space:
mode:
authorEltrac <hi@guhub.cn>2025-02-18 20:24:41 +0800
committerEltrac <hi@guhub.cn>2025-02-18 20:24:41 +0800
commit423cfe133ae8a8f9d7a87b2c4d151af3fd7aa7fc (patch)
tree10c4d8eedcc65155b0f234d6d74be6663bcf5bb0 /objects
parent875cc3e70c11e589c548775c3208659f50daf7df (diff)
feat: entering new rooms
- door connects two rooms - detects if player enters a door, if so, then switch room and update player location
Diffstat (limited to 'objects')
-rw-r--r--objects/Direction.lua12
-rw-r--r--objects/Door.lua27
-rw-r--r--objects/room.lua33
3 files changed, 57 insertions, 15 deletions
diff --git a/objects/Direction.lua b/objects/Direction.lua
new file mode 100644
index 0000000..eaf843a
--- /dev/null
+++ b/objects/Direction.lua
@@ -0,0 +1,12 @@
+Direction = {
+ LEFT = 1, RIGHT = 2, TOP = 3, BOTTOM = 4
+}
+
+OppositeDirection = {
+ [Direction.LEFT] = Direction.RIGHT,
+ [Direction.RIGHT] = Direction.LEFT,
+ [Direction.TOP] = Direction.BOTTOM,
+ [Direction.BOTTOM] = Direction.TOP
+}
+
+return Direction, OppositeDirection
diff --git a/objects/Door.lua b/objects/Door.lua
index 0be3b34..d667406 100644
--- a/objects/Door.lua
+++ b/objects/Door.lua
@@ -2,8 +2,8 @@ Door = {}
Door.__index = Door
local function calculateDoorLocation(obj)
- local roomX, roomY = obj.room:getX(), obj.room:getY()
- local roomW, roomH = obj.room:getWidth(), obj.room:getHeight()
+ 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
if loc == Direction.LEFT or loc == Direction.RIGHT then
@@ -19,13 +19,14 @@ local function calculateDoorLocation(obj)
end
end
-function Door:new(location, room)
+function Door:new(location, within, to)
local obj = {
- to = nil,
location = location,
size = config.graphics.doorSize,
thickness = config.graphics.doorThickness,
- room = room,
+
+ within = within,
+ to = to,
x = nil, -- location and size is calculated afterwards
y = nil,
@@ -44,4 +45,20 @@ function Door:setTo(room)
self.to = room
end
+function Door.connect(room1, room2, way)
+ local door1, door2
+ if way == "lr" then
+ door1 = Door:new(Direction.LEFT, room1, room2)
+ door2 = Door:new(Direction.RIGHT, room2, room1)
+ room1:addDoor(door1)
+ room2:addDoor(door2)
+ elseif way == "tb" then
+ door1 = Door:new(Direction.TOP, room1, room2)
+ door2 = Door:new(Direction.BOTTOM, room2, room1)
+ room1:addDoor(door1)
+ room2:addDoor(door2)
+ end
+ return door1, door2
+end
+
return Door
diff --git a/objects/room.lua b/objects/room.lua
index 2fc09e7..63360e1 100644
--- a/objects/room.lua
+++ b/objects/room.lua
@@ -7,23 +7,21 @@ local RoomType = {
LOOT = 3
}
-Direction = {
- LEFT = 1, RIGHT = 2, TOP = 3, BOTTOM = 4
-}
-
-function Room:new()
+function Room:new(type, w, h)
local obj = {
- width = 0.85, height = 0.8,
+ type = type or RoomType.INITIAL,
+ width = w or 0.85,
+ height = h or 0.8,
doors = {},
- borders = {},
enemies = {},
isCleared = false,
- type = RoomType.INITIAL
+
+ -- borders are automatically caculated afterwards
+ borders = {},
}
setmetatable(obj, Room)
- table.insert(obj.doors, Door:new(math.random(1,4), obj))
obj.borders = obj:getBorders()
return obj
@@ -53,7 +51,11 @@ function Room:getBorders()
return { left, right, top, bottom }
end
-function Room:switch(to)
+function Room:addDoor(door)
+ table.insert(self.doors, door)
+end
+
+function Room.switch(to)
if type(to) == "table" then
G.currentRoom = to
elseif type(to) == "number" then
@@ -61,4 +63,15 @@ function Room:switch(to)
end
end
+function Room.generate()
+ local room1 = Room:new()
+ local room2 = Room:new()
+ Door.connect(room1, room2, "lr")
+
+ local room3 = Room:new()
+ Door.connect(room3, room2, "tb")
+
+ return room1
+end
+
return Room