-
Notifications
You must be signed in to change notification settings - Fork 26
/
Monster.ttslua
203 lines (165 loc) · 6.98 KB
/
Monster.ttslua
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
local Check = require("Kdm/Util/Check")
local EventManager = require("Kdm/Util/EventManager")
local Location = require("Kdm/Location")
local log = require("Kdm/Log").ForModule("Monster")
local NamedObject = require("Kdm/NamedObject")
local Ui = require("Kdm/Ui")
local Util = require("Kdm/Util/Util")
---------------------------------------------------------------------------------------------------
local Monster = {}
Monster.STATS = {
["movement"] = true,
["toughness"] = true,
["damage"] = true,
["speed"] = true,
["accuracy"] = true,
["evasion"] = true,
["luck"] = true,
}
Monster.TOKEN_STATS = {
["Movement Token"] = "movement",
["Toughness Token"] = "toughness",
["Damage Token"] = "damage",
["Speed Token"] = "speed",
["Accuracy Token"] = "accuracy",
["Evasion Token"] = "evasion",
["Luck Token"] = "luck",
}
---------------------------------------------------------------------------------------------------
function Monster.Init(saveState)
Monster.boardObject = NamedObject.Get("Showdown Board")
Monster.stats = {}
Monster.tokenStats = {}
for stat, _ in pairs(Monster.STATS) do
Monster.stats[stat] = saveState[stat] or 0
Monster.tokenStats[stat] = 0
end
-----------------------------------------------------------------------------------------------
local ui = Ui.Create3d("Monster", Monster.boardObject, 10.74)
Monster.ui = ui
Monster.statCounters = {}
local x = -6.264187
local xEnd = -6.628941
local width = xEnd - x
local y1 = 1.288212
local y1End = 1.649527
local height = y1End - y1
local y7 = 4.921028
local dy = (y7 - y1) / 6
for i, stat in ipairs({
"movement",
"toughness",
"damage",
"speed",
"accuracy",
"evasion",
"luck",
}) do
local y = y1 + (i - 1) * dy
Monster.statCounters[stat] = ui:Counter({ id = stat, topLeft = { x = x, y = y }, bottomRight = { x = x + width, y = y + height }, fontSize = 260, text = Monster.Stat(stat), onValueChanged = function(delta)
Monster.SetStat(stat, Monster.stats[stat] + delta)
end })
end
ui:ApplyToObject()
-----------------------------------------------------------------------------------------------
local monsterTokensLocation = Location.Get("Monster Tokens")
monsterTokensLocation:AddDropHandler(function(object)
Monster.UpdateStats()
end)
monsterTokensLocation:AddPickUpHandler(function(object)
if object and object.getGMNotes() == "Tokens" and Monster.TOKEN_STATS[object.getName()] then
log:Debugf("Unregistering token [%s]%s for collisions", object.getGUID(), object.getName())
object.unregisterCollisions()
end
Monster.UpdateStats()
end)
EventManager.AddHandler("onObjectCollisionEnter", Monster.OnObjectCollisionEnter)
end
---------------------------------------------------------------------------------------------------
function Monster.PostInit()
Monster.UpdateStats()
end
---------------------------------------------------------------------------------------------------
function Monster.Save()
local saveState = {}
for stat, _ in pairs(Monster.STATS) do
saveState[stat] = Monster.stats[stat] or 0
end
return saveState
end
---------------------------------------------------------------------------------------------------
function Monster.Stat(stat)
assert(Check(Monster.STATS[stat]))
return Monster.stats[stat] + Monster.tokenStats[stat]
end
---------------------------------------------------------------------------------------------------
function Monster.SetStat(stat, value)
assert(Check(Monster.STATS[stat], "Unrecognized stat %s", stat))
assert(Check.Num(value))
log:Debugf("Setting monster stat %s to %d", stat, value)
if Monster.stats[stat] != value then
Monster.stats[stat] = value
local newValue = Monster.Stat(stat)
Monster.statCounters[stat]:SetText(newValue)
EventManager.FireEvent(EventManager.ON_MONSTER_STAT_CHANGED, stat, newValue)
end
end
---------------------------------------------------------------------------------------------------
function Monster.UpdateStats()
local tokenStats = {}
for _, object in ipairs(Location.Get("Monster Tokens"):AllObjects()) do
local stat = Monster.TOKEN_STATS[object.getName()]
if object.getGMNotes() == "Tokens" and stat then
local value = Util.TokenValue(object)
log:Debugf("Found token [%s]%s with value %d", object.getGUID(), object.getName(), value)
tokenStats[stat] = (tokenStats[stat] or 0) + value
object.registerCollisions()
end
end
Monster.UpdateTokenStats(tokenStats)
end
---------------------------------------------------------------------------------------------------
function Monster.UpdateTokenStats(tokenStats)
for stat, _ in pairs(Monster.STATS) do
local tokenStat = tokenStats[stat] or 0
if Monster.tokenStats[stat] != tokenStat then
Monster.tokenStats[stat] = tokenStat
local newValue = Monster.Stat(stat)
log:Debugf("Updating monster stat %s to %d", stat, newValue)
Monster.statCounters[stat]:SetText(newValue)
EventManager.FireEvent(EventManager.ON_MONSTER_STAT_CHANGED, stat, newValue)
end
end
end
---------------------------------------------------------------------------------------------------
function Monster.OnObjectCollisionEnter(object, collisionInfo)
-- This is a hack to detect flipped tokens
-- There's no onFlip event, and flipping doesn't trigger onPickUp, so we rely on this to detect flips
-- of *already registered* tokens.
local collisionObject = collisionInfo.collision_object
if collisionObject != Monster.boardObject then
return
end
log:Debugf("[%s]%s collided with [%s]%s", object.getGUID(), object.getName(), collisionObject.getGUID(), collisionObject.getName())
local monsterTokensLocation = Location.Get("Monster Tokens")
for _, location in ipairs(Location.ObjectLocations(object)) do
if location == monsterTokensLocation then
log:Debugf("Token [%s]%s found in %s, updating stats", object.getGUID(), object.getName(), location)
Monster.UpdateStats()
end
end
end
---------------------------------------------------------------------------------------------------
return {
Init = Monster.Init,
PostInit = Monster.PostInit,
Save = Monster.Save,
Movement = function() return Monster.Stat("movement") end,
Toughness = function() return Monster.Stat("toughness") end,
Damage = function() return Monster.Stat("damage") end,
Speed = function() return Monster.Stat("speed") end,
Accuracy = function() return Monster.Stat("accuracy") end,
Evasion = function() return Monster.Stat("evasion") end,
Luck = function() return Monster.Stat("luck") end,
SetStat = Monster.SetStat,
}