libs: use loveutils

This commit is contained in:
Kazhnuz 2019-02-25 16:00:52 +01:00
parent 294ebaeb8b
commit 227b16ddda
20 changed files with 202 additions and 378 deletions

View File

@ -0,0 +1,16 @@
local Filesystem = {}
function Filesystem.exists(filepath)
local info = love.filesystem.getInfo( filepath )
local exists = false
if (info == nil) then
exists = false
else
exists = true
end
return exists
end
return Filesystem

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

View File

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

View File

@ -0,0 +1,104 @@
local Math = {}
function Math.wrap(x, startnumber, endnumber)
local delta = endnumber - startnumber
if delta < 0 then
err("endnumber must be larger than startnumber")
end
while x >= endnumber do
x = x - delta
end
while x < startnumber do
x = x + delta
end
return x
end
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)
vecy = 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

View File

@ -1,35 +0,0 @@
local Draw = {}
function Draw.resetColor()
love.graphics.setColor(1,1,1,1)
end
function Draw.setColor(r, g, b, a)
local alpha = a or 255
love.graphics.setColor(r/255,g/255,b/255,a/255)
end
function Draw.box(x, y, w, h, r, g, b, a)
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
love.graphics.setColor(r, g, b, 0.3 * a)
love.graphics.rectangle("fill", x, y, w, h)
love.graphics.setColor(r, g, b, 1 * a)
love.graphics.rectangle("line", x, y, w, h)
Draw.resetColor()
end
function Draw.printShadow(string,x,y)
local Color = love.graphics.getColor()
love.graphics.setColor({0,0,0})
love.graphics.print(string,x+1,y+1)
love.graphics.setColor(Color)
love.graphics.print(string,x,y)
end
return Draw

View File

@ -1,162 +0,0 @@
local Math = {}
function Math.wrap(x, max)
-- Wrap the number in order to be between 0 and the maximum
while x > max do
x = x - max
end
while x < 0 do
x = x + max
end
return x
end
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.roundAngle(angle, num)
-- Note : cette fonction arrondi un angle à un certain nombre de position
local angle = angle
local num = num
--num = math.floor(360 / num)
angle = Math.round(angle / num) * num
return angle
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 -- On inverse y pour que les calculs correspondent à notre manière de fonctionner
vecx = x2 - x1
angle = math.atan2(vecy, vecx)
angle = math.deg(angle)
--print(x2 .. "-" .. x1 .. "=" .. vecx .. " ; " ..
-- y2 .. "-" .. y1 .. "=" .. vecy .. " -> " .. angle)
return angle
end
function Math.wrapAngle(angle)
return Math.wrap(angle, 360)
end
function Math.getMode(angle)
local angle = Math.wrapAngle(angle)
local mode
if (angle <= 45) or (angle >= 315) then
mode = 0
elseif (angle > 45) and (angle < 135) then
mode = 1
elseif (angle >= 135) and (angle <= 225) then
mode = 2
elseif (angle > 225) and (angle < 315) then
mode = 3
end
mode = mode * 90
return mode
end
function Math.getModeCoord(x, y, angle, dist)
local mode = Math.getMode(angle)
local x, y = x, y
if (mode == 0) then
x = x + dist
y = y
elseif (mode == 90) then
x = x
y = y + dist
elseif (mode == 180) then
x = x - dist
y = y
elseif (mode == 270) then
x = x
y = y - dist
end
return x, y
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.invertBool(bool)
if bool == true then
bool = false
else
bool = true
end
return bool
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

View File

@ -1,81 +0,0 @@
local Sensors = {}
function Sensors.fake()
local sensorData = {}
sensorData.lenght = 0
sensorData.x = 0
sensorData.y = 0
sensorData.terrain = 0
sensorData.slope = 0
sensorData.startx = 0
sensorData.starty = 0
sensorData.debugName = ""
sensorData.angle = 0
return sensorData
end
function Sensors.getShortest(sensorList)
local sensorList = sensorList or {}
local sensor = sensorList[1]
for i,v in ipairs(sensorList) do
if (v.lenght < sensor.lenght) and (v.terrain ~= 0) then
sensor = v
end
i = i + 1
end
return sensor
end
function Sensors.getMainGroundSensor(sensorList)
local sensorList = sensorList or {}
local sensor = sensorList[1]
for i,v in ipairs(sensorList) do
if (v.lenght < sensor.lenght) and ((v.lenght > 1) or (v.angle ~=0)) and (v.terrain ~= 0) then
sensor = v
end
i = i + 1
end
return sensor
end
function Sensors.haveTerrain(sensorList)
local sensorList = sensorList or {}
local bool = false
for i,v in ipairs(sensorList) do
if (v.terrain ~= 0) then
bool = true
break;
end
i = i + 1
end
return bool
end
function Sensors.haveNoTerrain(sensorList)
local sensorList = sensorList or {}
local bool = false
for i,v in ipairs(sensorList) do
if (v.terrain == 0) then
bool = true
break;
end
i = i + 1
end
return bool
end
function Sensors.haveAllTerrain(sensorList)
local sensorList = sensorList or {}
local bool = true
for i,v in ipairs(sensorList) do
if (v.terrain == 0) then
bool = false
break;
end
i = i + 1
end
return bool
end
return Sensors

View File

@ -19,7 +19,7 @@ datas = require "datas"
-- On charge ensuite les différents modules, qui contiennnent les fonctionnalités
-- utiles internes au jeu, tel que le moteur de menu.
utils = require "libs.utils"
utils = require "libs.loveutils"
menus = require "modules.menus"
assets = require "modules.assets"
save = require "modules.savegame"

View File

@ -84,7 +84,7 @@ function Assets:setBackground(background)
end
function Assets:drawBackground()
resetColor()
utils.graphics.resetColor()
love.graphics.draw(self.background, 0, 0)
end

View File

@ -28,7 +28,7 @@ function ProgressBar:new(sprite, outerfont, innerfont)
end
function ProgressBar:draw(label, x, y, w, stat, innerlabel)
resetColor()
utils.graphics.resetColor()
local wLabel = self.outerfont:getWidth(label)
local hLabel = self.outerfont:getHeight()

View File

@ -57,14 +57,14 @@ function Sprite:draw(id, x, y, angle, sx, sy, ox, oy, kx, ky)
local kx = kx or 0
local kx = kx or 0
local angle = math.rad(angle)
x, y = floorCoord(x, y)
x, y = utils.math.floorCoord(x, y)
self.animations[id]:draw(self.image, x, y, angle, sx, sy, ox, oy, kx, ky)
end
function Sprite:drawIcon(id, x, y, angle, sx, sy, ox, oy, kx, ky)
love.graphics.setColor(0, 0, 0)
self:draw(id, x+1, y+1, angle, sx, sy, ox, oy, kx, ky)
resetColor()
utils.graphics.resetColor()
self:draw(id, x, y, angle, sx, sy, ox, oy, kx, ky)
end

View File

@ -25,7 +25,7 @@ function TextBox:new(sprite)
end
function TextBox:draw(x, y, w, h)
resetColor()
utils.graphics.resetColor()
local w = math.floor(w / 8) * 8
local h = math.floor(h / 8) * 8
local x1, x2, y1, y2

View File

@ -1,34 +0,0 @@
function resetColor()
love.graphics.setColor(255,255,255)
end
function drawBox(x, y, w, h, r, g, b)
x, y = floorCoord(x, y)
w, h = floorCoord(w, h)
love.graphics.setColor(r, g, b, 80)
love.graphics.rectangle("fill", x, y, w, h)
love.graphics.setColor(r, g, b, 255)
love.graphics.rectangle("line", x, y, w, h)
resetColor()
end
function numberToString(x, unitsNumber)
local unitsNumber = unitsNumber or 1
local string = ""
local x = x
if (x >= math.pow(10, unitsNumber)) then
x = unitsNumber*10 - 1
string = string .. x
else
for i=1, (unitsNumber-1) do
if (x < math.pow(10, unitsNumber-i)) then
string = string .. "0"
end
end
string = string .. x
end
return string
end

View File

@ -1,48 +0,0 @@
require "modules.functions.draw"
function invertBool(bool)
if bool == true then
bool = false
else
bool = true
end
return bool
end
function floorCoord(x, y)
return math.floor(x), math.floor(y)
end
function pixeliseCoord(x, y, factor)
x, y = floorCoord(x / factor, y / factor)
x = x * factor
y = y * factor
return x, y
end
function math.sign(x)
if (x > 0) then return 1 end
if (x < 0) then return -1 end
if (x == 0) then return 0 end
end
function sortByUpdateOrder(a, b)
return a:getUpdateOrder() < b:getUpdateOrder()
end
function wrap(variable, max)
if (max > 0) then
while variable > max do
variable = variable - max
end
while variable < 0 do
variable = variable + max
end
return variable
else
return -1
end
end

View File

@ -98,7 +98,7 @@ function TextMenu:draw()
if self.selected == i and self.focus == true then
love.graphics.setColor(85, 170, 255)
else
resetColor()
utils.graphics.resetColor()
end
if (self.center) then
love.graphics.printf(v.label, self.x, widgety, self.w, "center")

View File

@ -68,7 +68,7 @@ function stateDebugMenu:draw(dt)
love.graphics.rectangle("fill", 0, 0, 16, 272)
love.graphics.setColor(0, 170, 0)
love.graphics.rectangle("fill", 480-16, 0, 16, 272)
utils.draw.resetColor()
utils.graphics.resetColor()
assets.fonts["large"]:set()
assets.sprites["banner"]:draw(1, 0, 8)

View File

@ -12,7 +12,7 @@ function Level:initCamera(x, y)
end
function Level:floorCameraCoord()
self.camera.x, self.camera.y = floorCoord(self.camera.x, self.camera.y)
self.camera.x, self.camera.y = utils.math.floorCoord(self.camera.x, self.camera.y)
print(self.camera.x .. ";" .. self.camera.y)
end
@ -26,8 +26,8 @@ function Level:cameraFollowPlayer(id)
local player = self:getPlayerByID(id)
if (player ~= nil) then
local playx, playy = floorCoord(player.center.x,
player.center.y)
local playx, playy = utils.math.floorCoord(player.center.x,
player.center.y)
local camx, camy = self.camera.x + (self.cameraWidth/2),
self.camera.y + (self.cameraHeight/2)

View File

@ -24,7 +24,7 @@
local string = "total players : " .. #playerList .. "\n"
for i,v in ipairs(playerList) do
v.id = i
local x, y = floorCoord(v.x, v.y)
local x, y = utils.math.floorCoord(v.x, v.y)
string = string .. "p" .. v.id .. " : " .. x .. ";" .. y .. "\n"
end
return string

View File

@ -22,11 +22,11 @@ function Level:drawBackgroundColor()
local r, g, b = self.backcolor
love.graphics.setColor(r, g, b)
love.graphics.rectangle("fill", 0, 0, 480, 272)
resetColor()
utils.graphics.resetColor()
end
function Level:drawHUD(dt)
resetColor()
utils.graphics.resetColor()
local hp = 0
local mp = 0
local weapon = 0
@ -51,5 +51,5 @@ function Level:drawHUD(dt)
love.graphics.printf( numberToString(self.gold, 4), 373, 25, 96-18, "right")
love.graphics.setColor(255, 255, 85)
love.graphics.printf( "G", 373, 25, 96, "right")
resetColor()
utils.graphics.resetColor()
end

View File

@ -37,7 +37,7 @@ local animation = 1
end
function Player:draw(dt)
local drawx, drawy = floorCoord(self.center.x, self.center.y)
local drawx, drawy = utils.math.floorCoord(self.center.x, self.center.y)
local animation = self:getAnimation()
--if (self.direction == -1) then drawx = drawx + self.w end
assets.sprites[self.stats.race]:draw(animation, drawx, drawy, 0, self.direction, 1, 16, 36)