diff --git a/animation.lua b/animation.lua index 57ad9f6..33b84ea 100644 --- a/animation.lua +++ b/animation.lua @@ -58,8 +58,7 @@ end ---@param y number The Y position to draw at. function Animation:draw(index, x, y) local frame = self.frames[index] - local mesh = frame.atlas.meshes[frame.sprite] - love.graphics.draw(mesh, x, y) + frame.atlas:draw(frame.sprite, x, y) end return Animation diff --git a/atlas.lua b/atlas.lua index dc3253e..2a0ce2a 100644 --- a/atlas.lua +++ b/atlas.lua @@ -9,7 +9,9 @@ local Atlas = Object:extend() ---@param image love.Image The source image for the atlas. ---@param sw integer The width 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. self.image = image @@ -19,6 +21,12 @@ function Atlas:new(image, sw, sh) ---The height of each atlas's sprite. 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. self.iw = image:getPixelWidth() @@ -61,4 +69,13 @@ function Atlas:new(image, sw, sh) 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 diff --git a/font.lua b/font.lua index cdede0f..014fd72 100644 --- a/font.lua +++ b/font.lua @@ -32,7 +32,7 @@ function Font:draw(text, x, y) local sprite = self.characters[c] if sprite then 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 diff --git a/main.lua b/main.lua index 9736725..dfd1976 100644 --- a/main.lua +++ b/main.lua @@ -72,7 +72,7 @@ function love.draw() push:start() BlockyFont:draw("Hello, world!") - ActiveUnit:draw(100, 100) + ActiveUnit:draw(240, 180) UnitMenu:draw() if StateMenu then diff --git a/nine_slice.lua b/nine_slice.lua index 0b3a7a3..0c1f769 100644 --- a/nine_slice.lua +++ b/nine_slice.lua @@ -16,7 +16,7 @@ function NineSlice:draw(x, y, width, height) local gy = y + ty * sh if sprite then - love.graphics.draw(atlas.meshes[sprite], gx, gy) + atlas:draw(sprite, gx, gy) else love.graphics.rectangle("fill", gx, gy, sw, sh) end diff --git a/units.lua b/units.lua index 8005716..ef8d9bc 100644 --- a/units.lua +++ b/units.lua @@ -11,8 +11,8 @@ local function rowState(atlas, row, len, on_finish, next) return spannedState(atlas, start, len, on_finish, next) end -local leafRanger = Atlas(love.graphics.newImage("assets/leaf-ranger.png"), 288, 128) -local bringerOfDeath = Atlas(love.graphics.newImage("assets/bringer-of-death.png"), 140, 93) +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, 105, 93) return { {