crankani/src/entity.lua

29 lines
742 B
Lua
Raw Normal View History

-- 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)
function Entity:init(img, health)
Entity.super.init(self, img)
self.health = health or 10
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)
self.health = math.max(self.health - amount, 0)
if self.health == 0 then
self:remove()
end
end