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 | |
| parent | 254afa93abe7dfec2f4bc1ee16a56436343c05f6 (diff) | |
refactor: clearer initialization code
| -rw-r--r-- | config.lua | 25 | ||||
| -rw-r--r-- | logic/MapGenerator.lua | 25 | ||||
| -rw-r--r-- | logic/physics.lua | 6 | ||||
| -rw-r--r-- | objects/Loot.lua | 7 | ||||
| -rw-r--r-- | objects/Player.lua | 6 | ||||
| -rw-r--r-- | utils/bodyLifecycleManager.lua | 4 |
6 files changed, 50 insertions, 23 deletions
@@ -53,6 +53,7 @@ config = { map = { initialRoomCount = 7, extendedRoomCount = 5, + initialHeartCount = 3, }, defaultMusic = "briskFight" @@ -68,9 +69,7 @@ function initGame() local mapGen = require "logic.MapGenerator" local bodyLifeCycleManager = require "utils.bodyLifecycleManager" local audio = require "utils.audio" - - audio.stopMusic() - speaker.init() + local physics = require "logic.physics" -- initialize game state G = { @@ -79,17 +78,11 @@ function initGame() roomCleared = 0, } - love.physics.setMeter(64) - G.world = love.physics.newWorld(0, 0, true) - - -- body life cycle manager - G.BodyLifeCycleManager = bodyLifeCycleManager.new() - - G.player = Player:new() - - G.allRooms = mapGen.generate(config.map.initialRoomCount, true) - G.currentRoom = G.allRooms[1] - G.currentRoom:init() - - G.world:setCallbacks(onContact) + -- initialize other components + audio.stopMusic() + speaker.init() + physics.init(G) + bodyLifeCycleManager.init(G) + Player.init(G) + mapGen.init(G) end 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 diff --git a/logic/physics.lua b/logic/physics.lua index ef27713..feb69a4 100644 --- a/logic/physics.lua +++ b/logic/physics.lua @@ -1,5 +1,11 @@ local physics = {} +function physics.init(G) + love.physics.setMeter(64) + G.world = love.physics.newWorld(0, 0, true) + G.world:setCallbacks(onContact) +end + function physics.bodifyObject(world, object, shape, bodyType) object.physicsBody = love.physics.newBody(world, object.x, object.y, bodyType or "dynamic") diff --git a/objects/Loot.lua b/objects/Loot.lua index 1e39211..e0817cc 100644 --- a/objects/Loot.lua +++ b/objects/Loot.lua @@ -1,4 +1,4 @@ -local physics = require "logic.physics" +local heartTypes = require "data.heartTypes" local Loot = {} Loot.__index = Loot @@ -18,6 +18,11 @@ function Loot:new(name, x, y) return obj end +function Loot:random(x, y) + local type = utils.any(heartTypes) + return Loot:new(type, x, y) +end + function Loot:onContact(other) local loots = G.currentRoom.objects.loots if other.objectType == "player" then diff --git a/objects/Player.lua b/objects/Player.lua index 5103b17..2701f53 100644 --- a/objects/Player.lua +++ b/objects/Player.lua @@ -16,7 +16,7 @@ function Player.new() zoom = 1/16, speed = config.player.speed, shieldedTill = 0, - hearts = { "purpleheart", "redheart", "redheart", "redheart", "shinyheart", "shinyheart", "stickyheart" }, + hearts = { "redheart", "redheart", "redheart" }, facing = Direction.DOWN, moving = false, frameTimer = FrameTimer:new(config.frameRate, 2), @@ -29,6 +29,10 @@ function Player.new() return player end +function Player.init(G) + G.player = Player:new() +end + -- placement and location function Player:setX(x) diff --git a/utils/bodyLifecycleManager.lua b/utils/bodyLifecycleManager.lua index 219537c..6afbcb1 100644 --- a/utils/bodyLifecycleManager.lua +++ b/utils/bodyLifecycleManager.lua @@ -10,6 +10,10 @@ function bodyLifeCycleManager.new() return self end +function bodyLifeCycleManager.init(G) + G.BodyLifeCycleManager = bodyLifeCycleManager.new() +end + function bodyLifeCycleManager:create(object, shape, bodyType, sensor) table.insert( self.toCreate, |
