crankani/src/bullet.lua

58 lines
1.3 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)
local POWER_SIZE_LOOKUP <const> = {
[1] = 2,
[2] = 4,
[3] = 6,
[4] = 10,
}
local POWER_DAMAGE_LOOKUP <const> = {
[1] = 1,
[2] = 3,
[3] = 5,
[4] = 10,
}
-- Bullet's defaults assume a friendly bullet
function Bullet:init(power, vector, collisionMask)
Bullet.super.init(self)
self.power = power
local size = POWER_SIZE_LOOKUP[power]
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)
self.vector = vector or {x=3, y=0}
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(POWER_DAMAGE_LOOKUP[self.power])
end
if #collisions >= 1 then
self:remove()
end
end