diff options
| author | Eltrac <hi@guhub.cn> | 2025-03-16 21:31:13 +0800 |
|---|---|---|
| committer | Eltrac <hi@guhub.cn> | 2025-03-16 21:31:13 +0800 |
| commit | 2c24af53829edaf2b6473a438d74adcdac1fdc96 (patch) | |
| tree | d6a586dc30eb6ea849320e7abea2979668085eb7 /logic/MapGenerator.lua | |
| parent | 254afa93abe7dfec2f4bc1ee16a56436343c05f6 (diff) | |
refactor: clearer initialization code
Diffstat (limited to 'logic/MapGenerator.lua')
| -rw-r--r-- | logic/MapGenerator.lua | 25 |
1 files changed, 20 insertions, 5 deletions
diff --git a/logic/MapGenerator.lua b/logic/MapGenerator.lua index f95ffed..a8dd658 100644 --- a/logic/MapGenerator.lua +++ b/logic/MapGenerator.lua @@ -1,4 +1,5 @@ local Room = require "objects.room" +local Loot = require "objects.loot" local mapGenerator = {} @@ -28,12 +29,12 @@ function mapGenerator.continue(roomCount, from) -- continue game by extending the map local newMap = mapGenerator.generate(roomCount or config.map.extendedRoomCount, true) local room = from or G.currentRoom - + -- attempt to find an available room local connected = false local attempts = 0 local maxAttempts = 10 - + while not connected and attempts < maxAttempts do -- if doorfull if room:isDoorFull() then @@ -45,7 +46,7 @@ function mapGenerator.continue(roomCount, from) end end end - + -- try to connect local entranceDoor, exitDoor = room:connect(newMap[1]) if entranceDoor then @@ -60,10 +61,10 @@ function mapGenerator.continue(roomCount, from) -- if failed, generate a new map newMap = mapGenerator.generate(roomCount or config.map.extendedRoomCount, true) end - + attempts = attempts + 1 end - + if not connected then print("Warning: Failed to connect new rooms after " .. maxAttempts .. " attempts") return false @@ -77,4 +78,18 @@ function mapGenerator.continue(roomCount, from) return true end +function mapGenerator.init(G) + -- generate map + G.allRooms = mapGenerator.generate(config.map.initialRoomCount, true) + G.currentRoom = G.allRooms[1] + + -- add random initial loot + for i=1, config.map.initialHeartCount do + table.insert(G.currentRoom.objects.loots, Loot:random()) + end + + -- initialize room + G.currentRoom:init() +end + return mapGenerator |
