diff --git a/include/AssetManager.h b/include/AssetManager.h index 6e14f85..4d6931e 100644 --- a/include/AssetManager.h +++ b/include/AssetManager.h @@ -23,7 +23,7 @@ public: AssetManager(Manager* manager); ~AssetManager(); - void createProjectile(Vector2D pos, Vector2D velocity, int scale, int range, int speed, const char* texturePath, TeamLabel teamLabel); + void createProjectile(Vector2D pos, Vector2D velocity, int scale, int range, int speed, const char* texturePath, Entity::TeamLabel teamLabel); void createPowerup(Vector2D pos, PowerupType type); Vector2D calculateSpawnPosition(); diff --git a/include/CollisionHandler.h b/include/CollisionHandler.h index cbd0423..1fdba31 100644 --- a/include/CollisionHandler.h +++ b/include/CollisionHandler.h @@ -43,16 +43,16 @@ public: // temporary function, remove once game.cpp cleaned up std::vector getColliders( - std::initializer_list const& groupLabels, - std::initializer_list const& teamLabels = {}, + std::initializer_list const& groupLabels, + std::initializer_list const& teamLabels = {}, bool negateTeam = false); template T getAnyIntersection( Entity* entity, Vector2D posMod = {}, - std::initializer_list const& groupLabels = {}, - std::initializer_list const& teamLabels = {}, + std::initializer_list const& groupLabels = {}, + std::initializer_list const& teamLabels = {}, bool negateTeam = false); void update(); diff --git a/include/Entity.h b/include/Entity.h index ad0f2b5..82ed6a5 100644 --- a/include/Entity.h +++ b/include/Entity.h @@ -16,27 +16,29 @@ using ComponentBitSet = std::bitset; using GroupBitSet = std::bitset; using ComponentArray = std::array; -enum class GroupLabel -{ - MAPTILES, - PLAYERS, - ENEMIES, - COLLIDERS, - PROJECTILE, - HEARTS, - POWERUPS -}; - -enum class TeamLabel -{ - NONE, - BLUE, - RED -}; - class Entity { public: + +/*! TODO */ + enum class GroupLabel + { + MAPTILES, + PLAYERS, + ENEMIES, + COLLIDERS, + PROJECTILE, + HEARTS, + POWERUPS + }; + + enum class TeamLabel + { + NONE, //!< No team + BLUE, //!< Team blue + RED //!< Team red + }; + explicit Entity(Manager& mManager) : manager(mManager) { }; diff --git a/include/Game.h b/include/Game.h index 1f76a8f..9a9bddf 100644 --- a/include/Game.h +++ b/include/Game.h @@ -6,12 +6,12 @@ #include #include "Vector2D.h" +#include "Entity.h" class AssetManager; class CollisionHandler; class TextureManager; class SoundManager; -enum class TeamLabel; class Game { @@ -37,13 +37,13 @@ public: static SoundManager* soundManager; void refreshPlayers(); - TeamLabel getWinner() const; + Entity::TeamLabel getWinner() const; private: - void setWinner(TeamLabel winningTeam); + void setWinner(Entity::TeamLabel winningTeam); int counter = 0; bool isRunning = false; SDL_Window* window; - TeamLabel winner; + Entity::TeamLabel winner; }; diff --git a/include/PopupWindow.h b/include/PopupWindow.h index 2ee3202..ccd01f6 100644 --- a/include/PopupWindow.h +++ b/include/PopupWindow.h @@ -3,8 +3,9 @@ #include #include +#include "Entity.h" + class Game; -enum class TeamLabel; class PopupWindow { @@ -17,7 +18,7 @@ public: bool interacted; - void renderWinnerPopup(TeamLabel winner); + void renderWinnerPopup(Entity::TeamLabel winner); private: SDL_Renderer* renderer; diff --git a/src/AssetManager.cpp b/src/AssetManager.cpp index 1dff706..03a1d6a 100644 --- a/src/AssetManager.cpp +++ b/src/AssetManager.cpp @@ -37,14 +37,14 @@ Mix_Chunk* AssetManager::getSound(std::string id) { return soundEffects.at(id); } -void AssetManager::createProjectile(Vector2D pos, Vector2D velocity, int scale, int range, int speed, const char* texturePath, TeamLabel teamLabel) { +void AssetManager::createProjectile(Vector2D pos, Vector2D velocity, int scale, int range, int speed, const char* texturePath, Entity::TeamLabel teamLabel) { auto& projectile(man->addEntity()); projectile.addComponent(pos.x, pos.y, 32, 32, scale); //32x32 is standard size for objects projectile.addComponent(texturePath); projectile.addComponent(range, speed, velocity); projectile.addComponent("projectile", 0.6f); - projectile.addGroup((size_t)GroupLabel::PROJECTILE); + projectile.addGroup((size_t)Entity::GroupLabel::PROJECTILE); projectile.setTeam(teamLabel); } @@ -67,7 +67,7 @@ void AssetManager::createPowerup(Vector2D pos, PowerupType type) { powerups.addComponent("powerup", 0.6f); powerups.addComponent(type); - powerups.addGroup((size_t)GroupLabel::POWERUPS); + powerups.addGroup((size_t)Entity::GroupLabel::POWERUPS); } Vector2D AssetManager::calculateSpawnPosition() @@ -81,7 +81,7 @@ Vector2D AssetManager::calculateSpawnPosition() spawnRect.x = rand() % (SCREEN_SIZE_WIDTH - spawnRect.w); spawnRect.y = rand() % (SCREEN_SIZE_HEIGHT - spawnRect.h); conflict = false; - for (auto cc : Game::collisionHandler->getColliders({ GroupLabel::MAPTILES })) + for (auto cc : Game::collisionHandler->getColliders({ Entity::GroupLabel::MAPTILES })) { if (SDL_HasIntersection(&spawnRect, &cc->collider) && strcmp(cc->tag, "projectile")) { diff --git a/src/CollisionHandler.cpp b/src/CollisionHandler.cpp index 1556e77..2ca742a 100644 --- a/src/CollisionHandler.cpp +++ b/src/CollisionHandler.cpp @@ -86,8 +86,8 @@ IntersectionBitSet CollisionHandler::getIntersectionWithBounds(Entity* entity, V } std::vector CollisionHandler::getColliders( - std::initializer_list const& groupLabels, - std::initializer_list const& teamLabels, + std::initializer_list const& groupLabels, + std::initializer_list const& teamLabels, bool negateTeam) { std::vector colliders; @@ -119,8 +119,8 @@ template<> IntersectionBitSet CollisionHandler::getAnyIntersection( Entity* entity, Vector2D posMod, - std::initializer_list const& groupLabels, - std::initializer_list const& teamLabels, + std::initializer_list const& groupLabels, + std::initializer_list const& teamLabels, bool negateTeam) { IntersectionBitSet intersections; @@ -134,8 +134,8 @@ template<> Entity* CollisionHandler::getAnyIntersection( Entity* entity, Vector2D posMod, - std::initializer_list const& groupLabels, - std::initializer_list const& teamLabels, + std::initializer_list const& groupLabels, + std::initializer_list const& teamLabels, bool negateTeam) { for (auto& collider : getColliders(groupLabels, teamLabels)) { diff --git a/src/Entity.cpp b/src/Entity.cpp index 0f523ea..7ed2f32 100644 --- a/src/Entity.cpp +++ b/src/Entity.cpp @@ -35,13 +35,13 @@ std::bitset Entity::getGroupBitSet() return groupBitSet; } -void Entity::setTeam(TeamLabel teamLabel) +void Entity::setTeam(Entity::TeamLabel teamLabel) { this->teamLabel = teamLabel; manager.addToTeam(this, (size_t) teamLabel); } -TeamLabel Entity::getTeam() +Entity::TeamLabel Entity::getTeam() { return teamLabel; } diff --git a/src/Game.cpp b/src/Game.cpp index 33c50c8..475ab6f 100644 --- a/src/Game.cpp +++ b/src/Game.cpp @@ -159,24 +159,24 @@ void Game::init(const char* title, int xpos, int ypos, int width, int height, bo //ecs implementation - player1.setTeam(TeamLabel::BLUE); + player1.setTeam(Entity::TeamLabel::BLUE); player1.addComponent(80,80,2); //posx, posy, scale player1.addComponent(player1Sprite, true); //adds sprite (32x32px), path needed player1.addComponent(SDL_SCANCODE_W, SDL_SCANCODE_S, SDL_SCANCODE_A, SDL_SCANCODE_D, SDL_SCANCODE_E, Vector2D(2, 0));//custom keycontrols can be added player1.addComponent("player", 0.8f); //adds tag (for further use, reference tag) player1.addComponent(5, Direction::LEFT); player1.addComponent(); - player1.addGroup((size_t) GroupLabel::PLAYERS); //tell programm what group it belongs to for rendering order + player1.addGroup((size_t) Entity::GroupLabel::PLAYERS); //tell programm what group it belongs to for rendering order - player2.setTeam(TeamLabel::RED); + player2.setTeam(Entity::TeamLabel::RED); player2.addComponent(600, 500, 2); player2.addComponent(player2Sprite, true); player2.addComponent(SDL_SCANCODE_UP, SDL_SCANCODE_DOWN, SDL_SCANCODE_LEFT, SDL_SCANCODE_RIGHT, SDL_SCANCODE_RCTRL, Vector2D(-2, 0)); player2.addComponent("enemy", 0.8f); player2.addComponent(5, Direction::RIGHT); player2.addComponent(); - player2.addGroup((size_t) GroupLabel::PLAYERS); + player2.addGroup((size_t) Entity::GroupLabel::PLAYERS); } void Game::selectCharacters(const char* &playerSprite, const char* &enemySprite) @@ -275,11 +275,11 @@ void Game::selectCharacters(const char* &playerSprite, const char* &enemySprite) this->isRunning = true; } -auto& tiles(manager.getGroup((size_t)GroupLabel::MAPTILES)); -auto& players(manager.getGroup((size_t)GroupLabel::PLAYERS)); -auto& projectiles(manager.getGroup((size_t)GroupLabel::PROJECTILE)); -auto& hearts(manager.getGroup((size_t)GroupLabel::HEARTS)); -auto& powerups(manager.getGroup((size_t)GroupLabel::POWERUPS)); +auto& tiles(manager.getGroup((size_t)Entity::GroupLabel::MAPTILES)); +auto& players(manager.getGroup((size_t)Entity::GroupLabel::PLAYERS)); +auto& projectiles(manager.getGroup((size_t)Entity::GroupLabel::PROJECTILE)); +auto& hearts(manager.getGroup((size_t)Entity::GroupLabel::HEARTS)); +auto& powerups(manager.getGroup((size_t)Entity::GroupLabel::POWERUPS)); void Game::handleEvents() { @@ -311,7 +311,7 @@ void Game::update() } // needs to be in game.cpp to have access to internal functions - for (auto& player : manager.getGroup((size_t) GroupLabel::PLAYERS)) { + for (auto& player : manager.getGroup((size_t) Entity::GroupLabel::PLAYERS)) { if (player->getComponent().getHealth() <= 0) { this->setWinner(player->getTeam()); } @@ -353,7 +353,7 @@ void Game::addTile(unsigned long id, int x, int y) auto& tile(manager.addEntity()); tile.addComponent(x, y, TILE_SIZE, TILE_SIZE, id); if (id == 1) tile.addComponent("water"); - tile.addGroup((size_t)GroupLabel::MAPTILES); + tile.addGroup((size_t)Entity::GroupLabel::MAPTILES); } bool Game::running() const @@ -361,13 +361,13 @@ bool Game::running() const return isRunning; } -void Game::setWinner(TeamLabel winningTeam) +void Game::setWinner(Entity::TeamLabel winningTeam) { this->winner = winningTeam; this->isRunning = false; } -TeamLabel Game::getWinner() const +Entity::TeamLabel Game::getWinner() const { return this->winner; } diff --git a/src/HealthComponent.cpp b/src/HealthComponent.cpp index b134b54..7bbaa60 100644 --- a/src/HealthComponent.cpp +++ b/src/HealthComponent.cpp @@ -26,7 +26,7 @@ void HealthComponent::setHealth(int health) void HealthComponent::refreshHearts() { // clear hearts if exist - for (auto& heart : this->entity->getManager().getGroup((size_t) GroupLabel::HEARTS)) { + for (auto& heart : this->entity->getManager().getGroup((size_t) Entity::GroupLabel::HEARTS)) { if (heart->getTeam() == this->entity->getTeam()) { heart->destroy(); } @@ -59,6 +59,6 @@ void HealthComponent::createHeartComponents(int x) auto& heart(this->entity->getManager().addEntity()); heart.addComponent(x,5,2); heart.addComponent("assets/heart.png"); - heart.addGroup((size_t)GroupLabel::HEARTS); + heart.addGroup((size_t)Entity::GroupLabel::HEARTS); heart.setTeam(this->entity->getTeam()); } \ No newline at end of file diff --git a/src/PopupWindow.cpp b/src/PopupWindow.cpp index d41c127..5912eaf 100644 --- a/src/PopupWindow.cpp +++ b/src/PopupWindow.cpp @@ -62,12 +62,12 @@ bool PopupWindow::shouldContinue() const { return continueGame; } -void PopupWindow::renderWinnerPopup(TeamLabel winner) { +void PopupWindow::renderWinnerPopup(Entity::TeamLabel winner) { SDL_RenderClear(this->renderer); //Maybe use texture manager (changes need to be made that it does not use game::renderer automatically, but receives one instead) - this->texture = winner == TeamLabel::BLUE ? + this->texture = winner == Entity::TeamLabel::BLUE ? IMG_LoadTexture(this->renderer, "assets/Player1Victory.png") : IMG_LoadTexture(this->renderer, "assets/Player2Victory.png"); diff --git a/src/PowerupComponent.cpp b/src/PowerupComponent.cpp index 76d4b09..18447f6 100644 --- a/src/PowerupComponent.cpp +++ b/src/PowerupComponent.cpp @@ -31,7 +31,7 @@ void PowerupComponent::update() if ((player = Game::collisionHandler->getAnyIntersection( entity, Vector2D(0, 0), - { GroupLabel::PLAYERS }, + { Entity::GroupLabel::PLAYERS }, {}, true)) != nullptr) { diff --git a/src/ProjectileComponent.cpp b/src/ProjectileComponent.cpp index c6e3238..f28e366 100644 --- a/src/ProjectileComponent.cpp +++ b/src/ProjectileComponent.cpp @@ -34,7 +34,7 @@ void ProjectileComponent::update() if ((player = Game::collisionHandler->getAnyIntersection( entity, Vector2D(0,0), - {GroupLabel::PLAYERS}, + {Entity::GroupLabel::PLAYERS}, {entity->getTeam()}, true)) != nullptr) { player->getComponent().modifyHealth(); diff --git a/src/TransformComponent.cpp b/src/TransformComponent.cpp index d586a1c..0556ca2 100644 --- a/src/TransformComponent.cpp +++ b/src/TransformComponent.cpp @@ -62,13 +62,13 @@ void TransformComponent::update() // TODO: move to separate functions - if (this->entity->hasGroup((size_t)GroupLabel::PLAYERS)) { + if (this->entity->hasGroup((size_t)Entity::GroupLabel::PLAYERS)) { IntersectionBitSet intersections = (CollisionHandler::getIntersectionWithBounds(entity, Vector2D(positionChange.x, 0)) | - (Game::collisionHandler->getAnyIntersection(entity, Vector2D(positionChange.x, 0), { GroupLabel::MAPTILES, GroupLabel::COLLIDERS })) & + (Game::collisionHandler->getAnyIntersection(entity, Vector2D(positionChange.x, 0), { Entity::GroupLabel::MAPTILES, Entity::GroupLabel::COLLIDERS })) & IntersectionBitSet("0011")) | (CollisionHandler::getIntersectionWithBounds(entity, Vector2D(0, positionChange.y)) | - (Game::collisionHandler->getAnyIntersection(entity, Vector2D(0, positionChange.y), { GroupLabel::MAPTILES, GroupLabel::COLLIDERS })) & + (Game::collisionHandler->getAnyIntersection(entity, Vector2D(0, positionChange.y), { Entity::GroupLabel::MAPTILES, Entity::GroupLabel::COLLIDERS })) & IntersectionBitSet("1100")); if (intersections.test((size_t)Direction::LEFT) || intersections.test((size_t)Direction::RIGHT)) diff --git a/src/main.cpp b/src/main.cpp index c07c71b..1b59151 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -35,9 +35,9 @@ int main(int argc, char* argv[]) SDL_Delay(frameDelay - frameTime); } } - TeamLabel winner = game->getWinner(); + Entity::TeamLabel winner = game->getWinner(); - PopupWindow popupWindow("Game over", winner == TeamLabel::BLUE ? + PopupWindow popupWindow("Game over", winner == Entity::TeamLabel::BLUE ? "Player1 won! Press 'C' to continue or 'Q' to quit." : "Player2 won! Press 'C' to continue or 'Q' to quit.");