User:Darkfrei/example functions
Contents
Prints table
print('{' ..table.concat(line,",")..'},')
Draw grid
function draw_grid ()
local grid_size = 20
love.graphics.setLineWidth (1)
love.graphics.setColor(0.25,0.25,0.25)
local width, height = love.graphics.getDimensions( )
for x = grid_size, width-1, grid_size do
love.graphics.line(x, 0, x, height)
end
for y = grid_size, height-1, grid_size do
love.graphics.line(0, y, width, y)
end
end
Draw mouse position
function draw_mouse ()
local mx, my = love.mouse.getPosition ()
local text = mx..' '..my
local font = love.graphics.getFont()
local w = font:getWidth(text)
local h = font:getHeight()
love.graphics.setColor(0,0,0)
love.graphics.rectangle('fill', mx, my-h, w, h)
love.graphics.setColor(1,1,1)
love.graphics.print(mx..' '..my,mx,my-h)
end
Beep
Define it:
local rate = 44100
local length = 1/32
local tone = 440 -- Hz
local p = math.floor(rate/tone) -- 128
local soundData = love.sound.newSoundData(length*rate, rate, 16, 1)
for i=0, length*rate-1 do soundData:setSample(i, i%p>p/2 and 1 or -1) end
local source = love.audio.newSource(soundData)
local function beep() source:play() end
Call it:
beep()
Point in area
function is_in_area (mx,my, x,y,w,h) -- mouse position and rectangle
if (mx > x) and (mx < (x + w)) and
(my > y) and (my < (y + h)) then
return true
end
end
Is value in list
function is_value_in_list (value, list)
for i, v in pairs (list) do
if v == value then
return true
end
end
end
Normalization and multiplication
Set magnitude to this vector:
function normul (x, y, factor) -- normalization and multiplication
local d = (x*x+y*y)^0.5
factor= factor or 1
return factor*x/d, factor*y/d
end
Evaluate a point from any amount of control points
local function evaluate (curve, t)
local ccpc = curve:getControlPointCount( )
if ccpc > 1 then
return curve:evaluate(t)
elseif ccpc == 1 then
return curve:getControlPoint(1)
else
return 0, 0
end
end
Sprit string with separator
local function split (string, separator)
local tabl = {}
for str in string.gmatch(string, "[^"..separator.."]+") do
table.insert (tabl, str)
end
return tabl
end
--split ("123#456", "#") -- results: 123 456
Sprit string to symbols
local function split2 (string)
local tabl = {}
string:gsub(".", function(c) table.insert(tabl, tonumber (c)) end)
return tabl
end
--split2 ("123") -- results: 1, 2, 3