trying to write selector again

Co-authored-by: Ivan Yuriev <ivanyr44@gmail.com>
This commit is contained in:
Neckrat 2025-08-10 00:28:59 +03:00
parent af792bd2d5
commit 091e960ebd
3 changed files with 59 additions and 20 deletions

View File

@ -17,20 +17,54 @@ local keymap = {
select = control("mouse", "1") select = control("mouse", "1")
} }
local keymapCache = {}
function keymap:isDown(key) 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 type = keymap[key].type
local idx = keymap[key].key local idx = keymap[key].key
if type == "key" then if type == "key" then
return love.keyboard.isDown(idx) keymapCache[key] = 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
return love.mouse.isDown(tonumber(idx) --[[@as number]]) keymapCache[key] = love.mouse.isDown(tonumber(idx) --[[@as number]])
end end
return keymapCache[key]
end
--- Вернуть true, если клавиша нажата в этот тик (ток) и не была нажата в прошлый тик (youtube shorts)
function keymap:isJustPressed(key)
if not keymap[key] then
return false 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 end
return keymap return keymap

View File

@ -1,6 +1,7 @@
local utils = require "lib/utils" local utils = require "lib/utils"
--- @class Level --- @class Level
--- @field size Vec3
--- @field characters Character[] --- @field characters Character[]
--- @field characterGrid CharacterGrid --- @field characterGrid CharacterGrid
--- @field selector Selector --- @field selector Selector
@ -9,10 +10,12 @@ local level = {}
level.__index = level level.__index = level
local function new() local function new()
local size = Vec3 { 30, 30 } -- magic numbers for testing purposes only
return setmetatable({ return setmetatable({
size = size,
characters = {}, characters = {},
characterGrid = (require "lib/grid/character").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(30, 30), -- magic numbers for testing purposes only tileGrid = (require "lib/grid/tile").new(size.x, size.y),
selector = (require "lib/selector").new(), selector = (require "lib/selector").new(),
camera = (require "lib/camera").new() camera = (require "lib/camera").new()
}, level) }, level)
@ -23,6 +26,7 @@ function level:update(dt)
el:update(dt) el:update(dt)
end) end)
self.camera:update(dt) self.camera:update(dt)
self.selector:update(dt)
end end
function level:draw() function level:draw()

View File

@ -1,6 +1,7 @@
--- @class Selector --- @class Selector
--- @field id integer --- @field id Id
local selector = {} local selector = {}
selector.__index = selector
local function new() local function new()
return setmetatable({}, selector) return setmetatable({}, selector)
@ -11,23 +12,23 @@ function selector:select(characterId)
self.id = characterId self.id = characterId
end end
function selector:deselect() -- function selector:deselect()
self.id = nil -- self.id = nil
end -- end
--- TODO: сделать обработчик селектора --- TODO: сделать обработчик селектора
--- @param camera Camera function selector:update(dt)
function selector:update(camera, dt) if not Tree.controls:isJustPressed("select") then return end
if self.id then
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 return
end end
local mousePosition = camera:toWorldPosition(Vec3 { love.mouse.getX(), love.mouse.getY() }):floor() local characterId = Tree.level.characterGrid[mousePosition.x][mousePosition.y]
local characterPosition = Tree.level.positionGrid[mousePosition.x][mousePosition.y]
if Tree.controls:isDown("select") then self:select(characterId)
if characterPosition then print("я ЖОСКО заселектил ", characterId)
self:select(characterPosition)
end
end
end end
return { return {