git repos / saland

commit 6d8379fd

Poul Sander · 2020-07-13 14:10
6d8379fdd526ff0736d1d6c6983588e51a38ad7a patch · browse files
parent 54c2056b7dc7653077e15c02c9cd72645e47ef08

Stop spawning on top of each other

Changed files

M src/saland/GameRegion.cpp before
diff --git a/src/saland/GameRegion.cpp b/src/saland/GameRegion.cpp index 52c3d99..4b8ebed 100644 --- a/src/saland/GameRegion.cpp +++ b/src/saland/GameRegion.cpp
@@ -60,6 +60,14 @@ void GameRegion::SpawnMonster(const MonsterDef& def, float destX, float destY) {
bat->body->SetTransform(b2Vec2(bat.get()->X / 32.0f, bat.get()->Y / 32.0f),bat->body->GetAngle());
}
+static bool Intersect(const Placeable& p1, const Placeable& p2) {
+ double distance = std::sqrt( std::pow(p1.X-p2.X, 2) + std::pow(p1.Y-p2.Y,2));
+ if (distance < p1.Radius + p2.Radius) {
+ return true;
+ }
+ return false;
+}
+
void GameRegion::SpawnItem(const ItemDef& def, float destX, float destY) {
std::shared_ptr<MiscItem> barrel = std::make_shared<MiscItem>();
barrel.get()->Radius = def.radius;
@@ -70,6 +78,15 @@ void GameRegion::SpawnItem(const ItemDef& def, float destX, float destY) {
barrel.get()->destructible = def.isDestructible;
barrel.get()->health = def.health;
barrel.get()->name = def.itemid;
+ for (std::shared_ptr<Placeable>& target : placeables) {
+ if (target->removeMe) {
+ continue;
+ }
+ if (Intersect(*barrel.get(), *target.get())) {
+ std::cout << "Do not spawn " << def.itemid << "\n";
+ return;
+ }
+ }
placeables.push_back(barrel);
if (def.isStatic) {