first commit

This commit is contained in:
Kazhnuz 2018-08-29 18:28:12 +02:00
parent 24dd9f5be1
commit a30df91212
4 changed files with 258 additions and 1 deletions

101
README.md
View File

@ -1,2 +1,101 @@
# loveutils
basic love2D utility librairies. Contain kinda-sorted bits of codes that I use on my own project
Basic love2D utility librairies. Contain kinda-sorted bits of codes that I use on my own project
To use it just add the "loveutils" folder where you want and just add this piece of code :
````loveutils = require "loveutils"````
## loveutils.math
All the math functions
### loveutils.math.sign
````y = loveutils.math.sign(x)````
return the sign of x (0 if x is 0)
### loveutils.math.round
````y = loveutils.math.round(x)````
return a rounded version of x (is equal to math.floor if x < .5 and to math.ceil if x 5)
### loveutils.math.vector
````vecx, vecy = loveutils.math.vector(x1, y1, x2, y2)````
return the vector notation of two set of coordinates
### loveutils.math.getMiddlePoint
````midx, midy = loveutils.math.getMiddlePoint(x1, y1, x2, y2)````
return the point exactly at the middle of two points
### loveutils.math.pointDistance
````distance = loveutils.math.pointDistance(x1, y1, x2, y2)````
return the distance between two points
### loveutils.math.pointDirection
````angle = loveutils.math.pointDistance(x1, y1, x2, y2)````
return the angle between two points
### loveutils.math.numberToString
````string = loveutils.math.numberToString(x, lenght)````
return a string corresponding to the x numbers with at least *lenght* digit (for instance loveutils.math.numberToString(1, 3) will return "001")
### loveutils.math.floorCoord
````x, y = loveutils.math.floorCoord(x, y)````
return both floored coordinate
### loveutils.math.pixeliseCoord
````x, y = loveutils.math.floorCoord(x, y, floor)````
return x and y, pixelisated with a factor of *factor*, useful if you are making a tile detection system, for instance.
## loveutils.graphics
all my graphics manipulation functions
### loveutils.graphics.resetColor
````loveutils.graphics.resetColor()````
return nothing
reset love current drawing color to love.graphics.setColor(1,1,1,1)
### loveutils.graphics.box(x, y, w, h)
````loveutils.graphics.box(x, y, w, h)````
return nothing
draw an half-transparent box with solid outline
### loveutils.graphics.print(text, x, y, align, r, sx, sy, ox, oy, kx, ky)
````loveutils.graphics.print(text, x, y, align, r, sx, sy, ox, oy, kx, ky)````
return nothing
like love.graphics.print, but with alignement support.
### loveutils.graphics.printWithSpacing(text, x, y, align, spacing, r, sx, sy, ox, oy, kx, ky)
````loveutils.graphics.printWithSpacing(text, x, y, align, spacing)````
draw a text like loveutils.graphics.print, but with support of spacing : each character will have a space of "spacing" between them.
(doesn't support for the moment angle, spacing, etc.)

64
loveutils/graphics.lua Normal file
View File

@ -0,0 +1,64 @@
local Graphics = {}
function Graphics.resetColor()
love.graphics.setColor(1,1,1,1)
end
function Graphics.box(x, y, w, h)
local x = math.floor(x)
local y = math.floor(y)
local w = math.floor(w)
local h = math.floor(h)
local a = a or 1
local r, g, b, a = love.graphics.getColor( )
love.graphics.setColor(r, g, b, 0.3 * a)
love.graphics.rectangle("fill", x, y, w, h)
love.graphics.setColor(r, g, b, a)
love.graphics.rectangle("line", x, y, w, h)
end
function Graphics.print(text, x, y, align, r, sx, sy, ox, oy, kx, ky)
local width
local font = love.graphics.getFont()
width = font:getWidth(text)
if align == "center" then
width = (width/2)
elseif align == "right" then
width = width
else
width = 0
end
love.graphics.print(text, x - (width), y, r, sx, sy, ox, oy, kx, ky)
end
function Graphics.printWithSpacing(text, spacing, align, x, y, r, sx, sy, ox, oy, kx, ky)
-- DO NOT USE THIS FUNCTION IN A "UPDATE" FUNCTION !
-- it's pretty heavy to use as it use a loop to get every character in a text
local font = love.graphics.getFont()
local xx = 0
local lenght = string.len(text)
local basewidth = font:getWidth(text)
local width = basewidth + (spacing * lenght)
if align == "center" then
width = (width/2)
elseif align == "right" then
width = width
else
width = 0
end
for i=1, lenght do
local char = string.sub(text, i, i)
pos = math.floor(x + xx - width)
love.graphics.print(char, pos, y)
xx = xx + font:getWidth(char) + spacing
end
end
return Graphics

6
loveutils/init.lua Normal file
View File

@ -0,0 +1,6 @@
local cwd = (...):gsub('%.init$', '') .. "."
return {
math = require(cwd .. "math"),
graphics = require(cwd .. "graphics")
}

88
loveutils/math.lua Normal file
View File

@ -0,0 +1,88 @@
local Math = {}
function Math.sign(x)
if (x < 0) then
return -1
elseif (x > 0) then
return 1
else
return 0
end
end
function Math.round(num)
return math.floor(num + 0.5)
end
function Math.vector(x1, y1, x2, y2)
local vecx, vecy
vecx = x2 - x1
vexy = y2 - y1
return vecx, vecy
end
function Math.getMiddlePoint(x1, y1, x2, y2)
local newx, newy, vecx, vecy
vecx = math.max(x1, x2) - math.min(x1, x2)
vecy = math.max(y1, y2) - math.min(y1, y2)
newx = math.min(x1, x2) + (vecx / 2)
newy = math.min(y1, y2) + (vecy / 2)
return newx, newy
end
function Math.pointDistance(x1, y1, x2, y2)
local vecx, vecy
vecx = math.max(x1, x2) - math.min(x1, x2)
vexy = math.max(y1, y2) - math.min(y1, y2)
return math.sqrt(vecx^2 + vecy^2)
end
function Math.pointDirection(x1,y1,x2,y2)
local vecx, vecy, angle
vecy = y2 - y1
vecx = x2 - x1
angle = math.atan2(vecy, vecx)
return angle
end
function Math.numberToString(x, length)
local length = length or 1
local string = ""
local x = x
if (x >= math.pow(10, length)) then
x = unitsNumber*10 - 1
string = string .. x
else
for i=1, (length-1) do
if (x < math.pow(10, length-i)) then
string = string .. "0"
end
end
string = string .. x
end
return string
end
function Math.floorCoord(x, y)
return math.floor(x), math.floor(y)
end
function Math.pixeliseCoord(x, y, factor)
x, y = Math.floorCoord(x / factor, y / factor)
x = x * factor
y = y * factor
return x, y
end
return Math