From d8c9a652ae7cdc9fd481ef59a765b5bc10ab39ae Mon Sep 17 00:00:00 2001 From: Kazhnuz Date: Mon, 22 Mar 2021 14:57:21 +0100 Subject: [PATCH] feat: add options dialog Fix #5 --- .../datas/gamedata/events/test/rouge.lua | 9 +++++ .../datas/gamedata/maps/sti/plain/test.lua | 1 + .../datas/gamedata/maps/sti/plain/test.tmx | 1 + sonic-radiance.love/game/events/arguments.lua | 1 + .../game/events/event/dialogbox.lua | 35 ++++++++++++++++++- .../game/events/event/init.lua | 3 +- sonic-radiance.love/game/events/init.lua | 33 +++++++++++++++-- 7 files changed, 79 insertions(+), 4 deletions(-) create mode 100644 sonic-radiance.love/datas/gamedata/events/test/rouge.lua diff --git a/sonic-radiance.love/datas/gamedata/events/test/rouge.lua b/sonic-radiance.love/datas/gamedata/events/test/rouge.lua new file mode 100644 index 0000000..c79b0bf --- /dev/null +++ b/sonic-radiance.love/datas/gamedata/events/test/rouge.lua @@ -0,0 +1,9 @@ +return { + ["actions"] = { + {"dialogBox", "", "Hi Big Blue. Let's test a bit the choice dialog.", "Rouge", ""}, + {"optionBox", "", "What do you want ?", "Rouge", "", "Ring", "A health fruit", "Battling badnics", "optionFlag"}, + {"dialogBox", "optionFlag:1", "Oooh, in need of cash ?", "Rouge", ""}, + {"dialogBox", "optionFlag:2", "Did you failed your battle ? Well, you'll have to train more", "Rouge", ""}, + {"dialogBox", "optionFlag:3", "Let's see if you can keep it up", "Rouge", ""}, + } +} \ No newline at end of file diff --git a/sonic-radiance.love/datas/gamedata/maps/sti/plain/test.lua b/sonic-radiance.love/datas/gamedata/maps/sti/plain/test.lua index 11e4b3a..df0b581 100644 --- a/sonic-radiance.love/datas/gamedata/maps/sti/plain/test.lua +++ b/sonic-radiance.love/datas/gamedata/maps/sti/plain/test.lua @@ -485,6 +485,7 @@ return { properties = { ["charId"] = 2, ["charset"] = "perso2", + ["event"] = "test.rouge", ["isSolid"] = true } }, diff --git a/sonic-radiance.love/datas/gamedata/maps/sti/plain/test.tmx b/sonic-radiance.love/datas/gamedata/maps/sti/plain/test.tmx index 8509c2c..34f1bbf 100644 --- a/sonic-radiance.love/datas/gamedata/maps/sti/plain/test.tmx +++ b/sonic-radiance.love/datas/gamedata/maps/sti/plain/test.tmx @@ -105,6 +105,7 @@ + diff --git a/sonic-radiance.love/game/events/arguments.lua b/sonic-radiance.love/game/events/arguments.lua index 453672c..0d78b62 100644 --- a/sonic-radiance.love/game/events/arguments.lua +++ b/sonic-radiance.love/game/events/arguments.lua @@ -2,6 +2,7 @@ return { ["wait"] = {"duration"}, ["simpleMessage"] = {"message"}, ["dialogBox"] = {"message", "title", "avatar"}, + ["optionBox"] = {"message", "title", "avatar", "option1", "option2", "option3", "flag"}, --[name] = {args...}, } diff --git a/sonic-radiance.love/game/events/event/dialogbox.lua b/sonic-radiance.love/game/events/event/dialogbox.lua index dfcb9a1..359dcc5 100644 --- a/sonic-radiance.love/game/events/event/dialogbox.lua +++ b/sonic-radiance.love/game/events/event/dialogbox.lua @@ -9,7 +9,40 @@ function DialogBox:new(controller, args) end function DialogBox:start() - Talkies.say(self.arguments.title, self.arguments.message) + local args = self:addOptions() + + if (args == nil) then + Talkies.say(self.arguments.title, self.arguments.message) + else + Talkies.say(self.arguments.title, self.arguments.message, args) + end +end + +function DialogBox:addOptions() + local args = {} + args.options = {} + local haveAddedSomething = false + if (self.arguments.option1 ~= nil) then + table.insert(args.options, {self.arguments.option1, function() self:setOption(1) end}) + haveAddedSomething = true + end + if (self.arguments.option2 ~= nil) then + table.insert(args.options, {self.arguments.option2, function() self:setOption(2) end}) + haveAddedSomething = true + end + if (self.arguments.option3 ~= nil) then + table.insert(args.options, {self.arguments.option3, function() self:setOption(3) end}) + haveAddedSomething = true + end + if (haveAddedSomething) then + return args + end +end + +function DialogBox:setOption(num) + print("option " .. num .. " chosen") + self.events:addFlag(self.arguments.flag, num) + self:finish() end function DialogBox:update(dt) diff --git a/sonic-radiance.love/game/events/event/init.lua b/sonic-radiance.love/game/events/event/init.lua index 607095d..759c906 100644 --- a/sonic-radiance.love/game/events/event/init.lua +++ b/sonic-radiance.love/game/events/event/init.lua @@ -1,5 +1,6 @@ return { ["wait"] = require("game.events.event.wait"), ["simpleMessage"] = require("game.events.event.simpleMessage"), - ["dialogBox"] = require("game.events.event.dialogbox") + ["dialogBox"] = require("game.events.event.dialogbox"), + ["optionBox"] = require("game.events.event.dialogbox") } \ No newline at end of file diff --git a/sonic-radiance.love/game/events/init.lua b/sonic-radiance.love/game/events/init.lua index 027ffc7..2be7d12 100644 --- a/sonic-radiance.love/game/events/init.lua +++ b/sonic-radiance.love/game/events/init.lua @@ -12,10 +12,12 @@ function EventManager:new(scene) self.currentStepId = 0 self.currentStep = nil self.stepList = {} + self.flags = {} end function EventManager:startEvent(owner, stepList) self.stepList = stepList + self.flags = {} self.owner = owner self.isStarted = true self.currentStepId = 0 @@ -23,6 +25,29 @@ function EventManager:startEvent(owner, stepList) self.scene:startEvent() end +function EventManager:addFlag(flagname, flagcontent) + if (flagcontent == true) then + flagcontent = "V" + end + if (flagcontent == false) then + flagcontent = "F" + end + local flag = flagname .. ":" .. flagcontent + table.insert(self.flags, flag) +end + +function EventManager:haveFlag(flagToTest) + if (flagToTest == nil or flagToTest == "") then + return true + end + for i, flag in ipairs(self.flags) do + if (flag == flagToTest) then + return true + end + end + return false +end + function EventManager:update(dt) if (self.isStarted) then if (self.currentStep ~= nil) then @@ -38,8 +63,12 @@ function EventManager:switchStep() self.currentStepId = self.currentStepId + 1 local stepData = eventUtils.getStepDatas(self.stepList[self.currentStepId]) core.debug:print("event", "Starting step " .. stepData.name) - if (stepObjectList[stepData.name] ~= nil) then - self.currentStep = stepObjectList[stepData.name](self, stepData.arguments) + if (not self:haveFlag(stepData.condition)) then + self:switchStep() + else + if (stepObjectList[stepData.name] ~= nil) then + self.currentStep = stepObjectList[stepData.name](self, stepData.arguments) + end end else self:endEvent()