local IMG_PATH = "games/watching-the-neighbors/imgs/"
local SFX_PATH = "games/watching-the-neighbors/sfx/"
local game = {}

function game:setup()
    size(512, 480)
    self.gameState = "playing"
    
    self.playerState = "jerking"
    self.playerAnimations = {
          jerking = "guyJerk"
        , cumming = "guyCum"
        , hiding = "guyHide"
    }
    self.playerFrame = 0
    self.playerTimer = 0
    self.playerCumCountdown = 0
    self.playerPleasure = 0
    self.playerCaught = false

    self.coupleSusMeter = love.math.random(1, 3)
    self.coupleGlareMeter = 0
    self.coupleState = "fucking"
    self.coupleAnimations = {
          fucking = "coupleFuck"
        , sus = "coupleSus"
        , glare = "coupleGlare"
    }
    self.coupleFrame = 0
    self.coupleTimer = 0
    
    self.eyeState = "looking"
    self.eyeAnimations = {
          looking = "eyeSearching"
        , found = "eyeFound"
    }
    self.eyeTimer = 0
    self.eyeFrame = 0
    
    if not self.sprites then
        self.sprites = {
              background = loadImage(IMG_PATH .. "background.png")
            , gameOver = loadImage(IMG_PATH .. "game-over.png")
            , gameWin = loadImage(IMG_PATH .. "you-win.png")
            , window = loadImage(IMG_PATH .. "window.png")
            , barBackground = loadImage(IMG_PATH .. "bar-background.png")
            , barCum = loadImage(IMG_PATH .. "bar-cum.png")
            , barOutline = loadImage(IMG_PATH .. "bar-outline.png")
            , coupleFuck = {
                  loadImage(IMG_PATH .. "couple-0001.png")
                , loadImage(IMG_PATH .. "couple-0002.png")
                , loadImage(IMG_PATH .. "couple-0003.png")
                , loadImage(IMG_PATH .. "couple-0004.png")
                , loadImage(IMG_PATH .. "couple-0005.png")
            }
            
            , coupleSus = {
                  loadImage(IMG_PATH .. "couple-sus-0001.png")
                , loadImage(IMG_PATH .. "couple-sus-0002.png")
            }
            
            , coupleGlare = {
                  loadImage(IMG_PATH .. "couple-glare-0001.png")
                , loadImage(IMG_PATH .. "couple-glare-0002.png")
            }
            
            , guyJerk = {
                  loadImage(IMG_PATH .. "guy-jerk-0001.png")
                , loadImage(IMG_PATH .. "guy-jerk-0002.png")
                , loadImage(IMG_PATH .. "guy-jerk-0003.png")
                , loadImage(IMG_PATH .. "guy-jerk-0004.png")
                , loadImage(IMG_PATH .. "guy-jerk-0005.png")
                , loadImage(IMG_PATH .. "guy-jerk-0006.png")
            }
    
            , guyCum = {
                  loadImage(IMG_PATH .. "guy-jerk-0001.png")
                , loadImage(IMG_PATH .. "guy-jerk-0002.png")
                , loadImage(IMG_PATH .. "guy-jerk-0003.png")
                , loadImage(IMG_PATH .. "guy-jerk-0004.png")
                , loadImage(IMG_PATH .. "guy-jerk-0005.png")
                , loadImage(IMG_PATH .. "guy-jerk-0006.png")
                , loadImage(IMG_PATH .. "guy-cum-0001.png")
                , loadImage(IMG_PATH .. "guy-cum-0002.png")
                , loadImage(IMG_PATH .. "guy-cum-0003.png")
                , loadImage(IMG_PATH .. "guy-cum-0004.png")
                , loadImage(IMG_PATH .. "guy-cum-0005.png")
                , loadImage(IMG_PATH .. "guy-cum-0006.png")
                , loadImage(IMG_PATH .. "guy-cum-0007.png")
                , loadImage(IMG_PATH .. "guy-cum-0008.png")
                , loadImage(IMG_PATH .. "guy-cum-0009.png")
                , loadImage(IMG_PATH .. "guy-cum-0010.png")
                , loadImage(IMG_PATH .. "guy-cum-0011.png")
                , loadImage(IMG_PATH .. "guy-cum-0012.png")
                , loadImage(IMG_PATH .. "guy-cum-0013.png")
            }
    
            , guyHide = {
                  loadImage(IMG_PATH .. "guy-hide-0001.png")
                , loadImage(IMG_PATH .. "guy-hide-0002.png")
            }
    
            , eyeSearching = {
                  loadImage(IMG_PATH .. "eye-search-0001.png")
                , loadImage(IMG_PATH .. "eye-search-0002.png")
                , loadImage(IMG_PATH .. "eye-search-0003.png")
                , loadImage(IMG_PATH .. "eye-search-0004.png")
                , loadImage(IMG_PATH .. "eye-search-0005.png")
            }
    
            , eyeFound = {
                  loadImage(IMG_PATH .. "eye-found-0001.png")
                , loadImage(IMG_PATH .. "eye-found-0002.png")
            }
        }
    end

    if not self.sfx then
        self.sfx = {
              coupleHump = love.audio.newSource("sfx/couple-hump.wav", "static")
            , guyJerk = love.audio.newSource("sfx/guy-jerk.wav", "static")
            , cum = love.audio.newSource("sfx/cum.wav", "static")
        }
    end
    
    self.sfx.guyJerk:setVolume(0.6)
    self.sfx.coupleHump:setVolume(0.05)
end

function game:playerAnimation()
    return self.sprites[self.playerAnimations[self.playerState]]
end

function game:playerCurrentFrame()
    return self:playerAnimation()[1 + self.playerFrame]
end

function game:playerFrameCount()
    return #self:playerAnimation()
end

function game:playerAnimate()
    self.playerTimer = self.playerTimer + deltaTime / 1000
    if self.playerTimer > 1 / 12 then
        self.playerTimer = 0
        self.playerFrame = self.playerFrame + 1
    end
    self.playerFrame = self.playerFrame % self:playerFrameCount()
    if self.playerFrame == 2 and not self.sfx.guyJerk:isPlaying() then
        self.sfx.guyJerk:play()
    end
    if self.playerFrame == 8 and not self.sfx.cum:isPlaying() then
        self.sfx.cum:play()
    end
end

function game:coupleAnimation()
    return self.sprites[self.coupleAnimations[self.coupleState]]
end

function game:coupleCurrentFrame()
    return self:coupleAnimation()[1 + self.coupleFrame]
end

function game:coupleFrameCount()
    return #self:coupleAnimation()
end

function game:coupleAnimate()
    self.coupleTimer = self.coupleTimer + deltaTime / 1000
    if self.coupleTimer > 1 / 12 then
        self.coupleTimer = 0
        self.coupleFrame = self.coupleFrame + 1
    end
    self.coupleFrame = self.coupleFrame % self:coupleFrameCount()
    if self.coupleFrame == 4 and not self.sfx.coupleHump:isPlaying() then
        self.sfx.coupleHump:play()
    end
end

function game:coupleTickSusness()
    self.coupleSusMeter = self.coupleSusMeter - deltaTime / 1000
    if self.gameState == "game over" then
        self.coupleState = "glare"
    elseif self.gameState == "game win" then
        self.coupleState = "sus"
    elseif self.coupleSusMeter < 0.5 and self.coupleState == "fucking" then
        self.coupleState = "sus"
    elseif self.coupleSusMeter < 0 and self.coupleState == "sus" then
        self.coupleState = "glare"
        self.coupleGlareMeter = love.math.random(1,2)
    elseif self.coupleGlareMeter > 0 and self.coupleState == "glare" then
        self.coupleGlareMeter = self.coupleGlareMeter - deltaTime / 1000
    elseif self.coupleState == "glare" then
        self.coupleState = "fucking"
        self.coupleSusMeter = love.math.random(2, 5)
        self.coupleGlareMeter = 0
    end
end

function game:eyeAnimation()
    return self.sprites[self.eyeAnimations[self.eyeState]]
end

function game:eyeCurrentFrame()
    return self:eyeAnimation()[1 + self.eyeFrame]
end

function game:eyeFrameCount()
    return #self:eyeAnimation()
end

function game:eyeAnimate()
    if self.gameOver then
        self.eyeState = "found"
    end
    self.eyeTimer = self.eyeTimer + deltaTime / 1000
    if self.eyeTimer > 1 / 12 then
        self.eyeTimer = 0
        self.eyeFrame = self.eyeFrame + 1
    end
    self.eyeFrame = self.eyeFrame % self:eyeFrameCount()
end


function game:draw()
    if self.gameState == "game over screen" then
        image(self.sprites.gameOver)
        return
    elseif self.gameState == "game win screen" then
        image(self.sprites.gameWin)
        return
    end
    if self.playerState == "jerking" and self.coupleState == "glare" then
        self.playerState = "hiding"
        self.gameState = "game over"
    elseif self.gameState == "game over" then
        self.playerState = "hiding"
        self.eyeState = "found"
    elseif keyIsDown("space") and self.playerState ~= "cumming" then
        self.playerState = "hiding"
        self.playerPleasure = self.playerPleasure - deltaTime / 1000 * 10
        self.playerPleasure = math.max(0, self.playerPleasure)
    elseif self.playerState ~= "cumming" then
        self.playerPleasure = self.playerPleasure + deltaTime / 1000 * 20
        self.playerState = "jerking"
        if self.playerPleasure >= 365 then
            self.playerState = "cumming"
            self.gameState = "game win"
            self.playerPleasure = 0
        end
    end
    background(255)
    imageMode(CORNER)

    image(self.sprites.background)
    image(self.sprites.window)

    self:coupleAnimate()
    image(self:coupleCurrentFrame())
    

    self:playerAnimate()
    image(self:playerCurrentFrame())

    image(self.sprites.barBackground)
    love.graphics.setScissor(0, 420 - self.playerPleasure, width, height)
    image(self.sprites.barCum)
    love.graphics.setScissor()
    image(self.sprites.barOutline)
    
    self:eyeAnimate()
    if self.coupleState == "glare" then
        image(game:eyeCurrentFrame())
    end
    self:coupleTickSusness()
end 

function game:keyPressed()
    if self.gameState ~= "playing" and key == "space" then
        if self.gameState == "game win" then
            self.gameState = "game win screen"
        elseif self.gameState == "game over" then
            self.gameState = "game over screen"
        else
            self:setup()
        end
    end
end

return {
      themes = { "Unwanted Voyeurism", "Peeping" }
    , name = "Watchin' The Neighbors"
    , about = "Enjoy your neighbors intimate private time. Honestly, it's their fault for not buying curtains! Just, try not to get caught."
    , game = game
    , id = 1
}
