animated fox walk demo
BIN
assets/sprites/fox/walk_e.png
Normal file
|
After Width: | Height: | Size: 21 KiB |
BIN
assets/sprites/fox/walk_n.png
Normal file
|
After Width: | Height: | Size: 20 KiB |
BIN
assets/sprites/fox/walk_ne.png
Normal file
|
After Width: | Height: | Size: 22 KiB |
BIN
assets/sprites/fox/walk_nw.png
Normal file
|
After Width: | Height: | Size: 23 KiB |
BIN
assets/sprites/fox/walk_s.png
Normal file
|
After Width: | Height: | Size: 21 KiB |
BIN
assets/sprites/fox/walk_se.png
Normal file
|
After Width: | Height: | Size: 24 KiB |
BIN
assets/sprites/fox/walk_sw.png
Normal file
|
After Width: | Height: | Size: 22 KiB |
BIN
assets/sprites/fox/walk_w.png
Normal file
|
After Width: | Height: | Size: 21 KiB |
8
lib/camera.lua
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
require 'lib.entity'
|
||||||
|
|
||||||
|
__Camera = {
|
||||||
|
pivotPosition = Vec3 {},
|
||||||
|
position = Vec3 {},
|
||||||
|
velocity = Vec3 {},
|
||||||
|
fixed = false,
|
||||||
|
}
|
||||||
@ -6,9 +6,9 @@ __Entity = {
|
|||||||
sprite = __AnimatedSprite,
|
sprite = __AnimatedSprite,
|
||||||
position = Vec3 {},
|
position = Vec3 {},
|
||||||
velocity = Vec3 {},
|
velocity = Vec3 {},
|
||||||
direction = 0, -- clockwise radians
|
rotation = 0, -- clockwise radians
|
||||||
friction = 0.98,
|
friction = 0.98,
|
||||||
speed = 1
|
speed = 1 -- m/s
|
||||||
}
|
}
|
||||||
|
|
||||||
function Entity(id)
|
function Entity(id)
|
||||||
@ -32,20 +32,28 @@ function __Entity:processMovement()
|
|||||||
end
|
end
|
||||||
|
|
||||||
function __Entity:namedDirection()
|
function __Entity:namedDirection()
|
||||||
|
local get_direction_index = function(rotation)
|
||||||
|
local pi = math.pi
|
||||||
|
rotation = rotation % (2 * pi)
|
||||||
|
local shifted = (rotation + pi / 8) % (2 * pi)
|
||||||
|
local index = math.floor(shifted / (pi / 4)) + 1
|
||||||
|
return index
|
||||||
|
end
|
||||||
|
|
||||||
local lookup = {
|
local lookup = {
|
||||||
"e",
|
"e",
|
||||||
"se",
|
"ne",
|
||||||
"s",
|
|
||||||
"sw",
|
|
||||||
"w",
|
|
||||||
"nw",
|
|
||||||
"n",
|
"n",
|
||||||
"ne"
|
"nw",
|
||||||
|
"w",
|
||||||
|
"sw",
|
||||||
|
"s",
|
||||||
|
"se",
|
||||||
}
|
}
|
||||||
local idx = math.floor((4 * self.direction / math.pi) + math.pi / 8) % 8
|
|
||||||
return lookup[idx + 1]
|
return lookup[get_direction_index(self.rotation)]
|
||||||
end
|
end
|
||||||
|
|
||||||
function __Entity:lookAt(vec)
|
function __Entity:lookAt(vec)
|
||||||
self.direction = (vec - self.position):direction()
|
self.rotation = (vec - self.position):direction()
|
||||||
end
|
end
|
||||||
|
|||||||
@ -8,7 +8,7 @@ function AnimatedSprite(id)
|
|||||||
local table = {}
|
local table = {}
|
||||||
local bundle = AssetBundle.files.sprites[id]
|
local bundle = AssetBundle.files.sprites[id]
|
||||||
for key, value in pairs(bundle) do
|
for key, value in pairs(bundle) do
|
||||||
table[key] = Animation(value, 64, 64)
|
table[key] = Animation(value, 96, 96)
|
||||||
end
|
end
|
||||||
|
|
||||||
return setmetatable(table, { __index = __AnimatedSprite })
|
return setmetatable(table, { __index = __AnimatedSprite })
|
||||||
|
|||||||
@ -54,7 +54,7 @@ function __Vec3:normalize()
|
|||||||
end
|
end
|
||||||
|
|
||||||
function __Vec3:direction()
|
function __Vec3:direction()
|
||||||
return math.atan2(self.y, self.x)
|
return -math.atan2(self.y, self.x)
|
||||||
end
|
end
|
||||||
|
|
||||||
function __Vec3:dot(other)
|
function __Vec3:dot(other)
|
||||||
|
|||||||
28
main.lua
@ -34,7 +34,7 @@ function Player:init()
|
|||||||
end
|
end
|
||||||
|
|
||||||
function Player:update(dt)
|
function Player:update(dt)
|
||||||
local speed = 30
|
local speed = 10
|
||||||
local friction = 0.05
|
local friction = 0.05
|
||||||
local acc = vec2(0, 0)
|
local acc = vec2(0, 0)
|
||||||
|
|
||||||
@ -66,31 +66,35 @@ require "lib.entity"
|
|||||||
require "lib.vec3"
|
require "lib.vec3"
|
||||||
|
|
||||||
|
|
||||||
Deer = nil
|
Fox = nil
|
||||||
|
|
||||||
function love.load()
|
function love.load()
|
||||||
AssetBundle:load()
|
AssetBundle:load()
|
||||||
Deer = Entity('deer')
|
Player:init();
|
||||||
Deer.position = Vec3 { 200, 200, 0 }
|
|
||||||
Deer.sprite.playing = "walk_e"
|
Fox = Entity('fox')
|
||||||
|
Fox.position = Vec3 { 200, 200, 0 }
|
||||||
|
Fox.sprite.playing = "walk_ne"
|
||||||
end
|
end
|
||||||
|
|
||||||
local lastDir
|
local lastDir
|
||||||
function love.update(dt)
|
function love.update(dt)
|
||||||
local mouse = Vec3 { love.mouse.getPosition() }
|
local mouse = Vec3 { love.mouse.getPosition() }
|
||||||
if (mouse) then Deer:lookAt(mouse) end
|
if (mouse) then Fox:lookAt(mouse) end
|
||||||
|
|
||||||
if (lastDir and lastDir ~= Deer:namedDirection()) then
|
if (lastDir and lastDir ~= Fox:namedDirection()) then
|
||||||
print(Deer:namedDirection())
|
Fox.sprite.playing = "walk_" .. Fox:namedDirection()
|
||||||
end
|
end
|
||||||
|
|
||||||
lastDir = Deer:namedDirection()
|
lastDir = Fox:namedDirection()
|
||||||
Deer:update(dt)
|
Player:update(dt)
|
||||||
|
Fox.position = Vec3 { Player.position().x, Player.position().y, 0 }
|
||||||
|
Fox:update(dt)
|
||||||
end
|
end
|
||||||
|
|
||||||
function love.draw()
|
function love.draw()
|
||||||
love.graphics.draw(Deer.sprite:getTexture(), Deer.sprite:getQuad(), Deer.position.x, Deer.position.y, Deer.direction,
|
love.graphics.draw(Fox.sprite:getTexture(), Fox.sprite:getQuad(), Fox.position.x, Fox.position.y, Fox.direction,
|
||||||
4, 4, 32, 32)
|
2, 2, 48, 48)
|
||||||
end
|
end
|
||||||
|
|
||||||
function love.conf(t)
|
function love.conf(t)
|
||||||
|
|||||||