72 lines
1.8 KiB
Lua
72 lines
1.8 KiB
Lua
Baleba = {}
|
|
class('Baleba').extends(AnimatedSprite)
|
|
|
|
function randomFloat(lower, greater)
|
|
return lower + math.random() * (greater - lower);
|
|
end
|
|
|
|
local balebaImageTable = Graphics.imagetable.new("assets/sprites/baleba")
|
|
|
|
function Baleba:init(x, y, player, loop)
|
|
Baleba.super.init(self, balebaImageTable)
|
|
|
|
-- Animation properties
|
|
self:addState("run", 1,4, {tickStep = 2})
|
|
self:setDefaultState("run")
|
|
self:playAnimation()
|
|
|
|
-- Collision properties
|
|
self:setZIndex(11)
|
|
self:setCollideRect(3, 25, 40, 15)
|
|
self:setTag(154)
|
|
|
|
-- 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)
|
|
self.danger:add()
|
|
|
|
-- Move to initial position
|
|
self:moveTo(x, y)
|
|
end
|
|
|
|
function Baleba:update()
|
|
self:updateAnimation()
|
|
|
|
-- Speedup when player is moving right
|
|
if self.player == nil or self.player.isMovingRight() == false then
|
|
self.xVelocity = self.standrate
|
|
else
|
|
self.xVelocity = 2 + self.standrate
|
|
end
|
|
|
|
self:moveTo(self.x-self.xVelocity, self.y)
|
|
|
|
-- 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)
|
|
self.danger:add()
|
|
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()
|
|
end |