local bumpLeftX = 27
local bumpRightX = -27

local floorLevel = 0
local stepHeight = 25

local right = false

local walking = true

function onCreate()
  makeAnimatedLuaSprite("bg", "monster/hallway", 0, 0)
  addAnimationByPrefix("bg", "cycle", "Hallway", 24, true)
  scaleObject("bg", 1.8, 1.8, true)
  updateHitbox("bg")
  setScrollFactor("bg", 0, 0)
  screenCenter("bg", 'xy')
  addLuaSprite("bg", false)

  makeAnimatedLuaSprite("spookyBg", "monster/spookyHallway", 0, 0)
  addAnimationByPrefix("spookyBg", "cycle", "Hallway", 24, true)
  scaleObject("spookyBg", 1.8, 1.8, true)
  updateHitbox("spookyBg")
  setScrollFactor("spookyBg", 0, 0)
  screenCenter("spookyBg", 'xy')
  addLuaSprite("spookyBg", false)

  makeLuaSprite("vig", "monster/vig", 0, 0)
  setScrollFactor("vig", 0, 0)
  screenCenter("vig", 'xy')
  setBlendMode("vig", "MULTIPLY")
  addLuaSprite("vig", true)

  makeLuaSprite("redVig", "monster/redVig", 0, 0)
  setScrollFactor("redVig", 0, 0)
  screenCenter("redVig", 'xy')
  setBlendMode("redVig", "MULTIPLY")
  addLuaSprite("redVig", true)

  makeLuaSprite("trippyV", "monster/trippyV", 0, 0)
  setScrollFactor("trippyV", 0, 0)
  screenCenter("trippyV", 'xy')
  setBlendMode("trippyV", "MULTIPLY")
  addLuaSprite("trippyV", true)

  makeLuaSprite("trippy", "monster/trippyO", 0, 0)
  setScrollFactor("trippy", 0, 0)
  screenCenter("trippy", 'xy')
  setBlendMode("trippy", "ADD")
  setProperty("trippy.alpha", 0.25)
  addLuaSprite("trippy", false)

  makeLuaSprite("add", "monster/add", 0, 0)
  setScrollFactor("add", 0, 0)
  screenCenter("add", 'xy')
  setBlendMode("add", "ADD")
  setProperty("add.alpha", 0.05)
  addLuaSprite("add", false)

  if not flashingLights then
    setProperty("add.visible", false)
  end

  makeLuaSprite("43", "monster/43", 0, 0)
  setScrollFactor("43", 0, 0)
  screenCenter("43", 'xy')
  -- addLuaSprite("43", true)
  setObjectCamera("43", "other")

  setProperty('spookyBg.visible', false)
  setProperty("redVig.visible", false)
  setProperty("camGame.alpha", 0)
  setProperty('skipCountdown', true);
  setProperty("trippy.visible", false)
  setProperty("trippyV.visible", false)
  setProperty("trippyV.alpha", 0.5)

  playAnim("bg", "cycle", "true")
end

function onCreatePost()

  for i = 0,3 do
    setPropertyFromGroup('strumLineNotes',i,'x',-330)
  end

  local x = screenWidth * 0.3 - (getPropertyFromGroup('strumLineNotes',0,'width') / 2)
  local xx = screenWidth * 0.4 - (getPropertyFromGroup('strumLineNotes',0,'width') / 2)
  local xxx = screenWidth * 0.6 - (getPropertyFromGroup('strumLineNotes',0,'width') / 2)
  local xxxx = screenWidth * 0.7 - (getPropertyFromGroup('strumLineNotes',0,'width') / 2)

  setPropertyFromGroup('strumLineNotes',4,'x',x)
  setPropertyFromGroup('strumLineNotes',5,'x',xx)
  setPropertyFromGroup('strumLineNotes',6,'x',xxx)
  setPropertyFromGroup('strumLineNotes',7,'x',xxxx)

  -- the things i do bro

  setProperty("timeBar.visible", false)
  setProperty("timeTxt.visible", false)

  setProperty('healthBar.visible', false)
  setProperty('healthBarBG.visible', false)
  setProperty('iconP1.visible', false)
  setProperty('iconP2.visible', false)
  setProperty("scoreTxt.visible", false)

  setProperty('showRating', false)
  setProperty('showComboNum', false)

  setProperty("boyfriend.visible", false)

  setProperty("camFollow.x", 470)
  setProperty("camFollow.y", 300)
  setProperty("isCameraOnForcedPos", true)
  setProperty('cameraSpeed', 100)

  if shadersEnabled then
    callScript('scripts/noiseShader', 'init', {})
    callScript('scripts/noiseShader','setShaderIntensity', {0.09})    
  end

  runHaxeCode('FlxG.camera.snapToTarget();')

end

function onBeatHit()
  if curBeat % 2 == 0 then
    setProperty("add.alpha", 0.15)
    doTweenAlpha("lightbu", "add", 0.05, (crochet/1000) / playbackRate, 'circOut')
  end
  
  if not walking then return end
  
  if right then
    doTweenX("bump", "dad", bumpLeftX, (crochet/1000) / playbackRate, 'quadOut')
    doTweenY("step", "dad", floorLevel - stepHeight, (crochet/1000/2) / playbackRate, 'circOut')

    right = false
  else
    doTweenX("bump", "dad", bumpRightX, (crochet/1000) / playbackRate, 'quadOut')
    doTweenY("step", "dad", floorLevel - stepHeight, (crochet/1000/2) / playbackRate, 'circOut')

    right = true
  end
end

--  this itchyluff still does events the old fashion way

function onStepHit()
  -- start of song
  if curStep == 8 then
    setProperty("camGame.alpha", 1)
  end

  -- chikki chikki cha zoom
  if curStep == 520 then
    setProperty("defaultCamZoom", 1.4)
  elseif curStep == 648 then
    setProperty("defaultCamZoom", 1.2)
  end

  -- first scary zoom
  if curStep == 360 then
    doTweenZoom("scaryZoom", "game", 1.6, ((crochet/1000) * 8) / playbackRate, 'sineIn')
    doTweenAlpha("bgFade", "bg", 0.25, ((crochet/1000) * 8) / playbackRate, 'sineIn')
    
  elseif curStep == 392 then
    doTweenAlpha("bgFadeBack", "bg", 1, (crochet/1000) / playbackRate, 'expoOut')
    doTweenZoom("scaryZoomBack", "game", 1.2, (crochet/1000) / playbackRate, 'expoOut')
  end

  -- like idk  camera
  if curStep == 648 then
    setProperty("defaultCamZoom", 1.3)
  end

  if curStep == 696 then
    setProperty("defaultCamZoom", 1.4)
  end

  if curStep == 712 then
    setProperty("defaultCamZoom", 1.2)
  end

  -- second scary zoom
  if curStep == 744 then
    doTweenZoom("scaryZoom", "game", 1.6, ((crochet/1000) * 8) / playbackRate, 'sineIn')
    doTweenAlpha("bgFade", "bg", 0.25, ((crochet/1000) * 8) / playbackRate, 'sineIn')
  end

  -- when music drops out before part B
  if curStep == 776 then
    setProperty("camGame.alpha", 0)
    setProperty("defaultCamZoom", 1.4)

    for i = 4,7 do
      setPropertyFromGroup('strumLineNotes',i,'alpha',0)
    end
  end

  -- mosnter says hi
  if curStep == 840 then
    
    setProperty("camGame.alpha", 1)
    setProperty("bg.visible", false)
    setProperty("spookyBg.visible", true)

    setProperty("spookyBg.alpha", 0.1)

    setProperty("redVig.visible", true)

    for i = 4,7 do
      setPropertyFromGroup('strumLineNotes',i,'alpha',1)
    end
  end

  -- part B start
  if curStep == 972 then
    setProperty("defaultCamZoom", 1.15)
    doTweenZoom("scaryZoom", "game", 1.15, (crochet/1000) / playbackRate, 'expoOut')
    setProperty("spookyBg.alpha", 1)

  end

  -- wait black midi now
  if curStep == 1090 then
    setProperty("camGame.alpha", 0)

    for i = 4,7 do
      setPropertyFromGroup('strumLineNotes',i,'alpha',0)
    end
  end

  -- wait acutal black midi now
  if curStep == 1096 then

    setProperty("trippy.visible", true)
    setProperty("trippyV.visible", true)
    setProperty("redVig.alpha", 0.25)

    for i = 4,7 do
      setPropertyFromGroup('strumLineNotes',i,'alpha',0)
    end

    setProperty("camGame.alpha", 1)
  end

  -- wait acutal black midi now
  if curStep == 1192 then
    noteTweenAlpha("note0", 4, 1, (crochet / 1000) * 4 / playbackRate, 'linear')
    noteTweenAlpha("note1", 5, 1, (crochet / 1000) * 4 / playbackRate, 'linear')
    noteTweenAlpha("note2", 6, 1, (crochet / 1000) * 4 / playbackRate, 'linear')
    noteTweenAlpha("note3", 7, 1, (crochet / 1000) * 4 / playbackRate, 'linear')
  end

  -- its getting real
  if curStep == 1224 then

    setProperty("defaultCamZoom", 1.4)
  end

  if curStep == 1320 then
    doTweenZoom("scaryZoom", "game", 2, ((crochet/1000) * 8) / playbackRate, 'sineIn')
    doTweenAlpha("bgFade", "spookyBg", 0.25, ((crochet/1000) * 8) / playbackRate, 'sineIn')
  end

  if curStep == 1352 then
    walking = false

    cameraZoomOnBeat = false

    setProperty("add.visible", false)

    doTweenAlpha("bgFadeBack", "spookyBg", 1, (crochet/1000) / playbackRate, 'expoOut')
    doTweenZoom("scaryZoomBack", "game", 1.2, (crochet/1000) / playbackRate, 'expoOut')

    cancelTween("bump")
    cancelTween("step")

    cameraFade("game", 'black', 0.2, true, true)

    scaleObject("dad", 0.8, 0.4)
    doTweenY("slipUp", "dad", floorLevel - 40, (crochet / 1000) / 2 / playbackRate, 'circOut')
    doTweenAngle("slipAngle", "dad", -15, (crochet / 1000) * 2 / playbackRate, 'circOut')
    doTweenX("squashUpDadX", "dad.scale", 0.5, (crochet / 1000) * 2 / playbackRate, 'circOut')
    doTweenY("squashUpDadY", "dad.scale", 0.7, (crochet / 1000) * 2 / playbackRate, 'circOut')
  end
end

function onTweenCompleted(tag, vars)
  if tag == "step" then
    if not walking then return end
    doTweenY("step", "dad", floorLevel, (crochet/1000/2) / playbackRate, 'circIn')
  elseif tag == "slipUp" then
    doTweenY("slipDown", "dad", floorLevel + 500, (crochet / 1000) / playbackRate, 'circIn')
  elseif tag == "slipDown" then
    callMethod('spookyBg.animation.curAnim.pause')
  end
end

function onDestroy()
  if shadersEnabled then
    runHaxeCode([[
        FlxG.game.setFilters([]);
    ]])
  end
end

function onUpdate() 
    if curStep >= 1352 then
        playAnim("dad", "trip", true)
    end
end