improvement: port the selection to guisystem

It's a special guiElement, as it's not drawn by the GUISystem, but
the input handling code is usefull.
This commit is contained in:
Kazhnuz 2021-09-16 22:28:57 +02:00
parent 2932a7d85a
commit 5b0bdc68ce
2 changed files with 24 additions and 29 deletions

View file

@ -12,15 +12,11 @@ function HeroFighter:new(owner, character, id)
self:initVoices() self:initVoices()
self.selection = nil
self.exp = self.abstract.exp self.exp = self.abstract.exp
end end
function HeroFighter:update(dt) function HeroFighter:update(dt)
HeroFighter.super.update(self, dt) HeroFighter.super.update(self, dt)
if (self.selection ~= nil) then
self.selection:update(dt)
end
end end
function HeroFighter:getAbstract() function HeroFighter:getAbstract()
@ -72,9 +68,9 @@ function HeroFighter:verifyTargets()
if (needTarget) then if (needTarget) then
if (targetEnnemies) then if (targetEnnemies) then
self.selection = SelectionSystem(self, self.owner.turnSystem.ennemies, true) SelectionSystem(self, self.owner.turnSystem.ennemies, true)
else else
self.selection = SelectionSystem(self, self.owner, false) SelectionSystem(self, self.owner, false)
end end
else else
self.action:start() self.action:start()
@ -87,7 +83,6 @@ function HeroFighter:attack()
end end
function HeroFighter:receiveTarget(target) function HeroFighter:receiveTarget(target)
self.selection = nil
if (self.action ~= nil) then if (self.action ~= nil) then
self.action:setTarget(target) self.action:setTarget(target)
self.action:start() self.action:start()
@ -96,7 +91,6 @@ end
function HeroFighter:goBackToMenu() function HeroFighter:goBackToMenu()
self.turnSystem.scene.gui:setFocus("battleMenu") self.turnSystem.scene.gui:setFocus("battleMenu")
self.selection = nil
end end
-- LIFE functions -- LIFE functions

View file

@ -1,45 +1,42 @@
local SelectionSystem = Object:extend() local GuiElement = require "birb.modules.gui.elements.parent"
local SelectionSystem = GuiElement:extend()
function SelectionSystem:new(owner, fighterSide, onlyAlive) function SelectionSystem:new(owner, fighterSide, onlyAlive)
SelectionSystem.super.new(self, "selection", 0, 0, 1, 1)
self.fighterList = fighterSide:getTargets(onlyAlive) self.fighterList = fighterSide:getTargets(onlyAlive)
self.owner = owner self.owner = owner
self.assets = self.owner.assets self.assets = self.owner.assets
self.selectedTarget = 1 self.selectedTarget = 1
self:updateTarget()
self:getFocus()
end end
function SelectionSystem:update(dt) function SelectionSystem:keypressed(key)
--Faire en sorte que cela permette de choisir la cible if (key == "A") then
local keys = self.owner.turnSystem.scene:getKeys(1) self.assets.sfx["mSelect"]:play()
self:purgeTarget() self:selectTarget()
if (keys["up"].isPressed) then elseif (key == "B") then
self.assets.sfx["mBack"]:play()
self:goBack()
elseif (key == "up") then
self:purgeTarget()
if (self.selectedTarget == 1) then if (self.selectedTarget == 1) then
self.selectedTarget = #self.fighterList self.selectedTarget = #self.fighterList
else else
self.selectedTarget = self.selectedTarget - 1 self.selectedTarget = self.selectedTarget - 1
end end
self.assets.sfx["mBeep"]:play() self.assets.sfx["mBeep"]:play()
end self:updateTarget()
if (keys["down"].isPressed) then elseif (key == "down") then
self:purgeTarget()
if (self.selectedTarget == #self.fighterList) then if (self.selectedTarget == #self.fighterList) then
self.selectedTarget = 1 self.selectedTarget = 1
else else
self.selectedTarget = self.selectedTarget + 1 self.selectedTarget = self.selectedTarget + 1
end end
self.assets.sfx["mBeep"]:play() self.assets.sfx["mBeep"]:play()
self:updateTarget()
end end
self:updateTarget()
if (keys["A"].isPressed) then
self.assets.sfx["mSelect"]:play()
self:selectTarget()
end
if (keys["B"].isPressed) then
self.assets.sfx["mBack"]:play()
self:goBack()
end
end end
function SelectionSystem:purgeTarget() function SelectionSystem:purgeTarget()
@ -53,11 +50,15 @@ function SelectionSystem:updateTarget()
end end
function SelectionSystem:selectTarget() function SelectionSystem:selectTarget()
self:looseFocus()
self:destroy()
self:purgeTarget() self:purgeTarget()
self.owner:receiveTarget(self.fighterList[self.selectedTarget]) self.owner:receiveTarget(self.fighterList[self.selectedTarget])
end end
function SelectionSystem:goBack() function SelectionSystem:goBack()
self:looseFocus()
self:destroy()
self:purgeTarget() self:purgeTarget()
self.owner:goBackToMenu() self.owner:goBackToMenu()
end end