refactor: move Weather instance to WorldInfo
This commit is contained in:
parent
0ea842580c
commit
9c4db8fa10
@ -209,11 +209,10 @@ void LevelScreen::update(float delta) {
|
|||||||
playerController->postUpdate(delta, !inputLocked, hud->isPause());
|
playerController->postUpdate(delta, !inputLocked, hud->isPause());
|
||||||
|
|
||||||
hud->update(hudVisible);
|
hud->update(hudVisible);
|
||||||
|
|
||||||
|
const auto& weather = level->getWorld()->getInfo().weather;
|
||||||
decorator->update(
|
decorator->update(
|
||||||
hud->isPause() ? 0.0f : delta,
|
hud->isPause() ? 0.0f : delta, *camera, weather.a, weather.b
|
||||||
*camera,
|
|
||||||
worldRenderer->weather.a,
|
|
||||||
worldRenderer->weather.b
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -111,6 +111,7 @@ void WorldRenderer::setupWorldShader(
|
|||||||
const EngineSettings& settings,
|
const EngineSettings& settings,
|
||||||
float fogFactor
|
float fogFactor
|
||||||
) {
|
) {
|
||||||
|
const auto& weather = level.getWorld()->getInfo().weather;
|
||||||
shader.use();
|
shader.use();
|
||||||
shader.uniformMatrix("u_model", glm::mat4(1.0f));
|
shader.uniformMatrix("u_model", glm::mat4(1.0f));
|
||||||
shader.uniformMatrix("u_proj", camera.getProjection());
|
shader.uniformMatrix("u_proj", camera.getProjection());
|
||||||
@ -152,7 +153,7 @@ void WorldRenderer::renderLevel(
|
|||||||
bool pause,
|
bool pause,
|
||||||
bool hudVisible
|
bool hudVisible
|
||||||
) {
|
) {
|
||||||
weather.update(delta);
|
const auto& weather = level.getWorld()->getInfo().weather;
|
||||||
|
|
||||||
texts->render(ctx, camera, settings, hudVisible, false);
|
texts->render(ctx, camera, settings, hudVisible, false);
|
||||||
|
|
||||||
@ -200,7 +201,7 @@ void WorldRenderer::renderLevel(
|
|||||||
|
|
||||||
setupWorldShader(entityShader, camera, settings, fogFactor);
|
setupWorldShader(entityShader, camera, settings, fogFactor);
|
||||||
|
|
||||||
std::array<WeatherPreset*, 2> weatherInstances {&weather.a, &weather.b};
|
std::array<const WeatherPreset*, 2> weatherInstances {&weather.a, &weather.b};
|
||||||
for (const auto& weather : weatherInstances) {
|
for (const auto& weather : weatherInstances) {
|
||||||
float zero = weather->fall.minOpacity;
|
float zero = weather->fall.minOpacity;
|
||||||
float one = weather->fall.maxOpacity;
|
float one = weather->fall.maxOpacity;
|
||||||
@ -335,6 +336,7 @@ void WorldRenderer::draw(
|
|||||||
) {
|
) {
|
||||||
timer += delta * !pause;
|
timer += delta * !pause;
|
||||||
auto world = level.getWorld();
|
auto world = level.getWorld();
|
||||||
|
const auto& weather = world->getInfo().weather;
|
||||||
const Viewport& vp = pctx.getViewport();
|
const Viewport& vp = pctx.getViewport();
|
||||||
camera.aspect = vp.getWidth() / static_cast<float>(vp.getHeight());
|
camera.aspect = vp.getWidth() / static_cast<float>(vp.getHeight());
|
||||||
|
|
||||||
|
|||||||
@ -11,6 +11,7 @@
|
|||||||
#include "typedefs.hpp"
|
#include "typedefs.hpp"
|
||||||
|
|
||||||
#include "presets/WeatherPreset.hpp"
|
#include "presets/WeatherPreset.hpp"
|
||||||
|
#include "world/Weather.hpp"
|
||||||
|
|
||||||
class Level;
|
class Level;
|
||||||
class Player;
|
class Player;
|
||||||
@ -34,44 +35,6 @@ class ModelBatch;
|
|||||||
class Assets;
|
class Assets;
|
||||||
struct EngineSettings;
|
struct EngineSettings;
|
||||||
|
|
||||||
struct Weather {
|
|
||||||
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 / glm::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);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
class WorldRenderer {
|
class WorldRenderer {
|
||||||
Engine& engine;
|
Engine& engine;
|
||||||
const Level& level;
|
const Level& level;
|
||||||
@ -113,7 +76,6 @@ public:
|
|||||||
std::unique_ptr<ParticlesRenderer> particles;
|
std::unique_ptr<ParticlesRenderer> particles;
|
||||||
std::unique_ptr<BlockWrapsRenderer> blockWraps;
|
std::unique_ptr<BlockWrapsRenderer> blockWraps;
|
||||||
std::unique_ptr<PrecipitationRenderer> precipitation;
|
std::unique_ptr<PrecipitationRenderer> precipitation;
|
||||||
Weather weather {};
|
|
||||||
|
|
||||||
static bool showChunkBorders;
|
static bool showChunkBorders;
|
||||||
static bool showEntitiesDebug;
|
static bool showEntitiesDebug;
|
||||||
|
|||||||
@ -9,4 +9,10 @@ public:
|
|||||||
virtual ~Serializable() {}
|
virtual ~Serializable() {}
|
||||||
virtual dv::value serialize() const = 0;
|
virtual dv::value serialize() const = 0;
|
||||||
virtual void deserialize(const dv::value& src) = 0;
|
virtual void deserialize(const dv::value& src) = 0;
|
||||||
|
|
||||||
|
void deserializeOpt(const dv::optionalvalue& opt) {
|
||||||
|
if (opt.ptr) {
|
||||||
|
deserialize(*opt.ptr);
|
||||||
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@ -69,6 +69,8 @@ LevelController::LevelController(
|
|||||||
}
|
}
|
||||||
|
|
||||||
void LevelController::update(float delta, bool pause) {
|
void LevelController::update(float delta, bool pause) {
|
||||||
|
level->getWorld()->getInfo().weather.update(delta);
|
||||||
|
|
||||||
for (const auto& [_, player] : *level->players) {
|
for (const auto& [_, player] : *level->players) {
|
||||||
if (player->isSuspended()) {
|
if (player->isSuspended()) {
|
||||||
continue;
|
continue;
|
||||||
|
|||||||
@ -1,31 +1,43 @@
|
|||||||
#include "libhud.hpp"
|
#include "libhud.hpp"
|
||||||
|
#include "world/Level.hpp"
|
||||||
|
#include "world/World.hpp"
|
||||||
|
|
||||||
using namespace scripting;
|
using namespace scripting;
|
||||||
|
|
||||||
|
static Weather& require_weather() {
|
||||||
|
if (level == nullptr) {
|
||||||
|
throw std::runtime_error("world is not open");
|
||||||
|
}
|
||||||
|
return level->getWorld()->getInfo().weather;
|
||||||
|
}
|
||||||
|
|
||||||
static int l_change(lua::State* L) {
|
static int l_change(lua::State* L) {
|
||||||
WeatherPreset weather {};
|
WeatherPreset preset {};
|
||||||
weather.deserialize(lua::tovalue(L, 1));
|
preset.deserialize(lua::tovalue(L, 1));
|
||||||
float time = lua::tonumber(L, 2);
|
float time = lua::tonumber(L, 2);
|
||||||
std::string name;
|
std::string name;
|
||||||
if (lua::isstring(L, 3)) {
|
if (lua::isstring(L, 3)) {
|
||||||
name = lua::tostring(L, 3);
|
name = lua::tostring(L, 3);
|
||||||
}
|
}
|
||||||
renderer->weather.change(std::move(weather), time, std::move(name));
|
auto& weather = require_weather();
|
||||||
|
weather.change(std::move(preset), time, std::move(name));
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int l_get_current(lua::State* L) {
|
static int l_get_current(lua::State* L) {
|
||||||
if (renderer->weather.t > 0.5f) {
|
const auto& weather = require_weather();
|
||||||
return lua::pushstring(L, renderer->weather.nameB);
|
if (weather.t > 0.5f) {
|
||||||
|
return lua::pushstring(L, weather.nameB);
|
||||||
} else {
|
} else {
|
||||||
return lua::pushstring(L, renderer->weather.nameA);
|
return lua::pushstring(L, weather.nameA);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static int l_get_fall_intencity(lua::State* L) {
|
static int l_get_fall_intencity(lua::State* L) {
|
||||||
const auto& a = renderer->weather.a;
|
auto& weather = require_weather();
|
||||||
const auto& b = renderer->weather.b;
|
const auto& a = weather.a;
|
||||||
float t = renderer->weather.t;
|
const auto& b = weather.b;
|
||||||
|
float t = weather.t;
|
||||||
return lua::pushnumber(L,
|
return lua::pushnumber(L,
|
||||||
(a.fall.texture.empty() ? 0.0f : (1.0f - t)) +
|
(a.fall.texture.empty() ? 0.0f : (1.0f - t)) +
|
||||||
(b.fall.texture.empty() ? 0.0f : t)
|
(b.fall.texture.empty() ? 0.0f : t)
|
||||||
@ -33,17 +45,23 @@ static int l_get_fall_intencity(lua::State* L) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static int l_get_current_data(lua::State* L) {
|
static int l_get_current_data(lua::State* L) {
|
||||||
if (renderer->weather.t > 0.5f) {
|
auto& weather = require_weather();
|
||||||
return lua::pushvalue(L, renderer->weather.b.serialize());
|
if (weather.t > 0.5f) {
|
||||||
|
return lua::pushvalue(L, weather.b.serialize());
|
||||||
} else {
|
} else {
|
||||||
return lua::pushvalue(L, renderer->weather.a.serialize());
|
return lua::pushvalue(L, weather.a.serialize());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int l_is_transition(lua::State* L) {
|
||||||
|
return lua::pushboolean(L, require_weather().t < 1.0f);
|
||||||
|
}
|
||||||
|
|
||||||
const luaL_Reg weatherlib[] = {
|
const luaL_Reg weatherlib[] = {
|
||||||
{"change", wrap_hud<l_change>},
|
{"change", wrap_hud<l_change>},
|
||||||
{"get_current", wrap_hud<l_get_current>},
|
{"get_current", wrap_hud<l_get_current>},
|
||||||
{"get_current_data", wrap_hud<l_get_current_data>},
|
{"get_current_data", wrap_hud<l_get_current_data>},
|
||||||
{"get_fall_intencity", wrap_hud<l_get_fall_intencity>},
|
{"get_fall_intencity", wrap_hud<l_get_fall_intencity>},
|
||||||
|
{"is_transition", wrap_hud<l_is_transition>},
|
||||||
{NULL, NULL}
|
{NULL, NULL}
|
||||||
};
|
};
|
||||||
|
|||||||
63
src/world/Weather.hpp
Normal file
63
src/world/Weather.hpp
Normal 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);
|
||||||
|
}
|
||||||
|
};
|
||||||
@ -208,6 +208,7 @@ void WorldInfo::deserialize(const dv::value& root) {
|
|||||||
totalTime = timeobj["total-time"].asNumber();
|
totalTime = timeobj["total-time"].asNumber();
|
||||||
}
|
}
|
||||||
if (root.has("weather")) {
|
if (root.has("weather")) {
|
||||||
|
weather.deserialize(root["weather"]);
|
||||||
fog = root["weather"]["fog"].asNumber();
|
fog = root["weather"]["fog"].asNumber();
|
||||||
}
|
}
|
||||||
nextInventoryId = root["next-inventory-id"].asInteger(2);
|
nextInventoryId = root["next-inventory-id"].asInteger(2);
|
||||||
@ -231,8 +232,8 @@ dv::value WorldInfo::serialize() const {
|
|||||||
timeobj["day-time-speed"] = daytimeSpeed;
|
timeobj["day-time-speed"] = daytimeSpeed;
|
||||||
timeobj["total-time"] = totalTime;
|
timeobj["total-time"] = totalTime;
|
||||||
|
|
||||||
auto& weatherobj = root.object("weather");
|
root["weather"] = weather.serialize();
|
||||||
weatherobj["fog"] = fog;
|
root["weather"]["fog"] = fog;
|
||||||
|
|
||||||
root["next-inventory-id"] = nextInventoryId;
|
root["next-inventory-id"] = nextInventoryId;
|
||||||
root["next-entity-id"] = nextEntityId;
|
root["next-entity-id"] = nextEntityId;
|
||||||
|
|||||||
@ -4,9 +4,10 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include "io/fwd.hpp"
|
|
||||||
#include "content/ContentPack.hpp"
|
#include "content/ContentPack.hpp"
|
||||||
#include "interfaces/Serializable.hpp"
|
#include "interfaces/Serializable.hpp"
|
||||||
|
#include "io/fwd.hpp"
|
||||||
|
#include "Weather.hpp"
|
||||||
#include "typedefs.hpp"
|
#include "typedefs.hpp"
|
||||||
#include "util/timeutil.hpp"
|
#include "util/timeutil.hpp"
|
||||||
|
|
||||||
@ -33,7 +34,6 @@ struct WorldInfo : public Serializable {
|
|||||||
/// 0.5 - is noon
|
/// 0.5 - is noon
|
||||||
float daytime = timeutil::time_value(10, 00, 00);
|
float daytime = timeutil::time_value(10, 00, 00);
|
||||||
|
|
||||||
// looking bad
|
|
||||||
float daytimeSpeed = 1.0f;
|
float daytimeSpeed = 1.0f;
|
||||||
|
|
||||||
/// @brief total time passed in the world (not depending on daytimeSpeed)
|
/// @brief total time passed in the world (not depending on daytimeSpeed)
|
||||||
@ -46,6 +46,8 @@ struct WorldInfo : public Serializable {
|
|||||||
|
|
||||||
int major = 0, minor = -1;
|
int major = 0, minor = -1;
|
||||||
|
|
||||||
|
Weather weather {};
|
||||||
|
|
||||||
dv::value serialize() const override;
|
dv::value serialize() const override;
|
||||||
void deserialize(const dv::value& src) override;
|
void deserialize(const dv::value& src) override;
|
||||||
};
|
};
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user