From fcf14b20c4fdfd1693583ae88416e080408ae79a Mon Sep 17 00:00:00 2001 From: Nimac0 Date: Sun, 28 Jan 2024 14:56:09 +0100 Subject: [PATCH] framework for randomly spawning items not collectable yet, dont get destroyed either, WIP --- include/AssetManager.h | 3 +++ include/Component.h | 3 ++- include/Constants.h | 2 ++ include/Powerup.h | 26 ++++++++++++++++++++++++++ include/TextureDict.h | 10 +++++++++- src/AssetManager.cpp | 21 +++++++++++++++++++++ src/Game.cpp | 28 ++++++++++++++++++---------- src/Powerup.cpp | 38 ++++++++++++++++++++++++++++++++++++++ src/SpriteComponent.cpp | 7 +------ src/TileComponent.cpp | 6 ++---- src/main.cpp | 2 ++ 11 files changed, 124 insertions(+), 22 deletions(-) create mode 100644 include/Powerup.h create mode 100644 src/Powerup.cpp diff --git a/include/AssetManager.h b/include/AssetManager.h index 61b94d2..fe73d00 100644 --- a/include/AssetManager.h +++ b/include/AssetManager.h @@ -2,6 +2,8 @@ #include #include +#include "Powerup.h" + class Vector2D; class Manager; @@ -13,6 +15,7 @@ public: ~AssetManager(); void createProjectile(Vector2D pos, Vector2D velocity, bool source, int scale, int range, int speed, const char* texturePath); + void createPowerup(Vector2D pos, PowerupType type); //texture management void addTexture(std::string id, const char* path); diff --git a/include/Component.h b/include/Component.h index bc788ac..e84b64d 100644 --- a/include/Component.h +++ b/include/Component.h @@ -9,7 +9,8 @@ enum class GroupLabel ENEMIES, COLLIDERS, PROJECTILE, - HEARTS + HEARTS, + POWERUPS }; class Component diff --git a/include/Constants.h b/include/Constants.h index 502bd2b..55dc3a2 100644 --- a/include/Constants.h +++ b/include/Constants.h @@ -17,3 +17,5 @@ constexpr int TILE_SIZE = 32; constexpr int MAP_SIZE_X = 25; constexpr int MAP_SIZE_Y = 20; +constexpr int SPAWN_ATTEMPTS = 30; + diff --git a/include/Powerup.h b/include/Powerup.h new file mode 100644 index 0000000..eaef7ac --- /dev/null +++ b/include/Powerup.h @@ -0,0 +1,26 @@ +#pragma once + +#include "Component.h" +#include "Manager.h" +#include "Vector2D.h" +#include + +enum class PowerupType +{ + HEART, + WALKINGSPEED, + SHOOTINGSPEED +}; + +class Powerup +{ +public: + Powerup(){} + ~Powerup(){} + + static Vector2D calculateSpawnPosition(); + static PowerupType calculateType(); + +private: + Manager* manager; +}; \ No newline at end of file diff --git a/include/TextureDict.h b/include/TextureDict.h index 847e649..40d146f 100644 --- a/include/TextureDict.h +++ b/include/TextureDict.h @@ -2,15 +2,23 @@ #include #include +#include "Powerup.h" class TextureDict { public: - const std::map textureDictionary = { + const std::map tileDictionary = { {1, "assets/water.png"}, {2, "assets/dirt.png"}, {3, "assets/grass.png"}, {7, "assets/grass_water_left.png"}, {9, "assets/grass_water_right.png"} }; + + + std::map powerupDictionary = { + {PowerupType::HEART, "assets/heart.png"}, + {PowerupType::WALKINGSPEED, "assets/heart.png"}, + {PowerupType::SHOOTINGSPEED, "assets/heart.png"} + }; }; diff --git a/src/AssetManager.cpp b/src/AssetManager.cpp index 766fe27..7cbf0b9 100644 --- a/src/AssetManager.cpp +++ b/src/AssetManager.cpp @@ -23,4 +23,25 @@ void AssetManager::createProjectile(Vector2D pos, Vector2D velocity, bool source projectile.addComponent(range, speed, velocity, source); projectile.addComponent("projectile", 0.6f); projectile.addGroup((size_t)GroupLabel::PROJECTILE); +} + +void AssetManager::createPowerup(Vector2D pos, PowerupType type) { + TextureDict textureDict; + + auto& powerups(man->addEntity()); + powerups.addComponent(pos.x, pos.y, 32, 32, 1); //32x32 is standard size for objects + auto it = textureDict.powerupDictionary.find(type); + if (it == textureDict.powerupDictionary.end()) { + std::cout << "it end" << std::endl; + } + + try { + powerups.addComponent(it->second.data()); + } + catch (std::runtime_error e) { + std::cout << e.what() << std::endl; + } + + powerups.addComponent("powerup", 0.6f); + powerups.addGroup((size_t)GroupLabel::POWERUPS); } \ No newline at end of file diff --git a/src/Game.cpp b/src/Game.cpp index a979a8b..665226e 100644 --- a/src/Game.cpp +++ b/src/Game.cpp @@ -5,6 +5,7 @@ #include "AssetManager.h" #include "Map.h" #include "TextureManager.h" +#include "Powerup.h" Map* map; Manager manager; @@ -20,7 +21,6 @@ std::vector Game::colliders; auto& player(manager.addEntity()); auto& enemy(manager.addEntity()); auto& wall(manager.addEntity()); -//auto& projectile (manager.addEntity()); Game::Game() = default; @@ -131,6 +131,7 @@ auto& players(manager.getGroup((size_t)GroupLabel::PLAYERS)); auto& enemies(manager.getGroup((size_t)GroupLabel::ENEMIES)); auto& projectiles(manager.getGroup((size_t)GroupLabel::PROJECTILE)); auto& hearts(manager.getGroup((size_t)GroupLabel::HEARTS)); +auto& powerups(manager.getGroup((size_t)GroupLabel::POWERUPS)); void Game::handleEvents() { @@ -151,16 +152,23 @@ void Game::update() Vector2D playerPos = player.getComponent().position; Vector2D enemyPos = enemy.getComponent().position; + int powerupSpawn = rand() % 500; + manager.refresh(); manager.update(); + if (powerupSpawn == 0) + { + assets->createPowerup(Powerup::calculateSpawnPosition(), Powerup::calculateType()); + } + for (auto cc : colliders) { - if (SDL_HasIntersection(&player.getComponent().collider, &cc->collider) && strcmp(cc->tag, "player") && cc->hasCollision) + if (SDL_HasIntersection(&player.getComponent().collider, &cc->collider) && strcmp(cc->tag, "player")) { player.getComponent().position = playerPos; } - if (SDL_HasIntersection(&enemy.getComponent().collider, &cc->collider) && strcmp(cc->tag, "enemy") && cc->hasCollision) + if (SDL_HasIntersection(&enemy.getComponent().collider, &cc->collider) && strcmp(cc->tag, "enemy")) { enemy.getComponent().position = enemyPos; } @@ -218,17 +226,17 @@ void Game::render() { SDL_RenderClear(renderer); for (auto& t : tiles) - { t->draw(); - } - for (auto& p : players) - { + + for (auto& p : powerups) p->draw(); - } + + for (auto& p : players) + p->draw(); + for (auto& e : enemies) - { e->draw(); - } + for (auto& p : projectiles) p->draw(); diff --git a/src/Powerup.cpp b/src/Powerup.cpp new file mode 100644 index 0000000..3110c5c --- /dev/null +++ b/src/Powerup.cpp @@ -0,0 +1,38 @@ +#include "Powerup.h" + +#include "TextureDict.h" +#include +#include "Constants.h" +#include "Game.h" +#include "ColliderComponent.h" + +Vector2D Powerup::calculateSpawnPosition() +{ + 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 : Game::colliders) + { + if (SDL_HasIntersection(&spawnRect, &cc->collider) && strcmp(cc->tag, "projectile")) + { + conflict = true; + break; + } + } + if (conflict) continue; + spawnPos = Vector2D(spawnRect.x, spawnRect.y); + } + return spawnPos; +} + +PowerupType Powerup::calculateType() +{ + PowerupType type = PowerupType(rand() % 3); + return type; +} \ No newline at end of file diff --git a/src/SpriteComponent.cpp b/src/SpriteComponent.cpp index 6af7d6b..79221f9 100644 --- a/src/SpriteComponent.cpp +++ b/src/SpriteComponent.cpp @@ -73,10 +73,5 @@ void SpriteComponent::playAnimation(AnimationType type) void SpriteComponent::setDirection(SpriteDirection direction) { - if (direction == RIGHT) { - this->flipped = true; - return; - } - - this->flipped = false; + this->flipped = direction == RIGHT; } \ No newline at end of file diff --git a/src/TileComponent.cpp b/src/TileComponent.cpp index accd38a..c6e5cec 100644 --- a/src/TileComponent.cpp +++ b/src/TileComponent.cpp @@ -15,13 +15,11 @@ TileComponent::TileComponent(int x, int y, int w, int h, int id) this->tileRect.h = h; tileID = id; - auto it = textureDict.textureDictionary.find(tileID); //every id has its own distinct texture (in texturedict.h) - if (it == textureDict.textureDictionary.end()) { + auto it = textureDict.tileDictionary.find(tileID); //every id has its own distinct texture (in texturedict.h) + if (it == textureDict.tileDictionary.end()) { std::cout << "it end" << std::endl; return; } - bool test = it == textureDict.textureDictionary.end(); - // std::cout << it->second.data() << std::endl; this->path = it->second.data(); } diff --git a/src/main.cpp b/src/main.cpp index 2e7a993..f33d4c6 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,10 +1,12 @@ #include "Game.h" #include "Constants.h" +#include Game* game = nullptr; int main(int argc, char* argv[]) { + srand(time(NULL)); const int frameDelay = 1000 / FPS; Uint32 frameStart;