Skip to content

Commit 8b892d8

Browse files
GooberRFrafalh
andauthored
Add "follow killer" switch for spectate mode (#273)
* Add spectate_mode_follow_killer command * Move logic to multi_spectate.cpp * optimize code * rename function * Update multi_spectate.cpp remove unused bool --------- Co-authored-by: Rafał Harabień <[email protected]>
1 parent 223e2a5 commit 8b892d8

File tree

4 files changed

+28
-0
lines changed

4 files changed

+28
-0
lines changed

docs/CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ Version 1.9.0 (not released yet)
6161
- Add `version` command
6262
- Support autocompleting of level filenames and console commands from only 1 character
6363
- Add support for `bluebeard.bty` (sound config file) in mods
64+
- Add `spectate_mode_follow_killer` command (when player you are spectating dies, spectate their killer)
6465
- Remove level editor popups that stop user from navigating between modes until rebuilding
6566

6667
[@is-this-c](https://github.com/is-this-c)

game_patch/hud/multi_spectate.cpp

+23
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ static rf::Player* g_spectate_mode_target;
2424
static rf::Camera* g_old_target_camera = nullptr;
2525
static bool g_spectate_mode_enabled = false;
2626
static bool g_spawned_in_current_level = false;
27+
static bool g_spectate_mode_follow_killer = false;
2728

2829
void player_fpgun_set_player(rf::Player* pp);
2930

@@ -203,6 +204,18 @@ bool multi_spectate_execute_action(rf::ControlConfigAction action, bool was_pres
203204
return false;
204205
}
205206

207+
void multi_spectate_on_player_kill(rf::Player* victim, rf::Player* killer)
208+
{
209+
if (!g_spectate_mode_enabled) {
210+
return;
211+
}
212+
if (g_spectate_mode_follow_killer && g_spectate_mode_target == victim && killer != rf::local_player) {
213+
// spectate killer if we were spectating victim
214+
// avoid spectating ourselves if we somehow managed to kill the victim
215+
multi_spectate_set_target_player(killer);
216+
}
217+
}
218+
206219
void multi_spectate_on_destroy_player(rf::Player* player)
207220
{
208221
if (player != rf::local_player) {
@@ -272,6 +285,15 @@ static ConsoleCommand2 spectate_mode_minimal_ui_cmd{
272285
"Toggles spectate mode minimal UI",
273286
};
274287

288+
static ConsoleCommand2 spectate_mode_follow_killer_cmd{
289+
"spectate_mode_follow_killer",
290+
[]() {
291+
g_spectate_mode_follow_killer = !g_spectate_mode_follow_killer;
292+
rf::console::printf("Follow killer mode is %s", g_spectate_mode_follow_killer ? "enabled" : "disabled");
293+
},
294+
"When a player you're spectating dies, automatically spectate their killer",
295+
};
296+
275297
#if SPECTATE_MODE_SHOW_WEAPON
276298

277299
static void player_render_new(rf::Player* player)
@@ -322,6 +344,7 @@ void multi_spectate_appy_patch()
322344

323345
spectate_cmd.register_cmd();
324346
spectate_mode_minimal_ui_cmd.register_cmd();
347+
spectate_mode_follow_killer_cmd.register_cmd();
325348

326349
// Note: HUD rendering doesn't make sense because life and armor isn't synced
327350

game_patch/hud/multi_spectate.h

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ void multi_spectate_appy_patch();
1414
void multi_spectate_after_full_game_init();
1515
void multi_spectate_level_init();
1616
void multi_spectate_render();
17+
void multi_spectate_on_player_kill(rf::Player* player, rf::Player* killer);
1718
void multi_spectate_on_destroy_player(rf::Player* player);
1819
void multi_spectate_player_create_entity_post(rf::Player* player, rf::Entity* entity);
1920
bool multi_spectate_is_spectating();

game_patch/multi/kill.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "../rf/localize.h"
1010
#include "../rf/multi.h"
1111
#include "../rf/weapon.h"
12+
#include "../hud/multi_spectate.h"
1213
#include "server_internal.h"
1314

1415
bool kill_messages = true;
@@ -148,6 +149,8 @@ void on_player_kill(rf::Player* killed_player, rf::Player* killer_player)
148149
}
149150

150151
multi_apply_kill_reward(killer_player);
152+
153+
multi_spectate_on_player_kill(killed_player, killer_player);
151154
}
152155
}
153156

0 commit comments

Comments
 (0)