Skip to content

Commit

Permalink
Fix editor save/load functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulKsr committed Jul 9, 2020
1 parent eb9a758 commit 8ff64ef
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 121 deletions.
121 changes: 4 additions & 117 deletions include/game/systems/editor_system.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,53 +5,10 @@
#include "game/components/inputs.h"
#include "game/components/ui_context.h"
#include "game/components/world_context.h"
#include "ui/vs_parser.h"
#include "world/vs_chunk_manager.h"

void updateEditorSystem(entt::registry& mainRegistry)
{
const auto& inputs = mainRegistry.ctx<Inputs>();

const auto& worldContext = mainRegistry.ctx<WorldContext>();

const auto& uiContext = mainRegistry.ctx<UIContext>();

if (inputs.mouseTrace.bHasHit)
{
const auto mouseLocation = inputs.mouseTrace.hitLocation;
// Check if block is placed in bounds
if (!worldContext.bounds.isLocationInside(mouseLocation))
{
return;
}
// For debugging
worldContext.world->getDebugDraw()->drawSphere(mouseLocation, 0.5F, {255, 0, 0});

if (inputs.rightButtonState == InputState::JustUp)
{
worldContext.world->getChunkManager()->setBlock(
mouseLocation, uiContext.editorSelectedBlockID + 1);
}
else if (inputs.middleButtonState == InputState::JustUp)
{
worldContext.world->getChunkManager()->setBlock(
mouseLocation - 0.05F * inputs.mouseTrace.hitNormal, 0);
}
}
}

// if (uiContext.bShouldResetEditor && !world->getChunkManager()->shouldReinitializeChunks())
// {
// // TODO: Clear world
// // world->getChunkManager()->clearBlocks();
// VSEditor::setPlaneBlocks(world);
// uiContext.bShouldResetEditor = false;
// }

// if (uiContext.bShouldSaveToFile)
// {
// VSChunkManager::VSWorldData worldData = world->getChunkManager()->getData();
// VSParser::writeToFile(worldData, uiContext.saveFilePath);
// uiContext.bShouldSaveToFile = false;
// }
void updateEditorSystem(entt::registry& mainRegistry);

// if (uiContext.bShouldSaveBuilding)
// {
Expand All @@ -61,74 +18,4 @@ void updateEditorSystem(entt::registry& mainRegistry)
// uiContext.bShouldSaveBuilding = false;
// }

// if (uiContext.bShouldLoadFromFile)
// {
// VSChunkManager::VSWorldData worldData = VSParser::readFromFile(uiContext.loadFilePath);
// world->getChunkManager()->setWorldData(worldData);
// uiContext.bShouldLoadFromFile = false;
// }

// // Set blocks for a plane, should be called with the world as parameter that was returned by
// // initWorld() after the chunks were initialized
// void setPlaneBlocks(VSWorld* editorWorld)
// {
// const auto worldSize = editorWorld->getChunkManager()->getWorldSize();
// const auto worldSizeHalf = worldSize / 2;
// for (int x = -worldSizeHalf.x; x < worldSizeHalf.x; x++)
// {
// for (int z = -worldSizeHalf.z; z < worldSizeHalf.z; z++)
// {
// editorWorld->getChunkManager()->setBlock({x, -worldSizeHalf.y, z}, 1);
// }
// }
// }

// VSChunkManager::VSBuildingData extractBuildFromPlane(VSWorld* editorWorld)
// {
// VSChunkManager::VSBuildingData buildData;
// const auto worldSize = editorWorld->getChunkManager()->getWorldSize();
// const auto worldSizeHalf = worldSize / 2;
// int xMin = worldSize.x;
// int xMax = -worldSize.x;
// int yMin = worldSize.y;
// int yMax = -worldSize.y;
// int zMin = worldSize.z;
// int zMax = -worldSize.z;

// for (int x = -worldSizeHalf.x; x < worldSizeHalf.x; x++)
// {
// for (int z = -worldSizeHalf.z; z < worldSizeHalf.z; z++)
// {
// for (int y = -worldSizeHalf.y; y < worldSizeHalf.y; y++)
// {
// int blockID = editorWorld->getChunkManager()->getBlock({x, y, z});
// if (blockID > 1 || (blockID != 0 && y != -worldSizeHalf.y))
// {
// xMin = std::min(xMin, x);
// yMin = std::min(yMin, y);
// zMin = std::min(zMin, z);
// xMax = std::max(xMax, x);
// yMax = std::max(yMax, y);
// zMax = std::max(zMax, z);
// }
// }
// }
// }

// buildData.buildSize = {
// std::abs(xMax - xMin) + 1, std::abs(yMax - yMin) + 1, std::abs(zMax - zMin) + 1};

// for (int x = xMin; x <= xMax; x++)
// {
// for (int y = yMin; y <= yMax; y++)
// {
// for (int z = zMin; z <= zMax; z++)
// {
// buildData.blocks.emplace_back(
// editorWorld->getChunkManager()->getBlock({x, y, z}));
// }
// }
// }

// return buildData;
// }
VSChunkManager::VSBuildingData extractBuildFromPlane(VSWorld* editorWorld);
5 changes: 5 additions & 0 deletions include/game/systems/menu_system.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ void updateMenuSystem(entt::registry& mainRegistry)
}
}

if (app->getWorldName() == uiContext.editorWorldName)
{

}

// Update world state with ui state
if (uiContext.bShouldUpdateChunks)
{
Expand Down
4 changes: 2 additions & 2 deletions include/ui/vs_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
namespace VSParser
{
// Writes the world data to a json file using the nlohmann json library. Returns true if successful.
bool writeToFile(VSChunkManager::VSWorldData& worldData, std::filesystem::path path);
bool writeToFile(const VSChunkManager::VSWorldData& worldData, std::filesystem::path path);

// Writes the build data to a json file using the nlohmann json library. Returns true if successful.
bool writeBuildToFile(VSChunkManager::VSBuildingData& buildData, std::filesystem::path path);
bool writeBuildToFile(const VSChunkManager::VSBuildingData& buildData, std::filesystem::path path);

[[nodiscard]] VSChunkManager::VSWorldData readFromFile(std::filesystem::path path);

Expand Down
109 changes: 109 additions & 0 deletions source/game/systems/editor_system.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
#include "game/systems/editor_system.h"
#include "ui/vs_parser.h"

void updateEditorSystem(entt::registry& mainRegistry)
{
const auto& inputs = mainRegistry.ctx<Inputs>();

const auto& worldContext = mainRegistry.ctx<WorldContext>();

auto& uiContext = mainRegistry.ctx<UIContext>();

if (inputs.mouseTrace.bHasHit)
{
const auto mouseLocation = inputs.mouseTrace.hitLocation;
// Check if block is placed in bounds
if (!worldContext.bounds.isLocationInside(mouseLocation))
{
// Do nothing
}
else
{
// For debugging
worldContext.world->getDebugDraw()->drawSphere(mouseLocation, 0.5F, {255, 0, 0});

if (inputs.rightButtonState == InputState::JustUp)
{
worldContext.world->getChunkManager()->setBlock(
mouseLocation, uiContext.editorSelectedBlockID + 1);
}
else if (inputs.middleButtonState == InputState::JustUp)
{
worldContext.world->getChunkManager()->setBlock(
mouseLocation - 0.05F * inputs.mouseTrace.hitNormal, 0);
}
}
}

if (uiContext.bShouldSaveToFile)
{
const VSChunkManager::VSWorldData worldData =
worldContext.world->getChunkManager()->getData();
VSParser::writeToFile(worldData, uiContext.saveFilePath);
uiContext.bShouldSaveToFile = false;
}

if (uiContext.bShouldLoadFromFile)
{
VSChunkManager::VSWorldData worldData = VSParser::readFromFile(uiContext.loadFilePath);
worldContext.world->getChunkManager()->setWorldData(worldData);
uiContext.bShouldLoadFromFile = false;
}

if (uiContext.bShouldSaveBuilding)
{
// Extract editor plane and save building
const VSChunkManager::VSBuildingData buildData = extractBuildFromPlane(worldContext.world);
VSParser::writeBuildToFile(buildData, uiContext.saveBuildingPath);
uiContext.bShouldSaveBuilding = false;
}
}

VSChunkManager::VSBuildingData extractBuildFromPlane(VSWorld* editorWorld)
{
VSChunkManager::VSBuildingData buildData;
const auto worldSize = editorWorld->getChunkManager()->getWorldSize();
const auto worldSizeHalf = worldSize / 2;
int xMin = worldSize.x;
int xMax = -worldSize.x;
int yMin = worldSize.y;
int yMax = -worldSize.y;
int zMin = worldSize.z;
int zMax = -worldSize.z;

for (int x = -worldSizeHalf.x; x < worldSizeHalf.x; x++)
{
for (int z = -worldSizeHalf.z; z < worldSizeHalf.z; z++)
{
for (int y = -worldSizeHalf.y; y < worldSizeHalf.y; y++)
{
int blockID = editorWorld->getChunkManager()->getBlock({x, y, z});
if (blockID > 1 || (blockID != 0 && y != 0))
{
xMin = std::min(xMin, x);
yMin = std::min(yMin, y);
zMin = std::min(zMin, z);
xMax = std::max(xMax, x);
yMax = std::max(yMax, y);
zMax = std::max(zMax, z);
}
}
}
}

buildData.buildSize = {
std::abs(xMax - xMin) + 1, std::abs(yMax - yMin) + 1, std::abs(zMax - zMin) + 1};

for (int x = xMin; x <= xMax; x++)
{
for (int y = yMin; y <= yMax; y++)
{
for (int z = zMin; z <= zMax; z++)
{
buildData.blocks.emplace_back(editorWorld->getChunkManager()->getBlock({x, y, z}));
}
}
}

return buildData;
}
4 changes: 2 additions & 2 deletions source/ui/vs_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace VSParser
{
bool writeToFile(VSChunkManager::VSWorldData& worldData, std::filesystem::path path)
bool writeToFile(const VSChunkManager::VSWorldData& worldData, std::filesystem::path path)
{
std::ofstream outFile;
outFile.open(path);
Expand All @@ -24,7 +24,7 @@ namespace VSParser
return true;
}

bool writeBuildToFile(VSChunkManager::VSBuildingData& buildData, std::filesystem::path path)
bool writeBuildToFile(const VSChunkManager::VSBuildingData& buildData, std::filesystem::path path)
{
std::ofstream outFile;
outFile.open(path);
Expand Down

0 comments on commit 8ff64ef

Please sign in to comment.