From 586ea68d2bd44b54712090ce64625dd5ee60790c Mon Sep 17 00:00:00 2001 From: PeaAshMeter Date: Fri, 12 Dec 2025 05:20:11 +0300 Subject: [PATCH] minimal end turn button implementation --- lib/level/camera.lua | 4 +- lib/level/level.lua | 1 - lib/level/selector.lua | 7 +--- lib/level/turn_order.lua | 1 - lib/simple_ui/level/cpanel.lua | 14 ++++++- lib/simple_ui/level/end_turn.lua | 64 ++++++++++++++++++++++++++++++++ lib/simple_ui/level/layout.lua | 6 ++- main.lua | 14 +------ 8 files changed, 88 insertions(+), 23 deletions(-) create mode 100644 lib/simple_ui/level/end_turn.lua diff --git a/lib/level/camera.lua b/lib/level/camera.lua index b2d5138..77981d1 100644 --- a/lib/level/camera.lua +++ b/lib/level/camera.lua @@ -38,7 +38,7 @@ local controlMap = { } function camera:update(dt) - if self.animationNode and not (self.animationNode.t >= 1) then + if self.animationNode and self.animationNode.state == "running" then self.animationNode:update(dt) -- тик анимации self.position = utils.lerp(self.animationBeginPosition, self.animationEndPosition, self.animationNode:getValue()) return @@ -100,7 +100,7 @@ end --- @param position Vec3 --- @param animationNode AnimationNode function camera:animateTo(position, animationNode) - if self.animationNode then self.animationNode:finish() end + if self.animationNode and self.animationNode.state ~= "finished" then self.animationNode:finish() end self.animationNode = animationNode self.animationEndPosition = position self.animationBeginPosition = self.position diff --git a/lib/level/level.lua b/lib/level/level.lua index 8dd3d33..16991e9 100644 --- a/lib/level/level.lua +++ b/lib/level/level.lua @@ -35,7 +35,6 @@ function level:update(dt) el:update(dt) end) - self.camera:update(dt) self.selector:update(dt) end diff --git a/lib/level/selector.lua b/lib/level/selector.lua index 2f966a1..a5d8f98 100644 --- a/lib/level/selector.lua +++ b/lib/level/selector.lua @@ -34,11 +34,8 @@ function selector:update(dt) char:try(Tree.behaviors.spellcaster, function(b) if not b.cast then - -- тут какая-то страшная дичь, я даже не уверен что оно работает - -- зато я точно уверен, что это надо было писать не так - if not selectedId then self:select(selectedId) end - if selectedId ~= Tree.level.turnOrder.current and Tree.level.turnOrder.isTurnsEnabled then return end - return self:select(selectedId) + if not selectedId then self:select(nil) end + return end if b.cast:cast(char, mousePosition) then self:lock() diff --git a/lib/level/turn_order.lua b/lib/level/turn_order.lua index f47db4b..f03c836 100644 --- a/lib/level/turn_order.lua +++ b/lib/level/turn_order.lua @@ -30,7 +30,6 @@ end --- --- Если в очереди на ход больше никого нет, заканчиваем раунд function turnOrder:next() - Tree.level.selector:select(nil) self.actedQueue:insert(self.current) local next = self.pendingQueue:peek() if not next then return self:endRound() end diff --git a/lib/simple_ui/level/cpanel.lua b/lib/simple_ui/level/cpanel.lua index 93ebb53..5ecb915 100644 --- a/lib/simple_ui/level/cpanel.lua +++ b/lib/simple_ui/level/cpanel.lua @@ -4,13 +4,14 @@ local Element = require "lib.simple_ui.element" local Rect = require "lib.simple_ui.rect" local SkillRow = require "lib.simple_ui.level.skill_row" local Bars = require "lib.simple_ui.level.bottom_bars" - +local EndTurnButton = require "lib.simple_ui.level.end_turn" --- @class CharacterPanel : UIElement --- @field animationNode AnimationNode --- @field state "show" | "idle" | "hide" --- @field skillRow SkillRow --- @field bars BottomBars +--- @field endTurnButton EndTurnButton local characterPanel = setmetatable({}, Element) characterPanel.__index = characterPanel @@ -19,6 +20,7 @@ function characterPanel.new(characterId) t.state = "show" t.skillRow = SkillRow(characterId) t.bars = Bars(characterId) + t.endTurnButton = EndTurnButton {} return setmetatable(t, characterPanel) end @@ -69,6 +71,14 @@ function characterPanel:update(dt) height = self.bars.bounds.height + self.skillRow.bounds.height } + self.endTurnButton.bounds = Rect { + x = self.bounds.x + self.bounds.width + 32, + y = self.bounds.y + 12, + width = 150, + height = self.bounds.height - 24 + } + self.endTurnButton:update(dt) + if not characterPanelCanvas then characterPanelCanvas = love.graphics.newCanvas(self.bounds.width, self.bounds.height) end @@ -107,6 +117,8 @@ function characterPanel:draw() love.graphics.setShader(Tree.assets.files.shaders.reveal) love.graphics.setColor(1, 1, 1, 1) + self.endTurnButton:draw() + love.graphics.push() love.graphics.translate(self.bounds.x, self.bounds.y) love.graphics.draw(characterPanelCanvas) diff --git a/lib/simple_ui/level/end_turn.lua b/lib/simple_ui/level/end_turn.lua new file mode 100644 index 0000000..eb6a2d1 --- /dev/null +++ b/lib/simple_ui/level/end_turn.lua @@ -0,0 +1,64 @@ +local Element = require "lib.simple_ui.element" +local AnimationNode = require "lib.animation_node" +local easing = require "lib.utils.easing" + +--- @class EndTurnButton : UIElement +--- @field hovered boolean +--- @field onClick function? +local endTurnButton = setmetatable({}, Element) +endTurnButton.__index = endTurnButton + +function endTurnButton:update(dt) + local mx, my = love.mouse.getPosition() + if self:hitTest(mx, my) then + self.hovered = true + if Tree.controls:isJustPressed("select") then + if self.onClick then self.onClick() end + Tree.controls:consume("select") + end + else + self.hovered = false + end +end + +function endTurnButton:draw() + love.graphics.setColor(38 / 255, 50 / 255, 56 / 255) + love.graphics.rectangle("fill", self.bounds.x, self.bounds.y, self.bounds.width, self.bounds.height) + + if self.hovered then + love.graphics.setColor(0.1, 0.1, 0.1) + love.graphics.rectangle("fill", self.bounds.x, self.bounds.y, self.bounds.width, self.bounds.height) + end + + + love.graphics.setColor(0.95, 0.95, 0.95) + local font = love.graphics.newFont(16) + love.graphics.printf("end_turn", font, self.bounds.x, + (self.bounds.y + self.bounds.height / 2) - (font:getHeight() / 2), self.bounds.width, "center") + + + love.graphics.setLineWidth(2) + self:drawBorder("outer") + love.graphics.setColor(1, 1, 1) +end + +function endTurnButton:onClick() + Tree.level.turnOrder:next() + Tree.level.selector:select(nil) + local cid = Tree.level.turnOrder.current + local playing = Tree.level.characters[cid] + if not playing:has(Tree.behaviors.map) then return end + + AnimationNode { + function(node) + Tree.level.camera:animateTo(playing:has(Tree.behaviors.map).displayedPosition, node) + end, + duration = 1500, + easing = easing.easeInOutCubic, + onEnd = function() Tree.level.selector:select(cid) end + }:run() +end + +return function(values) + return endTurnButton:new(values) +end diff --git a/lib/simple_ui/level/layout.lua b/lib/simple_ui/level/layout.lua index 7a3280c..0c9774a 100644 --- a/lib/simple_ui/level/layout.lua +++ b/lib/simple_ui/level/layout.lua @@ -1,15 +1,19 @@ local CPanel = require "lib.simple_ui.level.cpanel" +local build + local layout = {} function layout:update(dt) + if self.characterPanel then self.characterPanel:update(dt) end + local cid = Tree.level.selector:selected() if cid then self.characterPanel = CPanel(cid) self.characterPanel:show() + self.characterPanel:update(dt) elseif Tree.level.selector:deselected() then self.characterPanel:hide() end - if self.characterPanel then self.characterPanel:update(dt) end end function layout:draw() diff --git a/main.lua b/main.lua index ec2f83d..3b2629c 100644 --- a/main.lua +++ b/main.lua @@ -25,21 +25,11 @@ local lt = "0" function love.update(dt) local t1 = love.timer.getTime() Tree.controls:poll() - testLayout:update(dt) -- логика UI-слоя должна отработать раньше всех, потому что нужно перехватить жесты и не пустить их дальше + Tree.level.camera:update(dt) -- сначала логика камеры, потому что на нее завязан UI + testLayout:update(dt) -- потом UI, потому что нужно перехватить жесты и не пустить их дальше Tree.panning:update(dt) Tree.level:update(dt) - -- для тестов очереди ходов - -- удалить как только появится ui для людей - if Tree.controls:isJustPressed("endTurnTest") then - Tree.level.turnOrder:next() - print("Now playing:", Tree.level.turnOrder.current) - end - if Tree.controls:isJustPressed("toggleTurns") then - print('toggle turns') - Tree.level.turnOrder:toggleTurns() - end - Tree.controls:cache() local t2 = love.timer.getTime()