Подпишитесь на наш Telegram-канал, чтобы всегда быть в курсе важных обновлений! Перейти

LUA скрипт Simple anim class ( Братский lerp )

Nike.lua
Олдфаг
Олдфаг
Статус
Оффлайн
Регистрация
13 Окт 2020
Сообщения
2,758
Реакции
1,465
Ну пока практиковал метатаблицы сделал это:

code_language.lua:
Expand Collapse Copy
_DEBUG = true

local animation = { }
function animation:new( initial_value )
  local animation = { }
    animation.data = {
      value = initial_value or 0,
      weight = 0
    }

    --спизжено у сальватора
    animation.get_type = function( value )
      if type( getmetatable( value ) ) == "table" and value.__type then
        return value.__type.name:lower( )
      end

      return type( value )
    end

    animation.update = function( self, condition, duration, destination )
      assert(
        animation.get_type( self.value ) == animation.get_type( destination ),
        string.format( "[animation] Type mismath: required %s, received %s.", animation.get_type( self.value ), animation.get_type( destination ) )
      )

      local clock = globals.frametime / duration
      self.weight = self.weight + ( condition and clock or -clock )
      self.weight = math.clamp( self.weight, 0, 1 )
 
      if animation.get_type( self.value ) == "imcolor" then
        self.value = color(
          self.value.r + ( destination.r - self.value.r ) * self.weight,
          self.value.g + ( destination.g - self.value.g ) * self.weight,
          self.value.b + ( destination.b - self.value.b ) * self.weight,
          self.value.a + ( destination.a - self.value.a ) * self.weight
        )

        return self.value
      end

      self.value = self.value + ( destination - self.value ) * self.weight
      return self.value
    end

  return setmetatable( { }, {
    __index = animation.data,
    __call = animation.update,
    __metatable = false
  } )
end

юзать так:
code_language.lua:
Expand Collapse Copy
local alpha = animation:new( 0 )

events.render:set( function( )
   alpha(
    true,
    0.2, 255
   )

  render.rect( vector( 500, 500 ), vector( 600, 600 ), color( 255, alpha.value ) )
end )

--ну или так
events.render:set( function( )
  local a = alpha(
    true,
    0.2, 255
  )

  render.rect( vector( 500, 500 ), vector( 600, 600 ), color( 255, a ) )
end )
 
Назад
Сверху Снизу