improvement: use the predicate system for events

This commit is contained in:
Kazhnuz 2021-07-17 20:09:05 +02:00
parent d2db4e91a3
commit f7221971c3
6 changed files with 19 additions and 54 deletions

View file

@ -1,6 +1,7 @@
return { return {
["actions"] = { ["actions"] = {
{"dialogBox", "haveMoney:lt:300", "You seem to be a bit short on money...", "Mighty", ""}, {"dialogBox", "haveMoney:eq:0", "You don't have any rings !", "Mighty", ""},
{"dialogBox", {"haveMoney:lt:300", "haveMoney:gt:0"}, "You seems to only have a few rings", "Mighty", ""},
{"dialogBox", "haveMoney:ge:300", "Wow you're rich !", "Mighty", ""} {"dialogBox", "haveMoney:ge:300", "Wow you're rich !", "Mighty", ""}
} }
} }

View file

@ -2,7 +2,8 @@ return {
["actions"] = { ["actions"] = {
{"dialogBox", "", "Hi Big Blue. Let's test a bit the choice dialog.", "Rouge", ""}, {"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"}, {"optionBox", "", "What do you want ?", "Rouge", "", "Ring", "A health fruit", "Battling badnics", "optionFlag"},
{"dialogBox", "optionFlag:1", "Oooh, in need of cash ?", "Rouge", ""}, {"dialogBox", {"optionFlag:1", "haveMoney:lt:300"}, "Oooh, in need of cash ?", "Rouge", ""},
{"dialogBox", {"optionFlag:1", "haveMoney:gt:300"}, "You doesn't seem that poor to me.", "Rouge", ""},
{"dialogBox", "optionFlag:2", "Did you failed your battle ? Well, you'll have to train more", "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", ""}, {"dialogBox", "optionFlag:3", "Let's see if you can keep it up", "Rouge", ""},
} }

View file

@ -0,0 +1,12 @@
local Conditions = {}
function Conditions.haveMoney(cond, predicate, asker)
return predicate.utils.testVariables(game.loot.rings , cond[2], cond[3])
end
function Conditions.default(cond, predicate, asker)
local flag = predicate.utils.merge(cond)
return asker:haveFlag(flag)
end
return Conditions

View file

@ -1,8 +0,0 @@
local Conditions = {}
local ConditionsUtils = require "game.events.conditions.utils"
function Conditions.haveMoney(cond)
return ConditionsUtils.testVariables(game.loot.rings , cond[2], cond[3])
end
return Conditions

View file

@ -1,31 +0,0 @@
local ConditionsUtils = {}
function ConditionsUtils.testVariables(var1, testType, var2)
local var2 = tonumber(var2)
if (testType == "eq" and var1 == var2) then
return true
end
if (testType == "ne" and var1 ~= var2) then
return true
end
if (testType == "gt" and var1 > var2) then
return true
end
if (testType == "ge" and var1 >= var2) then
return true
end
if (testType == "lt" and var1 < var2) then
return true
end
if (testType == "le" and var1 <= var2) then
return true
end
return false
end
function ConditionsUtils.testBool(bool, boolType)
return (bool == (boolType == "V"))
end
return ConditionsUtils;

View file

@ -3,6 +3,7 @@ local EventManager = Object:extend()
local stepObjectList = require "game.events.event" local stepObjectList = require "game.events.event"
local Conditions = require "game.events.conditions" local Conditions = require "game.events.conditions"
local Predicates = require "birb.classes.predicate"
function EventManager:new(scene) function EventManager:new(scene)
self.scene = scene self.scene = scene
@ -37,19 +38,8 @@ function EventManager:addFlag(flagname, flagcontent)
end end
function EventManager:testCondition(condition) function EventManager:testCondition(condition)
if (utils.string.isEmpty(condition)) then local predicate = Predicates.createPredicate(condition, Conditions, self)
return true return predicate:solve()
end
local conditionArgs = utils.string.split(condition, ":")
if (Conditions[conditionArgs[1]] == nil) then
return self:haveFlag(condition)
else
local conditionFulfilled = Conditions[conditionArgs[1]](conditionArgs)
if (conditionArgs[#conditionArgs] == "not") then
conditionFulfilled = (not conditionFulfilled)
end
return conditionFulfilled
end
end end
function EventManager:haveFlag(flagToTest) function EventManager:haveFlag(flagToTest)