git repos / saland

commit 6e755a36

sago007 · 2018-09-09 10:49
6e755a365e41c24eb8f768c3324e10b336c46d91 patch · browse files
parent 8ba6a44c7d750df4c8318c2943b8dd8d5b75cb0c

Now possible to add blocking tiles with "w"

Changed files

M src/sagotmx/tmx_struct.h before
M src/saland/Game.cpp before
M src/saland/model/World.cpp before
M src/saland/model/World.hpp before
diff --git a/src/sagotmx/tmx_struct.h b/src/sagotmx/tmx_struct.h index 30d5314..e9b5723 100644 --- a/src/sagotmx/tmx_struct.h +++ b/src/sagotmx/tmx_struct.h
@@ -531,6 +531,18 @@ inline uint32_t getTileFromLayer(const TileMap& m, const TileLayer& l, int x, in
return global_tile_id;
}
+inline void setTileOnLayerNumber(TileMap& m, int layer_number, int x, int y, uint32_t tile) {
+ size_t tile_index = (m.height*y+x)*sizeof(uint32_t);
+ TileLayer& l = m.layers.at(layer_number);
+ if (tile_index > l.data.payload.size()-sizeof(uint32_t) ) {
+ throw SagoTiledException("ERROR: setTileOnLayerNumber called with coordiantes out-of-bound. Called with (%d, %d). Limit (%d, %d). Or the layer is corrupt. "
+ "Reported number of tiles in layer: %ld", x, y, m.width-1, m.height-1, l.data.payload.size()/sizeof(uint32_t));
+ }
+ std::string& data = l.data.payload;
+ uint32_t* dp = reinterpret_cast<uint32_t*> (&data[tile_index]);
+ *dp = tile;
+}
+
} //tiled
} //sago
diff --git a/src/saland/Game.cpp b/src/saland/Game.cpp index d2519ee..9b3c1ab 100644 --- a/src/saland/Game.cpp +++ b/src/saland/Game.cpp
@@ -341,6 +341,12 @@ void Game::ProcessInput(const SDL_Event& event, bool& processed) {
}
processed = true;
}
+ if (event.key.keysym.sym == SDLK_w) {
+ int layer_number = 2; // Do not hardcode
+ uint32_t tile = 485;
+ sago::tiled::setTileOnLayerNumber(data->world.tm, layer_number, data->world_mouse_x/32, data->world_mouse_y/32, tile);
+ data->world.init_physics(data->physicsBox);
+ }
}
}
diff --git a/src/saland/model/World.cpp b/src/saland/model/World.cpp index f6ded4e..87a243d 100644 --- a/src/saland/model/World.cpp +++ b/src/saland/model/World.cpp
@@ -73,7 +73,7 @@ static b2Body* AddStaticBody(b2World* world) {
return body;
}
-static void AddStaticTilesToWorld(b2World* world, const sago::tiled::TileMap& tm, const sago::tiled::TileLayer& layer) {
+static b2Body* AddStaticTilesToWorld(b2World* world, const sago::tiled::TileMap& tm, const sago::tiled::TileLayer& layer) {
b2Body* body = AddStaticBody(world);
for (int i = 0; i < tm.height; ++i) {
for (int j = 0; j < tm.width; ++j) {
@@ -84,15 +84,14 @@ static void AddStaticTilesToWorld(b2World* world, const sago::tiled::TileMap& tm
AddRectToBody(body, i*32.0f, j*32.0f, 32.0f, 32.0f);
}
}
+ return body;
}
-void World::init(std::shared_ptr<b2World>& world) {
- this->physicsWorld = world;
- std::string tsx_file = sago::GetFileContent("maps/terrain.tsx");
- std::string tmx_file = sago::GetFileContent("maps/sample1.tmx");
- ts = sago::tiled::string2tileset(tsx_file);
- tm = sago::tiled::string2tilemap(tmx_file);
- tm.tileset.alternativeSource = &ts;
+void World::init_physics(std::shared_ptr<b2World>& world) {
+ for (b2Body* b : managed_bodies) {
+ world->DestroyBody(b);
+ }
+ managed_bodies.clear();
const std::vector<sago::tiled::TileObjectGroup>& object_groups = tm.object_groups;
for (const auto& group : object_groups) {
for (const auto& item : group.objects) {
@@ -100,7 +99,8 @@ void World::init(std::shared_ptr<b2World>& world) {
continue;
}
if (item.x > 0 && item.y > 0 && item.width > 0 && item.height > 0) {
- AddStaticRect(physicsWorld.get(), item.x, item.y, item.width, item.height);
+ b2Body* bodyAdded = AddStaticRect(physicsWorld.get(), item.x, item.y, item.width, item.height);
+ managed_bodies.push_back(bodyAdded);
}
}
}
@@ -109,7 +109,18 @@ void World::init(std::shared_ptr<b2World>& world) {
if (layer.name != "blocking") {
continue;
}
- AddStaticTilesToWorld(physicsWorld.get(), tm, layer);
+ b2Body* bodyAdded = AddStaticTilesToWorld(physicsWorld.get(), tm, layer);
+ managed_bodies.push_back(bodyAdded);
}
}
+void World::init(std::shared_ptr<b2World>& world) {
+ this->physicsWorld = world;
+ std::string tsx_file = sago::GetFileContent("maps/terrain.tsx");
+ std::string tmx_file = sago::GetFileContent("maps/sample1.tmx");
+ ts = sago::tiled::string2tileset(tsx_file);
+ tm = sago::tiled::string2tilemap(tmx_file);
+ tm.tileset.alternativeSource = &ts;
+ init_physics(world);
+}
+
diff --git a/src/saland/model/World.hpp b/src/saland/model/World.hpp index b92186d..47a53e7 100644 --- a/src/saland/model/World.hpp +++ b/src/saland/model/World.hpp
@@ -32,10 +32,12 @@ class World {
public:
World();
void init(std::shared_ptr<b2World>& world);
+ void init_physics(std::shared_ptr<b2World>& world);
//private:
sago::tiled::TileSet ts;
sago::tiled::TileMap tm;
std::shared_ptr<b2World> physicsWorld;
+ std::vector<b2Body*> managed_bodies;
};
#endif /* WORLD_HPP */