перемещение персонажа по нажатию на кнопку

- я официально в тильте отэкспериментов с ui, щас возьму и напишу лютейшую императивщину
This commit is contained in:
PeaAshMeter 2025-09-04 02:00:06 +03:00
parent bd6ec87dd0
commit bab4b006ca
5 changed files with 46 additions and 25 deletions

View File

@ -12,6 +12,7 @@ local characterId = 1
--- @field info Info --- @field info Info
--- @field graphics Graphics --- @field graphics Graphics
--- @field logic Logic --- @field logic Logic
--- @field cast boolean @todo тестовое поле, которое отвечает за то, кастуется ли перемещение в данный момент
local character = {} local character = {}
character.__index = character character.__index = character

View File

@ -57,7 +57,7 @@ function level:draw()
self.tileGrid:draw() self.tileGrid:draw()
--- Это отрисовка пути персонажа к мышке --- Это отрисовка пути персонажа к мышке
if self.selector.id and path then if self.selector.id and self.characters[self.selector.id].cast and path then
love.graphics.setColor(0.6, 0.75, 0.5) love.graphics.setColor(0.6, 0.75, 0.5)
for p in path:values() do for p in path:values() do
love.graphics.circle("fill", p.x + 0.45, p.y + 0.45, 0.1) love.graphics.circle("fill", p.x + 0.45, p.y + 0.45, 0.1)

View File

@ -29,6 +29,8 @@ function selector:update(dt)
if not characterId and self.id then -- временная обработка события "побежать к точке" if not characterId and self.id then -- временная обработка события "побежать к точке"
local char = Tree.level.characters[self.id] local char = Tree.level.characters[self.id]
if char.cast then
char.cast = false
local charPos = char.logic.mapLogic.position local charPos = char.logic.mapLogic.position
local path = (require "lib.pathfinder")(charPos, mousePosition) local path = (require "lib.pathfinder")(charPos, mousePosition)
path:pop_front() path:pop_front()
@ -36,6 +38,7 @@ function selector:update(dt)
for p in path:values() do print(p) end for p in path:values() do print(p) end
char:followPath(path) char:followPath(path)
end end
end
self:select(characterId) self:select(characterId)
print("[Selector]:", mousePosition, characterId and "selected " .. characterId or "deselected") print("[Selector]:", mousePosition, characterId and "selected " .. characterId or "deselected")

View File

@ -2,30 +2,46 @@ local Vec3 = require "lib.utils.vec3"
local ui = require "lib.ui.core" local ui = require "lib.ui.core"
--- @type Rectangle --- @class SkillButton : Rectangle
local ReactiveRectangle = ui.Rectangle { --- @field owner Character
local SkillButton = ui.Rectangle {
size = Vec3 { 100, 100 }, size = Vec3 { 100, 100 },
color = { 1, 0, 0 }, color = { 1, 0, 0 },
state = { active = false } owner = nil
} }
function ReactiveRectangle:update(dt) function SkillButton:update(dt)
getmetatable(self):update(dt) ui.Rectangle.update(self, dt)
self.color = self.owner.cast and { 0, 1, 0 } or { 1, 0, 0 }
self:onTap(function() self:onTap(function()
self.state.active = not self.state.active self.owner.cast = not self.owner.cast
self.color = self.state.active and { 0, 1, 0 } or { 1, 0, 0 }
end) end)
end end
local Layout = ui.Root { local skillRows = {}
local layout = {}
function layout:build()
return ui.Root {
child = ui.Align { child = ui.Align {
alignment = "bottom_center", alignment = "bottom_center",
child = ui.Row { child =
--- для каждого персонажа строим свой ряд скиллов, сохраняем его на потом и возвращаем
--- если персонаж не выделен, не возвращаем ничего
(function()
local id = Tree.level.selector.id
if not id then return nil end
if skillRows[id] then return skillRows[id] end
local r =
ui.Row {
children = { children = {
ReactiveRectangle setmetatable({ owner = Tree.level.characters[id] }, { __index = SkillButton })
} }
} }
skillRows[id] = r
return r
end)()
} }
} }
end
return Layout return layout

View File

@ -1,8 +1,8 @@
-- CameraLoader = require 'lib/camera' -- CameraLoader = require 'lib/camera'
local Widgets = require "lib.ui.layout"
local character = require "lib/character/character" local character = require "lib/character/character"
require "lib/tree" require "lib/tree"
local layout = require "lib.ui.layout"
function love.conf(t) function love.conf(t)
t.console = true t.console = true
@ -28,6 +28,7 @@ local lt = "0"
function love.update(dt) function love.update(dt)
local t1 = love.timer.getTime() local t1 = love.timer.getTime()
Tree.controls:poll() Tree.controls:poll()
Widgets = layout:build()
Widgets:update(dt) -- логика UI-слоя должна отработать раньше всех, потому что нужно перехватить жесты и не пустить их дальше Widgets:update(dt) -- логика UI-слоя должна отработать раньше всех, потому что нужно перехватить жесты и не пустить их дальше
Tree.panning:update(dt) Tree.panning:update(dt)
Tree.level:update(dt) Tree.level:update(dt)