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