git repos / saland

commit 1af5bc17

Poul Sander · 2021-05-08 20:13
1af5bc1787b9a300c4c3955292cb55e0f72a579b patch · browse files
parent 543faa40de0c285fb32ffe65668df6536ee430d3

Prevent spawning prefabs on top of prefabs

Changed files

M src/saland/GameRegion.cpp before
M src/saland/GameRegion.hpp before
M src/saland/Prefabs.hpp before
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 <cmath>
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<MiscItem> barrel = std::make_shared<MiscItem>();
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 <vector>
@@ -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);