trying to write selector again
Co-authored-by: Ivan Yuriev <ivanyr44@gmail.com>
This commit is contained in:
parent
af792bd2d5
commit
091e960ebd
@ -17,20 +17,54 @@ local keymap = {
|
||||
select = control("mouse", "1")
|
||||
}
|
||||
|
||||
local keymapCache = {}
|
||||
|
||||
function keymap:isDown(key)
|
||||
if not keymap[key] then return false end
|
||||
if not keymap[key] then
|
||||
keymapCache[key] = false
|
||||
return false
|
||||
end
|
||||
|
||||
local type = keymap[key].type
|
||||
local idx = keymap[key].key
|
||||
if type == "key" then
|
||||
return love.keyboard.isDown(idx)
|
||||
keymapCache[key] = love.keyboard.isDown(idx)
|
||||
end
|
||||
|
||||
if type == "mouse" then
|
||||
if not tonumber(idx) then return false end
|
||||
return love.mouse.isDown(tonumber(idx) --[[@as number]])
|
||||
keymapCache[key] = love.mouse.isDown(tonumber(idx) --[[@as number]])
|
||||
end
|
||||
|
||||
return false
|
||||
|
||||
return keymapCache[key]
|
||||
end
|
||||
|
||||
--- Вернуть true, если клавиша нажата в этот тик (ток) и не была нажата в прошлый тик (youtube shorts)
|
||||
function keymap:isJustPressed(key)
|
||||
if not keymap[key] then
|
||||
return false
|
||||
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
|
||||
local keymapCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCache =
|
||||
keymapCache[key]
|
||||
keymapCache[key] = false
|
||||
return
|
||||
keymapCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCacheCache
|
||||
end
|
||||
|
||||
return keymap
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
local utils = require "lib/utils"
|
||||
|
||||
--- @class Level
|
||||
--- @field size Vec3
|
||||
--- @field characters Character[]
|
||||
--- @field characterGrid CharacterGrid
|
||||
--- @field selector Selector
|
||||
@ -9,10 +10,12 @@ local level = {}
|
||||
level.__index = level
|
||||
|
||||
local function new()
|
||||
local size = Vec3 { 30, 30 } -- magic numbers for testing purposes only
|
||||
return setmetatable({
|
||||
size = size,
|
||||
characters = {},
|
||||
characterGrid = (require "lib/grid/character").new(30, 30), -- magic numbers for testing purposes only
|
||||
tileGrid = (require "lib/grid/tile").new(30, 30), -- magic numbers for testing purposes only
|
||||
characterGrid = (require "lib/grid/character").new(size.x, size.y),
|
||||
tileGrid = (require "lib/grid/tile").new(size.x, size.y),
|
||||
selector = (require "lib/selector").new(),
|
||||
camera = (require "lib/camera").new()
|
||||
}, level)
|
||||
@ -23,6 +26,7 @@ function level:update(dt)
|
||||
el:update(dt)
|
||||
end)
|
||||
self.camera:update(dt)
|
||||
self.selector:update(dt)
|
||||
end
|
||||
|
||||
function level:draw()
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
--- @class Selector
|
||||
--- @field id integer
|
||||
--- @field id Id
|
||||
local selector = {}
|
||||
selector.__index = selector
|
||||
|
||||
local function new()
|
||||
return setmetatable({}, selector)
|
||||
@ -11,23 +12,23 @@ function selector:select(characterId)
|
||||
self.id = characterId
|
||||
end
|
||||
|
||||
function selector:deselect()
|
||||
self.id = nil
|
||||
end
|
||||
-- function selector:deselect()
|
||||
-- self.id = nil
|
||||
-- end
|
||||
|
||||
--- TODO: сделать обработчик селектора
|
||||
--- @param camera Camera
|
||||
function selector:update(camera, dt)
|
||||
if self.id then
|
||||
function selector:update(dt)
|
||||
if not Tree.controls:isJustPressed("select") then return end
|
||||
|
||||
local mousePosition = Tree.level.camera:toWorldPosition(Vec3 { love.mouse.getX(), love.mouse.getY() }):floor()
|
||||
|
||||
if mousePosition.x >= Tree.level.size.x or mousePosition.y >= Tree.level.size.y or mousePosition.y < 0 or mousePosition.x < 0 then
|
||||
return
|
||||
end
|
||||
local mousePosition = camera:toWorldPosition(Vec3 { love.mouse.getX(), love.mouse.getY() }):floor()
|
||||
local characterPosition = Tree.level.positionGrid[mousePosition.x][mousePosition.y]
|
||||
if Tree.controls:isDown("select") then
|
||||
if characterPosition then
|
||||
self:select(characterPosition)
|
||||
end
|
||||
end
|
||||
local characterId = Tree.level.characterGrid[mousePosition.x][mousePosition.y]
|
||||
|
||||
self:select(characterId)
|
||||
print("я ЖОСКО заселектил ", characterId)
|
||||
end
|
||||
|
||||
return {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user