2023-09-30 19:19:04 +00:00
|
|
|
-- A superclass for all player and AI-controlled units
|
|
|
|
import "CoreLibs/object"
|
|
|
|
import "CoreLibs/graphics"
|
|
|
|
import "CoreLibs/sprites"
|
|
|
|
|
|
|
|
local gfx <const> = playdate.graphics
|
|
|
|
|
|
|
|
class("Entity").extends(gfx.sprite)
|
|
|
|
|
2023-09-30 19:34:29 +00:00
|
|
|
function Entity:init(img, health, armor)
|
2023-09-30 19:19:04 +00:00
|
|
|
Entity.super.init(self, img)
|
|
|
|
self.health = health or 10
|
2023-09-30 19:34:29 +00:00
|
|
|
self.armor = armor or 0
|
2023-09-30 19:19:04 +00:00
|
|
|
|
|
|
|
self:setCollideRect(0, 0, self:getSize())
|
|
|
|
|
|
|
|
-- most entities will be enemies, so we configure this mask by default
|
|
|
|
-- We don't set a collider mask because collision is always handled by
|
|
|
|
-- other objects (todo: consider player staying perfectly still)
|
|
|
|
self:setGroupMask(0x8)
|
|
|
|
end
|
|
|
|
|
|
|
|
function Entity:damage(amount)
|
2023-09-30 19:34:29 +00:00
|
|
|
if amount < self.armor then return end
|
|
|
|
|
|
|
|
self.health = math.max(self.health - (amount - self.armor), 0)
|
2023-09-30 19:19:04 +00:00
|
|
|
|
|
|
|
if self.health == 0 then
|
|
|
|
self:remove()
|
|
|
|
end
|
|
|
|
end
|