sonic-radiance/sonic-radiance.love/core/modules/assets/tileset.lua

121 lines
3.7 KiB
Lua
Raw Permalink Normal View History

-- assets/tileset :: tileset are automatic breakage of texture into quads. they
-- have the adventage of being automatized, reducing the ammount of code needed
-- to create quads.
-- They have two manners to be draw: with their quad id (in 1D) or by using their
-- place in the grid, in 2D.
--[[
Copyright © 2019 Kazhnuz
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
]]
local Tileset = Object:extend()
2019-03-23 12:02:01 +01:00
local cwd = (...):gsub('%.tileset$', '') .. "."
local Texture = require(cwd .. "texture")
2019-07-26 16:46:43 +02:00
-- INIT FUNCTIONS
-- Initilizing and configuring option
function Tileset:new(filepath)
2019-03-23 12:02:01 +01:00
self.texture = Texture(filepath .. ".png")
local data = require(filepath)
self.metadata = data.metadata
self:createQuads()
end
function Tileset:createGrid()
self.textureWidth, self.textureHeight = self.texture:getDimensions()
self.width, self.height = self.metadata.width, self.metadata.height
self.gridWidth, self.gridHeight = math.floor(self.textureWidth / self.width),
math.floor(self.textureHeight / self.height)
end
function Tileset:createQuads()
self.quads = {}
self:createGrid()
local quad, n
n = 1
for i=0, (self.gridHeight-1) do
for j=0, (self.gridWidth-1) do
quad = love.graphics.newQuad(j * self.width, i * self.height, self.width, self.height, self.textureWidth, self.textureHeight)
self.quads[n] = quad
n = n + 1
end
end
end
2019-07-26 16:46:43 +02:00
-- INFO FUNCTIONS
-- get information with these functions
function Tileset:getTileID_Grid(x, y)
local n = (y - 1) * self.gridWidth + x
return n
end
function Tileset:getTile_Grid(x, y)
return self:getTile(self:getTileID_Grid(x, y))
end
function Tileset:getTile(n)
return self.quads[n]
end
2019-07-26 16:46:43 +02:00
function Tileset:getDimensions()
return self.width, self.height
end
-- DRAW FUNCTIONS
-- Draw tileset using these functions
function Tileset:drawTile_Grid(i, j, x, y, r, sx, sy, ox, oy, kx, ky)
local tileID = self:getTileID_Grid(i, j)
2019-07-26 16:46:43 +02:00
local ox = ox or self.metadata.ox
local oy = oy or self.metadata.oy
2019-03-23 12:02:01 +01:00
self.texture:drawQuad(self.quads[tileID], x, y, r, sx, sy, ox, oy, kx, ky)
end
function Tileset:drawTile(id, x, y, r, sx, sy, ox, oy, kx, ky)
2019-07-26 16:46:43 +02:00
local ox = ox or self.metadata.ox
local oy = oy or self.metadata.oy
2019-03-23 12:02:01 +01:00
self.texture:drawQuad(self.quads[id], x, y, r, sx, sy, ox, oy, kx, ky)
end
function Tileset:drawTileMask_Grid(i, j, x, y, r, sx, sy, ox, oy, kx, ky)
local tileID = self:getTileID_Grid(i, j)
2019-07-26 16:46:43 +02:00
local ox = ox or self.metadata.ox
local oy = oy or self.metadata.oy
2019-03-23 12:02:01 +01:00
self.texture:drawMaskQuad(self.quads[tileID], x, y, r, sx, sy, ox, oy, kx, ky)
end
2019-03-23 12:02:01 +01:00
function Tileset:drawTileMask(id, x, y, r, sx, sy, ox, oy, kx, ky)
self.texture:drawMaskQuad(self.quads[id], x, y, r, sx, sy, ox, oy, kx, ky)
end
return Tileset