implemented asset loading and animations
This commit is contained in:
parent
c3ef4ef1ca
commit
b25dfaa2de
30
animation.lua
Normal file
30
animation.lua
Normal 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
47
asset_bundle.lua
Normal 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
|
||||||
|
Before Width: | Height: | Size: 77 KiB After Width: | Height: | Size: 77 KiB |
BIN
assets/sprites/deer/walk_e.png
Normal file
BIN
assets/sprites/deer/walk_e.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 4.2 KiB |
BIN
assets/sprites/deer/walk_w.png
Normal file
BIN
assets/sprites/deer/walk_w.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 3.5 KiB |
BIN
deer_walking.png
Normal file
BIN
deer_walking.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 4.2 KiB |
BIN
deer_walking_l.png
Normal file
BIN
deer_walking_l.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 3.5 KiB |
13
entity.lua
Normal file
13
entity.lua
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
local __Entity = {
|
||||||
|
sprite = {},
|
||||||
|
position = Vec3 {},
|
||||||
|
velocity = Vec3 {}
|
||||||
|
}
|
||||||
|
|
||||||
|
function __Entity:update()
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
local __Sprite = {
|
||||||
|
|
||||||
|
}
|
||||||
18
main.lua
18
main.lua
@ -31,8 +31,6 @@ end
|
|||||||
function Player:init()
|
function Player:init()
|
||||||
self.position = vec2(0, 0)
|
self.position = vec2(0, 0)
|
||||||
self.velocity = vec2(0, 0)
|
self.velocity = vec2(0, 0)
|
||||||
|
|
||||||
self.sprite = love.graphics.newImage("player.png")
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function Player:update(dt)
|
function Player:update(dt)
|
||||||
@ -63,16 +61,30 @@ function Player:update(dt)
|
|||||||
-- print(vec2_length(self.velocity))
|
-- print(vec2_length(self.velocity))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
require "animation"
|
||||||
|
require "asset_bundle"
|
||||||
|
|
||||||
|
AssetBundle:load()
|
||||||
|
|
||||||
|
|
||||||
function love.load()
|
function love.load()
|
||||||
Player:init()
|
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
|
end
|
||||||
|
|
||||||
function love.update(dt)
|
function love.update(dt)
|
||||||
Player:update(dt)
|
Player:update(dt)
|
||||||
|
animation_r:update(dt)
|
||||||
|
animation_l:update(dt)
|
||||||
end
|
end
|
||||||
|
|
||||||
function love.draw()
|
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
|
end
|
||||||
|
|
||||||
function love.conf(t)
|
function love.conf(t)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user