diff --git a/src/saland/GameRegion.cpp b/src/saland/GameRegion.cpp index decbcf4..c27e4bb 100644 --- a/src/saland/GameRegion.cpp +++ b/src/saland/GameRegion.cpp @@ -23,7 +23,6 @@ https://github.com/sago007/saland #include "GameRegion.hpp" #include "GameItems.hpp" -#include "Prefabs.hpp" #include GameRegion::GameRegion() { @@ -70,6 +69,22 @@ static bool Intersect(const Placeable& p1, const Placeable& p2) { return false; } +void GameRegion::SpawnPrefab(const Prefab& prefab, int destX, int destY) { + for (int i=destX; i < destX+prefab.width; ++i) { + for (int j=destY; j < destY+prefab.height; ++j) { + if (world.tile_protected(i, j)) { + std::cout << "Do not spawn prefab " << prefab.name << "(" << i << "," << j << "), Protected tile\n"; + return; + } + if (world.tile_blocking(i, j)) { + std::cout << "Do not spawn prefab " << prefab.name << "(" << i << "," << j << "), Blocked tile\n"; + return; + } + } + } + ApplyPrefab(world.tm, destX, destY, prefab); +} + void GameRegion::SpawnItem(const ItemDef& def, float destX, float destY) { std::shared_ptr barrel = std::make_shared(); barrel.get()->Radius = def.radius; @@ -213,7 +228,7 @@ void GameRegion::Init(int x, int y, const std::string& worldName, bool forceRese } } - ApplyPrefab(world.tm, 32, 32, getPrefab("basic_house")); + SpawnPrefab(getPrefab("basic_house"), 32, 32); MonsterDef batDef; batDef.radius = 16.0f; diff --git a/src/saland/GameRegion.hpp b/src/saland/GameRegion.hpp index efed9a4..92c7499 100644 --- a/src/saland/GameRegion.hpp +++ b/src/saland/GameRegion.hpp @@ -27,6 +27,7 @@ https://github.com/sago007/saland #include "model/World.hpp" #include "model/placeables.hpp" #include "GameItems.hpp" +#include "Prefabs.hpp" #include "../sagotmx/tmx_struct.h" #include "../terrain/WaterHandler.hpp" #include @@ -59,6 +60,7 @@ public: } void SpawnMonster(const MonsterDef& def, float destX, float destY) ; void SpawnItem(const ItemDef& def, float destX, float destY) ; + void SpawnPrefab(const Prefab& prefab, int destX, int destY); std::string GetRegionType(int x, int y) const; private: int region_x = 0; diff --git a/src/saland/Prefabs.hpp b/src/saland/Prefabs.hpp index 591acf1..6b413ec 100644 --- a/src/saland/Prefabs.hpp +++ b/src/saland/Prefabs.hpp @@ -41,6 +41,10 @@ void ApplyPrefabLayer(sago::tiled::TileMap& dest, int destX, int destY, const ch void ApplyPrefabObjectMarker(sago::tiled::TileMap& dest, int destX, int destY, const Prefab& prefab); +/** + * Does not validate if there are blocking elements underneth. + * Use GameRegion::SpawnPrefab check that. + * */ void ApplyPrefab(sago::tiled::TileMap& dest, int destX, int destY, const Prefab& prefab); Prefab getPrefab(const char* name);