2020-05-01 14:50:21 +02:00
|
|
|
local TurnController = Object:extend()
|
|
|
|
|
|
|
|
local Player = require "scenes.battlesystem.controllers.player"
|
2020-07-19 15:07:35 +02:00
|
|
|
local Ennemy = require "scenes.battlesystem.controllers.ennemy"
|
2020-05-01 14:50:21 +02:00
|
|
|
|
2020-07-19 16:20:57 +02:00
|
|
|
local HUD = require "scenes.battlesystem.gui.hud"
|
|
|
|
|
2020-05-01 14:50:21 +02:00
|
|
|
local maputils = require "scenes.battlesystem.utils"
|
|
|
|
|
2020-08-04 17:50:03 +02:00
|
|
|
function TurnController:new(scene, battleData)
|
2020-05-01 14:50:21 +02:00
|
|
|
self.scene = scene
|
|
|
|
self.world = scene.world
|
|
|
|
|
|
|
|
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 = {}
|
2020-07-19 15:07:35 +02:00
|
|
|
|
|
|
|
self.currentFighter = nil
|
2020-07-19 16:20:57 +02:00
|
|
|
|
2021-03-13 18:00:39 +01:00
|
|
|
self.canFleeBattle = true
|
|
|
|
|
2020-07-19 16:20:57 +02:00
|
|
|
self.hud = HUD(self)
|
|
|
|
|
|
|
|
self.player = Player(self)
|
2020-08-04 17:50:03 +02:00
|
|
|
self.ennemies = Ennemy(self, battleData)
|
2020-08-22 23:17:25 +02:00
|
|
|
|
|
|
|
-- Change the seed at the start of each battle
|
|
|
|
math.randomseed( os.time() )
|
2020-08-22 23:28:05 +02:00
|
|
|
self:applyDeath()
|
2020-05-01 14:50:21 +02:00
|
|
|
end
|
|
|
|
|
2021-03-13 18:00:39 +01:00
|
|
|
function TurnController:getChanceTooFlee(value)
|
|
|
|
if (self.canFleeBattle) then
|
|
|
|
local speedComparison = math.min(value / self.ennemies:getHighestSpeed(), 1.5)
|
|
|
|
return 25 + (50 * speedComparison)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function TurnController:fleeBattle()
|
|
|
|
self.player:fleeBattle()
|
|
|
|
self.scene:fleeBattle()
|
|
|
|
end
|
|
|
|
|
2021-03-13 17:26:05 +01:00
|
|
|
function TurnController:showMessage(message)
|
|
|
|
self.hud:showMessage(message)
|
|
|
|
end
|
|
|
|
|
2020-05-01 14:50:21 +02:00
|
|
|
function TurnController:startBattle()
|
|
|
|
self.isActive = true
|
2020-07-19 16:20:57 +02:00
|
|
|
self.hud:movePlayerHUD(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
function TurnController:finishBattle()
|
2020-07-25 12:15:50 +02:00
|
|
|
self.isActive = false
|
2020-07-19 16:20:57 +02:00
|
|
|
self.actionlist = {}
|
|
|
|
self.hud:movePlayerHUD(false)
|
|
|
|
self.scene:finishBattle()
|
2020-05-01 14:50:21 +02:00
|
|
|
end
|
|
|
|
|
2020-08-07 11:17:10 +02:00
|
|
|
function TurnController:looseBattle()
|
|
|
|
self.isActive = false
|
|
|
|
self.actionlist = {}
|
|
|
|
self.hud:movePlayerHUD(false)
|
|
|
|
self.scene:looseBattle()
|
|
|
|
end
|
|
|
|
|
2020-05-01 14:50:21 +02:00
|
|
|
function TurnController:update(dt)
|
2020-07-19 16:20:57 +02:00
|
|
|
self.player:update(dt)
|
|
|
|
self.ennemies:update(dt)
|
|
|
|
self.hud:update(dt)
|
2020-05-01 14:50:21 +02:00
|
|
|
if (self.isActive) then
|
2020-07-19 15:07:35 +02:00
|
|
|
if (self.currentFighter ~= nil) then
|
|
|
|
self.currentFighter:update(dt)
|
2020-05-01 14:50:21 +02:00
|
|
|
else
|
2020-08-05 11:40:29 +02:00
|
|
|
if (self.player:countAlive() > 0) then
|
|
|
|
self:nextAction()
|
2020-08-07 11:17:10 +02:00
|
|
|
else
|
|
|
|
self:looseBattle()
|
2020-08-05 11:40:29 +02:00
|
|
|
end
|
2020-05-01 14:50:21 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-08-06 19:10:54 +02:00
|
|
|
function TurnController:getRank()
|
|
|
|
return 3
|
|
|
|
end
|
|
|
|
|
|
|
|
function TurnController:getRewards()
|
|
|
|
local exp, ring = 0, 0
|
|
|
|
for i, ennemy in ipairs(self.ennemies.list) do
|
|
|
|
exp = exp + ennemy.abstract.data.giveExp
|
|
|
|
ring = ring + ennemy.abstract.data.giveRings
|
|
|
|
end
|
|
|
|
return exp, ring
|
|
|
|
end
|
|
|
|
|
2020-05-01 14:50:21 +02:00
|
|
|
function TurnController:nextAction()
|
|
|
|
if (self.turns.isFinished) or (self.turns.current >= #self.actionList) then
|
|
|
|
self:startNewTurn()
|
|
|
|
else
|
|
|
|
self.turns.current = self.turns.current + 1
|
2020-07-19 15:07:35 +02:00
|
|
|
core.debug:print("cbs/turns", "switching to next action")
|
2020-05-01 14:50:21 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
self:startAction()
|
|
|
|
end
|
|
|
|
|
|
|
|
function TurnController:startNewTurn()
|
2020-07-19 15:07:35 +02:00
|
|
|
core.debug:print("cbs/turns", "New Turn Starting")
|
2020-05-01 14:50:21 +02:00
|
|
|
self.turns.current = 1
|
|
|
|
self.turns.isFinished = false
|
|
|
|
self.turns.number = self.turns.number + 1
|
|
|
|
self:calculateTurn()
|
|
|
|
end
|
|
|
|
|
|
|
|
function TurnController:calculateTurn()
|
2020-07-19 15:07:35 +02:00
|
|
|
self.actionList = {}
|
|
|
|
self.player:putActions(self.actionList)
|
|
|
|
self.ennemies:putActions(self.actionList)
|
2020-05-01 14:50:21 +02:00
|
|
|
table.sort(self.actionList, maputils.sortBattlers)
|
|
|
|
end
|
|
|
|
|
2020-07-19 16:57:58 +02:00
|
|
|
function TurnController:removeAllActionsFromFighter(fighterToRemove)
|
|
|
|
for i, action in ipairs(self.actionlist) do
|
|
|
|
if action.fighter == fighterToRemove then
|
|
|
|
table.remove(self.actionlist, i)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-07-25 08:44:03 +02:00
|
|
|
function TurnController:applyDeath()
|
2020-07-25 12:15:50 +02:00
|
|
|
local ennemiesAlive = self.ennemies:applyDeath()
|
|
|
|
if (ennemiesAlive == 0) then
|
|
|
|
self:finishBattle()
|
|
|
|
end
|
2020-07-25 08:44:03 +02:00
|
|
|
self.player:applyDeath()
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2020-05-01 14:50:21 +02:00
|
|
|
function TurnController:startAction()
|
2020-07-19 15:07:35 +02:00
|
|
|
core.debug:print("cbs/turns", "Starting action " .. self.turns.current)
|
2020-05-01 14:50:21 +02:00
|
|
|
local nextAction = self.actionList[self.turns.current]
|
|
|
|
print(nextAction)
|
2020-07-19 15:07:35 +02:00
|
|
|
local nextFighter = nextAction.fighter
|
2020-07-25 12:03:14 +02:00
|
|
|
if (not nextFighter:canFight()) then
|
2020-05-01 14:50:21 +02:00
|
|
|
-- On skipe le personnage s'il a été detruit
|
|
|
|
self:nextAction()
|
|
|
|
else
|
2020-07-19 15:07:35 +02:00
|
|
|
self.currentFighter = nextFighter
|
|
|
|
core.debug:print("cbs/turns", "Activating " .. self.currentFighter.name)
|
|
|
|
self.currentFighter:setActive()
|
2020-07-25 12:03:14 +02:00
|
|
|
self.hud:moveBattleCursor(self.turns.current)
|
2020-05-01 14:50:21 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function TurnController:endAction()
|
2020-07-19 18:09:53 +02:00
|
|
|
self.currentFighter = nil
|
2020-05-01 14:50:21 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
function TurnController:sendSignalToCurrentBattler(signal, subSignal)
|
|
|
|
self.actionList[self.turns.current].actor:receiveSignal(signal, subSignal)
|
|
|
|
end
|
|
|
|
|
2020-07-19 16:20:57 +02:00
|
|
|
function TurnController:draw()
|
|
|
|
self.player:draw()
|
|
|
|
self.ennemies:draw()
|
2021-03-13 17:26:05 +01:00
|
|
|
self.hud:draw()
|
2020-07-19 16:20:57 +02:00
|
|
|
end
|
|
|
|
|
2020-05-01 14:50:21 +02:00
|
|
|
|
|
|
|
return TurnController
|