unknown
This commit is contained in:
94
source/scripts/bomber/enemy.lua
Normal file
94
source/scripts/bomber/enemy.lua
Normal file
@@ -0,0 +1,94 @@
|
||||
Enemy = {}
|
||||
class('Enemy').extends(NobleSprite)
|
||||
|
||||
function Enemy:init(x,y)
|
||||
Enemy.super.init(self)
|
||||
self:moveTo(x, y)
|
||||
self:setZIndex(4)
|
||||
self:add(x,y)
|
||||
self.markImage = Graphics.image.new("assets/sprites/enemy"..math.random(1,2)) -- TODO: make it random
|
||||
self.deadImage = Graphics.image.new("assets/sprites/enemy1_3")
|
||||
self.hitSound = playdate.sound.fileplayer.new("assets/audio/hit1")
|
||||
self:setImage(self.markImage)
|
||||
self.removed = false
|
||||
self:setGroups(CollideGroups.enemy)
|
||||
self:setCollidesWithGroups({
|
||||
CollideGroups.granade,
|
||||
CollideGroups.enemy
|
||||
})
|
||||
self:setCollideRect(-6, -6, 46, 46)
|
||||
self:setSize(32, 32)
|
||||
|
||||
self.vx = 0
|
||||
self.vy = 0
|
||||
self.isDying = false
|
||||
self.friction = 0.95
|
||||
end
|
||||
|
||||
function Enemy:update()
|
||||
if not BomberScene.instance then return end
|
||||
local speed = 0
|
||||
|
||||
if self.isDying then
|
||||
self.vx = self.vx * self.friction
|
||||
self.vy = self.vy * self.friction
|
||||
|
||||
self:moveBy(self.vx, self.vy + BomberScene.instance.scrollSpeed)
|
||||
|
||||
if math.abs(self.vx) < 0.1 and math.abs(self.vy) < 0.1 then
|
||||
self.isDying = false
|
||||
self.removed = true
|
||||
end
|
||||
elseif not self.removed then
|
||||
speed = math.random(0, 7)/10
|
||||
self:moveBy(0, BomberScene.instance.scrollSpeed + speed)
|
||||
else
|
||||
self:moveBy(0, BomberScene.instance.scrollSpeed)
|
||||
end
|
||||
|
||||
local actualX, actualY, collisions, numberOfCollisions = self:checkCollisions(self.x, self.y)
|
||||
|
||||
if numberOfCollisions > 0 then
|
||||
for i, collision in ipairs(collisions) do
|
||||
if collision.other:getTag() == 154 and collision.other.currentRadius <= 0.05 and not self.isDying then
|
||||
print("Collision with granade")
|
||||
self:setImage(self.deadImage)
|
||||
self.hitSound:play()
|
||||
self:applyExplosionForce(collision.other.x, collision.other.y)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
if self.y > 240 + 10 then
|
||||
if not self.removed then
|
||||
print("Removing enemy")
|
||||
self:remove()
|
||||
self:superRemove()
|
||||
self.removed = true
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function Enemy:applyExplosionForce(explosionX, explosionY)
|
||||
local dx = self.x - explosionX
|
||||
local dy = self.y - explosionY
|
||||
|
||||
local dist = math.sqrt(dx*dx + dy*dy)
|
||||
if dist == 0 then dist = 0.001 end
|
||||
|
||||
dx = dx / dist
|
||||
dy = dy / dist
|
||||
|
||||
local maxForce = 5
|
||||
local maxRadius = 100
|
||||
|
||||
local force = maxForce * (1 - math.min(dist, maxRadius) / maxRadius)
|
||||
force = math.max(force, 1)
|
||||
|
||||
self.vx = dx * force
|
||||
self.vy = dy * force * 0.5
|
||||
|
||||
self.isDying = true
|
||||
|
||||
self:setRotation(math.random() * 360)
|
||||
end
|
||||
Reference in New Issue
Block a user