Implement Atlas anchors

This commit is contained in:
mars 2023-04-20 21:11:56 -04:00
parent e4b65b2fd5
commit 886ad221a0
6 changed files with 24 additions and 8 deletions

View File

@ -58,8 +58,7 @@ end
---@param y number The Y position to draw at. ---@param y number The Y position to draw at.
function Animation:draw(index, x, y) function Animation:draw(index, x, y)
local frame = self.frames[index] local frame = self.frames[index]
local mesh = frame.atlas.meshes[frame.sprite] frame.atlas:draw(frame.sprite, x, y)
love.graphics.draw(mesh, x, y)
end end
return Animation return Animation

View File

@ -9,7 +9,9 @@ local Atlas = Object:extend()
---@param image love.Image The source image for the atlas. ---@param image love.Image The source image for the atlas.
---@param sw integer The width of each sprite. ---@param sw integer The width of each sprite.
---@param sh integer The height of each sprite. ---@param sh integer The height of each sprite.
function Atlas:new(image, sw, sh) ---@param ax integer? The anchor X coordinate of each sprite. Defaults to 0.
---@param ay integer? The anchor Y coordinate of each sprite. Defaults to 0.
function Atlas:new(image, sw, sh, ax, ay)
---The source image of the atlas. ---The source image of the atlas.
self.image = image self.image = image
@ -19,6 +21,12 @@ function Atlas:new(image, sw, sh)
---The height of each atlas's sprite. ---The height of each atlas's sprite.
self.sh = sh self.sh = sh
---The anchor X coordinate of each sprite.
self.ax = ax or 0
---The anchor Y coordinate of each sprite.
self.ay = ay or 0
---The width of the atlas in texels. ---The width of the atlas in texels.
self.iw = image:getPixelWidth() self.iw = image:getPixelWidth()
@ -61,4 +69,13 @@ function Atlas:new(image, sw, sh)
end end
end end
---Draws a sprite at a given location.
---@param sprite integer The index of the sprite to draw.
---@param x integer The X coordinate to draw at.
---@param y integer The Y coordinate to draw at.
function Atlas:draw(sprite, x, y)
local mesh = self.meshes[sprite]
love.graphics.draw(mesh, x - self.ax, y - self.ay)
end
return Atlas return Atlas

View File

@ -32,7 +32,7 @@ function Font:draw(text, x, y)
local sprite = self.characters[c] local sprite = self.characters[c]
if sprite then if sprite then
local x = (i - 1) * self.sw + x local x = (i - 1) * self.sw + x
love.graphics.draw(self.meshes[sprite], x, y) Font.super.draw(self, sprite, x, y)
end end
end end
end end

View File

@ -72,7 +72,7 @@ function love.draw()
push:start() push:start()
BlockyFont:draw("Hello, world!") BlockyFont:draw("Hello, world!")
ActiveUnit:draw(100, 100) ActiveUnit:draw(240, 180)
UnitMenu:draw() UnitMenu:draw()
if StateMenu then if StateMenu then

View File

@ -16,7 +16,7 @@ function NineSlice:draw(x, y, width, height)
local gy = y + ty * sh local gy = y + ty * sh
if sprite then if sprite then
love.graphics.draw(atlas.meshes[sprite], gx, gy) atlas:draw(sprite, gx, gy)
else else
love.graphics.rectangle("fill", gx, gy, sw, sh) love.graphics.rectangle("fill", gx, gy, sw, sh)
end end

View File

@ -11,8 +11,8 @@ local function rowState(atlas, row, len, on_finish, next)
return spannedState(atlas, start, len, on_finish, next) return spannedState(atlas, start, len, on_finish, next)
end end
local leafRanger = Atlas(love.graphics.newImage("assets/leaf-ranger.png"), 288, 128) local leafRanger = Atlas(love.graphics.newImage("assets/leaf-ranger.png"), 288, 128, 144, 128)
local bringerOfDeath = Atlas(love.graphics.newImage("assets/bringer-of-death.png"), 140, 93) local bringerOfDeath = Atlas(love.graphics.newImage("assets/bringer-of-death.png"), 140, 93, 105, 93)
return { return {
{ {