summaryrefslogtreecommitdiff
path: root/objects
diff options
context:
space:
mode:
authorEltrac <hi@guhub.cn>2025-03-14 09:06:36 +0800
committerEltrac <hi@guhub.cn>2025-03-14 09:06:36 +0800
commit982e54790e747c5ed5137c14006a44be4afcd9df (patch)
treed3bd7d9991382439de2352566a95cc21db055c05 /objects
parent78b207c925554d45de804c8e85efa23bbc32d8d4 (diff)
refactor: seperate object placement initializers
Diffstat (limited to 'objects')
-rw-r--r--objects/Enemy.lua24
-rw-r--r--objects/Obstacle.lua8
-rw-r--r--objects/room.lua33
3 files changed, 38 insertions, 27 deletions
diff --git a/objects/Enemy.lua b/objects/Enemy.lua
index a6e7738..7637082 100644
--- a/objects/Enemy.lua
+++ b/objects/Enemy.lua
@@ -32,6 +32,8 @@ function Enemy:new(name, location)
return obj
end
+-- enemy behaviors
+
function Enemy:move(dt)
-- deal with stunned status
if self.stunned and self.stunned > 0 then
@@ -89,6 +91,8 @@ function Enemy:facing()
return utils.angleToDirection(utils.atan2(dy, dx))
end
+-- enemy life cycle
+
function Enemy:takeDamage(damage)
self.health = self.health - damage
if self:isDead() then
@@ -118,6 +122,14 @@ function Enemy:die()
table.remove(enemies, utils.indexof(enemies, self))
end
+function Enemy:onHit(shot)
+ if enemyData[self.type] and enemyData[self.type].onHit then
+ enemyData[self.type].onHit(shot)
+ end
+end
+
+-- enemy status
+
function Enemy:stun(duration)
self.stunned = duration
end
@@ -126,12 +138,18 @@ function Enemy:getSticky(duration)
self.sticky = duration
end
-function Enemy:onHit(shot)
- if enemyData[self.type] and enemyData[self.type].onHit then
- enemyData[self.type].onHit(shot)
+-- initializers
+
+function Enemy:placeInRoom(room)
+ local offset = self.r*2 + G.player.r*2 + config.summonMargin
+ if not self.x or not self.y then
+ self.x, self.y = room:getLocation(self.presetLocation or "random", offset)
end
+ G.BodyLifeCycleManager:create(self)
end
+-- entry function
+
function Enemy:update(dt)
self:move(dt)
self.frameTimer:update(dt)
diff --git a/objects/Obstacle.lua b/objects/Obstacle.lua
index ef83a5b..2152722 100644
--- a/objects/Obstacle.lua
+++ b/objects/Obstacle.lua
@@ -20,6 +20,14 @@ function Obstacle:new(name, location, body)
return obj
end
+function Obstacle:placeInRoom(room)
+ local offset = self.w + G.player.r*2 + config.summonMargin
+ if not self.x or not self.y then
+ self.x, self.y = room:getLocation(self.presetLocation or "random", offset)
+ end
+ G.BodyLifeCycleManager:create(self, { self.w, self.h }, "static")
+end
+
function Obstacle:update(dt)
-- nothing yet
end
diff --git a/objects/room.lua b/objects/room.lua
index 6d5940e..e060c77 100644
--- a/objects/room.lua
+++ b/objects/room.lua
@@ -185,28 +185,6 @@ end
-- initializers
-function Room:initEnemies()
- for _, enemy in ipairs(self.objects.enemies) do
-
- local offset = enemy.r*2 + G.player.r*2 + config.summonMargin
- if not enemy.x or not enemy.y then
- enemy.x, enemy.y = self:getLocation(enemy.presetLocation or "random", offset)
- end
- G.BodyLifeCycleManager:create(enemy)
- end
-end
-
-function Room:initObstacles()
- for _, obs in ipairs(self.objects.obstacles) do
- local offset = obs.w + G.player.r*2 + config.summonMargin
- if not obs.x or not obs.y then
- obs.x, obs.y = self:getLocation(obs.presetLocation or "random", offset)
- end
-
- G.BodyLifeCycleManager:create(obs, { obs.w, obs.h }, "static")
- end
-end
-
function Room:addWalls()
local x, y, w, h = self:getX(), self:getY(), self:getWidth(), self:getHeight()
local thickness = 2
@@ -232,8 +210,15 @@ function Room:addWalls()
end
function Room:init()
- self:initEnemies()
- self:initObstacles()
+ -- handle objects that need to be placed
+ for _, group in pairs(self.objects) do
+ for _, object in ipairs(group) do
+ if object.placeInRoom then
+ object:placeInRoom(self)
+ end
+ end
+ end
+
self.walls = self:addWalls()
-- play music