Last active
April 26, 2017 23:03
-
-
Save dvdfu/70ea30a46d76da331243df1e63b88a76 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local Oscillator = {} | |
Oscillator.__index = Oscillator | |
function Oscillator.new(period) | |
assert(period > 0) | |
return setmetatable({ | |
period = period or 1, | |
time = 0 | |
}, Oscillator) | |
end | |
function Oscillator:update(dt) | |
if self.time + dt < self.period then | |
self.time = self.time + dt | |
else | |
self.time = self.time + dt - self.period | |
end | |
end | |
function Oscillator:reset() | |
self.time = 0 | |
end | |
function Oscillator:get() | |
return self.time | |
end | |
function Oscillator:getFraction() | |
return self.time / self.period | |
end | |
function Oscillator:getSin() | |
return math.sin(self:getFraction() * math.pi * 2) | |
end | |
function Oscillator:getCos() | |
return math.cos(self:getFraction() * math.pi * 2) | |
end | |
return setmetatable(Oscillator, | |
{ __call = function(_, ...) return Oscillator.new(...) end }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment