chore: separate turns from world

This commit is contained in:
Kazhnuz 2020-05-01 14:50:21 +02:00
parent 873cf12140
commit 34eb8e5ba5
3 changed files with 108 additions and 79 deletions

View file

@ -0,0 +1,102 @@
local TurnController = Object:extend()
local Player = require "scenes.battlesystem.controllers.player"
local Ennemy = require "scenes.battlesystem.controllers.player"
local maputils = require "scenes.battlesystem.utils"
function TurnController:new(scene)
self.scene = scene
self.world = scene.world
--self.player = Player(self)
--self.ennemies = Ennemy(self)
self.isActive = false
self.currentlyPlaying = ""
self.turns = {}
self.turns.current = 1
self.turns.number = 0
self.turns.isFinished = true
self.turns.changeBattler = true
self.actionList = {}
end
function TurnController:startBattle()
self.isActive = true
end
function TurnController:update(dt)
if (self.isActive) then
if (self.currentlyPlaying == "heroes") then
--self.player:update(dt)
elseif (self.currentlyPlaying == "ennemies") then
--self.ennemies:update(dt)
else
self:nextAction()
end
end
end
function TurnController:nextAction()
if (self.turns.isFinished) or (self.turns.current >= #self.actionList) then
self:startNewTurn()
else
self.turns.current = self.turns.current + 1
core.debug:print("cbs/world", "switching to next action")
end
self.scene.hud:moveBattleCursor(self.turns.current)
self:startAction()
end
function TurnController:startNewTurn()
core.debug:print("cbs/world", "New Turn Starting")
self.turns.current = 1
self.turns.isFinished = false
self.turns.number = self.turns.number + 1
self:calculateTurn()
end
function TurnController:calculateTurn()
self.actionList = self.world:generateActionList()
table.sort(self.actionList, maputils.sortBattlers)
end
function TurnController:startAction()
core.debug:print("cbs/world", "Starting action " .. self.turns.current)
local nextAction = self.actionList[self.turns.current]
print(nextAction)
local nextActor = nextAction.actor
if (nextActor.isDestroyed == true) then
-- On skipe le personnage s'il a été detruit
self:nextAction()
else
if (nextActor.side == "heroes") then
--self.player:setActive(nextActor)
self.currentlyPlaying = "heroes"
self.actionList[self.turns.current].actor:setActive()
elseif (nextActor.side == "ennemies") then
--self.ennemies:setActive(nextActor)
self.currentlyPlaying = "ennemies"
self.actionList[self.turns.current].actor:setActive()
end
end
end
function TurnController:endAction()
self.currentlyPlaying = ""
end
function TurnController:drawTurnList(x, y)
end
function TurnController:sendSignalToCurrentBattler(signal, subSignal)
self.actionList[self.turns.current].actor:receiveSignal(signal, subSignal)
end
return TurnController

View file

@ -4,6 +4,7 @@ local BattleSystem = Scene:extend()
local World = require "scenes.battlesystem.world" local World = require "scenes.battlesystem.world"
local MenuSystem = require "scenes.battlesystem.menu" local MenuSystem = require "scenes.battlesystem.menu"
local Turns = require "scenes.battlesystem.controllers"
local HUD = require "scenes.battlesystem.gui.hud" local HUD = require "scenes.battlesystem.gui.hud"
@ -30,11 +31,13 @@ function BattleSystem:initManagers()
self.datas = {} self.datas = {}
self.world = World(self) self.world = World(self)
self.menu = MenuSystem(self) self.menu = MenuSystem(self)
self.turns = Turns(self)
self.hud = HUD(self) self.hud = HUD(self)
end end
function BattleSystem:startBattle() function BattleSystem:startBattle()
self.world:startBattle() self.turns:startBattle()
self.hud:movePlayerHUD(true) self.hud:movePlayerHUD(true)
end end
@ -54,6 +57,7 @@ end
function BattleSystem:update(dt) function BattleSystem:update(dt)
self.world:update(dt) self.world:update(dt)
self.turns:update(dt)
self.hud:update(dt) self.hud:update(dt)
if (self.screen ~= nil) then if (self.screen ~= nil) then
self.screen:update(dt) self.screen:update(dt)

View file

@ -26,13 +26,7 @@ function World:new(scene, battlefile)
self.actors = {} self.actors = {}
self.globalID = 0 self.globalID = 0
self.turns = {}
self.turns.current = 1
self.turns.number = 0
self.turns.isFinished = true
self.turns.changeBattler = true
self.battlers = {} self.battlers = {}
self.actionlist = {}
self.heroNumber = 0 self.heroNumber = 0
self.ennNumber = 0 self.ennNumber = 0
@ -176,79 +170,10 @@ function World:update(dt)
actor:update(dt) actor:update(dt)
end end
if (self.isBattleActive) then
self:updateTurns(dt)
end
self.cursor:update(dt) self.cursor:update(dt)
self.map:update(dt) self.map:update(dt)
end end
function World:moveBattleCursor(dt)
local cursorSpeed = 16
if (self.turns.current == 1) then
cursorSpeed = 16 * 4
end
if math.abs(self.BattlerCursor - self.turns.current) > (cursorSpeed * dt) then
self.BattlerCursor = self.BattlerCursor - utils.math.sign(self.BattlerCursor - self.turns.current) * cursorSpeed * dt
else
self.BattlerCursor = self.turns.current
end
end
-- TURNS FUNCTIONS
-- Handle everything related to the turn system
function World:startBattle()
self.isBattleActive = true
end
function World:recalculateTurns()
self:generateActionList()
table.sort(self.actionlist, maputils.sortBattlers)
end
function World:updateTurns(dt)
if (self.turns.changeBattler) then
self:switchActiveBattler( )
end
end
function World:switchActiveBattler()
if (self:countEnnemies()==0) then
self:finishBattle()
else
if (self.turns.isFinished) or (self.turns.current >= #self.actionlist) then
self:switchToNextTurn()
else
self.turns.current = self.turns.current + 1
core.debug:print("cbs/world", "switching to action number " .. self.turns.current)
end
self:selectNextAction()
self.turns.changeBattler = false
self.scene.hud:moveBattleCursor(self.turns.current)
end
end
function World:switchToNextTurn()
core.debug:print("cbs/world", "turn finished")
self.turns.current = 1
self.turns.isFinished = false
self.turns.number = self.turns.number + 1
self:recalculateTurns()
end
function World:selectNextAction()
if (self.actionlist[self.turns.current].actor.isDestroyed == true) then
self:switchActiveBattler()
else
self.actionlist[self.turns.current].actor:setActive()
end
end
function World:finishBattle() function World:finishBattle()
self.isBattleActive = false self.isBattleActive = false
self.actionlist = {} self.actionlist = {}
@ -256,7 +181,7 @@ function World:finishBattle()
end end
function World:sendSignalToCurrentBattler(signal, subSignal) function World:sendSignalToCurrentBattler(signal, subSignal)
self.actionlist[self.turns.current].actor:receiveSignal(signal, subSignal) self.scene.turns:sendSignalToCurrentBattler(signal, subSignal)
end end
-- ACTIVEGRID FUNCTIONS -- ACTIVEGRID FUNCTIONS
@ -305,6 +230,4 @@ function World:drawShadows()
end end
end end
return World return World