-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathspectator.lua
More file actions
295 lines (232 loc) · 9.45 KB
/
spectator.lua
File metadata and controls
295 lines (232 loc) · 9.45 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
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
ACT_FREECAM_SUBMERGED = ACT_GROUP_SUBMERGED | allocate_mario_action(ACT_FLAG_MOVING)
ACT_FREECAM = allocate_mario_action(ACT_FLAG_MOVING)
ACT_FOLLOW_IDLE_SUBMERGED = ACT_GROUP_SUBMERGED | allocate_mario_action(ACT_FLAG_IDLE)
ACT_FOLLOW_IDLE = allocate_mario_action(ACT_FLAG_IDLE)
spectatorHideHud = false
local followTargetIndex = 0
---@param m MarioState
local function freecam(m)
-- set action to idle if we are not in freecam
if gPlayerSyncTable[m.playerIndex].spectatorState ~= SPECTATOR_STATE_FREECAM then
return set_mario_action(m, ACT_IDLE, 0)
end
-- set mario's angle to his intended pos
m.faceAngle.y = m.intendedYaw
-- drop any held object if we are holding a object
mario_drop_held_object(m)
-- set quicksand depth var
m.quicksandDepth = 0
-- air steps
update_air_without_turn(m)
perform_air_step(m, 0)
-- y velocity
if m.controller.buttonDown & Z_TRIG ~= 0 then
m.vel.y = -50
elseif m.controller.buttonDown & A_BUTTON ~= 0 then
m.vel.y = 50
else
m.vel.y = 0
end
-- set all velocity vars to 0 if we aren't holding the stick
if m.controller.rawStickX == 0 and m.controller.rawStickY == 0 then
m.vel.x = 0
m.vel.z = 0
m.forwardVel = 0
end
local speed = 25
if m.controller.buttonDown & B_BUTTON ~= 0 then
speed = 75
end
if m.forwardVel > 0 then
m.forwardVel = speed
end
return 0
end
---@param m MarioState
local function follow_idle(m)
if (gPlayerSyncTable[m.playerIndex].state ~= SPECTATOR
and (gPlayerSyncTable[m.playerIndex
].state ~= WILDCARD_ROLE
or gGlobalSyncTable.gamemode == FREEZE_TAG))
or gPlayerSyncTable[m.playerIndex].spectatorState ~= SPECTATOR_STATE_FOLLOW then
return set_mario_action(m, ACT_IDLE, 0)
end
perform_air_step(m, 0)
m.forwardVel = 0
m.vel.x = 0
m.vel.y = 0
m.vel.z = 0
m.slideVelX = 0
m.slideVelZ = 0
if not gNetworkPlayers[followTargetIndex].connected
or gPlayerSyncTable[followTargetIndex].state == SPECTATOR
or (gPlayerSyncTable[followTargetIndex].state == WILDCARD_ROLE
and gGlobalSyncTable.gamemode ~= FREEZE_TAG) then return 0 end
local targetMario = gMarioStates[followTargetIndex]
m.pos.x = targetMario.pos.x
m.pos.y = targetMario.pos.y
m.pos.z = targetMario.pos.z
return 0
end
---@param m MarioState
local function mario_update(m)
local s = gPlayerSyncTable[m.playerIndex]
if (s.state == SPECTATOR
or (s.state == WILDCARD_ROLE and gGlobalSyncTable.gamemode ~= FREEZE_TAG))
and (s.spectatorState ~= SPECTATOR_STATE_MARIO
or (gGlobalSyncTable.gamemode == SARDINES
and m.playerIndex ~= 0
and gPlayerSyncTable[0].state ~= WILDCARD_ROLE
and gPlayerSyncTable[0].state ~= RUNNER)) then
obj_set_model_extended(m.marioObj, E_MODEL_NONE)
elseif (s.state == SPECTATOR
or (s.state == WILDCARD_ROLE))
and gGlobalSyncTable.gamemode == SARDINES then
m.particleFlags = 0
end
if m.playerIndex ~= 0 then return end
if s.state ~= SPECTATOR
and (s.state ~= WILDCARD_ROLE
or gGlobalSyncTable.gamemode == FREEZE_TAG) then
s.spectatorState = SPECTATOR_STATE_MARIO
spectatorHideHud = false
return
end
if showSettings or isPaused then goto states end
if m.controller.buttonPressed & D_JPAD ~= 0 then
s.spectatorState = s.spectatorState - 1
if s.spectatorState < SPECTATOR_STATE_MARIO then s.spectatorState = SPECTATOR_STATE_FOLLOW end
end
if m.controller.buttonPressed & U_JPAD ~= 0 then
s.spectatorState = s.spectatorState + 1
if s.spectatorState > SPECTATOR_STATE_FOLLOW then s.spectatorState = SPECTATOR_STATE_MARIO end
end
if m.controller.buttonPressed & X_BUTTON ~= 0 then
spectatorHideHud = not spectatorHideHud
end
::states::
if s.spectatorState == SPECTATOR_STATE_FREECAM then
-- set action to freecam action depending on if we are submerged or not
if m.pos.y < m.waterLevel - 50 then
set_mario_action(m, ACT_FREECAM_SUBMERGED, 0)
else
set_mario_action(m, ACT_FREECAM, 0)
end
elseif s.spectatorState == SPECTATOR_STATE_FOLLOW then
-- set action to follow idle action depending on if we are submerged or not
if m.pos.y < m.waterLevel - 50 then
set_mario_action(m, ACT_FOLLOW_IDLE_SUBMERGED, 0)
else
set_mario_action(m, ACT_FOLLOW_IDLE, 0)
end
if isPaused or showSettings then return end
-- follow index selection
if m.controller.buttonPressed & R_JPAD ~= 0 or followTargetIndex == 0 then
local originalIndex = followTargetIndex
followTargetIndex = followTargetIndex + 1
if followTargetIndex >= MAX_PLAYERS then
followTargetIndex = originalIndex
goto endr
end
while not gNetworkPlayers[followTargetIndex].connected
or (gPlayerSyncTable[followTargetIndex].state == SPECTATOR
or gPlayerSyncTable[followTargetIndex].state == -1
or (gPlayerSyncTable[followTargetIndex].state == WILDCARD_ROLE
and gGlobalSyncTable.gamemode ~= FREEZE_TAG)) do
followTargetIndex = followTargetIndex + 1
if followTargetIndex >= MAX_PLAYERS then
followTargetIndex = originalIndex
break
end
end
::endr::
end
if m.controller.buttonPressed & L_JPAD ~= 0 then
local originalIndex = followTargetIndex
followTargetIndex = followTargetIndex - 1
while not gNetworkPlayers[followTargetIndex].connected
or gPlayerSyncTable[followTargetIndex].state == SPECTATOR
or gPlayerSyncTable[followTargetIndex].state == -1
or (gPlayerSyncTable[followTargetIndex].state == WILDCARD_ROLE
and gGlobalSyncTable.gamemode ~= FREEZE_TAG) do
followTargetIndex = followTargetIndex - 1
if followTargetIndex <= 0 then
followTargetIndex = originalIndex
break
end
end
end
-- if our follow taget index is invalid, go thru each player starting from index 1, and find a valid player
if not gNetworkPlayers[followTargetIndex].connected
or gPlayerSyncTable[followTargetIndex].state == SPECTATOR
or (gPlayerSyncTable[followTargetIndex].state == WILDCARD_ROLE
and gGlobalSyncTable.gamemode ~= FREEZE_TAG)
or followTargetIndex == 0
or gPlayerSyncTable[followTargetIndex].state == -1 then
followTargetIndex = 1
while not gNetworkPlayers[followTargetIndex].connected
or gPlayerSyncTable[followTargetIndex].state == SPECTATOR
or (gPlayerSyncTable[followTargetIndex].state == WILDCARD_ROLE
and gGlobalSyncTable.gamemode ~= FREEZE_TAG) do
followTargetIndex = followTargetIndex + 1
if followTargetIndex >= MAX_PLAYERS then followTargetIndex = 0; break end
end
end
end
end
local function hud_bottom_render()
if spectatorHideHud then return end
local s = gPlayerSyncTable[0]
local text = ""
if s.spectatorState == SPECTATOR_STATE_FREECAM then
text = "Freecam"
elseif s.spectatorState == SPECTATOR_STATE_MARIO then
text = "Mario"
elseif s.spectatorState == SPECTATOR_STATE_FOLLOW then
local isPlayerConncted = false
for i = 1, MAX_PLAYERS - 1 do
if gNetworkPlayers[i].connected
and gPlayerSyncTable[i].state ~= SPECTATOR
and (gPlayerSyncTable[i].state ~= WILDCARD_ROLE
or gGlobalSyncTable.gamemode == FREEZE_TAG) then
isPlayerConncted = true
break
end
end
if isPlayerConncted then
text = "< " .. strip_hex(gNetworkPlayers[followTargetIndex].name) .. " (" .. tostring(network_global_index_from_local(followTargetIndex)) .. ")" .. " >"
else
text = "Nobody is connected"
end
end
local scale = 1.5
local screenWidth = djui_hud_get_screen_width()
local screenHeight = djui_hud_get_screen_height()
local width = djui_hud_measure_text(text) * scale
local x = (screenWidth - width) / 2.0
local y = screenHeight - (32 * scale)
-- render rect
djui_hud_set_color(0, 0, 0, 128)
djui_hud_render_rect(x - (12 * scale), y, width + (24 * scale), (32 * scale))
djui_hud_set_color(255, 255, 255, 255);
djui_hud_print_text(text, x, y, scale);
end
local function hud_render()
-- sanity checks
if gPlayerSyncTable[0].state ~= SPECTATOR
and (gPlayerSyncTable[0].state ~= WILDCARD_ROLE
or gGlobalSyncTable.gamemode == FREEZE_TAG) then return end
if gGlobalSyncTable.roundState == ROUND_RUNNERS_WIN
or gGlobalSyncTable.roundState == ROUND_TAGGERS_WIN
or gGlobalSyncTable.roundState == ROUND_VOTING
or gGlobalSyncTable.roundState == ROUND_TOURNAMENT_LEADERBOARD then return end
djui_hud_set_font(djui_menu_get_font())
djui_hud_set_resolution(RESOLUTION_DJUI)
hud_bottom_render()
end
hook_event(HOOK_MARIO_UPDATE, mario_update)
hook_event(HOOK_ON_HUD_RENDER, hud_render)
hook_mario_action(ACT_FREECAM, freecam)
hook_mario_action(ACT_FREECAM_SUBMERGED, freecam)
hook_mario_action(ACT_FOLLOW_IDLE, follow_idle)
hook_mario_action(ACT_FOLLOW_IDLE_SUBMERGED, follow_idle)