From ed70bb65505f8d8698adb7d8c1a0093a35bad38a Mon Sep 17 00:00:00 2001 From: Kazhnuz Date: Wed, 5 Aug 2020 09:25:38 +0200 Subject: [PATCH] feat: add basic ennemy attack system Fix #8 --- .../ennemies/classics/motobug/init.lua | 2 + .../datas/gamedata/ennemies/skills/tackle.lua | 2 + .../scenes/battlesystem/actors/ennemy.lua | 2 +- .../controllers/fighters/character.lua | 14 +---- .../controllers/fighters/parent.lua | 15 +++-- .../fighters/systems/behaviours/init.lua | 5 ++ .../fighters/systems/behaviours/parent.lua | 61 +++++++++++++++++++ .../fighters/systems/behaviours/random.lua | 12 ++++ .../fighters/systems/ennemyaction.lua | 22 +++++++ .../controllers/fighters/villain.lua | 37 ++++++++++- 10 files changed, 153 insertions(+), 19 deletions(-) create mode 100644 sonic-radiance.love/scenes/battlesystem/controllers/fighters/systems/behaviours/init.lua create mode 100644 sonic-radiance.love/scenes/battlesystem/controllers/fighters/systems/behaviours/parent.lua create mode 100644 sonic-radiance.love/scenes/battlesystem/controllers/fighters/systems/behaviours/random.lua create mode 100644 sonic-radiance.love/scenes/battlesystem/controllers/fighters/systems/ennemyaction.lua diff --git a/sonic-radiance.love/datas/gamedata/ennemies/classics/motobug/init.lua b/sonic-radiance.love/datas/gamedata/ennemies/classics/motobug/init.lua index 130cd72..7de4f6b 100644 --- a/sonic-radiance.love/datas/gamedata/ennemies/classics/motobug/init.lua +++ b/sonic-radiance.love/datas/gamedata/ennemies/classics/motobug/init.lua @@ -8,6 +8,8 @@ return { turns = 2, hudHeight = 24, + behaviour = "random", + behaviourAlt = "random", giveExp = 20, giveRings = 30, diff --git a/sonic-radiance.love/datas/gamedata/ennemies/skills/tackle.lua b/sonic-radiance.love/datas/gamedata/ennemies/skills/tackle.lua index ae8b60d..0663022 100644 --- a/sonic-radiance.love/datas/gamedata/ennemies/skills/tackle.lua +++ b/sonic-radiance.love/datas/gamedata/ennemies/skills/tackle.lua @@ -10,6 +10,8 @@ return { targetNumber = 1, -- 0 for targeting all ennemies targetEnnemies = true, + isSpecial = false, + choregraphy = { -- the main attack choregraphy {"goTo", "none", "target", -0.4, 0, 0.5, true}, {"sendDamage", "none", 120, 100, false, false}, diff --git a/sonic-radiance.love/scenes/battlesystem/actors/ennemy.lua b/sonic-radiance.love/scenes/battlesystem/actors/ennemy.lua index 7317e6b..0a2157a 100644 --- a/sonic-radiance.love/scenes/battlesystem/actors/ennemy.lua +++ b/sonic-radiance.love/scenes/battlesystem/actors/ennemy.lua @@ -15,7 +15,7 @@ function Ennemy:draw() self:drawSprite(0, -self.z) local x, y = self.world.map:gridToPixel(self.x, self.y, true) - self.owner:drawHUD(x - 12, y - self.owner.abstract.data.hudHeight) + self.owner:drawHUD(x - 12, y - self.owner.abstract.data.hudHeight - self.z) if (self.isSelected) then local height = 32 diff --git a/sonic-radiance.love/scenes/battlesystem/controllers/fighters/character.lua b/sonic-radiance.love/scenes/battlesystem/controllers/fighters/character.lua index 60de919..0beb8e3 100644 --- a/sonic-radiance.love/scenes/battlesystem/controllers/fighters/character.lua +++ b/sonic-radiance.love/scenes/battlesystem/controllers/fighters/character.lua @@ -18,22 +18,15 @@ function HeroFighter:new(owner, character, id) self.statusbar = StatusBar(self.abstract, self.turnSystem.scene) self:initVoices() - self.action = nil - self.selection = nil end function HeroFighter:updateAssets(dt) self.statusbar:update(dt) - - if (self.action ~= nil) then - if (self.action.isStarted) then - self.action:update(dt) - end - end end function HeroFighter:update(dt) + HeroFighter.super.update(self, dt) if (self.selection ~= nil) then self.selection:update(dt) end @@ -111,11 +104,6 @@ function HeroFighter:goBackToMenu() self.selection = nil end -function HeroFighter:finishAction() - self.action = nil - self:setInactive() -end - -- LIFE functions function HeroFighter:setHP(value, relative) HeroFighter.super.setHP(self, value, relative) diff --git a/sonic-radiance.love/scenes/battlesystem/controllers/fighters/parent.lua b/sonic-radiance.love/scenes/battlesystem/controllers/fighters/parent.lua index 6399b6b..cfa69c8 100644 --- a/sonic-radiance.love/scenes/battlesystem/controllers/fighters/parent.lua +++ b/sonic-radiance.love/scenes/battlesystem/controllers/fighters/parent.lua @@ -18,6 +18,8 @@ function FighterParent:new(owner, isHero, id) self.isActive = false self.isAlive = true + + self.action = nil end -- LIFE handling functions @@ -77,10 +79,10 @@ function FighterParent:createActor() end function FighterParent:update(dt) - counter = counter + dt - if (counter > 1) then - counter = 0 - self:setInactive() + if (self.action ~= nil) then + if (self.action.isStarted) then + self.action:update(dt) + end end end @@ -111,6 +113,11 @@ function FighterParent:startAction() end +function FighterParent:finishAction() + self.action = nil + self:setInactive() +end + function FighterParent:getUniqueIdentificator() local side = 1 if (isHero == false) then diff --git a/sonic-radiance.love/scenes/battlesystem/controllers/fighters/systems/behaviours/init.lua b/sonic-radiance.love/scenes/battlesystem/controllers/fighters/systems/behaviours/init.lua new file mode 100644 index 0000000..6676ba4 --- /dev/null +++ b/sonic-radiance.love/scenes/battlesystem/controllers/fighters/systems/behaviours/init.lua @@ -0,0 +1,5 @@ +local folder = "scenes.battlesystem.controllers.fighters.systems.behaviours." + +return { + ["random"] = require(folder .. "random") +} diff --git a/sonic-radiance.love/scenes/battlesystem/controllers/fighters/systems/behaviours/parent.lua b/sonic-radiance.love/scenes/battlesystem/controllers/fighters/systems/behaviours/parent.lua new file mode 100644 index 0000000..52c2b92 --- /dev/null +++ b/sonic-radiance.love/scenes/battlesystem/controllers/fighters/systems/behaviours/parent.lua @@ -0,0 +1,61 @@ +local BehaviourParent = Object:extend() + +function BehaviourParent:new(fighter, skilldata, targetList, highestScore) + self.fighter = fighter + self.scoreList = {} + self.targetList = targetList + self.skilldata = skilldata + self.highestScore = highestScore + + self:initScoreList() + self:calculateAllScores() +end + +function BehaviourParent:initScoreList() + for i, target in ipairs(self.targetList) do + self.scoreList[target.name] = 0 + end +end + +function BehaviourParent:calculateAllScores() + for i, target in ipairs(self.targetList) do + self.scoreList[target.name] = self:calculateScore(target) + end +end + +function BehaviourParent:calculateScore(target) + return 0 +end + +function BehaviourParent:getScore(target) + return self.scoreList[target.name] +end + +function BehaviourParent:isBestTarget(target, bestTargetScore, isHighest) + if (bestTargetScore == nil) then + return true + else + if (isHighest) then + return (self:getScore(target) >= bestTargetScore) + else + return (self:getScore(target) <= bestTargetScore) + end + end +end + +function BehaviourParent:getTarget() + local bestTargetScore = nil + local finalTarget = nil + + for i, target in ipairs(self.targetList) do + if (self:isBestTarget(target, bestTargetScore, self.isHighest)) then + finalTarget = target + bestTargetScore = self:getScore(target) + end + end + + return finalTarget +end + + +return BehaviourParent diff --git a/sonic-radiance.love/scenes/battlesystem/controllers/fighters/systems/behaviours/random.lua b/sonic-radiance.love/scenes/battlesystem/controllers/fighters/systems/behaviours/random.lua new file mode 100644 index 0000000..9922355 --- /dev/null +++ b/sonic-radiance.love/scenes/battlesystem/controllers/fighters/systems/behaviours/random.lua @@ -0,0 +1,12 @@ +local ParentBehaviour = require "scenes.battlesystem.controllers.fighters.systems.behaviours.parent" +local RandomBehaviour = ParentBehaviour:extend() + +function RandomBehaviour:new(fighter, skilldata, targetList) + RandomBehaviour.super.new(self, fighter, skilldata, targetList, true) +end + +function RandomBehaviour:calculateScore(target) + return math.random(1, 200) +end + +return RandomBehaviour diff --git a/sonic-radiance.love/scenes/battlesystem/controllers/fighters/systems/ennemyaction.lua b/sonic-radiance.love/scenes/battlesystem/controllers/fighters/systems/ennemyaction.lua new file mode 100644 index 0000000..72e343d --- /dev/null +++ b/sonic-radiance.love/scenes/battlesystem/controllers/fighters/systems/ennemyaction.lua @@ -0,0 +1,22 @@ +local ActionParent = require "scenes.battlesystem.controllers.fighters.systems.actions.parent" +local EnnemyAction = ActionParent:extend() + +function EnnemyAction:new(fighter, skill) + self.data = game.skills:getEnnemySkillData(skill) + EnnemyAction.super.new(self, fighter) +end + +function EnnemyAction:needTarget() + return (self.data.targetNumber == 1), self.data.targetEnnemies +end + +function EnnemyAction:startAction() + core.debug:print("cbs/action", "Starting flee action") + self:loadChoregraphyFromSkill(self.data) +end + +function EnnemyAction:getData() + return self.data +end + +return EnnemyAction diff --git a/sonic-radiance.love/scenes/battlesystem/controllers/fighters/villain.lua b/sonic-radiance.love/scenes/battlesystem/controllers/fighters/villain.lua index e3a7313..b25dc4e 100644 --- a/sonic-radiance.love/scenes/battlesystem/controllers/fighters/villain.lua +++ b/sonic-radiance.love/scenes/battlesystem/controllers/fighters/villain.lua @@ -2,6 +2,9 @@ local FighterParent = require "scenes.battlesystem.controllers.fighters.parent" local VillainFighter = FighterParent:extend() local SimpleHPBar = require "game.modules.gui.simplehpbar" +local EnnemyAction = require "scenes.battlesystem.controllers.fighters.systems.ennemyaction" +local behaviourList = require "scenes.battlesystem.controllers.fighters.systems.behaviours" + local POSITIONS = {1, 3, 5} local ENNEMY_LINE = 11; @@ -19,7 +22,6 @@ function VillainFighter:updateAssets(dt) self.hpbar:update(dt) end - function VillainFighter:getAbstract() return game.ennemies:getEnnemyData(self.category, self.name) end @@ -30,9 +32,42 @@ function VillainFighter:createActor() end function VillainFighter:startAction() + core.debug:print("cbs/villainFighter", "choosing an action") + self.action = nil + self:selectAction() +end + +function VillainFighter:selectAction() + if (#self.abstract.skills < 1) then + self:finishAction() + else + local skillId = math.floor(math.random(1, #self.abstract.skills)) + local skill = self.abstract.skills[skillId] + self.action = EnnemyAction(self, skill) + self:verifyTargets() + end +end + +function VillainFighter:verifyTargets() + local needTarget, targetEnnemies = self.action:needTarget() + + if (needTarget) then + if (targetEnnemies) then + --fighter, skilldata, targetList + local Behaviour = behaviourList[self.abstract.data.behaviour] + local behav = Behaviour(self, self.action:getData(), self.owner.turnSystem.player:getTargets(true)) + self.action:setTarget(behav:getTarget()) + self.action:start() + else + --self.selection = SelectionSystem(self, self.owner, false) + end + else + self.action:start() + end end + function VillainFighter:endAction() end