Move spectate checking code to race gui

This commit is contained in:
Benau 2019-10-23 02:43:29 +08:00
parent f3a59b5649
commit 349fe30747
5 changed files with 54 additions and 42 deletions

View File

@ -545,6 +545,10 @@ public:
int getLiveJoinUntilTicks() const { return m_live_join_util; }
// ------------------------------------------------------------------------
void setLiveJoinKart(int util_ticks) { m_live_join_util = util_ticks; }
// ------------------------------------------------------------------------
/** Return the confirmed finish ticks (sent by the server)
* indicating that this kart has really finished the race. */
virtual int getNetworkConfirmedFinishTicks() const = 0;
}; // AbstractKart

View File

@ -327,7 +327,6 @@ void Kart::reset()
m_network_finish_check_ticks = 0;
m_network_confirmed_finish_ticks = 0;
m_enabled_network_spectator = false;
// Add karts back in case that they have been removed (i.e. in battle
// mode) - but only if they actually have a body (e.g. ghost karts
// don't have one).
@ -1369,43 +1368,6 @@ void Kart::update(int ticks)
m_saved_controller = NULL;
}
// FIXME: This shouldn't be in kart.cpp
auto cl = LobbyProtocol::get<ClientLobby>();
// Enable spectate mode after 2 seconds which allow the player to
// release left / right button if they keep pressing it after the
// finishing line (1 second here because m_network_finish_check_ticks is
// already 1 second ahead of time when crossing finished line)
if (cl && m_finished_race && m_controller->isLocalPlayerController()
&& race_manager->getNumLocalPlayers() == 1
&& race_manager->modeHasLaps()
&& World::getWorld()->isActiveRacePhase()
&& m_network_confirmed_finish_ticks > 0
&& World::getWorld()->getTicksSinceStart() >
m_network_confirmed_finish_ticks + stk_config->time2Ticks(1.0f)
&& !m_enabled_network_spectator)
{
m_enabled_network_spectator = true;
cl->setSpectator(true);
static bool msg_shown = false;
if (!msg_shown)
{
msg_shown = true;
RaceGUIBase* m = World::getWorld()->getRaceGUI();
if (m->getMultitouchGUI() != NULL)
{
m->recreateMultitouchGUI();
}
else
{
cl->addSpectateHelperMessage();
}
}
}//END OF FIXME
m_powerup->update(ticks);
// Reset any instant speed increase in the bullet kart

View File

@ -98,8 +98,6 @@ protected:
/** Is time flying activated */
bool m_is_jumping;
bool m_enabled_network_spectator;
/** The sign of torque to apply after hitting a bubble gum. */
bool m_bubblegum_torque_sign;
@ -589,6 +587,11 @@ public:
// ----------------------------------------------------------------------------------------
virtual Stars* getStarsEffect() const OVERRIDE
{ return m_stars_effect.get(); }
// ------------------------------------------------------------------------
/** Return the confirmed finish ticks (sent by the server)
* indicating that this kart has really finished the race. */
int getNetworkConfirmedFinishTicks() const OVERRIDE
{ return m_network_confirmed_finish_ticks; }
}; // Kart

View File

@ -77,7 +77,7 @@ RaceGUIBase::RaceGUIBase()
// I18N: Shown waiting for other players in network to finish loading or
// waiting
m_string_waiting_for_others = _("Waiting for others");
// I18N: Shown waiting for the server in network if live join or specatate
// I18N: Shown waiting for the server in network if live join or spectate
m_string_waiting_for_the_server = _("Waiting for the server");
m_music_icon = irr_driver->getTexture("notes.png");
@ -163,6 +163,7 @@ void RaceGUIBase::reset()
m_plunger_offset = core::vector2di(0,0);
m_plunger_speed = core::vector2df(0,0);
m_plunger_state = PLUNGER_STATE_INIT;
m_enabled_network_spectator = false;
clearAllMessages();
if (m_multitouch_gui != NULL)
@ -486,6 +487,45 @@ void RaceGUIBase::update(float dt)
m_referee->removeFromSceneGraph();
}
} // if referee node
// Check if switching to spectate mode is need
auto cl = LobbyProtocol::get<ClientLobby>();
World* w = World::getWorld();
if (m_enabled_network_spectator || !cl || !w)
return;
if (race_manager->getNumLocalPlayers() != 1 ||
!race_manager->modeHasLaps() ||
!w->isActiveRacePhase())
return;
for (unsigned i = 0; i < w->getNumKarts(); i++)
{
AbstractKart* k = w->getKart(i);
if (!k->getController()->isLocalPlayerController()
|| !k->hasFinishedRace())
continue;
// Enable spectate mode after 2 seconds which allow the player to
// release left / right button if they keep pressing it after the
// finishing line (1 second here because m_network_finish_check_ticks is
// already 1 second ahead of time when crossing finished line)
if (k->getNetworkConfirmedFinishTicks() > 0
&& w->getTicksSinceStart() >
k->getNetworkConfirmedFinishTicks() + stk_config->time2Ticks(1.0f))
{
m_enabled_network_spectator = true;
cl->setSpectator(true);
static bool msg_shown = false;
if (getMultitouchGUI() != NULL)
recreateMultitouchGUI();
else if (!msg_shown)
{
msg_shown = true;
cl->addSpectateHelperMessage();
}
}
}
} // update
// ----------------------------------------------------------------------------

View File

@ -149,7 +149,10 @@ private:
/** The referee scene node. */
Referee *m_referee;
/* True if spectating is possible in current GUI (when local player
* finished). */
bool m_enabled_network_spectator;
protected: