master
GianptDev 2022-08-30 15:18:44 +02:00
parent 73415dd83d
commit f306137d91
3 changed files with 134 additions and 128 deletions

View File

@ -70,3 +70,10 @@
- - it will be move to an external drive (still public and accessible).
- - that folder now will only contain media used by the readme or external links.
## 1.2.1
- figured out a cleaner way to make objects in lua, now that old definition hell has been cleared.
- replaced some log types from verbose to warning and action.

View File

@ -4,8 +4,9 @@
--- @package BeTweenApi
--- @author GianptDev
--- @release 1.2
--- @license MIT
--- @author GianptDev (_gianpy_)
--- @release 1.2.1
--- @meta
@ -20,7 +21,7 @@ BeTweenApi = {}
--- return the current version as a string.
--- @return string
function BeTweenApi.version_name ()
return "1.2"
return "1.2.1"
end

View File

@ -15,103 +15,23 @@ BeTweenApi.active_tweens = {}
--- -----------------------------------------------------------
--- Tween methods
--- Start the execution of this tween, it will be added in the active_tweens list until stopped or finished.
--- @param _ Tween
local function start (_)
--- do not run the tween twince.
if (_:is_running() == true) then
minetest.log("action", string.format("Tried to start tween '%p' twince.", _))
return
end
--- tween will start soon, add inside the tween loop.
table.insert(BeTweenApi.active_tweens, _)
minetest.log("verbose", string.format("Tween '%d' ~ '%p' is now running.", _:index(), _))
--- call the virtual.
if (_.on_start ~= nil) then
_.on_start(_)
end
end
--- stop the tween to work by removing it from the active list.
--- @param _ Tween
--- @param reset boolean false
local function stop (_, reset)
local index = _:index()
--- set default reset value.
if (reset == nil) then
reset = (reset == nil)
end
--- index does not exist because the tween is not running.
if (index == nil) then
minetest.log("action", string.format("Tried to stop tween '%p' wich wasn't running.", _))
return
end
--- reset the timer.
if (reset == true) then
_.timer = 0
end
--- tween is stopped now, remove from tween loop.
table.remove(BeTweenApi.active_tweens, index)
minetest.log("verbose", string.format("Tween '%p' has been stopped.", _))
--- call the virtual.
if (_.on_stop ~= nil) then
_.on_stop(_)
end
end
--- check if the tween is currently running.
--- @param _ Tween
--- @return boolean
local function is_running (_)
for i, tween in pairs(BeTweenApi.active_tweens) do
if (tween == _) then
return true
end
end
return false
end
--- get the running index of the tween, if the tween isn't running nil is returned.
--- @param _ Tween
--- @return integer | nil
local function index (_)
local index = 1
for i, tween in pairs(BeTweenApi.active_tweens) do
if (tween == _) then
return index
end
index = index + 1
end
return nil
end
--- -----------------------------------------------------------
--- Tween constructor
--- @class Tween
--- @field interpolation fun(x: number, y: number, t: number)
--- @field movement { [1]: number, [2]: number, [3]: boolean }
--- @field time number
--- @field timer number
--- @field loop boolean|integer
--- @field on_start fun(tween: Tween)
--- @field on_stop fun(tween: Tween)
--- @field on_step fun(tween: Tween, step: number)
--- @field on_loop fun(tween: Tween)
local Tween = {}
--- create a tween object that will interpolate an intial value to a destination in time, each value calculated in time are created from the function the tween is using, after the tween has been created it has run by calling :start()
---
---
--- --- callbacks, each of then give the tween.
--- on_start(tween) --- executed on tween start.
--- on_stop(tween) --- executed on tween stop.
@ -145,47 +65,125 @@ function BeTweenApi.tween (interpolation, movement, time, loop, callbacks)
return nil
end
-- callbacks are optional but make sure is a table.
if (callbacks == nil) then
callbacks = {}
--- create the Tween object and set all his values.
--- @type Tween
local tween = {}
--- yeah, basically it just copy everything defined on Tween.
for x, y in pairs(Tween) do
tween[x] = y
end
--- make the tween table.
--- @class Tween
--- @field interpolation fun(x: number, y: number, t: number)
--- @field movement { [1]: number, [2]: number, [3]: boolean }
--- @field time number
--- @field timer number this timer is used internally by the tween when running.
--- @field loop boolean | integer
--- @field on_start fun(tween: Tween) | nil
--- @field on_stop fun(tween: Tween) | nil
--- @field on_step fun(tween: Tween, step: number) | nil
--- @field on_loop fun(tween: Tween) | nil
local tween = {
interpolation = interpolation,
movement = {
movement[1], movement[2],
(movement[3] ~= nil) and movement[3] or false
},
time = time,
timer = 0.0,
loop = (loop ~= nil) and loop or false,
on_start = callbacks.on_start,
on_stop = callbacks.on_stop,
on_step = callbacks.on_step,
on_loop = callbacks.on_loop,
--- set input or default values for everything.
tween.timer = 0.0
tween.time = time
tween.loop = (loop ~= nil) and loop or false
--- methods
start = start,
stop = stop,
is_running = is_running,
index = index,
tween.interpolation = interpolation
if (callbacks ~= nil) then
tween.on_start = callbacks.on_start
tween.on_stop = callbacks.on_stop
tween.on_step = callbacks.on_step
tween.on_loop = callbacks.on_loop
end
tween.movement = {
movement[1], movement[2],
(movement[3] ~= nil) and movement[3] or false
}
minetest.log("verbose", string.format("New tween '%p' created.", tween))
minetest.log("action", ("New tween '%p' created."):format(tween))
return tween
end
--- -----------------------------------------------------------
--- Start the execution of this tween, it will be added in the active_tweens list until stopped or finished.
function Tween:start ()
--- do not run the tween twince.
if (self:is_running() == true) then
minetest.log("warning", ("Tried to start tween '%p' twince."):format(self))
return
end
--- tween will start soon, add inside the tween loop.
table.insert(BeTweenApi.active_tweens, self)
minetest.log("action", ("Tween '%d' ~ '%p' is now running."):format(self:index(), self))
--- call the virtual.
if (self.on_start ~= nil) then
self.on_start(self)
end
end
--- stop the tween to work by removing it from the active list.
--- @param reset boolean false
function Tween:stop (reset)
local index = self:index()
--- set default reset value.
if (reset == nil) then
reset = (reset == nil)
end
--- index does not exist because the tween is not running.
if (index == nil) then
minetest.log("warning", ("Tried to stop tween '%p' wich wasn't running."):format(self))
return
end
--- reset the timer.
if (reset == true) then
self.timer = 0
end
--- tween is stopped now, remove from tween loop.
table.remove(BeTweenApi.active_tweens, index)
minetest.log("action", ("Tween '%p' has been stopped."):format(self))
--- call the virtual.
if (self.on_stop ~= nil) then
self.on_stop(self)
end
end
--- check if the tween is currently running.
--- @return boolean
function Tween:is_running ()
for i, tween in pairs(BeTweenApi.active_tweens) do
if (tween == self) then
return true
end
end
return false
end
--- get the running index of the tween, if the tween isn't running nil is returned.
--- @return integer | nil
function Tween:index ()
local index = 1
for i, tween in pairs(BeTweenApi.active_tweens) do
if (tween == self) then
return index
end
index = index + 1
end
return nil
end
--- -----------------------------------------------------------