animated fox walk demo

This commit is contained in:
Ivan Yuriev 2025-04-06 02:53:01 +03:00
parent 8c39f16be3
commit e8ef33e91e
13 changed files with 45 additions and 25 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 21 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 23 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 21 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 24 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 21 KiB

8
lib/camera.lua Normal file
View File

@ -0,0 +1,8 @@
require 'lib.entity'
__Camera = {
pivotPosition = Vec3 {},
position = Vec3 {},
velocity = Vec3 {},
fixed = false,
}

View File

@ -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

View File

@ -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 })

View File

@ -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)

View File

@ -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)