diff options
| author | Eltrac <hi@guhub.cn> | 2025-03-14 09:06:36 +0800 |
|---|---|---|
| committer | Eltrac <hi@guhub.cn> | 2025-03-14 09:06:36 +0800 |
| commit | 982e54790e747c5ed5137c14006a44be4afcd9df (patch) | |
| tree | d3bd7d9991382439de2352566a95cc21db055c05 /objects/Enemy.lua | |
| parent | 78b207c925554d45de804c8e85efa23bbc32d8d4 (diff) | |
refactor: seperate object placement initializers
Diffstat (limited to 'objects/Enemy.lua')
| -rw-r--r-- | objects/Enemy.lua | 24 |
1 files changed, 21 insertions, 3 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) |
