MovableCrosshair = {}
class('MovableCrosshair').extends(playdate.graphics.sprite)

function MovableCrosshair:init()
    MovableCrosshair.super.init(self)

    self.lineLength = 10
    self.gapSize = 3
    
    self.baseX = 200
    self.baseY = 150
    self.moveRadius = 2
    self.moveSpeed = 2.3
    self.time = 0

    local totalSize = (self.lineLength + self.gapSize) * 2 + 10
    self:setSize(totalSize, totalSize)
    
    self.drawOffsetX = totalSize / 2
    self.drawOffsetY = totalSize / 2
    
    self:add(self.baseX, self.baseY)
    self:setCenter(0.5, 0.5)
    self:markDirty()
    self:setZIndex(11)
end

function MovableCrosshair:update()
    MovableCrosshair.super.update(self)
    self.time = self.time + playdate.display.getRefreshRate() / 1000

    local offsetX = math.sin(self.time) * self.moveRadius
    local offsetY = math.cos(self.time * 1.3) * self.moveRadius
    
    self:moveTo(self.baseX + offsetX, self.baseY + offsetY)
    self:markDirty()
end

function MovableCrosshair:draw()
    local centerX = self.drawOffsetX
    local centerY = self.drawOffsetY    

    playdate.graphics.drawLine(
        centerX - self.lineLength - self.gapSize, centerY,
        centerX - self.gapSize, centerY
    )
    
    playdate.graphics.drawLine(
        centerX + self.gapSize, centerY,
        centerX + self.lineLength + self.gapSize, centerY
    )
    
    playdate.graphics.drawLine(
        centerX, centerY - self.lineLength - self.gapSize,
        centerX, centerY - self.gapSize
    )
    
    playdate.graphics.drawLine(
        centerX, centerY + self.gapSize,
        centerX, centerY + self.lineLength + self.gapSize
    )
end

function MovableCrosshair:moveUp()
    if self.baseY > 5 then
        self.baseY = self.baseY - self.moveSpeed
    end
end

function MovableCrosshair:moveDown()
    if self.baseY < 235 then
        self.baseY = self.baseY + self.moveSpeed
    end
end

function MovableCrosshair:moveLeft()
    if self.baseX > 5 then
        self.baseX = self.baseX - self.moveSpeed
    end
end

function MovableCrosshair:moveRight()
    if self.baseX < 395 then
        self.baseX = self.baseX + self.moveSpeed
    end
end