fpv/source/scripts/balebaSprite.lua

75 lines
1.9 KiB
Lua
Raw Normal View History

2024-06-01 16:52:11 +03:00
Baleba = {}
2024-06-02 20:25:27 +03:00
class('Baleba').extends(NobleSprite)
2024-06-01 16:52:11 +03:00
function randomFloat(lower, greater)
2024-06-02 20:25:27 +03:00
return lower + math.random() * (greater - lower);
2024-06-01 16:52:11 +03:00
end
function Baleba:init(x, y, player, loop)
2024-06-02 20:25:27 +03:00
Baleba.super.init(self, "assets/sprites/baleba", true)
2024-06-01 16:52:11 +03:00
-- Animation properties
2024-06-02 20:25:27 +03:00
self.animation:addState("run", 1, 4)
self.animation.run.frameDuration = 2
self.animation:setState("run")
2024-06-01 16:52:11 +03:00
-- Collision properties
2024-06-03 00:31:15 +03:00
self:setZIndex(ZIndex.enemy)
2024-06-01 16:52:11 +03:00
self:setCollideRect(3, 25, 40, 15)
self:setTag(154)
2024-06-02 20:25:27 +03:00
self:setSize(64, 64)
2024-06-03 00:31:15 +03:00
self:setGroups(CollideGroups.enemy)
self:setCollidesWithGroups(
{
CollideGroups.player
})
2024-06-01 16:52:11 +03:00
-- Sprite properties
self.standrate = randomFloat(0.3, 2)
self.xVelocity = Baleba.standrate
self.player = player
self.loop = loop
-- Danger properties
self.danger = Danger(366, y, self)
2024-06-02 20:25:27 +03:00
self:add()
2024-06-01 16:52:11 +03:00
-- Move to initial position
self:moveTo(x, y)
end
function Baleba:update()
-- Speedup when player is moving right
if self.player == nil or self.player.isMovingRight() == false then
self.xVelocity = self.standrate
else
2024-06-12 19:02:03 +03:00
self.xVelocity = 1.2 + self.standrate
2024-06-01 16:52:11 +03:00
end
2024-06-02 20:25:27 +03:00
self:moveTo(self.x - self.xVelocity, self.y)
2024-06-01 16:52:11 +03:00
-- Danger arrow management
if self.x < 430 and self.danger ~= nil then
self.danger:remove()
self.danger = nil
elseif self.danger == nil and self.x > 430 then
self.danger = Danger(366, self.y, self)
end
-- Lifeciycle management
if self.x < 0 and self.loop == true then
self.standrate = randomFloat(0.5, 6)
self:moveTo(math.random(450, 600), math.random(50, 200))
elseif self.x < 0 then
self:destroy()
end
end
function Baleba:destroy()
if self.danger ~= nil then
self.danger:remove()
end
self:remove()
2024-06-02 20:25:27 +03:00
end