From 214272468df34bf88f6ae8fef8338dfab46e160f Mon Sep 17 00:00:00 2001 From: Benau Date: Sat, 5 Jan 2019 00:42:00 +0800 Subject: [PATCH] Add left or right player action to change spectating target --- src/input/input_manager.cpp | 23 +++++++++++++++++++++++ src/network/protocols/client_lobby.cpp | 14 +++++++++++--- 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/src/input/input_manager.cpp b/src/input/input_manager.cpp index d26c2db2c..42cdd4f36 100644 --- a/src/input/input_manager.cpp +++ b/src/input/input_manager.cpp @@ -38,6 +38,7 @@ #include "modes/profile_world.hpp" #include "modes/world.hpp" #include "network/network_config.hpp" +#include "network/protocols/client_lobby.hpp" #include "network/rewind_manager.hpp" #include "physics/physics.hpp" #include "race/history.hpp" @@ -806,6 +807,28 @@ void InputManager::dispatchInput(Input::InputType type, int deviceID, if (pk == NULL) { + if (auto cl = LobbyProtocol::get()) + { + Camera* cam = Camera::getActiveCamera(); + if (cl->isSpectator() && cam) + { + // Network spectating handling + int current_idx = 0; + if (cam->getKart()) + current_idx = cam->getKart()->getWorldKartId(); + if (action == PA_STEER_LEFT && value == 0) + current_idx = ++current_idx % World::getWorld()->getNumKarts(); + else if (action == PA_STEER_RIGHT && value == 0) + { + if (current_idx == 0) + current_idx = World::getWorld()->getNumKarts() - 1; + else + current_idx = --current_idx; + } + cam->setKart(World::getWorld()->getKart(current_idx)); + return; + } + } Log::error("InputManager::dispatchInput", "Trying to process " "action for an unknown player"); return; diff --git a/src/network/protocols/client_lobby.cpp b/src/network/protocols/client_lobby.cpp index 9e2121990..dcc947e20 100644 --- a/src/network/protocols/client_lobby.cpp +++ b/src/network/protocols/client_lobby.cpp @@ -21,6 +21,7 @@ #include "audio/sfx_manager.hpp" #include "config/user_config.hpp" #include "config/player_manager.hpp" +#include "graphics/camera.hpp" #include "guiengine/modaldialog.hpp" #include "guiengine/message_queue.hpp" #include "guiengine/screen_keyboard.hpp" @@ -280,14 +281,21 @@ void ClientLobby::addAllPlayers(Event* event) // Disable until render gui during loading is bug free //StateManager::get()->enterGameState(); - // Live join if state is CONNECTED + // Live join or spectate if state is CONNECTED if (m_state.load() == CONNECTED) { World* w = World::getWorld(); w->setLiveJoinWorld(true); + Camera* cam = Camera::getActiveCamera(); for (unsigned i = 0; i < w->getNumKarts(); i++) { AbstractKart* k = w->getKart(i); + // Change spectating target to first non-eliminated kart + if (isSpectator() && cam && !k->isEliminated()) + { + cam->setKart(k); + cam = NULL; + } // The final joining ticks will be set by server later if (k->getController()->isLocalPlayerController()) k->setLiveJoinKart(std::numeric_limits::max()); @@ -960,7 +968,7 @@ void ClientLobby::backToLobby(Event *event) { case BLR_NO_GAME_FOR_LIVE_JOIN: // I18N: Error message shown if live join failed in network - msg = _("No more game is available for live join."); + msg = _("No more game is available for live join or spectating."); break; case BLR_NO_PLACE_FOR_LIVE_JOIN: // I18N: Error message shown if live join failed in network @@ -988,7 +996,7 @@ void ClientLobby::backToLobby(Event *event) void ClientLobby::finishedLoadingWorld() { NetworkString* ns = getNetworkString(1); - // Live join if state is CONNECTED + // Live join or spectate if state is CONNECTED ns->setSynchronous(m_state.load() == CONNECTED); ns->addUInt8(LE_CLIENT_LOADED_WORLD); sendToServer(ns, true);