git repos / saland

commit 0e5612e2

Poul Sander · 2020-07-13 21:20
0e5612e2a22ff02cba94bc5aaab89f2e5da18d71 patch · browse files
parent aa9637b6062346f0427147d69d11d95358d2795e

Don't spawn items on blocking tiles

Changed files

M src/saland/GameRegion.cpp before
M src/saland/model/World.cpp before
M src/saland/model/World.hpp before
diff --git a/src/saland/GameRegion.cpp b/src/saland/GameRegion.cpp index 17d14f1..09e2f18 100644 --- a/src/saland/GameRegion.cpp +++ b/src/saland/GameRegion.cpp
@@ -79,7 +79,11 @@ void GameRegion::SpawnItem(const ItemDef& def, float destX, float destY) {
barrel.get()->health = def.health;
barrel.get()->name = def.itemid;
if (world.tile_protected(destX/32, destY/32)) {
- std::cout << "Do not spawn " << def.itemid << "(" << destX << "," << "destY" << "), Protected tile\n";
+ std::cout << "Do not spawn " << def.itemid << "(" << destX << "," << destY << "), Protected tile\n";
+ return;
+ }
+ if (world.tile_blocking(destX/32, destY/32)) {
+ std::cout << "Do not spawn " << def.itemid << "(" << destX << "," << destY << "), Blocked tile\n";
return;
}
for (std::shared_ptr<Placeable>& target : placeables) {
@@ -163,9 +167,10 @@ void GameRegion::Init(int x, int y, const std::string& worldName, bool forceRese
//Forrest region
if (region_x == -1 && region_y == 0) {
std::cout << "Forrest region\n";
+
for (int i=0; i<10; ++i) {
- int x = (rand()%97+1)*32+rand()%32;
- int y = (rand()%97+1)*32+rand()%32;
+ int x = (rand()%(world.tm.width-3)+1)*32+rand()%32;
+ int y = (rand()%(world.tm.height-3)+1)*32+rand()%32;
SpawnItem(pineDef, x, y);
std::cout << "Spawning at " << x << ", " << y << "\n";
}
diff --git a/src/saland/model/World.cpp b/src/saland/model/World.cpp index 89c95b0..b085508 100644 --- a/src/saland/model/World.cpp +++ b/src/saland/model/World.cpp
@@ -87,6 +87,19 @@ static b2Body* AddStaticTilesToWorld(b2World* world, const sago::tiled::TileMap&
return body;
}
+void fill_blocking_tiles(std::vector<bool>& output, const sago::tiled::TileMap& tm, const sago::tiled::TileLayer& layer) {
+ output.resize(tm.height*tm.width);
+ for (int i = 0; i < tm.height; ++i) {
+ for (int j = 0; j < tm.width; ++j) {
+ uint32_t gid = sago::tiled::getTileFromLayer(tm, layer, i, j);
+ if (gid == 0) {
+ continue;
+ }
+ output[i+j*tm.width] = true;
+ }
+ }
+}
+
void World::init_physics(std::shared_ptr<b2World>& world) {
for (b2Body* b : managed_bodies) {
b2Fixture* f = b->GetFixtureList();
@@ -117,6 +130,7 @@ void World::init_physics(std::shared_ptr<b2World>& world) {
}
b2Body* bodyAdded = AddStaticTilesToWorld(physicsWorld.get(), tm, layer);
managed_bodies.push_back(bodyAdded);
+ fill_blocking_tiles(blocking_tiles, tm, layer);
}
{
//Top left
@@ -142,6 +156,8 @@ void World::init_physics(std::shared_ptr<b2World>& world) {
}
}
+
+
void World::init(std::shared_ptr<b2World>& world, const std::string& mapFileName) {
this->physicsWorld = world;
std::string tmx_file = sago::GetFileContent(mapFileName);
@@ -200,3 +216,8 @@ bool World::tile_protected(int x, int y) const {
}
return protected_tiles.at(index);
}
+
+bool World::tile_blocking(int x, int y) const {
+ size_t index = x+y*tm.width;
+ return blocking_tiles.at(index);
+}
\ No newline at end of file
diff --git a/src/saland/model/World.hpp b/src/saland/model/World.hpp index 83435cb..130e7f3 100644 --- a/src/saland/model/World.hpp +++ b/src/saland/model/World.hpp
@@ -35,12 +35,14 @@ public:
void init(std::shared_ptr<b2World>& world, const std::string& mapFileName);
void init_physics(std::shared_ptr<b2World>& world);
bool tile_protected(int x, int y) const;
+ bool tile_blocking(int x, int y) const;
//private:
sago::tiled::TileSet ts;
sago::tiled::TileMap tm;
std::shared_ptr<b2World> physicsWorld;
std::vector<b2Body*> managed_bodies;
std::vector<bool> protected_tiles;
+ std::vector<bool> blocking_tiles;
};
#endif /* WORLD_HPP */