I noticed I was writing similar bits of code every time I wanted to keep track of time and so the idea came to me. This is just a small library I wrote to optimize timekeeping in my code. I'm interested if this is how OOP looks on lua and if I'm doing it right, any suggestions/comments are welcome. :з
Code: Yes, it's me, the source code
I documented the code pretty well.
Code: Select all
local Timer = require "love-timer"
local timer1 = Timer.new()
timer1:start() -- Now it's counting up from 0 seconds
local timer2 = Timer.new(10) -- Setting the timer for 10 seconds
timer2:start()
while not timer2() do -- timer2() is the same as timer2:check()
-- Busy loop waiting...
end
-- We are here after 10 seconds pass
print(#timer1) -- Should be somewhere around 10 seconds, #timer1 = timer1:time()
local timer3 = Timer.new(5) -- Setting the timer for 5 seconds
while #timer3 > 0 do
-- Busy waiting again...
end
print(#timer2) -- Should be around -5 seconds because we went over 10 set seconds
timer1:start() -- We actually don't need to stop(), start() works as a reset
print(#timer1) -- Should be around 0 seconds
while #timer1 < 4 do
-- Busy waitng for 4 seconds
end
timer1:stop()
print(#timer1) -- nil because timer is stopped
print( (Timer.new()):time() ) -- Same
timer1:set(5) -- timer1 is now a 5 second timer
timer2:set() -- timer2 is now just counting