Compare commits

...

4 Commits

Author SHA1 Message Date
mars c450fc0efb Display animated Leaf Ranger 2023-04-16 17:56:11 -04:00
mars c9dfd13823 Add leaf-ranger.png 2023-04-16 17:21:14 -04:00
mars 8619098d43 Add font.lua 2023-04-16 17:19:45 -04:00
mars 75f626bf50 Display font characters in a nice grid 2023-04-16 16:45:55 -04:00
5 changed files with 79 additions and 7 deletions

View File

@ -1,3 +1,4 @@
- `assets/leaf-ranger.png` is licensed under CC-BY 4.0 and was created by chierit (source: https://chierit.itch.io/elementals-leaf-ranger)
- `assets/small-blocky-font.png` is licensed under CC-BY 3.0 and was created by Jerom (source: https://opengameart.org/content/small-blocky-font)
- `lib/classic.lua` is licensed under the MIT license and was created by rxi (source: https://raw.githubusercontent.com/rxi/classic/master/classic.lua)
- `lib/push.lua` is licensed under the MIT license and was created by Ulysse Ramage (source: https://github.com/Ulydev/push)

BIN
assets/leaf-ranger.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 235 KiB

View File

@ -5,7 +5,7 @@ local Object = require "lib/classic"
local Atlas = Object:extend()
---Creates a new atlas.
---@param image love.Image
---@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)

39
font.lua Normal file
View File

@ -0,0 +1,39 @@
local Atlas = require "atlas"
---A set of character sprites that can be drawn as strings.
local Font = Atlas:extend()
---Creates a new font.
---@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.
---@param characters string[] A list of characters with indices corresponding to each sprite.
function Font:new(image, sw, sh, characters)
Font.super.new(self, image, sw, sh)
---A map of characters to sprites.
self.characters = {}
for sprite, ch in ipairs(characters) do
if ch then
self.characters[ch] = sprite
end
end
end
---Draws a string with this font.
function Font:draw(text, x, y)
local x = x or 0
local y = y or 0
for i = 1, #text do
local c = text:sub(i, i)
local sprite = self.characters[c]
if sprite then
local x = (i - 1) * self.sw + x
love.graphics.draw(self.meshes[sprite], x, y)
end
end
end
return Font

View File

@ -1,9 +1,10 @@
local Atlas = require "atlas"
local Font = require "font"
local push = require "lib/push"
local gameWidth, gameHeight = 320, 200
local windowWidth, windowHeight = love.window.getDesktopDimensions()
windowWidth, windowHeight = windowWidth*.7, windowHeight*.7 --make the window a bit smaller than the screen itself
windowWidth, windowHeight = windowWidth * .7, windowHeight * .7 --make the window a bit smaller than the screen itself
local pushOpts = {
fullscreen = false,
resizable = true,
@ -79,22 +80,53 @@ end
function love.load()
Menu = MultiMenu({ "Fight", "Use", "Pkmn", "Flee" })
local font = love.graphics.newImage("assets/small-blocky-font.png")
Font = Atlas(font, 8, 8)
local blockyFont = love.graphics.newImage("assets/small-blocky-font.png")
local blockFontChars = {
" ", "!", "\"", "#", "$", "%", "&", "'", "(", ")", "×", "+", ",", "-", ".", "/", --first line
"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", ":", ";", "<", "=", ">", "?", --second line
"@", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", --third line
"p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "[", "\\", "]", "^", "_", --fourth line
"@", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", --third line
"P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "{", "|", "}", "~", --fourth line
}
BlockyFont = Font(blockyFont, 8, 8, blockFontChars)
local leafRanger = love.graphics.newImage("assets/leaf-ranger.png")
LeafRanger = {
atlas = Atlas(leafRanger, 288, 128),
step = 0,
duration = 1.0 / 15.0,
frame = 1,
max = 12,
}
end
function love.draw()
push:start()
Menu:draw()
BlockyFont:draw("Hello, world!")
for sprite = 1,#Font.meshes do
love.graphics.draw(Font.meshes[sprite], sprite * 10 + 100, 100)
end
love.graphics.draw(LeafRanger.atlas.meshes[LeafRanger.frame])
push:finish()
end
function love.update(dt)
LeafRanger.step = LeafRanger.step + dt
if LeafRanger.step > LeafRanger.duration then
LeafRanger.step = 0
LeafRanger.frame = LeafRanger.frame + 1
if LeafRanger.frame > LeafRanger.max then
LeafRanger.frame = 1
end
end
end
function love.keypressed(key)
if key == "up" then
Menu:up()