This is a time management library. It can be used on any lua application, but it's specially tailored to work with LÖVE.
https://github.com/kikito/cron.lua
Here is a basic example showing how to use cron to make a gun that is able to shot every two seconds, using a "one-time" clock that triggers an action after 2 seconds. In this case, the action is setting a variable to true. It could be much more complicated than that.
Code: Select all
local cron = require 'cron'
local gunCoolDownClock
local gunReady = true
function fire()
if gunReady then
newBullet()
gunReady = false
end
-- next fire can happen only after two seconds
gunCoolDownClock = cron.after(2, function() gunReady = true end)
end
function love.update(dt)
-- This will activate the gun once enough time has passed
if gunCoolDownClock then gunCoolDownClock:update(dt) end
end
function love.keypress()
fire()
end
Code: Select all
local cron = require 'cron'
local alarmLoopClock
function love.load()
local sound = love.audio.newSource('beep.mp3')
alarmLoopClock = cron.every(10, function() sound:play() end)
end
function love.update(dt)
alarmLoopClock:update(dt)
end
You can use both anonymous functions and predefined functions. For example, the previous love.load could be written like this:
Code: Select all
function love.load()
local sound = love.audio.newSource('beep.mp3')
local playSound = function() sound:play() end
alarmLoopClock = cron.every(10, playSound)
end
Code: Select all
function love.load()
local sound = love.audio.newSource('beep.mp3')
alarmLoopClock = cron.every(10, sound.play, sound)
end
I've created a small demo that can give you a better taste. The looks are very spartan - this was on purpose. I wanted to make the source code as clear as possible. Give it a look and let me know what you think!
Appart from that, on the github page there's more documentation and examples on how to use the library.
Regards!