-- A superclass for all player and AI-controlled units import "CoreLibs/object" import "CoreLibs/graphics" import "CoreLibs/sprites" local gfx = playdate.graphics local geom = playdate.geometry class("Entity").extends(gfx.sprite) function Entity:init(img, health, armor) Entity.super.init(self, img) self.health = health or 10 self.armor = armor or 0 -- movement direction, every update() the entity will move along this vector and return -- collision data to the subclass self.vector = geom.vector2D.new(0, 0) 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 a bit too variable -- (but we should always include 0x2 and handle player collisions) self:setGroupMask(0x4) end function Entity:damage(amount) if amount < self.armor then return end self.health = math.max(self.health - (amount - self.armor), 0) if self.health == 0 then self:delete() end end function Entity:update() local collisions = select(3, self:moveWithCollisions(self.x + self.vector.dx, self.y + self.vector.dy)) return collisions end -- override this if you create timers function Entity:delete() self:remove() end