-- Autogenerated Essential => Figura script
-- Feature "animations" w/ args {{['type'] = 'IDLE',['target'] = 'SELF',['name'] = 'animation.demon_onesie.idle',['probability'] = 1,['priority'] = 1,['skips'] = 0,['loops'] = 1}}
do

  local animations = {{['type'] = 'IDLE',['target'] = 'SELF',['name'] = 'animation.demon_onesie.idle',['probability'] = 1,['priority'] = 1,['skips'] = 0,['loops'] = 1}}
  local currentAnim
  
  local afterDoneAnim
  
  local skipATick = false
  
  function animations_textureAnimationStart()
    for _, anim in ipairs(animations) do
      if anim.type == 'TEXTURE_ANIMATION_START' then
        if math.random() < anim.probability then
          if anim.skipsleft and anim.skipsleft > 0 then
            anim.skipsleft = anim.skipsleft - 1
          else
            anim.skipsleft = anim.skips
            animation[anim.name].play()
            skipATick = true
            if anim.on_complete then afterDoneAnim = anim.on_complete.name end
          end
        end
      end
    end
  end
  
  local op
  
  local function lengthxy(t)
    return math.sqrt(t[1]^2 + t[3]^2)
  end
  
  function tick()
    if skipATick then
      skipATick = false
      return
    end
  
    local p = player.getPos()
    if not op then op = p end
    local l = lengthxy({p[1] - op[1], p[2] - op[2], p[3] - op[3]})
  
    local anyAnimsPlaying = false
    for anim in pairs(animation) do
      if type(animation[anim]) ~= 'function' and animation[anim].getPlayState() == 'PLAYING' then
        anyAnimsPlaying = true
        break
      end
    end
  
    if not anyAnimsPlaying then
      if afterDoneAnim then
        afterDoneAnim = nil
        animation[afterDoneAnim].play()
      else
        for _, anim in ipairs(animations) do
          if anim.type == 'WALK' and l > 0.1 then
            if math.random() < (anim.probability or 1) then
              animation[anim.name].play()
              skipATick = true
            end
            return
          end
        end
        for _, anim in ipairs(animations) do
          if anim.type == 'IDLE' then
            animation[anim.name].play()
            skipATick = true
            return
          end
        end
      end
    end
  end
  
  if events and events.TICK then -- rewrite
    print('animations arent supported in the rewrite so far and thus are disabled for the converted models. the rest _should_ work fine, but look out for that')
  end
  
end

-- Feature "animate" w/ args 128, 2048
do

  local segmentHeight = 128
  local fullHeight = 2048
  
  local frames = math.floor(fullHeight / segmentHeight)
  local ticks = 0
  
  function tick()
    ticks = ticks + 1
    if ticks % 2 == 0 then
      local startedOver = false
      local uvYoffset = segmentHeight / fullHeight
      local function step(group)
        if group.getChildren then
          for k, v in pairs(group.getChildren()) do
            step(v)
          end
        else
          local uv = group.getUV()
          local newy = uv.y + uvYoffset
          if newy > 1 then
            startedOver = true
          end
          group.setUV({uv.x, newy % 1})
        end
      end
      for _, v in pairs(model) do
        step(v)
      end
  
      if startedOver and animations_textureAnimationStart then
        animations_textureAnimationStart()
      end
    end
  end
  
  if events and events.TICK then -- rewrite
    local uvY = 0 -- gotta manually track this bc no getUV
    events.TICK:register(function()
      ticks = ticks + 1
      if ticks % 2 == 0 then
        local startedOver = false
        local uvYoffset = segmentHeight / fullHeight
        local function step(group)
          if group:getType() == 'CUBE' then
            for k, v in pairs(group:getChildren()) do
              step(v)
            end
          else
            uvY = uvY + uvYoffset
            if uvY > 1 then
              startedOver = true
              uvY = uvY - 1
            end
            group:setUV(0, uvY)
          end
        end
        for _, v in pairs(models['player_model']:getChildren()) do
          step(v)
        end
  
        --if startedOver and animations_textureAnimationStart then
        --  animations_textureAnimationStart()
        --end
      end
    end)
  end
  
end
