local pd <const> = playdate
local gfx <const> = pd.graphics

local enemyRoombaImagetable <const> = gfx.imagetable.new("Images/enemyRoomba")

class('EnemyRoomba').extends(AnimatedSprite)

function EnemyRoomba:init(x, y)
   
    self.goingLeft = leftDirection
    self.speed = 2
    self.notDestroyed = true
    
    -- animations
    EnemyRoomba.super.init(self, enemyRoombaImagetable)
    self:addState("spawning", 1, 16, {tickStep = 1, nextAnimation = "idle"}).asDefault()
    self:addState("idle", 16, 16, {tickStep = 4})
    self:playAnimation()
    --self:printAllStates()
    --print(self.currentState)
    -- placement
    self:moveTo(x, y)
    self:setZIndex(-10000)
    self:setCollideRect( 0, 0, self:getSize() )
    self:setGroups(1)
    pd.timer.performAfterDelay(3000, function () self:shot() end)

end

function EnemyRoomba:update()
    
    if  self.currentState=="idle" then
        if self.goingLeft == true and self.x>25 then self:moveBy(-self.speed,0)
        elseif self.goingLeft == true and self.x<=25 then self.goingLeft = false
        elseif self.goingLeft == false and self.x<375 then self:moveBy(self.speed,0)
        elseif self.goingLeft == false and self.x>=375 then self.goingLeft = true
        end
    end    
    self:updateAnimation()
end

function EnemyRoomba:shot()
    if self.notDestroyed == true then 
        if self.goingLeft == true then EnemyLaserBeam((self.x),(self.y+self.height)/2+10,player.x,player.y)
        else  EnemyLaserBeam((self.x+self.width/2),(self.y+self.height)/2+10,player.x,player.y)
        end
        pd.timer.performAfterDelay(5000, function () self:shot() end)    
    end    
end

-- colidedObject:isa(EnemyRoomba) or colidedObject:isa(ObstacleTree) or colidedObject:isa(EnemyHandVacuum)