rotating deer demo

This commit is contained in:
Ivan Yuriev 2025-03-26 21:54:44 +03:00
parent 5d1c31cc9c
commit 8c39f16be3
4 changed files with 65 additions and 29 deletions

View File

@ -6,6 +6,9 @@ __Entity = {
sprite = __AnimatedSprite,
position = Vec3 {},
velocity = Vec3 {},
direction = 0, -- clockwise radians
friction = 0.98,
speed = 1
}
function Entity(id)
@ -17,7 +20,32 @@ function Entity(id)
end
function __Entity:update(dt)
self:processMovement()
if self.sprite then
self.sprite:update(dt)
end
end
function __Entity:processMovement()
self.position = self.position + self.velocity
self.velocity = self.velocity * self.friction
end
function __Entity:namedDirection()
local lookup = {
"e",
"se",
"s",
"sw",
"w",
"nw",
"n",
"ne"
}
local idx = math.floor((4 * self.direction / math.pi) + math.pi / 8) % 8
return lookup[idx + 1]
end
function __Entity:lookAt(vec)
self.direction = (vec - self.position):direction()
end

View File

@ -31,5 +31,5 @@ function __AnimatedSprite:getTexture()
if self.playing then
return self[self.playing].spriteSheet
end
return nil
return AssetBundle.files.sprites.fallback
end

View File

@ -1,16 +1,16 @@
require "lib.option"
__Vec3 = {
_x = 0,
_y = 0,
_z = 0,
x = 0,
y = 0,
z = 0,
}
function Vec3(vec)
return setmetatable({
_x = vec[1] or 0,
_y = vec[2] or 0,
_z = vec[3] or 0,
x = vec[1] or 0,
y = vec[2] or 0,
z = vec[3] or 0,
}, {
__index = __Vec3,
__tostring = __Vec3.__tostring,
@ -19,41 +19,48 @@ function Vec3(vec)
__unm = function(self)
return __Vec3.scale(self, -1)
end,
__sub = function(self, other)
return self + -other
end,
__eq = function(self, other)
return
self._x == other._x
and self._y == other._y
and self._z == other._z
self.x == other.x
and self.y == other.y
and self.z == other.z
end,
})
end
function __Vec3:add(other)
Vec3 { self._x + other._x, self._y + other._y, self._z + other._z }
return Vec3 { self.x + other.x, self.y + other.y, self.z + other.z }
end
function __Vec3:scale(factor)
return Vec3 { self._x * factor, self._y * factor, self._z * factor }
return Vec3 { self.x * factor, self.y * factor, self.z * factor }
end
function __Vec3:length()
return math.sqrt(self._x ^ 2 + self._y ^ 2 + self._z ^ 2)
return math.sqrt(self.x ^ 2 + self.y ^ 2 + self.z ^ 2)
end
function __Vec3:normalize()
local length = self:length()
if not length then return None end
return Some(Vec3 {
self._x / length,
self._y / length,
self._z / length
self.x / length,
self.y / length,
self.z / length
})
end
function __Vec3:direction()
return math.atan2(self.y, self.x)
end
function __Vec3:dot(other)
return self._x * other._x + self._y * other._y + self._z * other._z
return self.x * other.x + self.y * other.y + self.z * other.z
end
function __Vec3:__tostring()
return "Vec3{" .. self._x .. ", " .. self._y .. ", " .. self._z .. "}"
return "Vec3{" .. self.x .. ", " .. self.y .. ", " .. self.z .. "}"
end

View File

@ -63,33 +63,34 @@ end
require "lib.asset_bundle"
require "lib.entity"
require "lib.vec3"
Deer = nil
local tick = 0
function love.load()
AssetBundle:load()
Deer = Entity('deer')
Deer.position = Vec3 { 200, 200, 0 }
Deer.sprite.playing = "walk_e"
end
local lastDir
function love.update(dt)
Deer:update(dt)
tick = tick + 1
local mouse = Vec3 { love.mouse.getPosition() }
if (mouse) then Deer:lookAt(mouse) end
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
if (lastDir and lastDir ~= Deer:namedDirection()) then
print(Deer:namedDirection())
end
lastDir = Deer:namedDirection()
Deer:update(dt)
end
function love.draw()
love.graphics.draw(Deer.sprite:getTexture(), Deer.sprite:getQuad(), 200, 200, 0, 4, 4)
love.graphics.draw(Deer.sprite:getTexture(), Deer.sprite:getQuad(), Deer.position.x, Deer.position.y, Deer.direction,
4, 4, 32, 32)
end
function love.conf(t)