diff --git a/resources/start_dorm.lua b/resources/start_dorm.lua index e966fde..ba0c2e3 100644 --- a/resources/start_dorm.lua +++ b/resources/start_dorm.lua @@ -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 ) diff --git a/resources/start_uni_exterior.lua b/resources/start_uni_exterior.lua index 58fd3f8..0c947b4 100644 --- a/resources/start_uni_exterior.lua +++ b/resources/start_uni_exterior.lua @@ -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 ) diff --git a/resources/start_uni_interior.lua b/resources/start_uni_interior.lua index 8b8b57a..ae47bca 100644 --- a/resources/start_uni_interior.lua +++ b/resources/start_uni_interior.lua @@ -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!") \ No newline at end of file diff --git a/src/Character.cpp b/src/Character.cpp index f3c006c..b963c51 100644 --- a/src/Character.cpp +++ b/src/Character.cpp @@ -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(deltaMs) / 1000.0f; diff --git a/src/Character.h b/src/Character.h index 423f16a..38d7756 100644 --- a/src/Character.h +++ b/src/Character.h @@ -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; diff --git a/src/Game.cpp b/src/Game.cpp index 21a28b2..879ec2a 100644 --- a/src/Game.cpp +++ b/src/Game.cpp @@ -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(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(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(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(); }; diff --git a/src/Game.h b/src/Game.h index 9a32c73..aa6a0ad 100644 --- a/src/Game.h +++ b/src/Game.h @@ -56,6 +56,7 @@ namespace ZL { InteractiveObject* pickedUpObject = nullptr; std::unordered_map globalInts; + std::unordered_map globalFloats; MenuManager menuManager; diff --git a/src/ScriptEngine.cpp b/src/ScriptEngine.cpp index d772bc9..5f8bee8 100644 --- a/src/ScriptEngine.cpp +++ b/src/ScriptEngine.cpp @@ -17,6 +17,7 @@ namespace ZL { std::unordered_map triggerExitCallbacks; std::unordered_map cutsceneCompleteCallbacks; std::unordered_map* globalInts = nullptr; + std::unordered_map* 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* store) { + if (impl) impl->globalFloats = store; + } + void ScriptEngine::callLocationEnterCallback() { if (!impl || !impl->locationEnterCallback.valid()) return; auto result = impl->locationEnterCallback(); diff --git a/src/ScriptEngine.h b/src/ScriptEngine.h index 255259d..dd03155 100644 --- a/src/ScriptEngine.h +++ b/src/ScriptEngine.h @@ -29,6 +29,7 @@ public: void callTriggerExitCallback(const std::string& zoneId); void setGlobalStore(std::unordered_map* store); + void setGlobalFloatStore(std::unordered_map* store); void callLocationEnterCallback(); void callLocationExitCallback(); diff --git a/src/items/GameObjectLoader.cpp b/src/items/GameObjectLoader.cpp index a38e786..765dc13 100644 --- a/src/items/GameObjectLoader.cpp +++ b/src/items/GameObjectLoader.cpp @@ -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;