diff options
| author | Eltrac <hi@guhub.cn> | 2025-02-19 11:00:03 +0800 |
|---|---|---|
| committer | Eltrac <hi@guhub.cn> | 2025-02-19 11:00:03 +0800 |
| commit | 4e889cae1848a0e589f4fe94e83fbd91c1f9fcc2 (patch) | |
| tree | 75f0e7f445ebd85736d9914acd446bb38745c5c7 /logic/map.lua | |
| parent | 195dc2b37eb3c9dd6686012d405b44384ce6d4cd (diff) | |
refactor: separate logic and object
Diffstat (limited to 'logic/map.lua')
| -rw-r--r-- | logic/map.lua | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/logic/map.lua b/logic/map.lua new file mode 100644 index 0000000..4a6b766 --- /dev/null +++ b/logic/map.lua @@ -0,0 +1,66 @@ +local map = {} + +function map.switchRoom(to) + local destination + if type(to) == "table" then + destination = to + elseif type(to) == "number" then + destination = G.currentRoom.doors[Direction[to]] + end + + G.currentRoom = destination + G.enemies = destination.enemies +end + +function map.connectRoom(room1, room2, way) + local door1, door2 + + if type(way) == "number" then + local ways = {"lr","rl","tb","bt"} + way = ways[way] + end + + 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 == "rl" then + door1 = Door:new(Direction.RIGHT, room1, room2) + door2 = Door:new(Direction.LEFT, 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) + elseif way == "bt" then + door1 = Door:new(Direction.BOTTOM, room1, room2) + door2 = Door:new(Direction.TOP, room2, room1) + room1:addDoor(door1) + room2:addDoor(door2) + end + return door1, door2 +end + +function map.generate(roomCount) + local rooms = {} + local room1 = Room:new() + table.insert(rooms, room1) + + for i = 2, roomCount do + local newRoom = Room:new() + local previousRoom = rooms[math.random(#rooms)] + + newRoom.enemies = enemies.generate(math.random(3,6), newRoom) + + map.connectRoom(previousRoom, newRoom, math.random(4)) + + table.insert(rooms, newRoom) + end + + return rooms[1] +end + +return map |
