diff options
| author | Eltrac <hi@guhub.cn> | 2025-02-25 12:09:36 +0800 |
|---|---|---|
| committer | Eltrac <hi@guhub.cn> | 2025-02-25 12:09:36 +0800 |
| commit | 959a710f96176422733f876a678f5183384107b2 (patch) | |
| tree | 59d7125b2de8b27dc70b54ee3b0babc10ea0ce21 /objects/Enemy.lua | |
| parent | b810b9eeeb7a322989e963edad3a5377504a33b3 (diff) | |
feat: implement damage system
now enemies won't killed just by any bullets
Diffstat (limited to 'objects/Enemy.lua')
| -rw-r--r-- | objects/Enemy.lua | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/objects/Enemy.lua b/objects/Enemy.lua index 513267f..a17129e 100644 --- a/objects/Enemy.lua +++ b/objects/Enemy.lua @@ -9,6 +9,7 @@ function Enemy:new(name, location) y = (type(location) == "table" and location.y) or nil, body = Body:new("circle", enemyData[name] and enemyData[name].size or nil), speed = enemyData[name] and enemyData[name].speed or nil, + health = enemyData[name] and enemyData[name].health or nil, stunned = false, } @@ -50,6 +51,28 @@ function Enemy:moveTowardPlayer(dt) self.y = self.y + dy * dv end +function Enemy:getsAttacked(damage) + self.health = self.health - damage + if self:isDead() then + self:die() + end +end + +function Enemy:isDead() + return self.health <= 0 +end + +function Enemy:die() + -- drop loots + for i, item in ipairs(enemyData[self.type].drops) do + local temp = math.random(10) / 10 + if temp <= item.chances then + local offset = (i - 1) * 5 + loot.drop(self.x + offset, self.y + offset, item.type) + end + end +end + function Enemy:stun(duration) self.stunned = duration end |
