feat: add condition system for events

This commit is contained in:
Kazhnuz 2021-03-22 16:12:15 +01:00
parent cb7b087d5f
commit 83d3bfa775
7 changed files with 74 additions and 7 deletions

View file

@ -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

View file

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

View file

@ -503,6 +503,7 @@ return {
properties = {
["charId"] = 3,
["charset"] = "perso2",
["event"] = "test.mighty",
["isSolid"] = true
}
},

View file

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

View file

@ -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

View file

@ -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;

View file

@ -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