diff --git a/include/AssetManager.h b/include/AssetManager.h index 6cb3719..1d3c97a 100644 --- a/include/AssetManager.h +++ b/include/AssetManager.h @@ -27,14 +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); - /*! - * \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); + Vector2D calculateSpawnPosition(); + PowerupType calculateType(); //texture management void addTexture(std::string id, const char* path); diff --git a/src/AssetManager.cpp b/src/AssetManager.cpp index 3841cf3..25ce8ad 100644 --- a/src/AssetManager.cpp +++ b/src/AssetManager.cpp @@ -14,7 +14,6 @@ #include "Vector2D.h" #include "PowerupComponent.h" #include -#include AssetManager::AssetManager(Manager* manager) : man(manager) {} @@ -74,39 +73,33 @@ void AssetManager::createPowerup(Vector2D pos, std::function pic powerups.addGroup((size_t)Entity::GroupLabel::POWERUPS); } - -Vector2D AssetManager::calculateSpawnPosition(Vector2D size, Vector2D spawnArea) +Vector2D AssetManager::calculateSpawnPosition() { - Vector2D spawnPos = Vector2D(-1, -1); - - for(int i = 0; i <= SPAWN_ATTEMPTS; i++) - { - - SDL_Rect spawnRect = { - 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}); - 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; + 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; } -template -T AssetManager::calculateRandomType(int amount) +PowerupType AssetManager::calculateType() { - T type = T(rand() % amount); - return type; -} + PowerupType type = PowerupType(rand() % 3); + return type; +} \ No newline at end of file