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,
|
||||
position = Vec3 {},
|
||||
velocity = Vec3 {},
|
||||
direction = 0, -- clockwise radians
|
||||
rotation = 0, -- clockwise radians
|
||||
friction = 0.98,
|
||||
speed = 1
|
||||
speed = 1 -- m/s
|
||||
}
|
||||
|
||||
function Entity(id)
|
||||
@ -32,20 +32,28 @@ function __Entity:processMovement()
|
||||
end
|
||||
|
||||
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 = {
|
||||
"e",
|
||||
"se",
|
||||
"s",
|
||||
"sw",
|
||||
"w",
|
||||
"nw",
|
||||
"ne",
|
||||
"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
|
||||
|
||||
function __Entity:lookAt(vec)
|
||||
self.direction = (vec - self.position):direction()
|
||||
self.rotation = (vec - self.position):direction()
|
||||
end
|
||||
|
||||
@ -8,7 +8,7 @@ function AnimatedSprite(id)
|
||||
local table = {}
|
||||
local bundle = AssetBundle.files.sprites[id]
|
||||
for key, value in pairs(bundle) do
|
||||
table[key] = Animation(value, 64, 64)
|
||||
table[key] = Animation(value, 96, 96)
|
||||
end
|
||||
|
||||
return setmetatable(table, { __index = __AnimatedSprite })
|
||||
|
||||
@ -54,7 +54,7 @@ function __Vec3:normalize()
|
||||
end
|
||||
|
||||
function __Vec3:direction()
|
||||
return math.atan2(self.y, self.x)
|
||||
return -math.atan2(self.y, self.x)
|
||||
end
|
||||
|
||||
function __Vec3:dot(other)
|
||||
|
||||
28
main.lua
@ -34,7 +34,7 @@ function Player:init()
|
||||
end
|
||||
|
||||
function Player:update(dt)
|
||||
local speed = 30
|
||||
local speed = 10
|
||||
local friction = 0.05
|
||||
local acc = vec2(0, 0)
|
||||
|
||||
@ -66,31 +66,35 @@ require "lib.entity"
|
||||
require "lib.vec3"
|
||||
|
||||
|
||||
Deer = nil
|
||||
Fox = nil
|
||||
|
||||
function love.load()
|
||||
AssetBundle:load()
|
||||
Deer = Entity('deer')
|
||||
Deer.position = Vec3 { 200, 200, 0 }
|
||||
Deer.sprite.playing = "walk_e"
|
||||
Player:init();
|
||||
|
||||
Fox = Entity('fox')
|
||||
Fox.position = Vec3 { 200, 200, 0 }
|
||||
Fox.sprite.playing = "walk_ne"
|
||||
end
|
||||
|
||||
local lastDir
|
||||
function love.update(dt)
|
||||
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
|
||||
print(Deer:namedDirection())
|
||||
if (lastDir and lastDir ~= Fox:namedDirection()) then
|
||||
Fox.sprite.playing = "walk_" .. Fox:namedDirection()
|
||||
end
|
||||
|
||||
lastDir = Deer:namedDirection()
|
||||
Deer:update(dt)
|
||||
lastDir = Fox:namedDirection()
|
||||
Player:update(dt)
|
||||
Fox.position = Vec3 { Player.position().x, Player.position().y, 0 }
|
||||
Fox:update(dt)
|
||||
end
|
||||
|
||||
function love.draw()
|
||||
love.graphics.draw(Deer.sprite:getTexture(), Deer.sprite:getQuad(), Deer.position.x, Deer.position.y, Deer.direction,
|
||||
4, 4, 32, 32)
|
||||
love.graphics.draw(Fox.sprite:getTexture(), Fox.sprite:getQuad(), Fox.position.x, Fox.position.y, Fox.direction,
|
||||
2, 2, 48, 48)
|
||||
end
|
||||
|
||||
function love.conf(t)
|
||||
|
||||