animated deer demo

This commit is contained in:
Ivan Yuriev 2025-03-26 14:44:39 +03:00
parent 2d4221ecd2
commit 5d1c31cc9c
12 changed files with 86 additions and 85 deletions

View File

Before

Width:  |  Height:  |  Size: 77 KiB

After

Width:  |  Height:  |  Size: 77 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 4.2 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.5 KiB

View File

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

View File

@ -7,6 +7,8 @@ local __Animation = {
function Animation(image, width, height)
local animation = {}
local width = width or 0
local height = height or 0
animation.spriteSheet = image;
animation.quads = {};
@ -19,7 +21,7 @@ function Animation(image, width, height)
return setmetatable(animation, { __index = __Animation })
end
function __Animation:getFrame()
function __Animation:getQuad()
local frametime = 1 / self.fps
local frame = math.floor(self.currentTime / frametime)
return self.quads[frame + 1]
@ -28,3 +30,7 @@ end
function __Animation:update(dt)
self.currentTime = (self.currentTime + dt) % (#self.quads / self.fps)
end
function __Animation:reset()
self.currentTime = 0
end

View File

@ -1,4 +1,4 @@
require "option.option"
require "lib.option"
AssetBundle = {
root = "/assets",

23
lib/entity.lua Normal file
View File

@ -0,0 +1,23 @@
require 'lib.sprite'
require 'lib.vec3'
__Entity = {
id = nil,
sprite = __AnimatedSprite,
position = Vec3 {},
velocity = Vec3 {},
}
function Entity(id)
local t = {
id = id,
sprite = AnimatedSprite(id),
}
return setmetatable(t, { __index = __Entity })
end
function __Entity:update(dt)
if self.sprite then
self.sprite:update(dt)
end
end

35
lib/sprite.lua Normal file
View File

@ -0,0 +1,35 @@
require 'lib.asset_bundle'
require 'lib.animation'
__AnimatedSprite = {}
function AnimatedSprite(id)
local table = {}
local bundle = AssetBundle.files.sprites[id]
for key, value in pairs(bundle) do
table[key] = Animation(value, 64, 64)
end
return setmetatable(table, { __index = __AnimatedSprite })
end
function __AnimatedSprite:update(dt)
if self.playing then
self[self.playing]:update(dt)
end
end
function __AnimatedSprite:getQuad()
if self.playing then
return self[self.playing]:getQuad()
end
return nil
end
function __AnimatedSprite:getTexture()
if self.playing then
return self[self.playing].spriteSheet
end
return nil
end

View File

@ -1,4 +1,4 @@
require "option.option"
require "lib.option"
__Vec3 = {
_x = 0,

View File

@ -61,30 +61,35 @@ function Player:update(dt)
-- print(vec2_length(self.velocity))
end
require "animation"
require "asset_bundle"
require "lib.asset_bundle"
require "lib.entity"
AssetBundle:load()
Deer = nil
local tick = 0
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)
AssetBundle:load()
Deer = Entity('deer')
Deer.sprite.playing = "walk_e"
end
function love.update(dt)
Player:update(dt)
animation_r:update(dt)
animation_l:update(dt)
Deer:update(dt)
tick = tick + 1
if (tick % 100 == 0) then
if (Deer.sprite.playing == "walk_e") then
Deer.sprite.playing = "walk_w"
else
Deer.sprite.playing = "walk_e"
end
tick = 0
end
end
function love.draw()
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)
love.graphics.draw(Deer.sprite:getTexture(), Deer.sprite:getQuad(), 200, 200, 0, 4, 4)
end
function love.conf(t)

View File

@ -1,55 +0,0 @@
--- Tests
local none = None
local none1 = None
local some = Some(42)
local some1 = Some('foo')
assert(none:is_none())
assert(none1 == none)
assert(some:is_some())
assert(some ~= some1)
assert(some:unwrap(43) == 42)
assert(some:unwrap() == 42)
assert(none:unwrap(43) == 43)
assert(some:try(
function(x)
return x + 1
end
) == 43)
assert(none:try(
function(x)
return x + 1
end
) == None)
assert(tostring(some) == "Some(42)")
assert(tostring(none) == "None")
--- Usage
local function safe_div(a, b)
if (b == 0) then return None end
return Some(a / b)
end
local res = safe_div(10, 2)
assert(res:is_some())
local res1 = safe_div(10, 0)
assert(res1:is_none())
local chain = safe_div(10, 2):try(
function(x)
return safe_div(x, 5)
end
)
assert(chain:unwrap(0) == 1)
local chain1 = safe_div(10, 0):try(
function(x)
return safe_div(x, 5)
end
)
assert(chain1:unwrap(0) == 0)