Fixing hp bugs

This commit is contained in:
Vladislav Khorev 2026-05-18 09:17:05 +03:00
parent 49501e32ce
commit 21bf3f0710
10 changed files with 38 additions and 16 deletions

View File

@ -75,12 +75,13 @@ end
game_api.set_location_callbacks(
function()
print("Enter location dorm")
local hp = game_api.getIntValue("player_hp")
local hp = game_api.getFloatValue("player_hp")
game_api.set_player_hp(hp)
end,
function()
print("Exit location dorm")
local hp = game_api.get_player_hp()
game_api.setIntValue("player_hp", hp)
game_api.setFloatValue("player_hp", hp)
end
)

View File

@ -38,12 +38,13 @@ game_api.set_darklands_callbacks(
game_api.set_location_callbacks(
function()
print("Enter location uni exterior")
local hp = game_api.getIntValue("player_hp")
local hp = game_api.getFloatValue("player_hp")
game_api.set_player_hp(hp)
end,
function()
print("Exit location uni exterior")
local hp = game_api.get_player_hp()
game_api.setIntValue("player_hp", hp)
game_api.setFloatValue("player_hp", hp)
end
)

View File

@ -334,24 +334,26 @@ function setDay1setup()
game_api.set_npc_enabled(5, false)
end
game_api.set_location_callbacks(
function()
print("Enter location uni interior")
local hp = game_api.getIntValue("player_hp")
local hp = game_api.getFloatValue("player_hp")
game_api.set_player_hp(hp)
--[[
local need_sleep = game_api.getIntValue("need_sleep")
local day = game_api.getIntValue("day")
if (day==1) then
setDay1setup()
end
end]]
end,
function()
print("Exit location uni interior")
local hp = game_api.get_player_hp()
game_api.setIntValue("player_hp", hp)
game_api.setFloatValue("player_hp", hp)
end
)
print("Lua script loaded successfully!")

View File

@ -7,7 +7,6 @@
#include "GameConstants.h"
#include "Environment.h"
namespace ZL {
const float ATTACK_COOLDOWN_TIME = 1.6f;
@ -149,8 +148,6 @@ float Character::getSpeedMultiplier(float transitionProgress) const {
void Character::update(int64_t deltaMs) {
if (!enabled) return;
if (initialHp <= 0.f) initialHp = hp;
if (slowMoTimeRemaining > 0.0f) {
slowMoTimeRemaining -= static_cast<float>(deltaMs) / 1000.0f;

View File

@ -108,9 +108,7 @@ public:
std::string npcId;
std::string npcName;
float hp = 200.f;
// Captured lazily from `hp` on the first update() tick if left at zero;
// set explicitly if you need a different reference for the health bar.
float initialHp = 0.f;
float initialHp = 200.f;
int battle_state = 0;

View File

@ -189,7 +189,7 @@ namespace ZL
ItemRegistry::instance().loadFromJson("resources/config2/items.json", CONST_ZIP_FILE);
globalInts["player_hp"] = 200;
globalFloats["player_hp"] = 200;
LocationSetup uniInteriorParams;
uniInteriorParams.gameObjectsJsonPath = "resources/config2/gameobjects_uni_interior.json";
@ -226,6 +226,7 @@ namespace ZL
locations["uni_interior"] = std::make_shared<Location>(renderer, inventory);
locations["uni_interior"]->setup(uniInteriorParams);
locations["uni_interior"]->scriptEngine.setGlobalStore(&globalInts);
locations["uni_interior"]->scriptEngine.setGlobalFloatStore(&globalFloats);
locations["uni_interior"]->requestDarklandsTransition = [this]() { return startDarklandsTransition(); };
if (locations["uni_interior"]->player)
locations["uni_interior"]->player->onDeathAnimComplete = [this]() { startDarklandsTransition(); };
@ -245,6 +246,7 @@ namespace ZL
locations["uni_exterior"] = std::make_shared<Location>(renderer, inventory);
locations["uni_exterior"]->setup(uniExteriorParams);
locations["uni_exterior"]->scriptEngine.setGlobalStore(&globalInts);
locations["uni_exterior"]->scriptEngine.setGlobalFloatStore(&globalFloats);
locations["uni_exterior"]->requestDarklandsTransition = [this]() { return startDarklandsTransition(); };
if (locations["uni_exterior"]->player)
locations["uni_exterior"]->player->onDeathAnimComplete = [this]() { startDarklandsTransition(); };
@ -265,6 +267,7 @@ namespace ZL
locations["location_dorm"] = std::make_shared<Location>(renderer, inventory);
locations["location_dorm"]->setup(params_dorm);
locations["location_dorm"]->scriptEngine.setGlobalStore(&globalInts);
locations["location_dorm"]->scriptEngine.setGlobalFloatStore(&globalFloats);
locations["location_dorm"]->requestDarklandsTransition = [this]() { return startDarklandsTransition(); };
if (locations["location_dorm"]->player)
locations["location_dorm"]->player->onDeathAnimComplete = [this]() { startDarklandsTransition(); };

View File

@ -56,6 +56,7 @@ namespace ZL {
InteractiveObject* pickedUpObject = nullptr;
std::unordered_map<std::string, int> globalInts;
std::unordered_map<std::string, float> globalFloats;
MenuManager menuManager;

View File

@ -17,6 +17,7 @@ namespace ZL {
std::unordered_map<std::string, sol::protected_function> triggerExitCallbacks;
std::unordered_map<std::string, sol::protected_function> cutsceneCompleteCallbacks;
std::unordered_map<std::string, int>* globalInts = nullptr;
std::unordered_map<std::string, float>* globalFloats = nullptr;
sol::protected_function locationEnterCallback;
sol::protected_function locationExitCallback;
sol::protected_function darklandsEnterCallback;
@ -192,6 +193,18 @@ namespace ZL {
api.set_function("is_darklands",
[game]() { return game->isDarklands; });
api.set_function("setFloatValue",
[this_impl = impl.get()](const std::string& key, float value) {
if (this_impl->globalFloats)
(*this_impl->globalFloats)[key] = value;
});
api.set_function("getFloatValue",
[this_impl = impl.get()](const std::string& key) -> float {
if (!this_impl->globalFloats) return 0.0f;
auto it = this_impl->globalFloats->find(key);
return it != this_impl->globalFloats->end() ? it->second : 0.0f;
});
// set_player_hp(value) — sets the player's current HP directly.
api.set_function("set_player_hp",
[game](float value) {
@ -476,6 +489,10 @@ namespace ZL {
if (impl) impl->globalInts = store;
}
void ScriptEngine::setGlobalFloatStore(std::unordered_map<std::string, float>* store) {
if (impl) impl->globalFloats = store;
}
void ScriptEngine::callLocationEnterCallback() {
if (!impl || !impl->locationEnterCallback.valid()) return;
auto result = impl->locationEnterCallback();

View File

@ -29,6 +29,7 @@ public:
void callTriggerExitCallback(const std::string& zoneId);
void setGlobalStore(std::unordered_map<std::string, int>* store);
void setGlobalFloatStore(std::unordered_map<std::string, float>* store);
void callLocationEnterCallback();
void callLocationExitCallback();

View File

@ -351,6 +351,7 @@ namespace ZL {
npc->position = Eigen::Vector3f(npcData.positionX, npcData.positionY, npcData.positionZ);
npc->facingAngle = npcData.facingAngle;
npc->hp = npcData.hp;
npc->initialHp = npcData.hp;
npc->canAttack = npcData.canAttack;
npc->enabled = npcData.enabled;
npc->interactionRadius = npcData.interactionRadius;