local Audio = require("#Celeste.Audio")


function onBegin()
  local dist = player.Position.X - puppet.Position.X
  puppet.Facing = dist > 0 and 1 or dist < 0 and -1 or puppet.Facing

  while puppet.Speed.X ~= 0 do
    wait()
  end
  
  setFlag("nosk_change", true)
  playPuppetAnim("shriek")
  playSound("event:/CC/CC_HollowKnight/SFX/Boss SFX/NoskScreamShort",puppet.Center)
  wait(0.5)
  setFlag("nosk_change", true)
  playPuppetAnim("charge")
  AttackSfx = celeste.SoundSource()
  addComponentToBoss(AttackSfx:Play("event:/CC/CC_HollowKnight/SFX/Boss SFX/NoskRunning")) --Play returns itself hence the call to Play here
  --AttackSfx = Audio.Play("event:/CC/CC_HollowKnight/SFX/Boss SFX/NoskRunning", puppet.Center)
  setXSpeed(200 * puppet.Facing)
  local center = getStoredObjectFromBoss("spawn_center")
  local closeToCenter = math.abs(center - puppet.Position.X) < (15 * 8) --won't stop center if it starts close to it
  --wait until at end of arena/hit wall or hit arena center
  while puppet.Speed.X ~= 0 and (math.abs(center - puppet.Position.X) > 4 or closeToCenter) do
    wait()
  end
  if puppet.Speed.X == 0 then
    wait(0.2)
    puppet.Facing = puppet.Facing * -1
  else
    local choice = seededRandom()
    if choice % 4 == 0 then
      forceNextAttackIndex(0)
    end
    setXSpeed(0)
  end
  AttackSfx:Stop()
  --Audio.Stop(AttackSfx, false)
  wait(0.2)
end