Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local ret = {}
- ret.makeMathObject = function (self, initialvalue, updateSpeed)
- local thing = {}
- thing.currentValue = initialvalue or 0
- thing.nextValue = initialvalue or 0
- thing.updateSpeed = updateSpeed = 20
- thing.set = function(self, newValue)
- self.nextValue = newValue
- end
- thing.update = function(self, dt)
- --dt is in seconds passed
- --at 60 fps it would be 0.01667
- local t = dt * self.updateSpeed
- --(1-t * a)+(t * b)
- self.currentValue = ( (1-t)*self.currentValue ) + ( t*self.nextValue )
- end
- if not self.mathObjectMT then
- self.mathObjectMT = {} --is this something you can do? idk
- self.mathObjectMT.__utilGetNumber = function (value) --return number from value of unknown type
- if type(value) == "table" then
- if value.currentValue then --if it has a currentValue
- return value.currentValue
- else --if there were to be another special number type to check for, that goes here
- return nil
- end
- elseif type(value) == number then
- return value
- else --some other random type like a string
- return nil
- end
- end
- self.mathObjectMT.__add = function(a,b) -- a+b
- local getNum;
- if type(a) == "table" and a.currentValue then
- getNum = getmetatable(a).__utilGetNumber
- else
- getNum = getmetatable(b).__utilGetNumber
- end
- return getNum(a) + getNum(b)
- end
- self.mathObjectMT.__sub = function(a,b) -- a-b
- local getNum;
- if type(a) == "table" and a.currentValue then
- getNum = getmetatable(a).__utilGetNumber
- else
- getNum = getmetatable(b).__utilGetNumber
- end
- return getNum(a) - getNum(b)
- end
- self.mathObjectMT.__mul = function(a,b) -- a*b
- local getNum;
- if type(a) == "table" and a.currentValue then
- getNum = getmetatable(a).__utilGetNumber
- else
- getNum = getmetatable(b).__utilGetNumber
- end
- return getNum(a) * getNum(b)
- end
- self.mathObjectMT.__div = function(a,b) -- a/b
- local getNum;
- if type(a) == "table" and a.currentValue then
- getNum = getmetatable(a).__utilGetNumber
- else
- getNum = getmetatable(b).__utilGetNumber
- end
- return getNum(a) / getNum(b)
- end
- self.mathObjectMT.__pow = function(a,b) -- a^b
- local getNum;
- if type(a) == "table" and a.currentValue then
- getNum = getmetatable(a).__utilGetNumber
- else
- getNum = getmetatable(b).__utilGetNumber
- end
- return getNum(a) ^ getNum(b)
- end
- self.mathObjectMT.__unm = function(a) -- -a
- return -(getmetatable(a).__utilGetNumber(a))
- end
- end
- setmetatable(thing, self.mathObjectMT)
- return thing
- end
- return ret
Add Comment
Please, Sign In to add comment