From f7221971c3b134b58c4b288c0628545889af1191 Mon Sep 17 00:00:00 2001 From: Kazhnuz Date: Sat, 17 Jul 2021 20:09:05 +0200 Subject: [PATCH] improvement: use the predicate system for events --- .../datas/gamedata/events/test/mighty.lua | 3 +- .../datas/gamedata/events/test/rouge.lua | 3 +- .../game/events/conditions.lua | 12 +++++++ .../game/events/conditions/init.lua | 8 ----- .../game/events/conditions/utils.lua | 31 ------------------- sonic-radiance.love/game/events/init.lua | 16 ++-------- 6 files changed, 19 insertions(+), 54 deletions(-) create mode 100644 sonic-radiance.love/game/events/conditions.lua delete mode 100644 sonic-radiance.love/game/events/conditions/init.lua delete mode 100644 sonic-radiance.love/game/events/conditions/utils.lua diff --git a/sonic-radiance.love/datas/gamedata/events/test/mighty.lua b/sonic-radiance.love/datas/gamedata/events/test/mighty.lua index 1da68f9..4a82919 100644 --- a/sonic-radiance.love/datas/gamedata/events/test/mighty.lua +++ b/sonic-radiance.love/datas/gamedata/events/test/mighty.lua @@ -1,6 +1,7 @@ return { ["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", ""} } } \ No newline at end of file diff --git a/sonic-radiance.love/datas/gamedata/events/test/rouge.lua b/sonic-radiance.love/datas/gamedata/events/test/rouge.lua index c79b0bf..d737292 100644 --- a/sonic-radiance.love/datas/gamedata/events/test/rouge.lua +++ b/sonic-radiance.love/datas/gamedata/events/test/rouge.lua @@ -2,7 +2,8 @@ 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: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:3", "Let's see if you can keep it up", "Rouge", ""}, } diff --git a/sonic-radiance.love/game/events/conditions.lua b/sonic-radiance.love/game/events/conditions.lua new file mode 100644 index 0000000..d479a24 --- /dev/null +++ b/sonic-radiance.love/game/events/conditions.lua @@ -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 \ No newline at end of file diff --git a/sonic-radiance.love/game/events/conditions/init.lua b/sonic-radiance.love/game/events/conditions/init.lua deleted file mode 100644 index 3e6f34a..0000000 --- a/sonic-radiance.love/game/events/conditions/init.lua +++ /dev/null @@ -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 \ No newline at end of file diff --git a/sonic-radiance.love/game/events/conditions/utils.lua b/sonic-radiance.love/game/events/conditions/utils.lua deleted file mode 100644 index 882a0c9..0000000 --- a/sonic-radiance.love/game/events/conditions/utils.lua +++ /dev/null @@ -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; \ 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 72fbb89..af3530a 100644 --- a/sonic-radiance.love/game/events/init.lua +++ b/sonic-radiance.love/game/events/init.lua @@ -3,6 +3,7 @@ local EventManager = Object:extend() local stepObjectList = require "game.events.event" local Conditions = require "game.events.conditions" +local Predicates = require "birb.classes.predicate" function EventManager:new(scene) self.scene = scene @@ -37,19 +38,8 @@ function EventManager:addFlag(flagname, flagcontent) end function EventManager:testCondition(condition) - if (utils.string.isEmpty(condition)) then - return true - 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 + local predicate = Predicates.createPredicate(condition, Conditions, self) + return predicate:solve() end function EventManager:haveFlag(flagToTest)