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
|
|
|
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,
|
|
|
|
}
|
|
|
|
|
2023-09-30 02:30:01 +00:00
|
|
|
function Bullet:init(power, friendly)
|
|
|
|
Bullet.super.init(self)
|
|
|
|
self.power = power
|
2023-09-30 19:19:04 +00:00
|
|
|
local size = POWER_SIZE_LOOKUP[power]
|
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
|
|
|
|
|
|
|
local friendly = friendly or true
|
|
|
|
if friendly then
|
|
|
|
self:setGroupMask(0x4)
|
2023-09-30 02:30:01 +00:00
|
|
|
self:setCollidesWithGroupsMask(0x8)
|
2023-09-30 00:52:06 +00:00
|
|
|
self.direction = 1
|
|
|
|
else
|
|
|
|
self:setGroupMask(0x10)
|
|
|
|
self:setCollidesWithGroupsMask(0x2)
|
|
|
|
self.direction = -1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function Bullet:update()
|
2023-09-30 19:19:04 +00:00
|
|
|
local collisions = select(3, self:moveWithCollisions(self.x+self.direction, self.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
|
2023-09-30 00:52:06 +00:00
|
|
|
end
|