implemented asset loading and animations

This commit is contained in:
Ivan Yuriev 2025-03-24 23:03:45 +03:00
parent c3ef4ef1ca
commit b25dfaa2de
9 changed files with 106 additions and 4 deletions

30
animation.lua Normal file
View File

@ -0,0 +1,30 @@
local __Animation = {
spriteSheet = nil,
quads = nil,
fps = 12,
currentTime = 0
}
function Animation(image, width, height)
local animation = {}
animation.spriteSheet = image;
animation.quads = {};
for y = 0, image:getHeight() - height, height do
for x = 0, image:getWidth() - width, width do
table.insert(animation.quads, love.graphics.newQuad(x, y, width, height, image:getDimensions()))
end
end
return setmetatable(animation, { __index = __Animation })
end
function __Animation:getFrame()
local frametime = 1 / self.fps
local frame = math.floor(self.currentTime / frametime)
return self.quads[frame + 1]
end
function __Animation:update(dt)
self.currentTime = (self.currentTime + dt) % (#self.quads / self.fps)
end

47
asset_bundle.lua Normal file
View File

@ -0,0 +1,47 @@
require "option.option"
AssetBundle = {
root = "/assets",
files = {}
}
function AssetBundle:load()
local function enumerate(path)
local tree = {}
local contents = love.filesystem.getDirectoryItems(path)
for _, v in pairs(contents) do
local newPath = path .. "/" .. v
local type = love.filesystem.getInfo(newPath).type
if type == "file" then
local data = self.loadFile(newPath)
if data:is_some() then
tree[self.cutExtension(v)] = data:unwrap()
end
end
if type == "directory" then
tree[v] = enumerate(newPath)
end
end
return tree
end
self.files = enumerate(self.root)
end
function AssetBundle.loadFile(path)
local filedata = love.filesystem.newFileData(path)
local ext = filedata:getExtension()
if (ext == "png") then
local img = love.graphics.newImage(path)
img:setFilter("nearest", "nearest")
return
Some(img)
end
return None
end
function AssetBundle.cutExtension(filename)
return string.match(filename, '(.+)%.(.+)')
end

View File

Before

Width:  |  Height:  |  Size: 77 KiB

After

Width:  |  Height:  |  Size: 77 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.5 KiB

BIN
deer_walking.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.2 KiB

BIN
deer_walking_l.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.5 KiB

13
entity.lua Normal file
View File

@ -0,0 +1,13 @@
local __Entity = {
sprite = {},
position = Vec3 {},
velocity = Vec3 {}
}
function __Entity:update()
end
local __Sprite = {
}

View File

@ -31,8 +31,6 @@ end
function Player:init()
self.position = vec2(0, 0)
self.velocity = vec2(0, 0)
self.sprite = love.graphics.newImage("player.png")
end
function Player:update(dt)
@ -63,18 +61,32 @@ function Player:update(dt)
-- print(vec2_length(self.velocity))
end
require "animation"
require "asset_bundle"
AssetBundle:load()
function love.load()
Player:init()
local sheet_r = AssetBundle.files.sprites.deer.walk_e
local sheet_l = AssetBundle.files.sprites.deer.walk_w
animation_r = Animation(sheet_r, 64, 64)
animation_l = Animation(sheet_l, 64, 64)
end
function love.update(dt)
Player:update(dt)
animation_r:update(dt)
animation_l:update(dt)
end
function love.draw()
love.graphics.draw(Player.sprite, Player.position().x, Player.position().y)
local animation = Player.velocity().x > 0 and animation_r or animation_l
love.graphics.draw(animation.spriteSheet, animation:getFrame(), Player.position().x, Player.position().y, 0, 4, 4)
end
function love.conf(t)
t.console = true
end
end