Working on the server
This commit is contained in:
1 parent
075750442f
commit
28c6e83eda
30 files changed
+646
-362
No files matched your search
+82
-12
@@ -1,7 +1,85 @@
|
||||
#include "LocationState.h"
|
||||
#include <stdexcept>
|
||||
|
||||
namespace FRG {
|
||||
|
||||
std::shared_ptr<LocationLocalState> ClientLocationState::getOrCreate(
|
||||
const std::string& locationId)
|
||||
{
|
||||
auto& state = locations[locationId];
|
||||
if (!state) state = std::make_shared<LocationLocalState>();
|
||||
return state;
|
||||
}
|
||||
|
||||
void ClientLocationState::clear()
|
||||
{
|
||||
locations.clear();
|
||||
}
|
||||
|
||||
void LocationLocalState::save(nlohmann::json& out) const
|
||||
{
|
||||
out = {
|
||||
{"cameraAzimuth", cameraAzimuth},
|
||||
{"cameraInclination", cameraInclination}
|
||||
};
|
||||
}
|
||||
|
||||
void LocationLocalState::load(const nlohmann::json& in)
|
||||
{
|
||||
cameraAzimuth = in.at("cameraAzimuth").get<float>();
|
||||
cameraInclination = in.at("cameraInclination").get<float>();
|
||||
}
|
||||
|
||||
void ClientLocationState::save(nlohmann::json& out) const
|
||||
{
|
||||
out["locations"] = nlohmann::json::object();
|
||||
for (const auto& [id, state] : locations) {
|
||||
if (!state) continue;
|
||||
nlohmann::json value;
|
||||
state->save(value);
|
||||
out["locations"][id] = std::move(value);
|
||||
}
|
||||
}
|
||||
|
||||
void ClientLocationState::load(const nlohmann::json& in)
|
||||
{
|
||||
const auto& values = in.at("locations");
|
||||
if (!values.is_object())
|
||||
throw std::runtime_error("Client location state locations must be an object");
|
||||
for (auto& [id, state] : locations)
|
||||
if (state) *state = LocationLocalState{};
|
||||
for (const auto& [id, value] : values.items())
|
||||
getOrCreate(id)->load(value);
|
||||
}
|
||||
|
||||
void saveSinglePlayerPersistence(
|
||||
const nlohmann::json& networkState,
|
||||
const ClientLocationState& localState,
|
||||
nlohmann::json& out)
|
||||
{
|
||||
nlohmann::json savedLocalState;
|
||||
localState.save(savedLocalState);
|
||||
out = {
|
||||
{"persistenceVersion", kSinglePlayerPersistenceVersion},
|
||||
{"networkState", networkState},
|
||||
{"localState", std::move(savedLocalState)}
|
||||
};
|
||||
}
|
||||
|
||||
void loadSinglePlayerPersistence(
|
||||
const nlohmann::json& in,
|
||||
nlohmann::json& networkState,
|
||||
ClientLocationState& localState)
|
||||
{
|
||||
const int version = in.value("persistenceVersion", -1);
|
||||
if (version != kSinglePlayerPersistenceVersion)
|
||||
throw std::runtime_error(
|
||||
"Unsupported single-player persistence version: " +
|
||||
std::to_string(version));
|
||||
networkState = in.at("networkState");
|
||||
localState.load(in.at("localState"));
|
||||
}
|
||||
|
||||
namespace {
|
||||
nlohmann::json saveCar(const NpcCar& car)
|
||||
{
|
||||
@@ -44,12 +122,12 @@ namespace FRG {
|
||||
}
|
||||
|
||||
|
||||
std::vector<Eigen::Vector3f> LocationState::planPathExceptMyself(const Eigen::Vector3f& start, const Eigen::Vector3f& end, const std::string& entityId) const
|
||||
std::vector<Eigen::Vector3f> LocationNetworkState::planPathExceptMyself(const Eigen::Vector3f& start, const Eigen::Vector3f& end, const std::string& entityId) const
|
||||
{
|
||||
return navigationMaps[activeNavigationIndex].findPathToNearest(start, end, {});
|
||||
}
|
||||
|
||||
void LocationState::initNavigation(
|
||||
void LocationNetworkState::initNavigation(
|
||||
const std::vector<std::string>& paths, const std::string& zipFile)
|
||||
{
|
||||
const int savedActiveNavigationIndex = activeNavigationIndex;
|
||||
@@ -68,11 +146,8 @@ namespace FRG {
|
||||
: 0;
|
||||
}
|
||||
|
||||
void LocationState::save(nlohmann::json& out) const
|
||||
void LocationNetworkState::save(nlohmann::json& out) const
|
||||
{
|
||||
out["cameraAzimuth"] = cameraAzimuth;
|
||||
out["cameraInclination"] = cameraInclination;
|
||||
|
||||
out["activeNavigationIndex"] = activeNavigationIndex;
|
||||
|
||||
out["targetInteractiveObjectId"] = targetInteractiveObjectId;
|
||||
@@ -85,13 +160,8 @@ void LocationState::save(nlohmann::json& out) const
|
||||
};
|
||||
}
|
||||
|
||||
void LocationState::load(const nlohmann::json& in)
|
||||
void LocationNetworkState::load(const nlohmann::json& in)
|
||||
{
|
||||
//logger() << "LocationState::load step 1" << std::endl;
|
||||
|
||||
cameraAzimuth = in.value("cameraAzimuth", cameraAzimuth);
|
||||
cameraInclination = in.value("cameraInclination", cameraInclination);
|
||||
|
||||
activeNavigationIndex = in.value("activeNavigationIndex", activeNavigationIndex);
|
||||
|
||||
targetInteractiveObjectId = in.value("targetInteractiveObjectId", std::string());
|
||||
|
||||
Reference in new issue
Block a user