Skip to content

Commit

Permalink
Implement saving/loading buildings
Browse files Browse the repository at this point in the history
  • Loading branch information
kantoniak committed Nov 22, 2018
1 parent 976d1fa commit dea8d73
Show file tree
Hide file tree
Showing 13 changed files with 58 additions and 23 deletions.
Binary file added bin/save.dat
Binary file not shown.
4 changes: 2 additions & 2 deletions src/data/Chunk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ Chunk::Chunk() : roadGraph(SIDE_LENGTH) {
residentialSize = 0;
}

Chunk::Chunk(const Chunk & chunk) : roadGraph(chunk.getRoadGraph()) {
Chunk::Chunk(const Chunk& chunk) : roadGraph(chunk.getRoadGraph()), residential(chunk.residential) {
objectId = 0;
position = chunk.getPosition();
residentialSize = 0;
residentialSize = chunk.residential.size();
}

void Chunk::setObjectId(unsigned int objectId) {
Expand Down
11 changes: 9 additions & 2 deletions src/data/Chunk.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class Chunk {
constexpr unsigned static int SIDE_LENGTH = 64;

Chunk();
Chunk(const Chunk & chunk);
Chunk(const Chunk& chunk);

void setObjectId(unsigned int objectId);

Expand All @@ -46,9 +46,16 @@ class Chunk {
void addBuilding(data::buildings::Building building);
bool removeBuilding(data::buildings::Building building);

template <class Archive> void serialize(Archive& archive) {
template <class Archive> void save(Archive& archive) const {
archive(position);
archive(roadGraph);
archive(residential);
}

template <class Archive> void load(Archive& archive) {
archive(position);
archive(roadGraph);
archive(residential);
}

private:
Expand Down
3 changes: 1 addition & 2 deletions src/data/Layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ template <class T>
Layer<T>::Layer(unsigned int sideLength) : sideLength(sideLength), layerData(sideLength * sideLength, 0) {
}

template <class T>
Layer<T>::Layer(const Layer & layer) : sideLength(layer.sideLength), layerData(layer.layerData) {
template <class T> Layer<T>::Layer(const Layer& layer) : sideLength(layer.sideLength), layerData(layer.layerData) {
}

template <class T> const std::vector<T>& Layer<T>::getLayerData() const {
Expand Down
2 changes: 1 addition & 1 deletion src/data/Layer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ template <class T> class Layer {

public:
Layer(unsigned int sideLength);
Layer(const Layer & layer);
Layer(const Layer& layer);
const std::vector<T>& getLayerData() const;

protected:
Expand Down
4 changes: 3 additions & 1 deletion src/data/RoadGraph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ RoadGraph::RoadGraph(unsigned int sideLength)
: Layer(sideLength), neighborN(nullptr), neighborS(nullptr), neighborW(nullptr), neighborE(nullptr) {
}

RoadGraph::RoadGraph(const RoadGraph & roadGraph) : Layer(roadGraph), neighborN(roadGraph.neighborN), neighborS(roadGraph.neighborS), neighborW(roadGraph.neighborW), neighborE(roadGraph.neighborE) {
RoadGraph::RoadGraph(const RoadGraph& roadGraph)
: Layer(roadGraph), neighborN(roadGraph.neighborN), neighborS(roadGraph.neighborS), neighborW(roadGraph.neighborW),
neighborE(roadGraph.neighborE) {
}

void RoadGraph::setNeighborN(RoadGraph* neighborN) {
Expand Down
2 changes: 1 addition & 1 deletion src/data/RoadGraph.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class RoadGraph : public Layer<char> {
constexpr static char ROAD_NSWE = 16;

RoadGraph(unsigned int sideLength);
RoadGraph(const RoadGraph & roadGraph);
RoadGraph(const RoadGraph& roadGraph);

void setNeighborN(RoadGraph* neigborN);
void setNeighborS(RoadGraph* neigborS);
Expand Down
5 changes: 5 additions & 0 deletions src/data/buildings.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ struct Building {
unsigned short width;
unsigned short length;
unsigned short level;

template <class Archive> void serialize(Archive& archive) {
archive(x, y);
archive(width, length, level);
}
};
}
}
Expand Down
9 changes: 6 additions & 3 deletions src/io/SaveFileHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,21 @@ void SaveFileHandler::loadSave(world::World& world) {
archive(cameraState);

engine.getLogger().debug("Camera lookAt[%f][%f][%f], rot[%f][%f], dist[%f] ", cameraState.lookAt.x,
cameraState.lookAt.y, cameraState.lookAt.z, cameraState.rotationAroundX,
cameraState.rotationAroundY, cameraState.distance);
cameraState.lookAt.y, cameraState.lookAt.z, cameraState.rotationAroundX,
cameraState.rotationAroundY, cameraState.distance);
world.getCamera().emplace(cameraState);

unsigned int chunksCount;
archive(chunksCount);
engine.getLogger().debug("Loading %d chunks:", chunksCount);

for (unsigned int i=0; i<chunksCount; i++) {
for (unsigned int i = 0; i < chunksCount; i++) {
engine.getLogger().debug("Loading chunk %d", i);
data::Chunk chunk;
archive(chunk);
world.getMap().loadChunk(chunk);
}

engine.getLogger().debug("World loaded.");
}
}
34 changes: 25 additions & 9 deletions src/states/MapState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@ void MapState::init() {
world.getCamera().init(initialPerspective, initialCamera);
world.init();

// Load from file
saveFileHandler.loadSave(world);
city.name = "Warsaw";
city.people = 57950;
city.money = 445684;
world.getMap().setCurrentCity(&city);

createNewWorld();

// Overwrite by randoms
geometry.init(engine, world);
createRandomWorld();

if (!renderer.init()) {
engine.stop();
Expand Down Expand Up @@ -212,12 +214,19 @@ void MapState::onKey(int key, int scancode, int action, int mods) {
setCurrentAction(MapStateAction::BULDOZE);
}

if (key == GLFW_KEY_N && action == GLFW_RELEASE && mods == GLFW_MOD_CONTROL) {
this->createNewWorld();
}

if (key == GLFW_KEY_S && action == GLFW_RELEASE && mods == GLFW_MOD_CONTROL) {
saveFileHandler.createSave(world);
}

if (key == GLFW_KEY_L && action == GLFW_RELEASE && mods == GLFW_MOD_CONTROL) {
world.getMap().cleanup();
saveFileHandler.loadSave(world);
renderer.markTileDataForUpdate();
renderer.markBuildingDataForUpdate();
}
}

Expand Down Expand Up @@ -286,12 +295,19 @@ void MapState::onWindowResize(int width, int height) {
world.getCamera().updateAspect(width / (float)height);
}

void MapState::createRandomWorld() {
void MapState::createNewWorld() {

city.name = "Warsaw";
city.people = 57950;
city.money = 445684;
world.getMap().setCurrentCity(&city);
world.getMap().cleanup();

const glm::ivec2 mapSize = glm::ivec2(1, 1);
for (int x = 0; x < mapSize.x; x++) {
for (int y = 0; y < mapSize.y; y++) {
world.getMap().createChunk(glm::ivec2(x, y));
}
}
}

void MapState::createRandomWorld() {

/*const glm::ivec2 mapSize = glm::ivec2(2, 2);
for (int x = 0; x < mapSize.x; x++) {
Expand Down
1 change: 1 addition & 0 deletions src/states/MapState.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class MapState : public engine::GameState {

io::SaveFileHandler saveFileHandler;
data::City city;
void createNewWorld();
void createRandomWorld();

MapStateAction currentAction;
Expand Down
4 changes: 3 additions & 1 deletion src/world/Map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ void Map::cleanup() {
delete *it;
}
chunks.clear();
buildingCount = 0;
}

void Map::createChunk(glm::ivec2 position) {
Expand All @@ -20,10 +21,11 @@ void Map::createChunk(glm::ivec2 position) {
this->setChunkNeighbors(*chunk);
}

void Map::loadChunk(data::Chunk & chunk) {
void Map::loadChunk(data::Chunk& chunk) {
data::Chunk* ptr = new data::Chunk(chunk);
chunks.push_back(ptr);
this->setChunkNeighbors(*ptr);
buildingCount += ptr->getResidentialSize();
}

void Map::setChunkNeighbors(data::Chunk& chunk) {
Expand Down
2 changes: 1 addition & 1 deletion src/world/Map.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class Map {
void cleanup();

void createChunk(glm::ivec2 position);
void loadChunk(data::Chunk & chunk);
void loadChunk(data::Chunk& chunk);
void setChunkNeighbors(data::Chunk& chunk);

unsigned int getChunksCount();
Expand Down

0 comments on commit dea8d73

Please sign in to comment.