summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEltrac <hi@guhub.cn>2025-03-14 08:54:33 +0800
committerEltrac <hi@guhub.cn>2025-03-14 08:54:33 +0800
commit78b207c925554d45de804c8e85efa23bbc32d8d4 (patch)
tree0b817864c7379843fbadac46ba399183851879c4
parent0b61a40f7b81327f3266db3f8c198b83b7b72b10 (diff)
refactor: separate map generation logic
-rw-r--r--config.lua4
-rw-r--r--logic/MapGenerator.lua57
-rw-r--r--logic/map.lua61
3 files changed, 63 insertions, 59 deletions
diff --git a/config.lua b/config.lua
index e6aa398..4ede536 100644
--- a/config.lua
+++ b/config.lua
@@ -51,7 +51,7 @@ function initGame()
-- lazy load dependencies
local Player = require "objects.player"
- local map = require "logic.map"
+ local mapGen = require "logic.MapGenerator"
local bodyLifeCycleManager = require "utils.bodyLifecycleManager"
local audio = require "utils.audio"
@@ -72,7 +72,7 @@ function initGame()
G.player = Player:new()
- G.allRooms = map.generate(config.initialMapSize, true)
+ G.allRooms = mapGen.generate(config.initialMapSize, true)
G.currentRoom = G.allRooms[1]
G.currentRoom:init()
diff --git a/logic/MapGenerator.lua b/logic/MapGenerator.lua
new file mode 100644
index 0000000..f1f45df
--- /dev/null
+++ b/logic/MapGenerator.lua
@@ -0,0 +1,57 @@
+local Room = require "objects.room"
+
+local mapGenerator = {}
+
+function mapGenerator.generate(roomCount, initial)
+ local firstRoom = Room:new(initial and "initialRoom" or nil)
+ local map = { firstRoom }
+ local available = utils.copy(map)
+
+ for i = 2, (roomCount or config.initialMapSize) do
+ -- connect the room with a new room
+ local newRoom = Room:new()
+ local prevRoom = utils.any(available)
+ prevRoom:connect(newRoom)
+
+ if prevRoom:isDoorFull() then
+ table.remove(available, utils.indexof(available, prevRoom))
+ end
+
+ table.insert(map, newRoom)
+ table.insert(available, newRoom)
+ end
+
+ return map
+end
+
+function mapGenerator.continue(roomCount, from)
+ -- continue game by extending the map
+ local newMap = mapGenerator.generate(roomCount or config.extendedMapSize, true)
+ local room = from or G.currentRoom
+
+ -- try extending from the last room
+ local entranceDoor, exitDoor = room:connect(newMap[1])
+ if not entranceDoor or not exitDoor then
+ -- if not doorless direction available in this room
+ -- try extend from a random Room
+ repeat
+ room = utils.any(G.allRooms)
+ if room ~= from and not room:isDoorFull() then
+ print("extended from a random room")
+ entranceDoor, exitDoor = room:connect(newMap[1])
+ break
+ end
+ until false
+ end
+ if exitDoor then
+ newMap[1]:removeDoor(exitDoor.location)
+ end
+
+ for _, newRoom in ipairs(newMap) do
+ table.insert(G.allRooms, newRoom)
+ end
+
+ G.mapExpanded = true
+end
+
+return mapGenerator
diff --git a/logic/map.lua b/logic/map.lua
index 06f4c59..b955857 100644
--- a/logic/map.lua
+++ b/logic/map.lua
@@ -1,5 +1,5 @@
require "data.directions"
-local Room = require "objects.room"
+local mapGen = require "logic.MapGenerator"
local roomType = require "data.roomType"
local translator = require "i18n.translator"
local audio = require "utils.audio"
@@ -21,60 +21,7 @@ function map.switchRoom(destination)
end
end
-function map.generate(roomCount, initial)
- local firstRoom = Room:new(initial and "initialRoom" or nil)
- local map = { firstRoom }
- local available = utils.copy(map)
-
- for i = 2, (roomCount or config.initialMapSize) do
- -- connect the room with a new room
- local newRoom = Room:new()
- local prevRoom = utils.any(available)
- prevRoom:connect(newRoom)
-
- if prevRoom:isDoorFull() then
- table.remove(available, utils.indexof(available, prevRoom))
- end
-
- table.insert(map, newRoom)
- table.insert(available, newRoom)
- end
-
- return map
-end
-
-function map.continue(roomCount, from)
- -- continue game by extending the map
- local newMap = map.generate(roomCount or config.extendedMapSize, true)
- local room = from or G.currentRoom
-
- -- try extending from the last room
- local entranceDoor, exitDoor = room:connect(newMap[1])
- if not entranceDoor or not exitDoor then
- -- if not doorless direction available in this room
- -- try extend from a random Room
- repeat
- room = utils.any(G.allRooms)
- if room ~= from and not room:isDoorFull() then
- print("extended from a random room")
- entranceDoor, exitDoor = room:connect(newMap[1])
- break
- end
- until false
- end
- if exitDoor then
- newMap[1]:removeDoor(exitDoor.location)
- end
-
- for _, newRoom in ipairs(newMap) do
- table.insert(G.allRooms, newRoom)
- end
-
- G.mapExpanded = true
- speaker.speak(translator.T("newDoor"))
-end
-
--- boolean functions
+-- handle room clearance
function map.allCleared()
local allCleared = true
@@ -110,8 +57,8 @@ function map.update()
-- extend map if all rooms are cleared
if map.allCleared() then
- print "all cleared and try to continue"
- map.continue()
+ mapGen.continue()
+ speaker.speak(translator.T("newDoor"))
end
-- notify user if one's entered a new map