Project - develephant/luvit-docs GitHub Wiki

Install Lit, Luvi, and Luvit

https://luvit.io/install.html

Place the Lit, Luvi, and Luvit binaries in /usr/local/bin

Basic Project Layout

Create the following dir structure:

app
├── libs
│   └── mod.lua
└── main.lua

main.lua

--======================================================================--
--== App Options
--======================================================================--
local options = 
{
	tick_delay = 1000,
	main_class = 'Engine'
}
--======================================================================--
--== App
--======================================================================--
local App = require('core').Emitter:extend()

function App:initialize( options )
	print 'hello app'

    self.timer = require('timer')

	self.tick_timer = nil
	self.tick_delay = options.tick_delay or 1000

	--Init the main class
	local main_class = options.main_class or 'Engine'
	require('./libs/'..main_class):new( self )

	self:start()
end

function App:start()
	self.tick_timer = self.timer.setInterval(self.tick_delay, function()
		self:emit('tick')	
	end)
end

function App:stop()
	self.timer.clearInterval( self.tick_timer )
end

return App:new( options )

libs/mod.lua

--======================================================================--
--== Engine
--======================================================================--
local Engine = require('core').Emitter:extend()
--======================================================================--
--== requires
--======================================================================--
local math = require('math')
--======================================================================--
--== Methods
--======================================================================--
function Engine:initialize( App )
	self.App = App

	self.App:on('tick', function()
		self:doRandom()
	end)
end

function Engine:doRandom()
	p( math.random() )
end

return Engine

Build

Using Luvi, pack the app:

luvi ./app /usr/local/bin/luvit -o target_name

Run

./target_name

You should see a random number printed out every second.