Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions include/ClientLib/ClientSessionHandler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ namespace tsom
ClientSessionHandler(NetworkSession* session, Nz::ApplicationBase& app, ConfigFile& config, Nz::EnttWorld& world, ClientBlockLibrary& blockLibrary);
~ClientSessionHandler();

template<typename F> void ForEachPlayer(F&& callback) const;

inline entt::handle GetControlledEntity() const;
inline EntityRegistry& GetEntityRegistry();
inline const EntityRegistry& GetEntityRegistry() const;
Expand Down Expand Up @@ -92,6 +94,7 @@ namespace tsom
NazaraSignal(OnPlayerJoined, const PlayerInfo& /*playerInfo*/);
NazaraSignal(OnPlayerLeave, const PlayerInfo& /*playerInfo*/);
NazaraSignal(OnPlayerNameUpdate, const PlayerInfo& /*playerInfo*/, const std::string& /*newNickname*/);
NazaraSignal(OnPlayerListUpdated);

static constexpr Packets::Helper::EntityId InvalidEntity = Nz::MaxValue();

Expand Down
11 changes: 11 additions & 0 deletions include/ClientLib/ClientSessionHandler.inl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,17 @@

namespace tsom
{
template<typename F>
void ClientSessionHandler::ForEachPlayer(F&& callback) const
{
for (PlayerIndex index = 0; index < m_players.size(); ++index)
{
auto& playerInfoOpt = m_players[index];
if (playerInfoOpt)
callback(index, *playerInfoOpt);
}
}

inline entt::handle ClientSessionHandler::GetControlledEntity() const
{
return m_playerControlledEntity;
Expand Down
6 changes: 6 additions & 0 deletions src/ClientLib/ClientSessionHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,8 @@ namespace tsom
playerInfo.nickname = std::move(playerData.nickname).Str();
playerInfo.isAuthenticated = playerData.isAuthenticated;
}

OnPlayerListUpdated();
}

void ClientSessionHandler::HandlePacket(Packets::S_NetworkStrings&& networkStrings)
Expand Down Expand Up @@ -499,6 +501,7 @@ namespace tsom
playerInfo.isAuthenticated = playerJoin.isAuthenticated;

OnPlayerJoined(playerInfo);
OnPlayerListUpdated();
}

void ClientSessionHandler::HandlePacket(Packets::S_PlayerLeave&& playerLeave)
Expand All @@ -510,6 +513,7 @@ namespace tsom
}

OnPlayerLeave(*m_players[playerLeave.index]);
OnPlayerListUpdated();

m_players[playerLeave.index].reset();
}
Expand All @@ -525,6 +529,8 @@ namespace tsom
auto& playerInfo = *m_players[playerNameUpdate.index];

OnPlayerNameUpdate(playerInfo, playerNameUpdate.newNickname);
OnPlayerListUpdated();

playerInfo.nickname = std::move(playerNameUpdate.newNickname).Str();
if (playerInfo.textSprite)
playerInfo.textSprite->Update(Nz::SimpleTextDrawer::Draw(playerInfo.nickname, 48, Nz::TextStyle_Regular, (playerInfo.isAuthenticated) ? Nz::Color::White() : Nz::Color::Gray()), 0.01f);
Expand Down
48 changes: 44 additions & 4 deletions src/Game/States/GameState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,9 @@ namespace tsom
});

m_interactionLabel = CreateWidget<Nz::SimpleLabelWidget>();
m_playerListWidget = CreateWidget<Nz::LabelWidget>();
RefreshPlayerListWidget();
m_playerListWidget->Hide();

m_onUnhandledKeyPressed.Connect(stateData.canvas->OnUnhandledKeyPressed, [this](const Nz::WindowEventHandler*, const Nz::WindowEvent::KeyEvent& event)
{
Expand Down Expand Up @@ -416,10 +419,10 @@ namespace tsom
m_toolsMenu->Show();
UpdateMouseLock();
}

break;
}


case Nz::Keyboard::Scancode::Escape:
{
if (m_escapeMenu->IsVisible())
Expand Down Expand Up @@ -550,15 +553,19 @@ namespace tsom
break;
}

case Nz::Keyboard::Scancode::F10:
{
m_playerListWidget->Show();
break;
}

default:
break;
}
});

m_onUnhandledKeyReleased.Connect(stateData.canvas->OnUnhandledKeyReleased, [this](const Nz::WindowEventHandler*, const Nz::WindowEvent::KeyEvent& event)
{
auto& stateData = GetStateData();

switch (event.scancode)
{
case Nz::Keyboard::Scancode::Tab:
Expand All @@ -584,6 +591,12 @@ namespace tsom
break;
}

case Nz::Keyboard::Scancode::F10:
{
m_playerListWidget->Hide();
break;
}

default:
break;
}
Expand Down Expand Up @@ -647,7 +660,7 @@ namespace tsom
{ Chatbox::ColorItem{ Nz::Color::White() } },
{ Chatbox::TextItem{ " joined the server" } }
}
});
});

spdlog::info("{0} joined the server", playerInfo.nickname);
});
Expand Down Expand Up @@ -682,6 +695,11 @@ namespace tsom
spdlog::info("{0} renamed to {1}", playerInfo.nickname, newNickname);
});

m_onPlayerListUpdated.Connect(stateData.sessionHandler->OnPlayerListUpdated, [this]
{
RefreshPlayerListWidget();
});

m_onControlledShip.Connect(stateData.sessionHandler->OnControlledShip, [this](entt::handle shipEntity, entt::handle shipExteriorEntity, const Nz::Quaternionf& referenceRotation)
{
m_pilotedShip = PilotedShip{
Expand Down Expand Up @@ -1298,11 +1316,33 @@ namespace tsom

m_crosshairEntity.get<Nz::NodeComponent>().SetPosition({ newSize.x * 0.5f, newSize.y * 0.5f });
m_healthOxygen.entity.get<Nz::NodeComponent>().SetPosition({ newSize.x * 0.5f - m_healthOxygen.entity.get<Nz::GraphicsComponent>().GetAABB().x / 2.f, hudNextHeight });
m_playerListWidget->Center();

m_escapeMenu->Center();
m_toolsMenu->Center();
}

void GameState::RefreshPlayerListWidget()
{
Nz::RichTextDrawer textDrawer;
textDrawer.AppendText(""); //< will override later

std::size_t playerCount = 0;
GetStateData().sessionHandler->ForEachPlayer([&](PlayerIndex playerIndex, const ClientSessionHandler::PlayerInfo& playerInfo)
{
textDrawer.SetTextColor((playerInfo.isAuthenticated) ? Nz::Color::Yellow() : Nz::Color::Gray());
textDrawer.AppendText(playerInfo.nickname, (playerCount == 0) ? true : false); //< force a new block to avoid being merged with the first one
textDrawer.AppendText("\n");

playerCount++;
});

textDrawer.SetBlockText(0, fmt::format("Players ({0})\n", playerCount));

m_playerListWidget->UpdateText(textDrawer);
m_playerListWidget->Center();
}

Nz::Vector3f GameState::RaycastCamera(const Nz::Vector3f& from, const Nz::Vector3f& to)
{
auto& physSystem = GetStateData().world->GetSystem<Nz::Physics3DSystem>();
Expand Down
5 changes: 4 additions & 1 deletion src/Game/States/GameState.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ namespace tsom

void BindControlledEntitySignals();
void LayoutWidgets(const Nz::Vector2f& newSize) override;
void RefreshPlayerListWidget();
Nz::Vector3f RaycastCamera(const Nz::Vector3f& from, const Nz::Vector3f& to);
void OnTick(Nz::Time elapsedTime, bool lastTick);
void SendInputs();
Expand All @@ -114,6 +115,7 @@ namespace tsom
NazaraSlot(ClientSessionHandler, OnPlayerJoined, m_onPlayerJoined);
NazaraSlot(ClientSessionHandler, OnPlayerLeave, m_onPlayerLeave);
NazaraSlot(ClientSessionHandler, OnPlayerNameUpdate, m_onPlayerNameUpdate);
NazaraSlot(ClientSessionHandler, OnPlayerListUpdated, m_onPlayerListUpdated);
NazaraSlot(Nz::Canvas, OnUnhandledKeyPressed, m_onUnhandledKeyPressed);
NazaraSlot(Nz::Canvas, OnUnhandledKeyReleased, m_onUnhandledKeyReleased);
NazaraSlot(Nz::Canvas, OnUnhandledMouseButtonPressed, m_mouseButtonReleasedSlot);
Expand Down Expand Up @@ -160,7 +162,7 @@ namespace tsom

std::optional<ConsoleExecutor> m_consoleExecutor;
std::optional<PilotedShip> m_pilotedShip;
std::shared_ptr<DebugOverlay> m_debugOverlay;
std::shared_ptr<DebugOverlay> m_debugOverlay;
std::size_t m_toolIndex;
std::vector<InputRotation> m_predictedInputRotations;
std::vector<std::unique_ptr<ToolBase>> m_tools;
Expand All @@ -187,6 +189,7 @@ namespace tsom
Nz::UInt8 m_nextInputIndex;
CameraMode m_cameraMode;
HealthOxygen m_healthOxygen;
Nz::LabelWidget* m_playerListWidget;
Nz::SimpleLabelWidget* m_interactionLabel;
BlockSelectionBar* m_blockSelectionBar;
Chatbox* m_chatBox;
Expand Down
Loading