refactor
This commit is contained in:
parent
db620010a3
commit
70b3a4d91e
@ -66,22 +66,22 @@ LevelScreen::LevelScreen(
|
|||||||
frontend = std::make_unique<LevelFrontend>(
|
frontend = std::make_unique<LevelFrontend>(
|
||||||
player, controller.get(), assets, settings
|
player, controller.get(), assets, settings
|
||||||
);
|
);
|
||||||
worldRenderer = std::make_unique<WorldRenderer>(
|
renderer = std::make_unique<WorldRenderer>(
|
||||||
engine, *frontend, *player
|
engine, *frontend, *player
|
||||||
);
|
);
|
||||||
hud = std::make_unique<Hud>(engine, *frontend, *player);
|
hud = std::make_unique<Hud>(engine, *frontend, *player);
|
||||||
|
|
||||||
decorator = std::make_unique<Decorator>(
|
decorator = std::make_unique<Decorator>(
|
||||||
engine, *controller, *worldRenderer, assets, *player
|
engine, *controller, *renderer, assets, *player
|
||||||
);
|
);
|
||||||
|
|
||||||
keepAlive(settings.graphics.backlight.observe([=](bool) {
|
keepAlive(settings.graphics.backlight.observe([=](bool) {
|
||||||
player->chunks->saveAndClear();
|
player->chunks->saveAndClear();
|
||||||
worldRenderer->clear();
|
renderer->clear();
|
||||||
}));
|
}));
|
||||||
keepAlive(settings.graphics.denseRender.observe([=](bool) {
|
keepAlive(settings.graphics.denseRender.observe([=](bool) {
|
||||||
player->chunks->saveAndClear();
|
player->chunks->saveAndClear();
|
||||||
worldRenderer->clear();
|
renderer->clear();
|
||||||
frontend->getContentGfxCache().refresh();
|
frontend->getContentGfxCache().refresh();
|
||||||
}));
|
}));
|
||||||
keepAlive(settings.camera.fov.observe([=](double value) {
|
keepAlive(settings.camera.fov.observe([=](double value) {
|
||||||
@ -89,7 +89,7 @@ LevelScreen::LevelScreen(
|
|||||||
}));
|
}));
|
||||||
keepAlive(Events::getBinding(BIND_CHUNKS_RELOAD).onactived.add([=](){
|
keepAlive(Events::getBinding(BIND_CHUNKS_RELOAD).onactived.add([=](){
|
||||||
player->chunks->saveAndClear();
|
player->chunks->saveAndClear();
|
||||||
worldRenderer->clear();
|
renderer->clear();
|
||||||
return false;
|
return false;
|
||||||
}));
|
}));
|
||||||
|
|
||||||
@ -117,7 +117,7 @@ void LevelScreen::initializeContent() {
|
|||||||
for (auto& entry : content.getPacks()) {
|
for (auto& entry : content.getPacks()) {
|
||||||
initializePack(entry.second.get());
|
initializePack(entry.second.get());
|
||||||
}
|
}
|
||||||
scripting::on_frontend_init(hud.get(), worldRenderer.get());
|
scripting::on_frontend_init(hud.get(), renderer.get());
|
||||||
}
|
}
|
||||||
|
|
||||||
void LevelScreen::initializePack(ContentPackRuntime* pack) {
|
void LevelScreen::initializePack(ContentPackRuntime* pack) {
|
||||||
@ -139,7 +139,7 @@ void LevelScreen::loadDecorations() {
|
|||||||
}
|
}
|
||||||
auto data = io::read_object(CLIENT_FILE);
|
auto data = io::read_object(CLIENT_FILE);
|
||||||
if (data.has("weather")) {
|
if (data.has("weather")) {
|
||||||
worldRenderer->getWeather().deserialize(data["weather"]);
|
renderer->getWeather().deserialize(data["weather"]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -147,7 +147,7 @@ void LevelScreen::saveDecorations() {
|
|||||||
io::create_directory("world:client");
|
io::create_directory("world:client");
|
||||||
|
|
||||||
auto data = dv::object();
|
auto data = dv::object();
|
||||||
data["weather"] = worldRenderer->getWeather().serialize();
|
data["weather"] = renderer->getWeather().serialize();
|
||||||
io::write_json(CLIENT_FILE, data, true);
|
io::write_json(CLIENT_FILE, data, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -168,7 +168,7 @@ void LevelScreen::saveWorldPreview() {
|
|||||||
Viewport viewport(previewSize * 1.5, previewSize);
|
Viewport viewport(previewSize * 1.5, previewSize);
|
||||||
DrawContext ctx(&pctx, viewport, batch.get());
|
DrawContext ctx(&pctx, viewport, batch.get());
|
||||||
|
|
||||||
worldRenderer->draw(ctx, camera, false, true, 0.0f, *postProcessing);
|
renderer->draw(ctx, camera, false, true, 0.0f, *postProcessing);
|
||||||
auto image = postProcessing->toImage();
|
auto image = postProcessing->toImage();
|
||||||
image->flipY();
|
image->flipY();
|
||||||
imageio::write("world:preview.png", image.get());
|
imageio::write("world:preview.png", image.get());
|
||||||
@ -188,26 +188,15 @@ void LevelScreen::updateHotkeys() {
|
|||||||
if (Events::jpressed(keycode::F3)) {
|
if (Events::jpressed(keycode::F3)) {
|
||||||
debug = !debug;
|
debug = !debug;
|
||||||
hud->setDebug(debug);
|
hud->setDebug(debug);
|
||||||
worldRenderer->setDebug(debug);
|
renderer->setDebug(debug);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void LevelScreen::update(float delta) {
|
void LevelScreen::updateAudio() {
|
||||||
gui::GUI* gui = engine.getGUI();
|
|
||||||
auto menu = gui->getMenu();
|
|
||||||
|
|
||||||
bool inputLocked = menu->hasOpenPage() ||
|
|
||||||
hud->isInventoryOpen() ||
|
|
||||||
gui->isFocusCaught();
|
|
||||||
if (!gui->isFocusCaught()) {
|
|
||||||
updateHotkeys();
|
|
||||||
}
|
|
||||||
|
|
||||||
auto level = controller->getLevel();
|
|
||||||
auto player = playerController->getPlayer();
|
auto player = playerController->getPlayer();
|
||||||
auto camera = player->currentCamera;
|
auto camera = player->currentCamera;
|
||||||
|
|
||||||
bool paused = hud->isPause();
|
bool paused = hud->isPause();
|
||||||
|
|
||||||
audio::get_channel("regular")->setPaused(paused);
|
audio::get_channel("regular")->setPaused(paused);
|
||||||
audio::get_channel("ambient")->setPaused(paused);
|
audio::get_channel("ambient")->setPaused(paused);
|
||||||
glm::vec3 velocity {};
|
glm::vec3 velocity {};
|
||||||
@ -220,24 +209,34 @@ void LevelScreen::update(float delta) {
|
|||||||
camera->dir,
|
camera->dir,
|
||||||
glm::vec3(0, 1, 0)
|
glm::vec3(0, 1, 0)
|
||||||
);
|
);
|
||||||
const auto& settings = engine.getSettings();
|
}
|
||||||
|
|
||||||
if (!hud->isPause()) {
|
void LevelScreen::update(float delta) {
|
||||||
level->getWorld()->updateTimers(delta);
|
auto& gui = *engine.getGUI();
|
||||||
animator->update(delta);
|
|
||||||
|
if (!gui.isFocusCaught()) {
|
||||||
|
updateHotkeys();
|
||||||
}
|
}
|
||||||
if (!hud->isPause()) {
|
updateAudio();
|
||||||
|
|
||||||
|
auto menu = gui.getMenu();
|
||||||
|
bool inputLocked =
|
||||||
|
menu->hasOpenPage() || hud->isInventoryOpen() || gui.isFocusCaught();
|
||||||
|
bool paused = hud->isPause();
|
||||||
|
if (!paused) {
|
||||||
|
world.updateTimers(delta);
|
||||||
|
animator->update(delta);
|
||||||
playerController->update(delta, !inputLocked);
|
playerController->update(delta, !inputLocked);
|
||||||
}
|
}
|
||||||
controller->update(glm::min(delta, 0.2f), hud->isPause());
|
controller->update(glm::min(delta, 0.2f), paused);
|
||||||
playerController->postUpdate(delta, !inputLocked, hud->isPause());
|
playerController->postUpdate(delta, !inputLocked, paused);
|
||||||
|
|
||||||
hud->update(hudVisible);
|
hud->update(hudVisible);
|
||||||
|
|
||||||
const auto& weather = worldRenderer->getWeather();
|
const auto& weather = renderer->getWeather();
|
||||||
decorator->update(
|
const auto& player = *playerController->getPlayer();
|
||||||
hud->isPause() ? 0.0f : delta, *camera, weather.a, weather.b
|
const auto& camera = *player.currentCamera;
|
||||||
);
|
decorator->update(paused ? 0.0f : delta, camera, weather);
|
||||||
}
|
}
|
||||||
|
|
||||||
void LevelScreen::draw(float delta) {
|
void LevelScreen::draw(float delta) {
|
||||||
@ -249,7 +248,7 @@ void LevelScreen::draw(float delta) {
|
|||||||
if (!hud->isPause()) {
|
if (!hud->isPause()) {
|
||||||
scripting::on_entities_render(engine.getTime().getDelta());
|
scripting::on_entities_render(engine.getTime().getDelta());
|
||||||
}
|
}
|
||||||
worldRenderer->draw(
|
renderer->draw(
|
||||||
ctx, *camera, hudVisible, hud->isPause(), delta, *postProcessing
|
ctx, *camera, hudVisible, hud->isPause(), delta, *postProcessing
|
||||||
);
|
);
|
||||||
|
|
||||||
@ -264,8 +263,3 @@ void LevelScreen::onEngineShutdown() {
|
|||||||
}
|
}
|
||||||
controller->saveWorld();
|
controller->saveWorld();
|
||||||
}
|
}
|
||||||
|
|
||||||
LevelController* LevelScreen::getLevelController() const {
|
|
||||||
return controller.get();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|||||||
@ -22,7 +22,7 @@ class LevelScreen : public Screen {
|
|||||||
std::unique_ptr<LevelFrontend> frontend;
|
std::unique_ptr<LevelFrontend> frontend;
|
||||||
std::unique_ptr<LevelController> controller;
|
std::unique_ptr<LevelController> controller;
|
||||||
std::unique_ptr<PlayerController> playerController;
|
std::unique_ptr<PlayerController> playerController;
|
||||||
std::unique_ptr<WorldRenderer> worldRenderer;
|
std::unique_ptr<WorldRenderer> renderer;
|
||||||
std::unique_ptr<TextureAnimator> animator;
|
std::unique_ptr<TextureAnimator> animator;
|
||||||
std::unique_ptr<PostProcessing> postProcessing;
|
std::unique_ptr<PostProcessing> postProcessing;
|
||||||
std::unique_ptr<Decorator> decorator;
|
std::unique_ptr<Decorator> decorator;
|
||||||
@ -38,6 +38,7 @@ class LevelScreen : public Screen {
|
|||||||
|
|
||||||
void loadDecorations();
|
void loadDecorations();
|
||||||
void saveDecorations();
|
void saveDecorations();
|
||||||
|
void updateAudio();
|
||||||
public:
|
public:
|
||||||
LevelScreen(
|
LevelScreen(
|
||||||
Engine& engine, std::unique_ptr<Level> level, int64_t localPlayer
|
Engine& engine, std::unique_ptr<Level> level, int64_t localPlayer
|
||||||
@ -48,6 +49,4 @@ public:
|
|||||||
void draw(float delta) override;
|
void draw(float delta) override;
|
||||||
|
|
||||||
void onEngineShutdown() override;
|
void onEngineShutdown() override;
|
||||||
|
|
||||||
LevelController* getLevelController() const;
|
|
||||||
};
|
};
|
||||||
|
|||||||
@ -173,45 +173,7 @@ void Decorator::update(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Decorator::update(
|
void Decorator::updateBlockEmitters(const Camera& camera) {
|
||||||
float delta,
|
|
||||||
const Camera& camera,
|
|
||||||
const WeatherPreset& weatherA,
|
|
||||||
const WeatherPreset& weatherB
|
|
||||||
) {
|
|
||||||
float thunderRate = weatherA.thunderRate * weatherA.intensity +
|
|
||||||
weatherB.thunderRate * weatherB.intensity;
|
|
||||||
thunderTimer += delta;
|
|
||||||
util::PseudoRandom random(rand());
|
|
||||||
if (thunderTimer >= 1.0f) {
|
|
||||||
thunderTimer = 0.0f;
|
|
||||||
if (random.randFloat() < thunderRate) {
|
|
||||||
audio::play(
|
|
||||||
assets.get<audio::Sound>("ambient/thunder"),
|
|
||||||
glm::vec3(),
|
|
||||||
false,
|
|
||||||
1.0f,
|
|
||||||
1.0f + random.randFloat() - 0.5f,
|
|
||||||
false,
|
|
||||||
audio::PRIORITY_NORMAL,
|
|
||||||
audio::get_channel_index("ambient")
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
glm::ivec3 pos = camera.position;
|
|
||||||
for (int i = 0; i < ITERATIONS; i++) {
|
|
||||||
update(delta, pos - glm::ivec3(UPDATE_AREA_DIAMETER / 2), pos);
|
|
||||||
}
|
|
||||||
int randIters = std::min(50'000, static_cast<int>(delta * 24'000));
|
|
||||||
for (int i = 0; i < randIters; i++) {
|
|
||||||
if (weatherA.intensity > 1.e-3f) {
|
|
||||||
updateRandom(delta, pos, weatherA);
|
|
||||||
}
|
|
||||||
if (weatherB.intensity > 1.e-3f) {
|
|
||||||
updateRandom(delta, pos, weatherB);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
const auto& chunks = *player.chunks;
|
const auto& chunks = *player.chunks;
|
||||||
const auto& indices = *level.content.getIndices();
|
const auto& indices = *level.content.getIndices();
|
||||||
auto iter = blockEmitters.begin();
|
auto iter = blockEmitters.begin();
|
||||||
@ -243,7 +205,9 @@ void Decorator::update(
|
|||||||
}
|
}
|
||||||
iter++;
|
iter++;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Decorator::updateTextNotes() {
|
||||||
for (const auto& [id, player] : *level.players) {
|
for (const auto& [id, player] : *level.players) {
|
||||||
if (id == this->player.getId() ||
|
if (id == this->player.getId() ||
|
||||||
playerTexts.find(id) != playerTexts.end()) {
|
playerTexts.find(id) != playerTexts.end()) {
|
||||||
@ -273,3 +237,49 @@ void Decorator::update(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Decorator::updateRandomSounds(float delta, const Weather& weather) {
|
||||||
|
float thunderRate = weather.a.thunderRate * weather.a.intensity +
|
||||||
|
weather.b.thunderRate * weather.b.intensity;
|
||||||
|
thunderTimer += delta;
|
||||||
|
util::PseudoRandom random(rand());
|
||||||
|
if (thunderTimer >= 1.0f) {
|
||||||
|
thunderTimer = 0.0f;
|
||||||
|
if (random.randFloat() < thunderRate) {
|
||||||
|
audio::play(
|
||||||
|
assets.get<audio::Sound>("ambient/thunder"),
|
||||||
|
glm::vec3(),
|
||||||
|
false,
|
||||||
|
1.0f,
|
||||||
|
1.0f + random.randFloat() - 0.5f,
|
||||||
|
false,
|
||||||
|
audio::PRIORITY_NORMAL,
|
||||||
|
audio::get_channel_index("ambient")
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Decorator::update(
|
||||||
|
float delta,
|
||||||
|
const Camera& camera,
|
||||||
|
const Weather& weather
|
||||||
|
) {
|
||||||
|
updateRandomSounds(delta, weather);
|
||||||
|
|
||||||
|
glm::ivec3 pos = camera.position;
|
||||||
|
for (int i = 0; i < ITERATIONS; i++) {
|
||||||
|
update(delta, pos - glm::ivec3(UPDATE_AREA_DIAMETER / 2), pos);
|
||||||
|
}
|
||||||
|
int randIters = std::min(50'000, static_cast<int>(delta * 24'000));
|
||||||
|
for (int i = 0; i < randIters; i++) {
|
||||||
|
if (weather.a.intensity > 1.e-3f) {
|
||||||
|
updateRandom(delta, pos, weather.a);
|
||||||
|
}
|
||||||
|
if (weather.b.intensity > 1.e-3f) {
|
||||||
|
updateRandom(delta, pos, weather.b);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
updateBlockEmitters(camera);
|
||||||
|
updateTextNotes();
|
||||||
|
}
|
||||||
|
|||||||
@ -18,6 +18,7 @@ class Block;
|
|||||||
class Engine;
|
class Engine;
|
||||||
class LevelController;
|
class LevelController;
|
||||||
class WorldRenderer;
|
class WorldRenderer;
|
||||||
|
class Weather;
|
||||||
struct WeatherPreset;
|
struct WeatherPreset;
|
||||||
|
|
||||||
class Decorator {
|
class Decorator {
|
||||||
@ -44,6 +45,9 @@ class Decorator {
|
|||||||
const glm::ivec3& areaCenter,
|
const glm::ivec3& areaCenter,
|
||||||
const WeatherPreset& weather
|
const WeatherPreset& weather
|
||||||
);
|
);
|
||||||
|
void updateRandomSounds(float delta, const Weather& weather);
|
||||||
|
void updateBlockEmitters(const Camera& camera);
|
||||||
|
void updateTextNotes();
|
||||||
void addParticles(const Block& def, const glm::ivec3& pos);
|
void addParticles(const Block& def, const glm::ivec3& pos);
|
||||||
public:
|
public:
|
||||||
Decorator(
|
Decorator(
|
||||||
@ -57,7 +61,6 @@ public:
|
|||||||
void update(
|
void update(
|
||||||
float delta,
|
float delta,
|
||||||
const Camera& camera,
|
const Camera& camera,
|
||||||
const WeatherPreset& weatherA,
|
const Weather& weather
|
||||||
const WeatherPreset& weatherB
|
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user