Fixing player HP display

This commit is contained in:
Vladislav Khorev committed 2026-11-07 01:57:29 +06:00
1 parent f4bd3b6bed
commit b932a07cba
3 files changed
+33 -14

No files matched your search

+33 -10
View File
@@ -420,9 +420,24 @@ namespace FRG {
}
void MenuManager::refreshHudVisibility() {
const auto itemIds = server_.snapshot()->inventoryItemIds;
const auto snapshot = server_.snapshot();
// The initial state can be loaded after the gameplay HUD was first
// constructed (notably for the remote campaign). Keep the root derived
// from the authoritative snapshot instead of relying on the transition
// callback to switch between day and Darklands HUDs.
// Pushed in-game screens (inventory, phone, journal, and pause menu)
// temporarily own the UI root while keeping the logical UI state as
// Gameplay. Do not replace those roots from the per-frame refresh.
if (uiState_ == GameUiState::Gameplay && uiManager.menuStackSize() == 0) {
const auto desiredRoot = gameplayHudRoot();
if (uiManager.getRoot() != desiredRoot) {
uiManager.replaceRoot(desiredRoot);
applyCurrentHealthBar();
setupGameplayHudCallbacks();
}
}
const auto itemIds = snapshot->inventoryItemIds;
const TutorialStep step = snapshot->tutorialStep;
const bool tutorial = snapshot->isTutorialActive();
const bool hasPhone = std::find(itemIds.begin(), itemIds.end(), "phone") != itemIds.end();
@@ -2212,17 +2227,25 @@ namespace FRG {
void MenuManager::applyCurrentHealthBar() {
logger() << "MenuManager::applyCurrentHealthBar called step 1" << std::endl;
const auto state = server_.snapshot();
logger() << "currentPlayerMaxHp_ is " << state->playerMaxHp << std::endl;
if (state->playerMaxHp <= 0.f) return;
logger() << "MenuManager::applyCurrentHealthBar called step 2" << std::endl;
logger() << "currentPlayerHp_ is " << state->playerHp << std::endl;
const auto snapshot = server_.snapshot();
if (!snapshot) return;
const float fraction = std::clamp(state->playerHp / state->playerMaxHp, 0.f, 1.f);
const auto playerIt = snapshot->players.find(server_.localPlayerId());
if (playerIt == snapshot->players.end()) return;
const CharacterState& playerState = playerIt->second.state;
const float hp = playerState.hp;
const float maxHp = playerState.initialHp;
logger() << "currentPlayerMaxHp_ is " << maxHp << std::endl;
if (maxHp <= 0.f) return;
logger() << "MenuManager::applyCurrentHealthBar called step 2" << std::endl;
logger() << "currentPlayerHp_ is " << hp << std::endl;
const float fraction = std::clamp(hp / maxHp, 0.f, 1.f);
uiManager.setSliderValue("healthBarFill", fraction);
std::string hpText = std::to_string(static_cast<int>(state->playerHp)) + "/" +
std::to_string(static_cast<int>(state->playerMaxHp));
std::string hpText = std::to_string(static_cast<int>(hp)) + "/" +
std::to_string(static_cast<int>(maxHp));
if (hpText.size() < 7) hpText.insert(0, 7 - hpText.size(), ' ');
uiManager.setText("healthValue", hpText);
}
-2
View File
@@ -46,8 +46,6 @@ struct GameSnapshot {
bool isDarklands = false;
bool isNight = false;
bool isDawn = false;
float playerHp = 0.f;
float playerMaxHp = 0.f;
int money = 0;
TutorialStep tutorialStep = TutorialStep::Completed;
bool tutorialPhoneChatScreenOpened = false;
-2
View File
@@ -192,8 +192,6 @@ GameSnapshot InternalGameServer::buildSnapshot() const
result.isDarklands = state_.isDarklands;
result.isNight = state_.isNight;
result.isDawn = state_.isDawn;
result.playerHp = state_.playerHp;
result.playerMaxHp = state_.playerMaxHp;
result.money = state_.money;
result.tutorialStep = state_.tutorialStep;
result.tutorialPhoneChatScreenOpened = state_.tutorialPhoneChatScreenOpened;