0
0
mirror of https://github.com/Nimac0/SDL_Minigame synced 2026-01-12 07:53:43 +00:00

Compare commits

...

5 Commits

Author SHA1 Message Date
ines
2b411c5f6e
Merge pull request #94 from VEGO-Engine/helper-functions
Some checks failed
/ deploy (push) Has been cancelled
Helper functions
2024-12-17 23:04:58 +01:00
ineslelin
29743d11b0 add documentation to calculateSpawnPosition 2024-12-17 22:56:58 +01:00
ineslelin
7e088594ae incorporate requests into code 2024-12-17 22:48:49 +01:00
ineslelin
a49753e39a rework calculateSpawnPosition() 2024-12-03 12:06:09 +01:00
ineslelin
1cd604e1f0 rework calculateType() 2024-11-05 20:52:24 +01:00
2 changed files with 41 additions and 28 deletions

View File

@ -27,8 +27,14 @@ 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();
PowerupType calculateType();
/*!
* \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 <typename T> [[deprecated]] T calculateRandomType(int amount);
//texture management
void addTexture(std::string id, const char* path);

View File

@ -14,6 +14,7 @@
#include "Vector2D.h"
#include "PowerupComponent.h"
#include <iostream>
#include <algorithm>
AssetManager::AssetManager(Manager* manager) : man(manager) {}
@ -73,33 +74,39 @@ void AssetManager::createPowerup(Vector2D pos, std::function<void (Entity*)> pic
powerups.addGroup((size_t)Entity::GroupLabel::POWERUPS);
}
Vector2D AssetManager::calculateSpawnPosition()
Vector2D AssetManager::calculateSpawnPosition(Vector2D size, Vector2D spawnArea)
{
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() % (int)(spawnArea.x - size.x),
rand() % (int)(spawnArea.y - size.y),
size.x,
size.y
};
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)
{
spawnPos = Vector2D(spawnRect.x, spawnRect.y);
break;
}
}
return spawnPos;
}
PowerupType AssetManager::calculateType()
template <typename T>
T AssetManager::calculateRandomType(int amount)
{
PowerupType type = PowerupType(rand() % 3);
return type;
}
T type = T(rand() % amount);
return type;
}