crankani/src/bullet.lua

49 lines
1.2 KiB
Lua
Raw Normal View History

-- Code for bullets!
import "CoreLibs/object"
import "CoreLibs/graphics"
import "CoreLibs/sprites"
local gfx <const> = 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)
2023-09-30 02:07:02 +00:00
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
2023-09-30 22:23:42 +00:00
if #collisions >= 1 or self:outOfBounds() then
self:remove()
end
end
2023-09-30 22:23:42 +00:00
function Bullet:outOfBounds()
return self.x < 0 or self.y < 0 or self.x > 400 or self.y > 240
end