2025-10-12 03:03:02 +03:00

58 lines
1.5 KiB
Lua

--- @class Selector
--- @field id Id | nil
--- @field locked boolean
local selector = {}
selector.__index = selector
local function new()
return setmetatable({}, selector)
end
--- @param characterId integer | nil
function selector:select(characterId)
self.id = characterId
print("[Selector]:", characterId and "selected " .. characterId or "deselected")
end
function selector:update(dt)
if self.locked or 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 selectedId = Tree.level.characterGrid:get(Vec3 { mousePosition.x, mousePosition.y })
if not self.id then
return self:select(selectedId)
else
local char = Tree.level.characters[self.id]
char:try(Tree.behaviors.spellcaster, function(b)
if not b.cast then
self:select(selectedId)
return
end
if b.cast:cast(char, mousePosition) then
self:lock()
b.state = "running"
end
end)
end
end
--- Disables the selector until [unlock] is called
function selector:lock()
self.id = nil
self.locked = true
end
--- Cancels the effect of [lock]
function selector:unlock()
self.locked = false
end
return {
new = new
}