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
|
2023-10-01 05:48:06 +00:00
|
|
|
local geom <const> = playdate.geometry
|
2023-09-30 00:52:06 +00:00
|
|
|
|
|
|
|
class("Bullet").extends(gfx.sprite)
|
|
|
|
|
2023-10-01 05:48:06 +00:00
|
|
|
-- Bullet's defaults assume a (slow, weak) enemy 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
|
2023-10-01 05:48:06 +00:00
|
|
|
self.vector = vector or geom.vector2D.new(-1, 0)
|
|
|
|
local mask = collisionMask or 0x2
|
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-10-01 05:48:06 +00:00
|
|
|
local collisions = select(3, self:moveWithCollisions(self.x+self.vector.dx, self.y+self.vector.dy))
|
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
|
|
|
|
|
2023-09-30 22:23:42 +00:00
|
|
|
if #collisions >= 1 or self:outOfBounds() then
|
2023-09-30 19:19:04 +00:00
|
|
|
self:remove()
|
|
|
|
end
|
2023-09-30 00:52:06 +00:00
|
|
|
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
|