player refactoring

This commit is contained in:
Ivan Yuriev 2025-04-10 02:13:07 +03:00
parent edf7f0668d
commit 8fd4805cc7
5 changed files with 51 additions and 96 deletions

View File

@ -6,9 +6,10 @@ __Entity = {
sprite = __AnimatedSprite,
position = Vec3 {},
velocity = Vec3 {},
rotation = 0, -- clockwise radians
rotation = 0, -- clockwise radians
friction = 0.98,
speed = 1 -- m/s
speed = 3, -- m/s
rotation_speed = 1 --rad/sec
}
function Entity(id)
@ -22,6 +23,8 @@ end
function __Entity:update(dt)
self:processMovement()
if self.sprite then
local dir = self:namedDirection()
self.sprite.playing = "walk_" .. dir
self.sprite:update(dt)
end
end
@ -29,6 +32,7 @@ end
function __Entity:processMovement()
self.position = self.position + self.velocity
self.velocity = self.velocity * self.friction
self.rotation = self.velocity:direction()
end
function __Entity:namedDirection()

35
lib/player.lua Normal file
View File

@ -0,0 +1,35 @@
require "lib.entity"
require 'lib.vec3'
__Player = {}
function Player(sprite_id)
local e = Entity(sprite_id)
e.sprite.playing = "walk_e"
return setmetatable({ entity = e }, { __index = __Player })
end
function __Player:update(dt)
local acc = Vec3 {}
if (love.keyboard.isDown('w')) then
acc = acc + Vec3 { 0, -1 }
end
if (love.keyboard.isDown('a')) then
acc = acc + Vec3 { -1, 0 }
end
if (love.keyboard.isDown('d')) then
acc = acc + Vec3 { 1, 0 }
end
if (love.keyboard.isDown('s')) then
acc = acc + Vec3 { 0, 1 }
end
self.entity.velocity = self.entity.velocity
+ ((acc:normalize() or Vec3 {})
* self.entity.speed
* dt)
self.entity:update(dt)
end

View File

@ -24,7 +24,7 @@ function __AnimatedSprite:getQuad()
if self.playing then
return self[self.playing]:getQuad()
end
return nil
return love.graphics.newQuad(0, 0, 235, 235, AssetBundle.files.sprites.fallback)
end
function __AnimatedSprite:getTexture()

View File

@ -1,5 +1,3 @@
require "lib.option"
__Vec3 = {
x = 0,
y = 0,
@ -45,12 +43,12 @@ end
function __Vec3:normalize()
local length = self:length()
if not length then return None end
return Some(Vec3 {
if length == 0 then return nil end
return Vec3 {
self.x / length,
self.y / length,
self.z / length
})
}
end
function __Vec3:direction()

View File

@ -1,103 +1,21 @@
local Player = {}
local function vec2(x, y)
return function()
return { x = x, y = y }
end
end
local function vec2_length(v)
return math.sqrt(v().x * v().x + v().y * v().y)
end
local function vec2_add(v1, v2)
return vec2(v1().x + v2().x, v1().y + v2().y)
end
local function vec2_normalize(v)
local length = vec2_length(v)
if (length == 0) then return vec2(0, 0) end
return vec2(v().x / length, v().y / length)
end
local function vec2_scale(v, factor)
return vec2(v().x * factor, v().y * factor)
end
local function vec2_to_string(v)
return "{" .. v().x .. ", " .. v().y .. "}"
end
function Player:init()
self.position = vec2(0, 0)
self.velocity = vec2(0, 0)
end
function Player:update(dt)
local speed = 10
local friction = 0.05
local acc = vec2(0, 0)
if (love.keyboard.isDown('w')) then
acc = vec2_add(acc, vec2(0, -1))
end
if (love.keyboard.isDown('a')) then
acc = vec2_add(acc, vec2(-1, 0))
end
if (love.keyboard.isDown('d')) then
acc = vec2_add(acc, vec2(1, 0))
end
if (love.keyboard.isDown('s')) then
acc = vec2_add(acc, vec2(0, 1))
end
acc = vec2_scale(vec2_normalize(acc), speed * dt)
self.velocity = vec2_add(self.velocity, acc)
self.position = vec2_add(self.position, self.velocity)
self.velocity = vec2_scale(self.velocity, 1 - friction)
-- print(vec2_length(self.velocity))
end
require "lib.asset_bundle"
require "lib.entity"
require "lib.vec3"
require "lib.player"
Fox = nil
P = nil
function love.load()
AssetBundle:load()
Player:init();
Fox = Entity('fox')
Fox.position = Vec3 { 200, 200, 0 }
Fox.sprite.playing = "walk_ne"
P = Player('fox')
end
local lastDir
function love.update(dt)
Fox.rotation = Fox.velocity:direction()
if (lastDir and lastDir ~= Fox:namedDirection()) then
local t = Fox.sprite[Fox.sprite.playing].currentTime
Fox.sprite.playing = "walk_" .. Fox:namedDirection()
Fox.sprite[Fox.sprite.playing].currentTime = t
end
lastDir = Fox:namedDirection()
Player:update(dt)
Fox.velocity = Vec3 { Player.velocity().x, Player.velocity().y, 0 }
if Fox.velocity:length() < 0.1 then
Fox.sprite[Fox.sprite.playing].currentTime = 0
end
Fox:update(dt)
P:update(dt)
end
function love.draw()
love.graphics.draw(Fox.sprite:getTexture(), Fox.sprite:getQuad(), Fox.position.x, Fox.position.y, Fox.direction,
love.graphics.draw(P.entity.sprite:getTexture(), P.entity.sprite:getQuad(), P.entity.position.x, P.entity.position.y,
P.entity.direction,
2, 2, 48, 48)
end