controls rework
This commit is contained in:
parent
8da5f2f83f
commit
9fdeb7326c
@ -1,3 +1,5 @@
|
|||||||
|
local utils = require "lib/utils"
|
||||||
|
|
||||||
--- @alias Device "mouse" | "key" | "pad"
|
--- @alias Device "mouse" | "key" | "pad"
|
||||||
|
|
||||||
--- @param device Device
|
--- @param device Device
|
||||||
@ -8,7 +10,8 @@ local function control(device, key)
|
|||||||
return t
|
return t
|
||||||
end
|
end
|
||||||
|
|
||||||
local keymap = {
|
local controls = {}
|
||||||
|
controls.keymap = {
|
||||||
cameraMoveUp = control("key", "w"),
|
cameraMoveUp = control("key", "w"),
|
||||||
cameraMoveLeft = control("key", "a"),
|
cameraMoveLeft = control("key", "a"),
|
||||||
cameraMoveRight = control("key", "d"),
|
cameraMoveRight = control("key", "d"),
|
||||||
@ -17,51 +20,44 @@ local keymap = {
|
|||||||
select = control("mouse", "1")
|
select = control("mouse", "1")
|
||||||
}
|
}
|
||||||
|
|
||||||
local keymapCache = {}
|
local currentKeys = {}
|
||||||
|
local cachedKeys = {}
|
||||||
|
|
||||||
function keymap:isDown(key)
|
--- polling controls in O(n)
|
||||||
if not keymap[key] then
|
--- should be called at the beginning of every frame
|
||||||
keymapCache[key] = false
|
function controls:poll()
|
||||||
return false
|
for k, v in pairs(self.keymap) do
|
||||||
end
|
local type = v.type
|
||||||
|
local idx = v.key
|
||||||
local type = keymap[key].type
|
|
||||||
local idx = keymap[key].key
|
|
||||||
if type == "key" then
|
if type == "key" then
|
||||||
keymapCache[key] = love.keyboard.isDown(idx)
|
currentKeys[k] = love.keyboard.isDown(idx)
|
||||||
end
|
end
|
||||||
|
|
||||||
if type == "mouse" then
|
if type == "mouse" then
|
||||||
if not tonumber(idx) then return false end
|
if not tonumber(idx) then return false end
|
||||||
keymapCache[key] = love.mouse.isDown(tonumber(idx) --[[@as number]])
|
currentKeys[k] = love.mouse.isDown(tonumber(idx) --[[@as number]])
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
return keymapCache[key]
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
--- store active controls
|
||||||
--- Вернуть true, если клавиша нажата в этот тик (ток) и не была нажата в прошлый тик (youtube shorts)
|
--- should be called at the end of every frame
|
||||||
function keymap:isJustPressed(key)
|
function controls:cache()
|
||||||
if not keymap[key] then
|
for k, v in pairs(currentKeys) do
|
||||||
return false
|
cachedKeys[k] = v
|
||||||
end
|
end
|
||||||
|
|
||||||
if keymapCache[key] then
|
|
||||||
return false
|
|
||||||
end
|
|
||||||
|
|
||||||
local type = keymap[key].type
|
|
||||||
local idx = keymap[key].key
|
|
||||||
if type == "key" then
|
|
||||||
keymapCache[key] = love.keyboard.isDown(idx)
|
|
||||||
end
|
|
||||||
|
|
||||||
if type == "mouse" then
|
|
||||||
if not tonumber(idx) then return false end
|
|
||||||
keymapCache[key] = love.mouse.isDown(tonumber(idx) --[[@as number]])
|
|
||||||
end
|
|
||||||
|
|
||||||
return keymapCache[key]
|
|
||||||
end
|
end
|
||||||
|
|
||||||
return keymap
|
--- check if a control is active
|
||||||
|
--- @param key string
|
||||||
|
function controls:isDown(key)
|
||||||
|
return not not currentKeys[key]
|
||||||
|
end
|
||||||
|
|
||||||
|
--- check if a control was activated during current logical frame
|
||||||
|
--- @param key string
|
||||||
|
function controls:isJustPressed(key)
|
||||||
|
return currentKeys[key] and not cachedKeys[key]
|
||||||
|
end
|
||||||
|
|
||||||
|
return controls
|
||||||
|
|||||||
2
main.lua
2
main.lua
@ -16,8 +16,10 @@ function love.load()
|
|||||||
end
|
end
|
||||||
|
|
||||||
function love.update(dt)
|
function love.update(dt)
|
||||||
|
Tree.controls:poll()
|
||||||
Tree.panning:update(dt)
|
Tree.panning:update(dt)
|
||||||
Tree.level:update(dt)
|
Tree.level:update(dt)
|
||||||
|
Tree.controls:cache()
|
||||||
end
|
end
|
||||||
|
|
||||||
function love.draw()
|
function love.draw()
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user