54 lines
1.4 KiB
Lua
54 lines
1.4 KiB
Lua
-- Code for bullets!
|
|
import "CoreLibs/object"
|
|
import "CoreLibs/graphics"
|
|
import "CoreLibs/sprites"
|
|
|
|
local gfx <const> = playdate.graphics
|
|
local geom <const> = playdate.geometry
|
|
|
|
class("Bullet").extends(gfx.sprite)
|
|
|
|
-- Bullet's defaults assume a (slow, weak) enemy bullet
|
|
function Bullet:init(size, damage, vector, collisionMask)
|
|
Bullet.super.init(self)
|
|
|
|
self.power = power
|
|
self.damage = damage or 1
|
|
self.vector = vector or geom.vector2D.new(-1, 0)
|
|
local mask = collisionMask or 0x2
|
|
|
|
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.dx, self.y+self.vector.dy))
|
|
local realCollision = false
|
|
|
|
for i=1, #collisions, 1 do
|
|
-- anything the bullet can collide with should be damaged by it, but it has to hit an actual pixel
|
|
local obj = collisions[i].other
|
|
if self:alphaCollision(obj) then
|
|
obj:damage(self.damage)
|
|
realCollision = true
|
|
end
|
|
end
|
|
|
|
if realCollision 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
|