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& 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& 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& world) { for (b2Body* b : managed_bodies) { b2Fixture* f = b->GetFixtureList(); @@ -117,6 +130,7 @@ void World::init_physics(std::shared_ptr& 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& world) { } } + + void World::init(std::shared_ptr& 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& world, const std::string& mapFileName); void init_physics(std::shared_ptr& 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 physicsWorld; std::vector managed_bodies; std::vector protected_tiles; + std::vector blocking_tiles; }; #endif /* WORLD_HPP */