git repos / saland

commit e1e3ae60

Poul Sander · 2018-09-26 17:08
e1e3ae6087deaa678620e77b54aa72e36eb4ba9d patch · browse files
parent edee902796cb5d71630a13515be544c6c6664483

World is now sorta persistant

Changed files

M src/saland/Game.cpp before
M src/saland/GameRegion.cpp before
M src/saland/GameRegion.hpp before
M src/saland/model/World.cpp before
M src/saland/model/World.hpp before
diff --git a/src/saland/Game.cpp b/src/saland/Game.cpp index 672ce26..d4c1cb8 100644 --- a/src/saland/Game.cpp +++ b/src/saland/Game.cpp
@@ -150,8 +150,7 @@ Game::Game() {
}
Game::~Game() {
- std::string data2save = sago::tiled::tilemap2string(data->gameRegion.world.tm);
- sago::WriteFileContent("maps/sample1.tmx", data2save);
+ data->gameRegion.SaveRegion();
}
bool Game::IsActive() {
diff --git a/src/saland/GameRegion.cpp b/src/saland/GameRegion.cpp index 251a637..0ab547a 100644 --- a/src/saland/GameRegion.cpp +++ b/src/saland/GameRegion.cpp
@@ -24,13 +24,25 @@ https://github.com/sago007/saland
#include "GameRegion.hpp"
GameRegion::GameRegion() {
- Init();
+ Init(0, 0);
}
-void GameRegion::Init() {
+static std::string createFileName(int x, int y) {
+ std::string ret = std::string("maps/m")+std::to_string(x)+"x"+std::to_string(y)+".tmx" ;
+ return ret;
+}
+
+void GameRegion::Init(int x, int y) {
+ region_x = x;
+ region_y = y;
+ mapFileName = createFileName(region_x, region_y);
+ std::string loadMap = mapFileName;
+ if (!sago::FileExists(loadMap.c_str())) {
+ loadMap = "maps/sample1.tmx";
+ }
b2Vec2 gravity(0.0f, 0.0f);
physicsBox.reset(new b2World(gravity));
- world.init(physicsBox);
+ world.init(physicsBox, loadMap);
std::shared_ptr<MiscItem> barrel = std::make_shared<MiscItem>();
barrel.get()->Radius = 16.0f;
@@ -74,5 +86,8 @@ void GameRegion::Init() {
}
-
+void GameRegion::SaveRegion() {
+ std::string data2save = sago::tiled::tilemap2string(world.tm);
+ sago::WriteFileContent(mapFileName.c_str(), data2save);
+}
diff --git a/src/saland/GameRegion.hpp b/src/saland/GameRegion.hpp index 4a7e6e9..476754a 100644 --- a/src/saland/GameRegion.hpp +++ b/src/saland/GameRegion.hpp
@@ -31,12 +31,15 @@ https://github.com/sago007/saland
class GameRegion {
public:
GameRegion();
- void Init();
+ void Init(int x, int y);
std::vector<std::shared_ptr<Placeable> > placeables;
std::shared_ptr<b2World> physicsBox;
+ void SaveRegion();
World world;
private:
-
+ int region_x = 0;
+ int region_y = 0;
+ std::string mapFileName = "maps/sample1.tmx";
};
#endif /* GAMEREGION_HPP */
diff --git a/src/saland/model/World.cpp b/src/saland/model/World.cpp index 5217200..91a02a0 100644 --- a/src/saland/model/World.cpp +++ b/src/saland/model/World.cpp
@@ -142,13 +142,17 @@ void World::init_physics(std::shared_ptr<b2World>& world) {
}
}
-void World::init(std::shared_ptr<b2World>& world) {
+void World::init(std::shared_ptr<b2World>& world, const std::string& mapFileName) {
this->physicsWorld = world;
std::string tsx_file = sago::GetFileContent("maps/terrain.tsx");
- std::string tmx_file = sago::GetFileContent("maps/sample1.tmx");
+ std::string tmx_file = sago::GetFileContent(mapFileName);
ts = sago::tiled::string2tileset(tsx_file);
tm = sago::tiled::string2tilemap(tmx_file);
tm.tileset.alternativeSource = &ts;
init_physics(world);
}
+void World::init(std::shared_ptr<b2World>& world) {
+ init(world, "maps/sample1.tmx");
+}
+
diff --git a/src/saland/model/World.hpp b/src/saland/model/World.hpp index 47a53e7..0567ca6 100644 --- a/src/saland/model/World.hpp +++ b/src/saland/model/World.hpp
@@ -32,6 +32,7 @@ class World {
public:
World();
void init(std::shared_ptr<b2World>& world);
+ void init(std::shared_ptr<b2World>& world, const std::string& mapFileName);
void init_physics(std::shared_ptr<b2World>& world);
//private:
sago::tiled::TileSet ts;