diff --git a/assets/sprites/fox/walk_e.png b/assets/sprites/fox/walk_e.png new file mode 100644 index 0000000..e3470a8 Binary files /dev/null and b/assets/sprites/fox/walk_e.png differ diff --git a/assets/sprites/fox/walk_n.png b/assets/sprites/fox/walk_n.png new file mode 100644 index 0000000..e60f6eb Binary files /dev/null and b/assets/sprites/fox/walk_n.png differ diff --git a/assets/sprites/fox/walk_ne.png b/assets/sprites/fox/walk_ne.png new file mode 100644 index 0000000..74f0987 Binary files /dev/null and b/assets/sprites/fox/walk_ne.png differ diff --git a/assets/sprites/fox/walk_nw.png b/assets/sprites/fox/walk_nw.png new file mode 100644 index 0000000..448bc53 Binary files /dev/null and b/assets/sprites/fox/walk_nw.png differ diff --git a/assets/sprites/fox/walk_s.png b/assets/sprites/fox/walk_s.png new file mode 100644 index 0000000..7b83e61 Binary files /dev/null and b/assets/sprites/fox/walk_s.png differ diff --git a/assets/sprites/fox/walk_se.png b/assets/sprites/fox/walk_se.png new file mode 100644 index 0000000..9c0e6a2 Binary files /dev/null and b/assets/sprites/fox/walk_se.png differ diff --git a/assets/sprites/fox/walk_sw.png b/assets/sprites/fox/walk_sw.png new file mode 100644 index 0000000..5b8fbc0 Binary files /dev/null and b/assets/sprites/fox/walk_sw.png differ diff --git a/assets/sprites/fox/walk_w.png b/assets/sprites/fox/walk_w.png new file mode 100644 index 0000000..50ecfd6 Binary files /dev/null and b/assets/sprites/fox/walk_w.png differ diff --git a/lib/camera.lua b/lib/camera.lua new file mode 100644 index 0000000..b5dcb4a --- /dev/null +++ b/lib/camera.lua @@ -0,0 +1,8 @@ +require 'lib.entity' + +__Camera = { + pivotPosition = Vec3 {}, + position = Vec3 {}, + velocity = Vec3 {}, + fixed = false, +} diff --git a/lib/entity.lua b/lib/entity.lua index 683c89c..6742995 100644 --- a/lib/entity.lua +++ b/lib/entity.lua @@ -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 diff --git a/lib/sprite.lua b/lib/sprite.lua index a5e6851..302817e 100644 --- a/lib/sprite.lua +++ b/lib/sprite.lua @@ -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 }) diff --git a/lib/vec3.lua b/lib/vec3.lua index 23707a4..530582a 100644 --- a/lib/vec3.lua +++ b/lib/vec3.lua @@ -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) diff --git a/main.lua b/main.lua index 6487809..5f6503e 100644 --- a/main.lua +++ b/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)