TileSheet.new: You can now specify the resolution of the tile that is on the canvas

This commit is contained in:
Van
2025-10-20 18:18:26 +03:00
parent cf88ccf67f
commit 3b8aed464d
3 changed files with 71 additions and 13 deletions

View File

@@ -1,23 +1,81 @@
local mathRound = math.round
---@class TileSheet ---@class TileSheet
---@field image string Name of texture ---@field image string Name of texture
---@field width number Number of tiles in the table horizontally ---@field width number Number of tiles in the table horizontally
---@field height number Number of tiles in the table vertically ---@field height number Number of tiles in the table vertically
local TileSheet = {} local TileSheet = {}
---@param image string Name of texture ---@param imagePath string
---@param width number Number of tiles in the table horizontally ---@return number, number
---@param height number Number of tiles in the table vertically local function getSizePng(imagePath)
---@return TileSheet local file = io.open(imagePath, 'rb')
function TileSheet:new(image, width, height) if not file then
error('Cannot open file: ' .. imagePath)
end
-- PNG signature check (8 bytes)
local signature = file:read(8)
if not signature or signature ~= '\137PNG\r\n\26\n' then
file:close()
error('Not a valid PNG file: ' .. imagePath)
end
-- Reading the first chunk (must be IHDR)
local length_data = file:read(4)
local chunk_type = file:read(4)
if not length_data or not chunk_type or chunk_type ~= 'IHDR' then
file:close()
error('Invalid PNG structure in file: ' .. imagePath)
end
-- Reading IHDR data (width and height)
local width_data = file:read(4)
local height_data = file:read(4)
if not width_data or not height_data then
file:close()
error('Incomplete PNG header in file:' .. imagePath)
end
file:close()
-- Converting big-endian bytes to numbers
local width = (string.byte(width_data, 1) * 16777216)
+ (string.byte(width_data, 2) * 65536)
+ (string.byte(width_data, 3) * 256)
+ string.byte(width_data, 4)
local height = (string.byte(height_data, 1) * 16777216)
+ (string.byte(height_data, 2) * 65536)
+ (string.byte(height_data, 3) * 256)
+ string.byte(height_data, 4)
return width, height
end
---Returns an instance of the TileSheet class by canvas name and single tile resolution.
---
---Warning: only `.png` images
---@param image string Name of texture.
---@param tileWidth number Width of one tile
---@param tileHeight number Height of one tile
---@return TileSheet
function TileSheet:new(image, tileWidth, tileHeight)
local modPath = core.get_modpath(core.get_current_modname())
-- The canvas image must be in `<mod_name>/textures`, otherwise it will not be retrieved.
local width, height = getSizePng(modPath .. '/textures/' .. image)
if width % tileWidth ~= 0 or height % tileHeight ~= 0 then
error('Tilesheet does not have an integer number of tiles with resolution ' .. tostring(tileWidth) ..' * '.. tostring(tileHeight))
end
---@type TileSheet ---@type TileSheet
local instance = setmetatable({ local instance = setmetatable({
image = image, image = image,
width = width, width = mathRound(width / tileWidth),
height = height, height = mathRound(height / tileHeight),
}, {__index = self}) }, {__index = self})
-- TODO возможно будет лучше сделать автоматическое определение высоты и ширины по разрешению текстуры
return instance return instance
end end
@@ -26,7 +84,7 @@ end
---@param y number ---@param y number
---@return string ---@return string
function TileSheet:getTextureName(x, y) function TileSheet:getTextureName(x, y)
local textureName = self.image .. "^[sheet:".. self.width .. "x" .. self.height .. ":" .. x .. "," .. y local textureName = self.image .. '^[sheet:'.. self.width .. 'x' .. self.height .. ':' .. x .. ',' .. y
return textureName return textureName
end end

View File

@@ -16,7 +16,7 @@ local defaultRock = Core.Node.Class:getExtended(defaultRockDef)
local rockFactory = defaultRock:getFactory() local rockFactory = defaultRock:getFactory()
---@type TileSheet ---@type TileSheet
local ts = Core.TileSheet:getModClassInstance("rocks_sheet.png", 7, 15) local ts = Core.TileSheet:getModClassInstance("rocks_sheet.png", 16, 16)
rockFactory:registerNodesByShortDef({ rockFactory:registerNodesByShortDef({
{"sylite", "Sylite", nil, {ts:t(0, 0)}}, {"sylite", "Sylite", nil, {ts:t(0, 0)}},

View File

@@ -17,7 +17,7 @@ local defaultSoil = Core.Node.Class:getExtended(defaultSoilDef)
local soilFactory = defaultSoil:getFactory() local soilFactory = defaultSoil:getFactory()
---@type TileSheet ---@type TileSheet
local ts = Core.TileSheet:getModClassInstance("soils_sheet.png", 7, 1) local ts = Core.TileSheet:getModClassInstance("soils_sheet.png", 16, 16)
local clayTiles = { local clayTiles = {
ts:t(0,0) ts:t(0,0)