From 1cd604e1f0c48e6f3d72ccd23d91fef0ba77ac35 Mon Sep 17 00:00:00 2001 From: ineslelin Date: Tue, 5 Nov 2024 20:52:24 +0100 Subject: [PATCH 1/4] rework calculateType() --- include/AssetManager.h | 2 +- src/AssetManager.cpp | 9 +++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/include/AssetManager.h b/include/AssetManager.h index 1d3c97a..1a92c76 100644 --- a/include/AssetManager.h +++ b/include/AssetManager.h @@ -28,7 +28,7 @@ public: void createPowerup(Vector2D pos, std::function pickupFunc, std::string texturePath); Vector2D calculateSpawnPosition(); - PowerupType calculateType(); + template T calculateType(int amount); //texture management void addTexture(std::string id, const char* path); diff --git a/src/AssetManager.cpp b/src/AssetManager.cpp index 25ce8ad..809ccd7 100644 --- a/src/AssetManager.cpp +++ b/src/AssetManager.cpp @@ -98,8 +98,9 @@ Vector2D AssetManager::calculateSpawnPosition() return spawnPos; } -PowerupType AssetManager::calculateType() +template +T AssetManager::calculateType(int amount) { - PowerupType type = PowerupType(rand() % 3); - return type; -} \ No newline at end of file + T type = T(rand() % amount); + return type; +} From a49753e39af117b7c21d17088e89f8a9936fb36f Mon Sep 17 00:00:00 2001 From: ineslelin Date: Tue, 3 Dec 2024 12:06:09 +0100 Subject: [PATCH 2/4] rework calculateSpawnPosition() --- include/AssetManager.h | 2 +- src/AssetManager.cpp | 48 +++++++++++++++++++++++------------------- 2 files changed, 27 insertions(+), 23 deletions(-) diff --git a/include/AssetManager.h b/include/AssetManager.h index 1a92c76..0d7a699 100644 --- a/include/AssetManager.h +++ b/include/AssetManager.h @@ -27,7 +27,7 @@ public: void createProjectile(Vector2D pos, Vector2D velocity, int scale, int range, int speed, const char* texturePath, Entity* owner); void createPowerup(Vector2D pos, std::function pickupFunc, std::string texturePath); - Vector2D calculateSpawnPosition(); + Vector2D calculateSpawnPosition(int width, int height); template T calculateType(int amount); //texture management diff --git a/src/AssetManager.cpp b/src/AssetManager.cpp index 809ccd7..4bf00be 100644 --- a/src/AssetManager.cpp +++ b/src/AssetManager.cpp @@ -14,6 +14,7 @@ #include "Vector2D.h" #include "PowerupComponent.h" #include +#include AssetManager::AssetManager(Manager* manager) : man(manager) {} @@ -73,29 +74,32 @@ void AssetManager::createPowerup(Vector2D pos, std::function pic powerups.addGroup((size_t)Entity::GroupLabel::POWERUPS); } -Vector2D AssetManager::calculateSpawnPosition() +Vector2D AssetManager::calculateSpawnPosition(int width, int height) { - Vector2D spawnPos = Vector2D(-1, -1); - bool conflict = false; - for (int i = 0; i <= SPAWN_ATTEMPTS; i++) - { - SDL_Rect spawnRect; - spawnRect.h = spawnRect.w = 32; - spawnRect.x = rand() % (SCREEN_SIZE_WIDTH - spawnRect.w); - spawnRect.y = rand() % (SCREEN_SIZE_HEIGHT - spawnRect.h); - conflict = false; - for (auto cc : this->man->getGame()->collisionHandler->getColliders({ Entity::GroupLabel::MAPTILES })) - { - if (SDL_HasIntersection(&spawnRect, &cc->collider) && strcmp(cc->tag, "projectile")) - { - conflict = true; - break; - } - } - if (conflict) continue; - spawnPos = Vector2D(spawnRect.x, spawnRect.y); - } - return spawnPos; + Vector2D spawnPos = Vector2D(-1, -1); + + for(int i = 0; i <= SPAWN_ATTEMPTS; i++) + { + SDL_Rect spawnRect = { + rand() % (SCREEN_SIZE_WIDTH - width), + rand() % (SCREEN_SIZE_HEIGHT - height), + width, + height + }; + + std::vector colliders = this->man->getGame()->collisionHandler->getColliders({Entity::GroupLabel::MAPTILES}); + bool conflict = std::any_of(colliders.begin(), colliders.end(), + [&](const auto& cc) { + return SDL_HasIntersection(&spawnRect, &cc->collider);} ); + + if(!conflict) + { + spawnPos = Vector2D(spawnRect.x, spawnRect.y); + break; + } + } + + return spawnPos; } template From 7e088594ae8b2a916661d39e1ccd944cac8c16aa Mon Sep 17 00:00:00 2001 From: ineslelin Date: Tue, 17 Dec 2024 22:48:49 +0100 Subject: [PATCH 3/4] incorporate requests into code --- include/AssetManager.h | 4 ++-- src/AssetManager.cpp | 13 +++++++------ 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/include/AssetManager.h b/include/AssetManager.h index 0d7a699..14d7bad 100644 --- a/include/AssetManager.h +++ b/include/AssetManager.h @@ -27,8 +27,8 @@ public: void createProjectile(Vector2D pos, Vector2D velocity, int scale, int range, int speed, const char* texturePath, Entity* owner); void createPowerup(Vector2D pos, std::function pickupFunc, std::string texturePath); - Vector2D calculateSpawnPosition(int width, int height); - template T calculateType(int amount); + Vector2D calculateSpawnPosition(Vector2D size, Vector2D spawnArea); + template [[deprecated]] T calculateRandomType(int amount); //texture management void addTexture(std::string id, const char* path); diff --git a/src/AssetManager.cpp b/src/AssetManager.cpp index 4bf00be..252fab5 100644 --- a/src/AssetManager.cpp +++ b/src/AssetManager.cpp @@ -74,17 +74,18 @@ void AssetManager::createPowerup(Vector2D pos, std::function pic powerups.addGroup((size_t)Entity::GroupLabel::POWERUPS); } -Vector2D AssetManager::calculateSpawnPosition(int width, int height) +Vector2D AssetManager::calculateSpawnPosition(Vector2D size, Vector2D spawnArea) { Vector2D spawnPos = Vector2D(-1, -1); for(int i = 0; i <= SPAWN_ATTEMPTS; i++) { + SDL_Rect spawnRect = { - rand() % (SCREEN_SIZE_WIDTH - width), - rand() % (SCREEN_SIZE_HEIGHT - height), - width, - height + rand() % (int)(spawnArea.x - size.x), + rand() % (int)(spawnArea.y - size.y), + size.x, + size.y }; std::vector colliders = this->man->getGame()->collisionHandler->getColliders({Entity::GroupLabel::MAPTILES}); @@ -103,7 +104,7 @@ Vector2D AssetManager::calculateSpawnPosition(int width, int height) } template -T AssetManager::calculateType(int amount) +T AssetManager::calculateRandomType(int amount) { T type = T(rand() % amount); return type; From 29743d11b0b8ab366bf4e11670393f6ab008a8b6 Mon Sep 17 00:00:00 2001 From: ineslelin Date: Tue, 17 Dec 2024 22:56:58 +0100 Subject: [PATCH 4/4] add documentation to calculateSpawnPosition --- include/AssetManager.h | 6 ++++++ src/AssetManager.cpp | 1 + 2 files changed, 7 insertions(+) diff --git a/include/AssetManager.h b/include/AssetManager.h index 14d7bad..6cb3719 100644 --- a/include/AssetManager.h +++ b/include/AssetManager.h @@ -27,6 +27,12 @@ public: void createProjectile(Vector2D pos, Vector2D velocity, int scale, int range, int speed, const char* texturePath, Entity* owner); void createPowerup(Vector2D pos, std::function pickupFunc, std::string texturePath); + /*! + * \brief Calculates a random spawn position for an object within a given area + * \param size The size (collision box) of the object + * \param spawnArea The area within which a spawn position will be calculated + * \returns Spawn Coordinates for the object + */ Vector2D calculateSpawnPosition(Vector2D size, Vector2D spawnArea); template [[deprecated]] T calculateRandomType(int amount); diff --git a/src/AssetManager.cpp b/src/AssetManager.cpp index 252fab5..3841cf3 100644 --- a/src/AssetManager.cpp +++ b/src/AssetManager.cpp @@ -74,6 +74,7 @@ void AssetManager::createPowerup(Vector2D pos, std::function pic powerups.addGroup((size_t)Entity::GroupLabel::POWERUPS); } + Vector2D AssetManager::calculateSpawnPosition(Vector2D size, Vector2D spawnArea) { Vector2D spawnPos = Vector2D(-1, -1);