refactor: move Weather instance to WorldInfo

This commit is contained in:
MihailRis
2025-02-28 18:49:44 +03:00
parent 0ea842580c
commit 9c4db8fa10
9 changed files with 116 additions and 61 deletions
+63
View File
@@ -0,0 +1,63 @@
#pragma once
#include <string>
#include "presets/WeatherPreset.hpp"
struct Weather : Serializable {
WeatherPreset a {};
WeatherPreset b {};
std::string nameA;
std::string nameB;
float t = 1.0f;
float speed = 0.0f;
void update(float delta) {
t += delta * speed;
t = std::min(t, 1.0f);
b.intensity = t;
a.intensity = 1.0f - t;
}
void change(WeatherPreset preset, float time, std::string name="") {
std::swap(a, b);
std::swap(nameA, nameB);
b = std::move(preset);
t = 0.0f;
speed = 1.0f / std::max(time, 1.e-5f);
nameB = std::move(name);
update(0.0f);
}
float fogOpacity() const {
return b.fogOpacity * t + a.fogOpacity * (1.0f - t);
}
float fogDencity() const {
return b.fogDencity * t + a.fogDencity * (1.0f - t);
}
float fogCurve() const {
return b.fogCurve * t + a.fogCurve * (1.0f - t);
}
dv::value serialize() const override {
return dv::object({
{"a", a.serialize()},
{"b", b.serialize()},
{"name-a", nameA},
{"name-b", nameB},
{"t", t},
{"speed", speed},
});
}
void deserialize(const dv::value& src) override {
a.deserializeOpt(src.at("a"));
b.deserializeOpt(src.at("b"));
src.at("name-a").get(nameA);
src.at("name-b").get(nameB);
src.at("t").get(t);
src.at("speed").get(speed);
}
};
+3 -2
View File
@@ -208,6 +208,7 @@ void WorldInfo::deserialize(const dv::value& root) {
totalTime = timeobj["total-time"].asNumber();
}
if (root.has("weather")) {
weather.deserialize(root["weather"]);
fog = root["weather"]["fog"].asNumber();
}
nextInventoryId = root["next-inventory-id"].asInteger(2);
@@ -231,8 +232,8 @@ dv::value WorldInfo::serialize() const {
timeobj["day-time-speed"] = daytimeSpeed;
timeobj["total-time"] = totalTime;
auto& weatherobj = root.object("weather");
weatherobj["fog"] = fog;
root["weather"] = weather.serialize();
root["weather"]["fog"] = fog;
root["next-inventory-id"] = nextInventoryId;
root["next-entity-id"] = nextEntityId;
+4 -2
View File
@@ -4,9 +4,10 @@
#include <string>
#include <vector>
#include "io/fwd.hpp"
#include "content/ContentPack.hpp"
#include "interfaces/Serializable.hpp"
#include "io/fwd.hpp"
#include "Weather.hpp"
#include "typedefs.hpp"
#include "util/timeutil.hpp"
@@ -33,7 +34,6 @@ struct WorldInfo : public Serializable {
/// 0.5 - is noon
float daytime = timeutil::time_value(10, 00, 00);
// looking bad
float daytimeSpeed = 1.0f;
/// @brief total time passed in the world (not depending on daytimeSpeed)
@@ -46,6 +46,8 @@ struct WorldInfo : public Serializable {
int major = 0, minor = -1;
Weather weather {};
dv::value serialize() const override;
void deserialize(const dv::value& src) override;
};