forked from nikki93/scene-creator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvariables.lua
More file actions
162 lines (134 loc) · 3.99 KB
/
variables.lua
File metadata and controls
162 lines (134 loc) · 3.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
-- Start / stop
function Common:startVariables()
self.variables = {}
end
-- Message kind definitions
function Common:defineVariablesMessageKinds()
-- From anyone to all
self:defineMessageKind("updateVariables", self.sendOpts.reliableToAll)
end
-- Utils
function Common:variablesReset()
if self._initialVariables then
self:send("updateVariables", self._initialVariables)
end
end
function Common:variablesNamesToValues()
local variableNameToValue = {}
if self.variables ~= nil then
for _, variable in ipairs(self.variables) do
variableNameToValue[variable.name] = variable.value
end
end
return variableNameToValue
end
function Common:variablesNames()
local names = {}
for i = 1, #self.variables do
names[i] = self.variables[i].name
end
return names
end
function Common:variableNameToId(name)
for i = 1, #self.variables do
if self.variables[i].name == name then
return self.variables[i].id
end
end
return nil
end
function Common:variableIdToName(id)
for i = 1, #self.variables do
if self.variables[i].id == id then
return self.variables[i].name
end
end
return "(none)"
end
function Common:variableIdToValue(id)
for i = 1, #self.variables do
if self.variables[i].id == id then
return self.variables[i].value
end
end
return 0
end
local function variableReachesValueTrigger(self, actorId, variableId, newValue)
self.behaviorsByName.Rules:fireTrigger(
"variable reaches value",
actorId,
{},
{
filter = function(params)
if params.variableId ~= variableId then
return false
end
if params.comparison == "equal" and newValue == params.value then
return true
end
if params.comparison == "less or equal" and newValue <= params.value then
return true
end
if params.comparison == "greater or equal" and newValue >= params.value then
return true
end
return false
end
}
)
end
local function fireVariableTriggers(self, variableId, newValue)
jsEvents.send(
"GHOST_MESSAGE",
{
messageType = "CHANGE_DECK_STATE",
data = {
variables = self.variables
}
}
)
for actorId, actor in pairs(self.actors) do
self.behaviorsByName.Rules:fireTrigger(
"variable changes",
actorId,
{},
{
filter = function(params)
return params.variableId == variableId
end
}
)
variableReachesValueTrigger(self, actorId, variableId, newValue)
end
end
function Common:variableSetToValue(variableId, value)
for i = 1, #self.variables do
if self.variables[i].id == variableId then
self.variables[i].value = value
fireVariableTriggers(self, variableId, self.variables[i].value)
end
end
end
function Common:variableChangeByValue(variableId, changeBy)
for i = 1, #self.variables do
if self.variables[i].id == variableId then
self.variables[i].value = self.variables[i].value + changeBy
fireVariableTriggers(self, variableId, self.variables[i].value)
end
end
end
-- Message receivers
function Common.receivers:updateVariables(time, variables)
for i = 1, #variables do
if not variables[i].value then
variables[i].value = variables[i].initialValue
end
end
self.variables = variables
self._initialVariables = util.deepCopyTable(variables or {})
end
function Common.receivers:postAddActor(time, actorId)
for i = 1, #self.variables do
variableReachesValueTrigger(self, actorId, self.variables[i].id, self.variables[i].value)
end
end