feat: add condition system for events
This commit is contained in:
parent
cb7b087d5f
commit
83d3bfa775
7 changed files with 74 additions and 7 deletions
|
@ -33,6 +33,14 @@ function Table.contain(table, content)
|
||||||
return false
|
return false
|
||||||
end
|
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)
|
function Table.reduce(list, fn)
|
||||||
local acc
|
local acc
|
||||||
for k, v in ipairs(list) do
|
for k, v in ipairs(list) do
|
||||||
|
|
|
@ -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", ""}
|
||||||
|
}
|
||||||
|
}
|
|
@ -503,6 +503,7 @@ return {
|
||||||
properties = {
|
properties = {
|
||||||
["charId"] = 3,
|
["charId"] = 3,
|
||||||
["charset"] = "perso2",
|
["charset"] = "perso2",
|
||||||
|
["event"] = "test.mighty",
|
||||||
["isSolid"] = true
|
["isSolid"] = true
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
|
@ -113,6 +113,7 @@
|
||||||
<properties>
|
<properties>
|
||||||
<property name="charId" type="int" value="3"/>
|
<property name="charId" type="int" value="3"/>
|
||||||
<property name="charset" value="perso2"/>
|
<property name="charset" value="perso2"/>
|
||||||
|
<property name="event" value="test.mighty"/>
|
||||||
<property name="isSolid" type="bool" value="true"/>
|
<property name="isSolid" type="bool" value="true"/>
|
||||||
</properties>
|
</properties>
|
||||||
</object>
|
</object>
|
||||||
|
|
8
sonic-radiance.love/game/events/conditions/init.lua
Normal file
8
sonic-radiance.love/game/events/conditions/init.lua
Normal 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
|
31
sonic-radiance.love/game/events/conditions/utils.lua
Normal file
31
sonic-radiance.love/game/events/conditions/utils.lua
Normal 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;
|
|
@ -2,6 +2,7 @@ local EventManager = Object:extend()
|
||||||
|
|
||||||
local stepObjectList = require "game.events.event"
|
local stepObjectList = require "game.events.event"
|
||||||
local eventUtils = require "game.events.utils"
|
local eventUtils = require "game.events.utils"
|
||||||
|
local Conditions = require "game.events.conditions"
|
||||||
|
|
||||||
|
|
||||||
function EventManager:new(scene)
|
function EventManager:new(scene)
|
||||||
|
@ -36,16 +37,27 @@ function EventManager:addFlag(flagname, flagcontent)
|
||||||
table.insert(self.flags, flag)
|
table.insert(self.flags, flag)
|
||||||
end
|
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)
|
function EventManager:haveFlag(flagToTest)
|
||||||
if (flagToTest == nil or flagToTest == "") then
|
if (flagToTest == nil or flagToTest == "") then
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
for i, flag in ipairs(self.flags) do
|
return utils.table.contain(self.flags, flagToTest)
|
||||||
if (flag == flagToTest) then
|
|
||||||
return true
|
|
||||||
end
|
|
||||||
end
|
|
||||||
return false
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function EventManager:update(dt)
|
function EventManager:update(dt)
|
||||||
|
@ -63,7 +75,7 @@ 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 (not self:haveFlag(stepData.condition)) then
|
if (not self:testCondition(stepData.condition)) then
|
||||||
self:switchStep()
|
self:switchStep()
|
||||||
else
|
else
|
||||||
if (stepObjectList[stepData.name] ~= nil) then
|
if (stepObjectList[stepData.name] ~= nil) then
|
||||||
|
|
Loading…
Reference in a new issue