﻿

velocityStacks.lua

Jan 14th, 2021
489
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. local ret = {}
2.
3. ret.new = function (self, stackLength)
4.     local thing = {}
5.     thing.speed = 10
6.     thing.stack = {}
7.     for i=1,stackLength do
8.         thing.stack[i] = 0
9.     end
10.
11.     thing.update = function(self, dt)
12.         --dt is in seconds passed
13.         --at 60 fps it would be 0.01667
14.
15.         --equation for framerate independence is ((1/(framerate/-0.6))+1)
16.         --dt is 1/framerate, framerate is 1/dt
17.         --final equation: (1/((1/dt)/-0.6))+1 = 1-(0.6*dt)
18.
19.         local t = 1-(self.speed*dt)
20.         if t < 0 then t = 0 end -- <--for SUPER low framerates
21.         for i=2,#(self.stack) do
22.             --self.stack[i] = (self.stack[i] + self.stack[i-1]) * 0.5
23.             self.stack[i] = ((1-t) * self.stack[i-1]) + (t * self.stack[i])
24.         end
25.
26.         --[[
27.         for a 2 item stack
28.         1: [1]
29.         2: ([2] + [1]) / 2
30.
31.         for a 3 item stack
32.         1: [1]
33.         2: ([2]+[1]) / 2
34.         3: ([3]+[2]) / 2
35.
36.         well now it's lerp instead of average
37.         if you wanted average at 60fps then change the 0.6 to 30
38.         the right value being 90% would be 6
39.         the right value being 10% would be 0.015
40.         1% = 59.4, 0.1% = 59.94, 0% = 60
41.         so if a frame lasts 0 seconds exactly, the values don't change
42.         ]]
43.     end
44.
45.     --set all values in the stack to a given value
46.     thing.init =  function(self, value)
47.         for i=1,#(self.stack) do
48.             self.stack[i] = value
49.         end
50.     end
51.
52.     --define metatable if it isn't defined already
53.     if not self.mathObjectMT then
54.         --make metatable for math interactions
55.         self.mathObjectMT = {} --is this something you can do? idk
56.         self.mathObjectMT.__utilGetNumber = function (value) --return number from value of unknown type
57.             if type(value) == "table" then
58.                 if value.stack then --if it has a stack
59.                     return value.stack[#(value.stack)]
60.                 elseif value.currentValue then --if it has a currentValue
61.                     return value.currentValue
62.
63.                 else --if there were to be another special number type to check for, that goes here
64.                     return nil
65.                 end
66.             elseif type(value) == number then
67.                 return value
68.             else --some other random type like a string
69.                 return nil
70.             end
71.         end
72.         --define math functions
73.         self.mathObjectMT.__add = function(a,b) -- a+b
74.             local getNum;
75.             if type(a) == "table" and a.stack then
76.                 getNum = getmetatable(a).__utilGetNumber
77.             else
78.                 getNum = getmetatable(b).__utilGetNumber
79.             end
80.             return getNum(a) + getNum(b)
81.         end
82.         self.mathObjectMT.__sub = function(a,b) -- a-b
83.             local getNum;
84.             if type(a) == "table" and a.stack then
85.                 getNum = getmetatable(a).__utilGetNumber
86.             else
87.                 getNum = getmetatable(b).__utilGetNumber
88.             end
89.             return getNum(a) - getNum(b)
90.         end
91.         self.mathObjectMT.__mul = function(a,b) -- a*b
92.             local getNum;
93.             if type(a) == "table" and a.stack then
94.                 getNum = getmetatable(a).__utilGetNumber
95.             else
96.                 getNum = getmetatable(b).__utilGetNumber
97.             end
98.             return getNum(a) * getNum(b)
99.         end
100.         self.mathObjectMT.__div = function(a,b) -- a/b
101.             local getNum;
102.             if type(a) == "table" and a.stack then
103.                 getNum = getmetatable(a).__utilGetNumber
104.             else
105.                 getNum = getmetatable(b).__utilGetNumber
106.             end
107.             return getNum(a) / getNum(b)
108.         end
109.         self.mathObjectMT.__pow = function(a,b) -- a^b
110.             local getNum;
111.             if type(a) == "table" and a.stack then
112.                 getNum = getmetatable(a).__utilGetNumber
113.             else
114.                 getNum = getmetatable(b).__utilGetNumber
115.             end
116.             return getNum(a) ^ getNum(b)
117.         end
118.         self.mathObjectMT.__unm = function(a) -- -a
119.             return -(getmetatable(a).__utilGetNumber(a))
120.         end
121.     end
122.     setmetatable(thing, self.mathObjectMT)
123.
124.     return thing
125. end
126.
127. return ret
RAW Paste Data