From 2c24af53829edaf2b6473a438d74adcdac1fdc96 Mon Sep 17 00:00:00 2001 From: Eltrac Date: Sun, 16 Mar 2025 21:31:13 +0800 Subject: refactor: clearer initialization code --- logic/MapGenerator.lua | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) (limited to 'logic/MapGenerator.lua') 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 -- cgit v1.2.3