mirror of
https://github.com/Nimac0/SDL_Minigame
synced 2026-01-12 07:53:43 +00:00
rework calculateSpawnPosition()
This commit is contained in:
parent
1cd604e1f0
commit
a49753e39a
@ -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<void (Entity*)> pickupFunc, std::string texturePath);
|
||||
|
||||
Vector2D calculateSpawnPosition();
|
||||
Vector2D calculateSpawnPosition(int width, int height);
|
||||
template <typename T> T calculateType(int amount);
|
||||
|
||||
//texture management
|
||||
|
||||
@ -14,6 +14,7 @@
|
||||
#include "Vector2D.h"
|
||||
#include "PowerupComponent.h"
|
||||
#include <iostream>
|
||||
#include <algorithm>
|
||||
|
||||
AssetManager::AssetManager(Manager* manager) : man(manager) {}
|
||||
|
||||
@ -73,28 +74,31 @@ void AssetManager::createPowerup(Vector2D pos, std::function<void (Entity*)> 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++)
|
||||
|
||||
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 }))
|
||||
SDL_Rect spawnRect = {
|
||||
rand() % (SCREEN_SIZE_WIDTH - width),
|
||||
rand() % (SCREEN_SIZE_HEIGHT - height),
|
||||
width,
|
||||
height
|
||||
};
|
||||
|
||||
std::vector<ColliderComponent*> 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)
|
||||
{
|
||||
if (SDL_HasIntersection(&spawnRect, &cc->collider) && strcmp(cc->tag, "projectile"))
|
||||
{
|
||||
conflict = true;
|
||||
spawnPos = Vector2D(spawnRect.x, spawnRect.y);
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (conflict) continue;
|
||||
spawnPos = Vector2D(spawnRect.x, spawnRect.y);
|
||||
}
|
||||
|
||||
return spawnPos;
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user