rerere284

mathObjects.lua

Dec 20th, 2020 (edited)
546
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.57 KB | None | 0 0
  1. local ret = {}
  2.  
  3. ret.makeMathObject = function (self, initialvalue, updateSpeed)
  4.     local thing = {}
  5.     thing.currentValue = initialvalue or 0
  6.     thing.nextValue = initialvalue or 0
  7.     thing.updateSpeed = updateSpeed = 20
  8.     thing.set = function(self, newValue)
  9.         self.nextValue = newValue
  10.     end
  11.     thing.update = function(self, dt)
  12.         --dt is in seconds passed
  13.         --at 60 fps it would be 0.01667
  14.         local t = dt * self.updateSpeed
  15.         --(1-t * a)+(t * b)
  16.         self.currentValue = ( (1-t)*self.currentValue ) + ( t*self.nextValue )
  17.     end
  18.    
  19.     if not self.mathObjectMT then
  20.         self.mathObjectMT = {} --is this something you can do? idk
  21.         self.mathObjectMT.__utilGetNumber = function (value) --return number from value of unknown type
  22.             if type(value) == "table" then
  23.                 if value.currentValue then --if it has a currentValue
  24.                     return value.currentValue
  25.                 else --if there were to be another special number type to check for, that goes here
  26.                     return nil
  27.                 end
  28.             elseif type(value) == number then
  29.                 return value
  30.             else --some other random type like a string
  31.                 return nil
  32.             end
  33.         end
  34.         self.mathObjectMT.__add = function(a,b) -- a+b
  35.             local getNum;
  36.             if type(a) == "table" and a.currentValue then
  37.                 getNum = getmetatable(a).__utilGetNumber
  38.             else
  39.                 getNum = getmetatable(b).__utilGetNumber
  40.             end
  41.             return getNum(a) + getNum(b)
  42.         end
  43.         self.mathObjectMT.__sub = function(a,b) -- a-b
  44.             local getNum;
  45.             if type(a) == "table" and a.currentValue then
  46.                 getNum = getmetatable(a).__utilGetNumber
  47.             else
  48.                 getNum = getmetatable(b).__utilGetNumber
  49.             end
  50.             return getNum(a) - getNum(b)
  51.         end
  52.         self.mathObjectMT.__mul = function(a,b) -- a*b
  53.             local getNum;
  54.             if type(a) == "table" and a.currentValue then
  55.                 getNum = getmetatable(a).__utilGetNumber
  56.             else
  57.                 getNum = getmetatable(b).__utilGetNumber
  58.             end
  59.             return getNum(a) * getNum(b)
  60.         end
  61.         self.mathObjectMT.__div = function(a,b) -- a/b
  62.             local getNum;
  63.             if type(a) == "table" and a.currentValue then
  64.                 getNum = getmetatable(a).__utilGetNumber
  65.             else
  66.                 getNum = getmetatable(b).__utilGetNumber
  67.             end
  68.             return getNum(a) / getNum(b)
  69.         end
  70.         self.mathObjectMT.__pow = function(a,b) -- a^b
  71.             local getNum;
  72.             if type(a) == "table" and a.currentValue then
  73.                 getNum = getmetatable(a).__utilGetNumber
  74.             else
  75.                 getNum = getmetatable(b).__utilGetNumber
  76.             end
  77.             return getNum(a) ^ getNum(b)
  78.         end
  79.         self.mathObjectMT.__unm = function(a) -- -a
  80.             return -(getmetatable(a).__utilGetNumber(a))
  81.         end
  82.     end
  83.     setmetatable(thing, self.mathObjectMT)
  84.     return thing
  85. end
  86.  
  87. return ret
Add Comment
Please, Sign In to add comment