rotating deer demo
This commit is contained in:
parent
5d1c31cc9c
commit
8c39f16be3
@ -6,6 +6,9 @@ __Entity = {
|
|||||||
sprite = __AnimatedSprite,
|
sprite = __AnimatedSprite,
|
||||||
position = Vec3 {},
|
position = Vec3 {},
|
||||||
velocity = Vec3 {},
|
velocity = Vec3 {},
|
||||||
|
direction = 0, -- clockwise radians
|
||||||
|
friction = 0.98,
|
||||||
|
speed = 1
|
||||||
}
|
}
|
||||||
|
|
||||||
function Entity(id)
|
function Entity(id)
|
||||||
@ -17,7 +20,32 @@ function Entity(id)
|
|||||||
end
|
end
|
||||||
|
|
||||||
function __Entity:update(dt)
|
function __Entity:update(dt)
|
||||||
|
self:processMovement()
|
||||||
if self.sprite then
|
if self.sprite then
|
||||||
self.sprite:update(dt)
|
self.sprite:update(dt)
|
||||||
end
|
end
|
||||||
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
|
||||||
|
|||||||
@ -31,5 +31,5 @@ function __AnimatedSprite:getTexture()
|
|||||||
if self.playing then
|
if self.playing then
|
||||||
return self[self.playing].spriteSheet
|
return self[self.playing].spriteSheet
|
||||||
end
|
end
|
||||||
return nil
|
return AssetBundle.files.sprites.fallback
|
||||||
end
|
end
|
||||||
|
|||||||
41
lib/vec3.lua
41
lib/vec3.lua
@ -1,16 +1,16 @@
|
|||||||
require "lib.option"
|
require "lib.option"
|
||||||
|
|
||||||
__Vec3 = {
|
__Vec3 = {
|
||||||
_x = 0,
|
x = 0,
|
||||||
_y = 0,
|
y = 0,
|
||||||
_z = 0,
|
z = 0,
|
||||||
}
|
}
|
||||||
|
|
||||||
function Vec3(vec)
|
function Vec3(vec)
|
||||||
return setmetatable({
|
return setmetatable({
|
||||||
_x = vec[1] or 0,
|
x = vec[1] or 0,
|
||||||
_y = vec[2] or 0,
|
y = vec[2] or 0,
|
||||||
_z = vec[3] or 0,
|
z = vec[3] or 0,
|
||||||
}, {
|
}, {
|
||||||
__index = __Vec3,
|
__index = __Vec3,
|
||||||
__tostring = __Vec3.__tostring,
|
__tostring = __Vec3.__tostring,
|
||||||
@ -19,41 +19,48 @@ function Vec3(vec)
|
|||||||
__unm = function(self)
|
__unm = function(self)
|
||||||
return __Vec3.scale(self, -1)
|
return __Vec3.scale(self, -1)
|
||||||
end,
|
end,
|
||||||
|
__sub = function(self, other)
|
||||||
|
return self + -other
|
||||||
|
end,
|
||||||
__eq = function(self, other)
|
__eq = function(self, other)
|
||||||
return
|
return
|
||||||
self._x == other._x
|
self.x == other.x
|
||||||
and self._y == other._y
|
and self.y == other.y
|
||||||
and self._z == other._z
|
and self.z == other.z
|
||||||
end,
|
end,
|
||||||
})
|
})
|
||||||
end
|
end
|
||||||
|
|
||||||
function __Vec3:add(other)
|
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
|
end
|
||||||
|
|
||||||
function __Vec3:scale(factor)
|
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
|
end
|
||||||
|
|
||||||
function __Vec3:length()
|
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
|
end
|
||||||
|
|
||||||
function __Vec3:normalize()
|
function __Vec3:normalize()
|
||||||
local length = self:length()
|
local length = self:length()
|
||||||
if not length then return None end
|
if not length then return None end
|
||||||
return Some(Vec3 {
|
return Some(Vec3 {
|
||||||
self._x / length,
|
self.x / length,
|
||||||
self._y / length,
|
self.y / length,
|
||||||
self._z / length
|
self.z / length
|
||||||
})
|
})
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function __Vec3:direction()
|
||||||
|
return math.atan2(self.y, self.x)
|
||||||
|
end
|
||||||
|
|
||||||
function __Vec3:dot(other)
|
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
|
end
|
||||||
|
|
||||||
function __Vec3:__tostring()
|
function __Vec3:__tostring()
|
||||||
return "Vec3{" .. self._x .. ", " .. self._y .. ", " .. self._z .. "}"
|
return "Vec3{" .. self.x .. ", " .. self.y .. ", " .. self.z .. "}"
|
||||||
end
|
end
|
||||||
|
|||||||
23
main.lua
23
main.lua
@ -63,33 +63,34 @@ end
|
|||||||
|
|
||||||
require "lib.asset_bundle"
|
require "lib.asset_bundle"
|
||||||
require "lib.entity"
|
require "lib.entity"
|
||||||
|
require "lib.vec3"
|
||||||
|
|
||||||
|
|
||||||
Deer = nil
|
Deer = nil
|
||||||
local tick = 0
|
|
||||||
|
|
||||||
function love.load()
|
function love.load()
|
||||||
AssetBundle:load()
|
AssetBundle:load()
|
||||||
Deer = Entity('deer')
|
Deer = Entity('deer')
|
||||||
|
Deer.position = Vec3 { 200, 200, 0 }
|
||||||
Deer.sprite.playing = "walk_e"
|
Deer.sprite.playing = "walk_e"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local lastDir
|
||||||
function love.update(dt)
|
function love.update(dt)
|
||||||
Deer:update(dt)
|
local mouse = Vec3 { love.mouse.getPosition() }
|
||||||
tick = tick + 1
|
if (mouse) then Deer:lookAt(mouse) end
|
||||||
|
|
||||||
if (tick % 100 == 0) then
|
if (lastDir and lastDir ~= Deer:namedDirection()) then
|
||||||
if (Deer.sprite.playing == "walk_e") then
|
print(Deer:namedDirection())
|
||||||
Deer.sprite.playing = "walk_w"
|
|
||||||
else
|
|
||||||
Deer.sprite.playing = "walk_e"
|
|
||||||
end
|
|
||||||
tick = 0
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
lastDir = Deer:namedDirection()
|
||||||
|
Deer:update(dt)
|
||||||
end
|
end
|
||||||
|
|
||||||
function love.draw()
|
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
|
end
|
||||||
|
|
||||||
function love.conf(t)
|
function love.conf(t)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user