implemented smooth camera & scrolling w/ mousewheel
This commit is contained in:
parent
0293409dd9
commit
16ea474526
31
camera.lua
31
camera.lua
@ -1,34 +1,45 @@
|
|||||||
local Vec3 = require "lib/vec3"
|
local Vec3 = require "lib/vec3"
|
||||||
local tree = require "lib/tree"
|
local tree = require "lib/tree"
|
||||||
|
|
||||||
|
local EPSILON = 0.001
|
||||||
|
|
||||||
--- @class (exact) Camera
|
--- @class (exact) Camera
|
||||||
--- @field target Vec3
|
--- @field position Vec3
|
||||||
|
--- @field velocity Vec3
|
||||||
--- @field speed number
|
--- @field speed number
|
||||||
--- @field pixelsPerMeter integer
|
--- @field pixelsPerMeter integer
|
||||||
local camera = {
|
local camera = {
|
||||||
target = Vec3 {},
|
position = Vec3 {},
|
||||||
|
velocity = Vec3 {},
|
||||||
speed = 5,
|
speed = 5,
|
||||||
pixelsPerMeter = 100
|
pixelsPerMeter = 100
|
||||||
}
|
}
|
||||||
|
|
||||||
|
---@todo Отрефакторить и вынести кнопки управления в controls, не должно быть таких ужасных проверок
|
||||||
function camera:update(dt)
|
function camera:update(dt)
|
||||||
local ws = tree.instance().wheelscroll
|
local ws = tree.instance().wheelscroll
|
||||||
if ws.delta:length() > 0 then
|
if ws.delta:length() > 0 then
|
||||||
local worldDelta = ws.delta * (1 / self.pixelsPerMeter)
|
local worldDelta = ws.delta:scale(1 / self.pixelsPerMeter):scale(dt):scale(self.speed)
|
||||||
self.target = self.target + worldDelta
|
self.velocity = self.velocity + worldDelta
|
||||||
return
|
elseif love.keyboard.isDown("w") or love.keyboard.isDown("a") or love.keyboard.isDown("s") or love.keyboard.isDown("d") then
|
||||||
|
if love.keyboard.isDown("w") then self.velocity = self.velocity + Vec3({ 0, -1 }) end
|
||||||
|
if love.keyboard.isDown("a") then self.velocity = self.velocity + Vec3({ -1 }) end
|
||||||
|
if love.keyboard.isDown("s") then self.velocity = self.velocity + Vec3({ 0, 1 }) end
|
||||||
|
if love.keyboard.isDown("d") then self.velocity = self.velocity + Vec3({ 1 }) end
|
||||||
|
self.velocity = (self.velocity:normalize() or Vec3 {}):scale(self.speed):scale(dt)
|
||||||
end
|
end
|
||||||
|
|
||||||
if love.keyboard.isDown("w") then self.target = self.target + Vec3({ 0, -dt * self.speed }) end
|
self.position = self.position + self.velocity
|
||||||
if love.keyboard.isDown("a") then self.target = self.target + Vec3({ -dt * self.speed }) end
|
self.velocity = self.velocity -
|
||||||
if love.keyboard.isDown("s") then self.target = self.target + Vec3({ 0, dt * self.speed }) end
|
self.velocity * 5 *
|
||||||
if love.keyboard.isDown("d") then self.target = self.target + Vec3({ dt * self.speed }) end
|
dt -- магическая формула, которая означает "экспоненциально замедлиться до нуля"
|
||||||
|
if self.velocity:length() < EPSILON then self.velocity = Vec3 {} end
|
||||||
end
|
end
|
||||||
|
|
||||||
function camera:attach()
|
function camera:attach()
|
||||||
love.graphics.push()
|
love.graphics.push()
|
||||||
love.graphics.scale(self.pixelsPerMeter)
|
love.graphics.scale(self.pixelsPerMeter)
|
||||||
love.graphics.translate(-self.target.x, -self.target.y)
|
love.graphics.translate(-self.position.x, -self.position.y)
|
||||||
end
|
end
|
||||||
|
|
||||||
function camera:detach()
|
function camera:detach()
|
||||||
|
|||||||
0
lib/controls.lua
Normal file
0
lib/controls.lua
Normal file
@ -16,4 +16,8 @@ function P.generateList(count, generator)
|
|||||||
return xs
|
return xs
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function P.sign(number)
|
||||||
|
return (number > 0 and 1) or (number == 0 and 0) or -1
|
||||||
|
end
|
||||||
|
|
||||||
return P
|
return P
|
||||||
|
|||||||
@ -1,22 +1,22 @@
|
|||||||
local Vec3 = require "lib/vec3"
|
local Vec3 = require "lib/vec3"
|
||||||
local MIDDLE_BUTTON = 3
|
|
||||||
|
|
||||||
--- @class (exact) WheelScroll
|
--- @class WheelScroll
|
||||||
--- @field x number
|
--- @field pos Vec3 | nil
|
||||||
--- @field y number
|
|
||||||
--- @field delta Vec3
|
--- @field delta Vec3
|
||||||
local wheelscroll = {
|
local wheelscroll = {
|
||||||
x = 0.0,
|
pos = nil,
|
||||||
y = 0.0,
|
|
||||||
delta = Vec3 {}
|
delta = Vec3 {}
|
||||||
}
|
}
|
||||||
|
|
||||||
function love.mousepressed(x, y, button)
|
function wheelscroll:update(dt)
|
||||||
if button == MIDDLE_BUTTON then
|
if love.mouse.isDown(3) then
|
||||||
wheelscroll.delta = Vec3 { wheelscroll.x, wheelscroll.y } - Vec3 { x, y }
|
local mouseX, mouseY = love.mouse.getPosition()
|
||||||
wheelscroll.x = x
|
if (wheelscroll.pos) then wheelscroll.delta = wheelscroll.pos - Vec3 { mouseX, mouseY } end
|
||||||
wheelscroll.y = y
|
wheelscroll.pos = Vec3 { mouseX, mouseY }
|
||||||
|
return
|
||||||
end
|
end
|
||||||
|
wheelscroll.pos = nil
|
||||||
|
wheelscroll.delta = Vec3 {}
|
||||||
end
|
end
|
||||||
|
|
||||||
return wheelscroll
|
return wheelscroll
|
||||||
|
|||||||
4
main.lua
4
main.lua
@ -4,6 +4,7 @@ require "character"
|
|||||||
|
|
||||||
local camera = require 'camera'
|
local camera = require 'camera'
|
||||||
local Vec3 = require "lib/vec3"
|
local Vec3 = require "lib/vec3"
|
||||||
|
local tree = require "lib/tree"
|
||||||
|
|
||||||
function love.conf(t)
|
function love.conf(t)
|
||||||
t.console = true
|
t.console = true
|
||||||
@ -11,7 +12,7 @@ end
|
|||||||
|
|
||||||
function love.load()
|
function love.load()
|
||||||
Camera = camera.new()
|
Camera = camera.new()
|
||||||
Camera.target = Vec3({})
|
Camera.position = Vec3({})
|
||||||
-- PlayerFaction = Faction
|
-- PlayerFaction = Faction
|
||||||
|
|
||||||
-- Hero1 = Character:create("Petya", 10)
|
-- Hero1 = Character:create("Petya", 10)
|
||||||
@ -27,6 +28,7 @@ end
|
|||||||
|
|
||||||
function love.update(dt)
|
function love.update(dt)
|
||||||
Camera:update(dt)
|
Camera:update(dt)
|
||||||
|
tree.instance().wheelscroll:update(dt)
|
||||||
end
|
end
|
||||||
|
|
||||||
function love.draw()
|
function love.draw()
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user