animated deer demo
This commit is contained in:
parent
2d4221ecd2
commit
5d1c31cc9c
|
Before Width: | Height: | Size: 77 KiB After Width: | Height: | Size: 77 KiB |
BIN
deer_walking.png
BIN
deer_walking.png
Binary file not shown.
|
Before Width: | Height: | Size: 4.2 KiB |
Binary file not shown.
|
Before Width: | Height: | Size: 3.5 KiB |
13
entity.lua
13
entity.lua
@ -1,13 +0,0 @@
|
|||||||
local __Entity = {
|
|
||||||
sprite = {},
|
|
||||||
position = Vec3 {},
|
|
||||||
velocity = Vec3 {}
|
|
||||||
}
|
|
||||||
|
|
||||||
function __Entity:update()
|
|
||||||
|
|
||||||
end
|
|
||||||
|
|
||||||
local __Sprite = {
|
|
||||||
|
|
||||||
}
|
|
||||||
@ -7,6 +7,8 @@ local __Animation = {
|
|||||||
|
|
||||||
function Animation(image, width, height)
|
function Animation(image, width, height)
|
||||||
local animation = {}
|
local animation = {}
|
||||||
|
local width = width or 0
|
||||||
|
local height = height or 0
|
||||||
animation.spriteSheet = image;
|
animation.spriteSheet = image;
|
||||||
animation.quads = {};
|
animation.quads = {};
|
||||||
|
|
||||||
@ -19,7 +21,7 @@ function Animation(image, width, height)
|
|||||||
return setmetatable(animation, { __index = __Animation })
|
return setmetatable(animation, { __index = __Animation })
|
||||||
end
|
end
|
||||||
|
|
||||||
function __Animation:getFrame()
|
function __Animation:getQuad()
|
||||||
local frametime = 1 / self.fps
|
local frametime = 1 / self.fps
|
||||||
local frame = math.floor(self.currentTime / frametime)
|
local frame = math.floor(self.currentTime / frametime)
|
||||||
return self.quads[frame + 1]
|
return self.quads[frame + 1]
|
||||||
@ -28,3 +30,7 @@ end
|
|||||||
function __Animation:update(dt)
|
function __Animation:update(dt)
|
||||||
self.currentTime = (self.currentTime + dt) % (#self.quads / self.fps)
|
self.currentTime = (self.currentTime + dt) % (#self.quads / self.fps)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function __Animation:reset()
|
||||||
|
self.currentTime = 0
|
||||||
|
end
|
||||||
@ -1,4 +1,4 @@
|
|||||||
require "option.option"
|
require "lib.option"
|
||||||
|
|
||||||
AssetBundle = {
|
AssetBundle = {
|
||||||
root = "/assets",
|
root = "/assets",
|
||||||
23
lib/entity.lua
Normal file
23
lib/entity.lua
Normal 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
35
lib/sprite.lua
Normal 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
|
||||||
@ -1,4 +1,4 @@
|
|||||||
require "option.option"
|
require "lib.option"
|
||||||
|
|
||||||
__Vec3 = {
|
__Vec3 = {
|
||||||
_x = 0,
|
_x = 0,
|
||||||
33
main.lua
33
main.lua
@ -61,30 +61,35 @@ function Player:update(dt)
|
|||||||
-- print(vec2_length(self.velocity))
|
-- print(vec2_length(self.velocity))
|
||||||
end
|
end
|
||||||
|
|
||||||
require "animation"
|
require "lib.asset_bundle"
|
||||||
require "asset_bundle"
|
require "lib.entity"
|
||||||
|
|
||||||
AssetBundle:load()
|
|
||||||
|
|
||||||
|
Deer = nil
|
||||||
|
local tick = 0
|
||||||
|
|
||||||
function love.load()
|
function love.load()
|
||||||
Player:init()
|
AssetBundle:load()
|
||||||
local sheet_r = AssetBundle.files.sprites.deer.walk_e
|
Deer = Entity('deer')
|
||||||
local sheet_l = AssetBundle.files.sprites.deer.walk_w
|
Deer.sprite.playing = "walk_e"
|
||||||
|
|
||||||
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)
|
Deer:update(dt)
|
||||||
animation_r:update(dt)
|
tick = tick + 1
|
||||||
animation_l:update(dt)
|
|
||||||
|
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
|
end
|
||||||
|
|
||||||
function love.draw()
|
function love.draw()
|
||||||
local animation = Player.velocity().x > 0 and animation_r or animation_l
|
love.graphics.draw(Deer.sprite:getTexture(), Deer.sprite:getQuad(), 200, 200, 0, 4, 4)
|
||||||
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)
|
||||||
|
|||||||
@ -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)
|
|
||||||
Loading…
x
Reference in New Issue
Block a user