-- Code for bullets! import "CoreLibs/object" import "CoreLibs/graphics" import "CoreLibs/sprites" local gfx = playdate.graphics class("Bullet").extends(gfx.sprite) -- Bullet's defaults assume a friendly bullet function Bullet:init(size, damage, vector, collisionMask) Bullet.super.init(self) self.power = power self.damage = damage or 1 self.vector = vector or {x=5, y=0} local mask = collisionMask or 0x4 local img = gfx.image.new(size, size) gfx.pushContext(img) gfx.fillCircleInRect(0, 0, size, size) gfx.popContext() self:setImage(img) self:setCollideRect(0, 0, self:getSize()) self.collisionResponse = gfx.sprite.kCollisionTypeOverlap self:setGroupMask(0x10) self:setCollidesWithGroupsMask(mask) end function Bullet:update() local collisions = select(3, self:moveWithCollisions(self.x+self.vector.x, self.y+self.vector.y)) for i=1, #collisions, 1 do -- anything the bullet can collide with should be damaged by it local obj = collisions[i].other obj:damage(self.damage) end if #collisions >= 1 or self:outOfBounds() then self:remove() end end function Bullet:outOfBounds() return self.x < 0 or self.y < 0 or self.x > 400 or self.y > 240 end