feat: add options dialog

Fix #5
This commit is contained in:
Kazhnuz 2021-03-22 14:57:21 +01:00
parent b4e7796a0a
commit d8c9a652ae
7 changed files with 79 additions and 4 deletions

View file

@ -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", ""},
}
}

View file

@ -485,6 +485,7 @@ return {
properties = { properties = {
["charId"] = 2, ["charId"] = 2,
["charset"] = "perso2", ["charset"] = "perso2",
["event"] = "test.rouge",
["isSolid"] = true ["isSolid"] = true
} }
}, },

View file

@ -105,6 +105,7 @@
<properties> <properties>
<property name="charId" type="int" value="2"/> <property name="charId" type="int" value="2"/>
<property name="charset" value="perso2"/> <property name="charset" value="perso2"/>
<property name="event" value="test.rouge"/>
<property name="isSolid" type="bool" value="true"/> <property name="isSolid" type="bool" value="true"/>
</properties> </properties>
</object> </object>

View file

@ -2,6 +2,7 @@ return {
["wait"] = {"duration"}, ["wait"] = {"duration"},
["simpleMessage"] = {"message"}, ["simpleMessage"] = {"message"},
["dialogBox"] = {"message", "title", "avatar"}, ["dialogBox"] = {"message", "title", "avatar"},
["optionBox"] = {"message", "title", "avatar", "option1", "option2", "option3", "flag"},
--[name] = {args...}, --[name] = {args...},
} }

View file

@ -9,7 +9,40 @@ function DialogBox:new(controller, args)
end end
function DialogBox:start() 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 end
function DialogBox:update(dt) function DialogBox:update(dt)

View file

@ -1,5 +1,6 @@
return { return {
["wait"] = require("game.events.event.wait"), ["wait"] = require("game.events.event.wait"),
["simpleMessage"] = require("game.events.event.simpleMessage"), ["simpleMessage"] = require("game.events.event.simpleMessage"),
["dialogBox"] = require("game.events.event.dialogbox") ["dialogBox"] = require("game.events.event.dialogbox"),
["optionBox"] = require("game.events.event.dialogbox")
} }

View file

@ -12,10 +12,12 @@ function EventManager:new(scene)
self.currentStepId = 0 self.currentStepId = 0
self.currentStep = nil self.currentStep = nil
self.stepList = {} self.stepList = {}
self.flags = {}
end end
function EventManager:startEvent(owner, stepList) function EventManager:startEvent(owner, stepList)
self.stepList = stepList self.stepList = stepList
self.flags = {}
self.owner = owner self.owner = owner
self.isStarted = true self.isStarted = true
self.currentStepId = 0 self.currentStepId = 0
@ -23,6 +25,29 @@ function EventManager:startEvent(owner, stepList)
self.scene:startEvent() self.scene:startEvent()
end 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) function EventManager:update(dt)
if (self.isStarted) then if (self.isStarted) then
if (self.currentStep ~= nil) then if (self.currentStep ~= nil) then
@ -38,8 +63,12 @@ function EventManager:switchStep()
self.currentStepId = self.currentStepId + 1 self.currentStepId = self.currentStepId + 1
local stepData = eventUtils.getStepDatas(self.stepList[self.currentStepId]) local stepData = eventUtils.getStepDatas(self.stepList[self.currentStepId])
core.debug:print("event", "Starting step " .. stepData.name) core.debug:print("event", "Starting step " .. stepData.name)
if (stepObjectList[stepData.name] ~= nil) then if (not self:haveFlag(stepData.condition)) then
self.currentStep = stepObjectList[stepData.name](self, stepData.arguments) self:switchStep()
else
if (stepObjectList[stepData.name] ~= nil) then
self.currentStep = stepObjectList[stepData.name](self, stepData.arguments)
end
end end
else else
self:endEvent() self:endEvent()