From 83d3bfa7757e3345452bf9c7e1dc777232b462ca Mon Sep 17 00:00:00 2001 From: Kazhnuz Date: Mon, 22 Mar 2021 16:12:15 +0100 Subject: [PATCH] feat: add condition system for events --- sonic-radiance.love/core/utils/table.lua | 8 +++++ .../datas/gamedata/events/test/mighty.lua | 6 ++++ .../datas/gamedata/maps/sti/plain/test.lua | 1 + .../datas/gamedata/maps/sti/plain/test.tmx | 1 + .../game/events/conditions/init.lua | 8 +++++ .../game/events/conditions/utils.lua | 31 +++++++++++++++++++ sonic-radiance.love/game/events/init.lua | 26 +++++++++++----- 7 files changed, 74 insertions(+), 7 deletions(-) create mode 100644 sonic-radiance.love/datas/gamedata/events/test/mighty.lua create mode 100644 sonic-radiance.love/game/events/conditions/init.lua create mode 100644 sonic-radiance.love/game/events/conditions/utils.lua diff --git a/sonic-radiance.love/core/utils/table.lua b/sonic-radiance.love/core/utils/table.lua index 90064a6..29b1122 100644 --- a/sonic-radiance.love/core/utils/table.lua +++ b/sonic-radiance.love/core/utils/table.lua @@ -33,6 +33,14 @@ function Table.contain(table, content) return false end +function Table.toString(table) + local string = "{" + for key, value in pairs(table) do + string = string .. key .. ":" .. value .. "," + end + return string .. "}" +end + function Table.reduce(list, fn) local acc for k, v in ipairs(list) do diff --git a/sonic-radiance.love/datas/gamedata/events/test/mighty.lua b/sonic-radiance.love/datas/gamedata/events/test/mighty.lua new file mode 100644 index 0000000..1da68f9 --- /dev/null +++ b/sonic-radiance.love/datas/gamedata/events/test/mighty.lua @@ -0,0 +1,6 @@ +return { + ["actions"] = { + {"dialogBox", "haveMoney:lt:300", "You seem to be a bit short on money...", "Mighty", ""}, + {"dialogBox", "haveMoney:ge:300", "Wow you're rich !", "Mighty", ""} + } +} \ 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 df0b581..95d38b9 100644 --- a/sonic-radiance.love/datas/gamedata/maps/sti/plain/test.lua +++ b/sonic-radiance.love/datas/gamedata/maps/sti/plain/test.lua @@ -503,6 +503,7 @@ return { properties = { ["charId"] = 3, ["charset"] = "perso2", + ["event"] = "test.mighty", ["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 34f1bbf..c45bcd6 100644 --- a/sonic-radiance.love/datas/gamedata/maps/sti/plain/test.tmx +++ b/sonic-radiance.love/datas/gamedata/maps/sti/plain/test.tmx @@ -113,6 +113,7 @@ + diff --git a/sonic-radiance.love/game/events/conditions/init.lua b/sonic-radiance.love/game/events/conditions/init.lua new file mode 100644 index 0000000..3e6f34a --- /dev/null +++ b/sonic-radiance.love/game/events/conditions/init.lua @@ -0,0 +1,8 @@ +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 new file mode 100644 index 0000000..882a0c9 --- /dev/null +++ b/sonic-radiance.love/game/events/conditions/utils.lua @@ -0,0 +1,31 @@ +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 2be7d12..670fcfe 100644 --- a/sonic-radiance.love/game/events/init.lua +++ b/sonic-radiance.love/game/events/init.lua @@ -2,6 +2,7 @@ local EventManager = Object:extend() local stepObjectList = require "game.events.event" local eventUtils = require "game.events.utils" +local Conditions = require "game.events.conditions" function EventManager:new(scene) @@ -36,16 +37,27 @@ function EventManager:addFlag(flagname, flagcontent) table.insert(self.flags, flag) 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 +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 + return utils.table.contain(self.flags, flagToTest) end function EventManager:update(dt) @@ -63,7 +75,7 @@ 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 (not self:haveFlag(stepData.condition)) then + if (not self:testCondition(stepData.condition)) then self:switchStep() else if (stepObjectList[stepData.name] ~= nil) then