From 8ff81ace72afbab9bbce01bd15d4aa354464a402 Mon Sep 17 00:00:00 2001 From: Benedikt Galbavy Date: Sun, 28 Jan 2024 14:51:01 +0100 Subject: [PATCH 1/9] Rewrite player collisions --- include/CollisionHandler.h | 44 ++++++++++++++++++ include/Component.h | 10 ---- include/Constants.h | 2 + include/Entity.h | 26 ++++++++++- include/Game.h | 4 +- include/Manager.h | 9 ++-- include/PlayerComponent.h | 9 ++++ include/Vector2D.h | 1 + src/ColliderComponent.cpp | 7 +-- src/CollisionHandler.cpp | 94 ++++++++++++++++++++++++++++++++++++++ src/Entity.cpp | 12 +++++ src/Game.cpp | 83 ++++++++++++++++++--------------- src/Manager.cpp | 39 ++++++++++++---- src/PlayerComponent.cpp | 1 + src/TransformComponent.cpp | 48 ++++++++++++++++--- src/Vector2D.cpp | 6 +++ 16 files changed, 324 insertions(+), 71 deletions(-) create mode 100644 include/CollisionHandler.h create mode 100644 include/PlayerComponent.h create mode 100644 src/CollisionHandler.cpp create mode 100644 src/PlayerComponent.cpp diff --git a/include/CollisionHandler.h b/include/CollisionHandler.h new file mode 100644 index 0000000..2783925 --- /dev/null +++ b/include/CollisionHandler.h @@ -0,0 +1,44 @@ +#pragma once + +#include "Entity.h" +#include "SpriteComponent.h" +#include "Vector2D.h" +#include + +class ColliderComponent; +class Manager; +class Entity; + +constexpr uint8_t DIRECTION_C = 4; + +enum class direction +{ + LEFT = 0, + RIGHT, + UP, + DOWN +}; + +using IntersectionBitSet = std::bitset; + +class CollisionHandler +{ +private: + Manager& manager; + +public: + + CollisionHandler(Manager& mManager) : + manager(mManager) { }; + ~CollisionHandler(); + + + static IntersectionBitSet getIntersection(Entity* entityA, Entity* entityB); // intersections relative to entityA + static IntersectionBitSet getIntersection(Entity* entityA, Entity* entityB, Vector2D posModA, Vector2D posModB); + static IntersectionBitSet getIntersectionWithBounds(Entity* entity);// will fail if speed high enough to switch from no collision to full overlap in one tick + static IntersectionBitSet getIntersectionWithBounds(Entity* entity, Vector2D posMod); + + std::vector getColliders(GroupLabel groupLabel); // temporary function, remove once game.cpp cleaned up + + void update(); +}; \ No newline at end of file diff --git a/include/Component.h b/include/Component.h index bc788ac..095b370 100644 --- a/include/Component.h +++ b/include/Component.h @@ -2,16 +2,6 @@ class Entity; -enum class GroupLabel -{ - MAP, - PLAYERS, - ENEMIES, - COLLIDERS, - PROJECTILE, - HEARTS -}; - class Component { public: diff --git a/include/Constants.h b/include/Constants.h index 502bd2b..cd330f5 100644 --- a/include/Constants.h +++ b/include/Constants.h @@ -3,9 +3,11 @@ #include using Group = std::size_t; +using Team = std::size_t; constexpr std::size_t MAX_COMPONENTS = 32; constexpr std::size_t MAX_GROUPS = 32; +constexpr std::size_t MAX_TEAMS = 8; // constexpr int SCREEN_SIZE_HEIGHT = 640; constexpr int SCREEN_SIZE_WIDTH = 800; diff --git a/include/Entity.h b/include/Entity.h index 326ff7c..630e2fe 100644 --- a/include/Entity.h +++ b/include/Entity.h @@ -15,10 +15,28 @@ using ComponentBitSet = std::bitset; using GroupBitSet = std::bitset; using ComponentArray = std::array; +enum class GroupLabel +{ + MAPTILES, + PLAYERS, + ENEMIES, + COLLIDERS, + PROJECTILE, + HEARTS +}; + +enum class TeamLabel +{ + NONE, + BLUE, + RED +}; + class Entity { public: - explicit Entity(Manager& mManager) : manager(mManager) { } + explicit Entity(Manager& mManager) : + manager(mManager) { }; void update() const; void draw() const; @@ -30,6 +48,11 @@ public: void addGroup(Group mGroup); void delGroup(Group mGroup); + void setTeam(TeamLabel teamLabel); + TeamLabel getTeam(); + + Manager& getManager() { return manager; }; + template bool hasComponent() const { return componentBitSet[getComponentTypeID()]; @@ -63,4 +86,5 @@ private: ComponentArray componentArray = {}; ComponentBitSet componentBitSet; GroupBitSet groupBitSet; + TeamLabel teamLabel; }; \ No newline at end of file diff --git a/include/Game.h b/include/Game.h index ed07f38..64fa511 100644 --- a/include/Game.h +++ b/include/Game.h @@ -5,7 +5,7 @@ #include class AssetManager; -class ColliderComponent; +class CollisionHandler; class Game { @@ -24,7 +24,7 @@ public: static void addTile(int id, int x, int y); static SDL_Renderer* renderer; static SDL_Event event; - static std::vector colliders; + static CollisionHandler* collisionHandler; static AssetManager* assets; bool getWinner(); diff --git a/include/Manager.h b/include/Manager.h index f85f3b8..0046c33 100644 --- a/include/Manager.h +++ b/include/Manager.h @@ -5,8 +5,7 @@ #include #include "Constants.h" - -class Entity; +#include "Entity.h" class Manager { @@ -18,9 +17,13 @@ public: void addToGroup(Entity* mEntity, Group mGroup); std::vector& getGroup(Group mGroup); + void addToTeam(Entity* mEntity, Team mTeam); + std::vector& getTeam(Team mTeam); + Entity& addEntity(); private: std::vector> entities; - std::array, MAX_GROUPS> groupedEntities; + std::array, MAX_GROUPS> entitiesByGroup; + std::array, MAX_TEAMS> entitiesByTeam; }; \ No newline at end of file diff --git a/include/PlayerComponent.h b/include/PlayerComponent.h new file mode 100644 index 0000000..922fd04 --- /dev/null +++ b/include/PlayerComponent.h @@ -0,0 +1,9 @@ +#pragma once + +#include "Component.h" + +class PlayerComponent : public Component +{ +public: +private: +}; \ No newline at end of file diff --git a/include/Vector2D.h b/include/Vector2D.h index 96380f8..11704a7 100644 --- a/include/Vector2D.h +++ b/include/Vector2D.h @@ -13,6 +13,7 @@ public: friend Vector2D& operator-(Vector2D& vector1, const Vector2D& vector2); friend Vector2D& operator*(Vector2D& vector1, const Vector2D& vector2); friend Vector2D& operator/(Vector2D& vector1, const Vector2D& vector2); + friend Vector2D& operator+=(Vector2D& vector1, const Vector2D& vector2); Vector2D& operator*(const int& i); Vector2D& zero(); diff --git a/src/ColliderComponent.cpp b/src/ColliderComponent.cpp index d1c59c0..d5ea1e5 100644 --- a/src/ColliderComponent.cpp +++ b/src/ColliderComponent.cpp @@ -1,5 +1,6 @@ #include "ColliderComponent.h" +#include "CollisionHandler.h" #include "Entity.h" #include "Game.h" #include "TransformComponent.h" @@ -26,13 +27,13 @@ void ColliderComponent::init() } transform = &entity->getComponent(); - Game::colliders.push_back(this); + //Game::collisionHandler->add(this); } void ColliderComponent::update() { - collider.x = transform->position.x; - collider.y = transform->position.y; + collider.x = transform->position.x - (transform->width - transform->width * transform->scale * this->hitboxScale) / 2; + collider.y = transform->position.y - (transform->width - transform->width * transform->scale * this->hitboxScale) / 2; collider.w = (transform->width * transform->scale) * this->hitboxScale; diff --git a/src/CollisionHandler.cpp b/src/CollisionHandler.cpp new file mode 100644 index 0000000..a96f54a --- /dev/null +++ b/src/CollisionHandler.cpp @@ -0,0 +1,94 @@ +#include "CollisionHandler.h" + +#include "ColliderComponent.h" +#include "Constants.h" +#include "Entity.h" +#include "Manager.h" +#include "Vector2D.h" + +#include +#include + +IntersectionBitSet CollisionHandler::getIntersection(Entity* entityA, Entity* entityB) +{ + return getIntersection(entityA, entityB, Vector2D(0,0), Vector2D(0,0)); +} + +IntersectionBitSet CollisionHandler::getIntersection(Entity* entityA, Entity* entityB, Vector2D posModA, Vector2D posModB) +{ + if (!entityA->hasComponent() || + !entityB->hasComponent()) + return std::bitset(); + + SDL_Rect* colliderA = &entityA->getComponent().collider; + SDL_Rect* colliderB = &entityB->getComponent().collider; + + if (!SDL_HasIntersection( + colliderA, + colliderB)) + return std::bitset(); + + std::bitset intersections; + + // checks all 4 directions to allow checking full overlap + if (colliderA->x + posModA.x < colliderB->x + colliderB->w + posModB.x) + intersections.set((size_t) direction::LEFT) = 1; + + if (colliderA->x + colliderA->w + posModA.x > colliderB->x + posModB.x) + intersections.set((size_t) direction::RIGHT) = 1; + + if (colliderA->y + posModA.y < colliderB->y + colliderB->h + posModB.y) + intersections.set((size_t) direction::UP) = 1; + + if (colliderA->y + colliderA->h + posModA.y > colliderB->y + posModB.y) + intersections.set((size_t) direction::DOWN) = 1; + + return intersections; +} + +IntersectionBitSet CollisionHandler::getIntersectionWithBounds(Entity* entity) +{ + return getIntersectionWithBounds(entity, Vector2D(0,0)); +} + +IntersectionBitSet CollisionHandler::getIntersectionWithBounds(Entity* entity, Vector2D posMod) +{ + if (!entity->hasComponent()) + return std::bitset(); + + SDL_Rect* collider = &entity->getComponent().collider; + + std::bitset intersections; + + // all 4 directions and both sides to allow checking for fully out of bounds + if (collider->x + posMod.x < 0 || + collider->x + posMod.x > SCREEN_SIZE_WIDTH) + intersections.set((size_t) direction::LEFT); + + if (collider->x + collider->w + posMod.x < 0 || + collider->x + collider->w + posMod.x > SCREEN_SIZE_WIDTH) + intersections.set((size_t) direction::RIGHT); + + if (collider->y + posMod.y < 0 || + collider->y + posMod.y > SCREEN_SIZE_HEIGHT) + intersections.set((size_t) direction::UP); + + if (collider->y + collider->h + posMod.y < 0 || + collider->y + collider->h + posMod.y > SCREEN_SIZE_HEIGHT) + intersections.set((size_t) direction::DOWN); + + return intersections; +} + +std::vector CollisionHandler::getColliders(GroupLabel groupLabel) +{ + std::vector colliders; + + for (auto& entity : manager.getGroup((size_t) groupLabel)) { + if (!entity->hasComponent()) + continue; + colliders.emplace_back(&entity->getComponent()); + } + + return colliders; +} \ No newline at end of file diff --git a/src/Entity.cpp b/src/Entity.cpp index 8ec725d..6817071 100644 --- a/src/Entity.cpp +++ b/src/Entity.cpp @@ -2,6 +2,7 @@ #include "Manager.h" #include "Component.h" +#include void Entity::update() const { @@ -28,3 +29,14 @@ void Entity::delGroup(Group mGroup) { groupBitSet[mGroup] = false; } + +void Entity::setTeam(TeamLabel teamLabel) +{ + teamLabel = teamLabel; + manager.addToTeam(this, (size_t) teamLabel); +} + +TeamLabel Entity::getTeam() +{ + return teamLabel; +} diff --git a/src/Game.cpp b/src/Game.cpp index a979a8b..a365d63 100644 --- a/src/Game.cpp +++ b/src/Game.cpp @@ -1,8 +1,10 @@ #include "Game.h" +#include "CollisionHandler.h" #include "Components.h" #include "AssetManager.h" +#include "Entity.h" #include "Map.h" #include "TextureManager.h" @@ -11,14 +13,15 @@ Manager manager; AssetManager* Game::assets = new AssetManager(&manager); +CollisionHandler* Game::collisionHandler = new CollisionHandler(manager); + SDL_Renderer* Game::renderer = nullptr; SDL_Event Game::event; -std::vector Game::colliders; +auto& player1(manager.addEntity()); +auto& player2(manager.addEntity()); -auto& player(manager.addEntity()); -auto& enemy(manager.addEntity()); auto& wall(manager.addEntity()); //auto& projectile (manager.addEntity()); @@ -110,25 +113,27 @@ void Game::init(const char* title, int xpos, int ypos, int width, int height, bo //ecs implementation - player.addComponent(80,80,2); //posx, posy, scale - player.addComponent("assets/chicken_knight_spritesheet.png", true); //adds sprite (32x32px), path needed - player.addComponent(SDL_SCANCODE_W, SDL_SCANCODE_S, SDL_SCANCODE_A, SDL_SCANCODE_D, SDL_SCANCODE_E, Vector2D(1, 0));//custom keycontrols can be added - player.addComponent("player", 0.8f); //adds tag (for further use, reference tag) - player.addComponent(5, &manager, true); - player.addGroup((size_t)GroupLabel::PLAYERS); //tell programm what group it belongs to for rendering order + player1.addComponent(80,80,2); //posx, posy, scale + player1.addComponent("assets/chicken_knight_spritesheet.png", true); //adds sprite (32x32px), path needed + player1.addComponent(SDL_SCANCODE_W, SDL_SCANCODE_S, SDL_SCANCODE_A, SDL_SCANCODE_D, SDL_SCANCODE_E, Vector2D(1, 0));//custom keycontrols can be added + player1.addComponent("player", 0.8f); //adds tag (for further use, reference tag) + player1.addComponent(5, &manager, true); + player1.addGroup((size_t) GroupLabel::PLAYERS); //tell programm what group it belongs to for rendering order + player1.setTeam(TeamLabel::BLUE); - enemy.addComponent(600, 500, 2); - enemy.addComponent("assets/chicken_spritesheet.png", true); - enemy.addComponent(SDL_SCANCODE_UP, SDL_SCANCODE_DOWN, SDL_SCANCODE_LEFT, SDL_SCANCODE_RIGHT, SDL_SCANCODE_RCTRL, Vector2D(-1, 0)); - enemy.addComponent("enemy", 0.8f); - enemy.addComponent(5, &manager, false); - enemy.addGroup((size_t)GroupLabel::ENEMIES); + player2.addComponent(600, 500, 2); + player2.addComponent("assets/chicken_spritesheet.png", true); + player2.addComponent(SDL_SCANCODE_UP, SDL_SCANCODE_DOWN, SDL_SCANCODE_LEFT, SDL_SCANCODE_RIGHT, SDL_SCANCODE_RCTRL, Vector2D(-1, 0)); + player2.addComponent("enemy", 0.8f); + player2.addComponent(5, &manager, false); + player2.addGroup((size_t) GroupLabel::PLAYERS); + player2.setTeam(TeamLabel::RED); } -auto& tiles(manager.getGroup((size_t)GroupLabel::MAP)); +auto& tiles(manager.getGroup((size_t)GroupLabel::MAPTILES)); auto& players(manager.getGroup((size_t)GroupLabel::PLAYERS)); -auto& enemies(manager.getGroup((size_t)GroupLabel::ENEMIES)); +auto& enemies(manager.getGroup((size_t)GroupLabel::PLAYERS)); auto& projectiles(manager.getGroup((size_t)GroupLabel::PROJECTILE)); auto& hearts(manager.getGroup((size_t)GroupLabel::HEARTS)); @@ -148,64 +153,70 @@ void Game::handleEvents() void Game::update() { - Vector2D playerPos = player.getComponent().position; - Vector2D enemyPos = enemy.getComponent().position; + Vector2D playerPos = player1.getComponent().position; + Vector2D enemyPos = player2.getComponent().position; manager.refresh(); manager.update(); - for (auto cc : colliders) + + + /*for (auto cc : Game::collisionHandler->getColliders(GroupLabel::COLLIDERS)) { - if (SDL_HasIntersection(&player.getComponent().collider, &cc->collider) && strcmp(cc->tag, "player") && cc->hasCollision) + if (cc->hasCollision && + strcmp(cc->tag, "player") && + SDL_HasIntersection(&player1.getComponent().collider, &cc->collider)) { - player.getComponent().position = playerPos; + player1.getComponent().position = playerPos; } - if (SDL_HasIntersection(&enemy.getComponent().collider, &cc->collider) && strcmp(cc->tag, "enemy") && cc->hasCollision) + if (cc->hasCollision && + strcmp(cc->tag, "enemy") && + SDL_HasIntersection(&player2.getComponent().collider, &cc->collider)) { - enemy.getComponent().position = enemyPos; + player2.getComponent().position = enemyPos; } - } + }*/ //checking if projectiles hit player1 or player2 for (auto& p : projectiles) { - if(SDL_HasIntersection(&enemy.getComponent().collider, &p->getComponent().collider) + if(SDL_HasIntersection(&player2.getComponent().collider, &p->getComponent().collider) && (p->getComponent().hasCollision) && !p->getComponent().getSource()) { //std::cout << "Enemy hit!"; p->getComponent().removeCollision(); p->destroy(); - enemy.getComponent().getDamage(); + player2.getComponent().getDamage(); //display updated health | pretty scuffed but works ig for(auto h : hearts) h->destroy(); - player.getComponent().createAllHearts(); - enemy.getComponent().createAllHearts(); + player1.getComponent().createAllHearts(); + player2.getComponent().createAllHearts(); - if(enemy.getComponent().getHealth() < 1) { + if(player2.getComponent().getHealth() < 1) { std::cout << "Player1 wins!" << std::endl; winner = true; isRunning = false; } } - if(SDL_HasIntersection(&player.getComponent().collider, &p->getComponent().collider) + if(SDL_HasIntersection(&player1.getComponent().collider, &p->getComponent().collider) && (p->getComponent().hasCollision) && p->getComponent().getSource()) { //std::cout << "Player hit!"; p->getComponent().removeCollision(); p->destroy(); - player.getComponent().getDamage(); + player1.getComponent().getDamage(); //display updated health for(auto h : hearts) h->destroy(); - player.getComponent().createAllHearts(); - enemy.getComponent().createAllHearts(); + player1.getComponent().createAllHearts(); + player2.getComponent().createAllHearts(); - if(player.getComponent().getHealth() < 1) { + if(player1.getComponent().getHealth() < 1) { std::cout << "Player2 wins!" << std::endl; winner = false; isRunning = false; @@ -251,7 +262,7 @@ void Game::addTile(int 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::MAP); + tile.addGroup((size_t)GroupLabel::MAPTILES); } bool Game::running() const diff --git a/src/Manager.cpp b/src/Manager.cpp index ba800b1..3759704 100644 --- a/src/Manager.cpp +++ b/src/Manager.cpp @@ -2,12 +2,7 @@ #include -#include "Entity.h" - -void Manager::update() -{ - for (auto& e : entities) e->update(); -} +#include "Constants.h" void Manager::draw() { @@ -18,7 +13,7 @@ void Manager::refresh() { for (auto i(0u); i < MAX_GROUPS; i++) { - auto& v(groupedEntities[i]); + auto& v(entitiesByGroup[i]); v.erase( std::remove_if(std::begin(v), std::end(v), [i](Entity* mEntity) @@ -27,6 +22,17 @@ void Manager::refresh() }), std::end(v)); } + for (auto i(0u); i < MAX_TEAMS; i++) + { + auto& v(entitiesByTeam[i]); + v.erase( + std::remove_if(std::begin(v), std::end(v), + [i](Entity* mEntity) + { + return !mEntity->isActive() || (size_t)(mEntity->getTeam()) != i; + }), std::end(v)); + } + entities.erase(std::remove_if(std::begin(entities), std::end(entities), [](const std::unique_ptr& mEntity) { @@ -35,14 +41,29 @@ void Manager::refresh() std::end(entities)); } +void Manager::update() +{ + for (auto& e : entities) e->update(); +} + void Manager::addToGroup(Entity* mEntity, Group mGroup) { - groupedEntities[mGroup].emplace_back(mEntity); + entitiesByGroup[mGroup].emplace_back(mEntity); } std::vector& Manager::getGroup(Group mGroup) { - return groupedEntities[mGroup]; + return entitiesByGroup[mGroup]; +} + +void Manager::addToTeam(Entity* mEntity, Team mTeam) +{ + entitiesByTeam[mTeam].emplace_back(mEntity); +} + +std::vector& Manager::getTeam(Team mTeam) +{ + return entitiesByTeam[mTeam]; } Entity& Manager::addEntity() diff --git a/src/PlayerComponent.cpp b/src/PlayerComponent.cpp new file mode 100644 index 0000000..3afa811 --- /dev/null +++ b/src/PlayerComponent.cpp @@ -0,0 +1 @@ +#include "PlayerComponent.h" \ No newline at end of file diff --git a/src/TransformComponent.cpp b/src/TransformComponent.cpp index 9f73934..f77278b 100644 --- a/src/TransformComponent.cpp +++ b/src/TransformComponent.cpp @@ -1,5 +1,12 @@ #include "TransformComponent.h" + +#include "CollisionHandler.h" +#include "ColliderComponent.h" #include "Constants.h" +#include "Entity.h" +#include "Game.h" +#include "Vector2D.h" +#include TransformComponent::TransformComponent() { @@ -44,12 +51,39 @@ void TransformComponent::update() // if(velocity.x != 0 && velocity.y != 0) float multiplier = velocity.x != 0 && velocity.y != 0 ? 0.707 : 1; //normalizes vector - Vector2D newPos( - position.x + velocity.x * speed * multiplier, - position.y + velocity.y * speed * multiplier + Vector2D positionChange( + velocity.x * speed * multiplier, + velocity.y * speed * multiplier ); - if (newPos.x < 0 || newPos.x + (this->width * this->scale) > SCREEN_SIZE_WIDTH || newPos.y < 0 || newPos.y + (this->height * this->scale) > SCREEN_SIZE_HEIGHT) - return; - position = newPos; -} + IntersectionBitSet intersectionsX = CollisionHandler::getIntersectionWithBounds(entity, positionChange); + for (auto& collider : Game::collisionHandler->getColliders(GroupLabel::MAPTILES)) { + intersectionsX |= CollisionHandler::getIntersection(entity, collider->entity, Vector2D(positionChange.x, 0), Vector2D(0, 0)); + } + for (auto& collider : Game::collisionHandler->getColliders(GroupLabel::COLLIDERS)) { + intersectionsX |= CollisionHandler::getIntersection(entity, collider->entity, Vector2D(positionChange.x, 0), Vector2D(0, 0)); + } + + IntersectionBitSet intersectionsY = CollisionHandler::getIntersectionWithBounds(entity, positionChange); + for (auto& collider : Game::collisionHandler->getColliders(GroupLabel::MAPTILES)) { + intersectionsY |= CollisionHandler::getIntersection(entity, collider->entity, Vector2D(0, positionChange.y), Vector2D(0, 0)); + } + for (auto& collider : Game::collisionHandler->getColliders(GroupLabel::COLLIDERS)) { + intersectionsY |= CollisionHandler::getIntersection(entity, collider->entity, Vector2D(0, positionChange.y), Vector2D(0, 0)); + } + + if (intersectionsX.test((size_t) direction::LEFT) && positionChange.x < 0) + positionChange.x = 0; + + if (intersectionsX.test((size_t) direction::RIGHT) && positionChange.x > 0) + positionChange.x = 0; + + if (intersectionsY.test((size_t) direction::UP) && positionChange.y < 0) + positionChange.y = 0; + + if (intersectionsY.test((size_t) direction::DOWN) && positionChange.y > 0) + positionChange.y = 0; + + position += positionChange; + +}; \ No newline at end of file diff --git a/src/Vector2D.cpp b/src/Vector2D.cpp index 7fae1a5..71d2848 100644 --- a/src/Vector2D.cpp +++ b/src/Vector2D.cpp @@ -36,6 +36,12 @@ Vector2D& operator/(Vector2D& vector1, const Vector2D& vector2) vector1.y /= vector2.y; return vector1; } +Vector2D& operator+=(Vector2D& vector1, const Vector2D& vector2) +{ + vector1.x += vector2.x; + vector1.y += vector2.y; + return vector1; +} Vector2D& Vector2D::operator*(const int& i) { this->x *= i; From 0c1c9a7f840438e1937d39f52d0434e126975578 Mon Sep 17 00:00:00 2001 From: Benedikt Galbavy Date: Sun, 28 Jan 2024 15:10:01 +0100 Subject: [PATCH 2/9] Hotfix only calculate for player --- src/Game.cpp | 18 ------------ src/TransformComponent.cpp | 60 +++++++++++++++++++++----------------- 2 files changed, 33 insertions(+), 45 deletions(-) diff --git a/src/Game.cpp b/src/Game.cpp index a365d63..0874e1a 100644 --- a/src/Game.cpp +++ b/src/Game.cpp @@ -159,24 +159,6 @@ void Game::update() manager.refresh(); manager.update(); - - - /*for (auto cc : Game::collisionHandler->getColliders(GroupLabel::COLLIDERS)) - { - if (cc->hasCollision && - strcmp(cc->tag, "player") && - SDL_HasIntersection(&player1.getComponent().collider, &cc->collider)) - { - player1.getComponent().position = playerPos; - } - if (cc->hasCollision && - strcmp(cc->tag, "enemy") && - SDL_HasIntersection(&player2.getComponent().collider, &cc->collider)) - { - player2.getComponent().position = enemyPos; - } - }*/ - //checking if projectiles hit player1 or player2 for (auto& p : projectiles) { if(SDL_HasIntersection(&player2.getComponent().collider, &p->getComponent().collider) diff --git a/src/TransformComponent.cpp b/src/TransformComponent.cpp index f77278b..3065e1f 100644 --- a/src/TransformComponent.cpp +++ b/src/TransformComponent.cpp @@ -56,34 +56,40 @@ void TransformComponent::update() velocity.y * speed * multiplier ); - IntersectionBitSet intersectionsX = CollisionHandler::getIntersectionWithBounds(entity, positionChange); - for (auto& collider : Game::collisionHandler->getColliders(GroupLabel::MAPTILES)) { - intersectionsX |= CollisionHandler::getIntersection(entity, collider->entity, Vector2D(positionChange.x, 0), Vector2D(0, 0)); + // TODO: move to separate functions + + if (this->entity->hasGroup((size_t) GroupLabel::PLAYERS)) { + IntersectionBitSet intersectionsX = CollisionHandler::getIntersectionWithBounds(entity, positionChange); + for (auto& collider : Game::collisionHandler->getColliders(GroupLabel::MAPTILES)) { + intersectionsX |= CollisionHandler::getIntersection(entity, collider->entity, Vector2D(positionChange.x, 0), Vector2D(0, 0)); + } + for (auto& collider : Game::collisionHandler->getColliders(GroupLabel::COLLIDERS)) { + intersectionsX |= CollisionHandler::getIntersection(entity, collider->entity, Vector2D(positionChange.x, 0), Vector2D(0, 0)); + } + + IntersectionBitSet intersectionsY = CollisionHandler::getIntersectionWithBounds(entity, positionChange); + for (auto& collider : Game::collisionHandler->getColliders(GroupLabel::MAPTILES)) { + intersectionsY |= CollisionHandler::getIntersection(entity, collider->entity, Vector2D(0, positionChange.y), Vector2D(0, 0)); + } + for (auto& collider : Game::collisionHandler->getColliders(GroupLabel::COLLIDERS)) { + intersectionsY |= CollisionHandler::getIntersection(entity, collider->entity, Vector2D(0, positionChange.y), Vector2D(0, 0)); + } + + if (this->entity->hasGroup((size_t) GroupLabel::PLAYERS) && this->entity->getTeam() == TeamLabel::BLUE) + std::cout << intersectionsX << std::endl; + + if (intersectionsX.test((size_t) direction::LEFT) && positionChange.x < 0) + positionChange.x = 0; + + if (intersectionsX.test((size_t) direction::RIGHT) && positionChange.x > 0) + positionChange.x = 0; + + if (intersectionsY.test((size_t) direction::UP) && positionChange.y < 0) + positionChange.y = 0; + + if (intersectionsY.test((size_t) direction::DOWN) && positionChange.y > 0) + positionChange.y = 0; } - for (auto& collider : Game::collisionHandler->getColliders(GroupLabel::COLLIDERS)) { - intersectionsX |= CollisionHandler::getIntersection(entity, collider->entity, Vector2D(positionChange.x, 0), Vector2D(0, 0)); - } - - IntersectionBitSet intersectionsY = CollisionHandler::getIntersectionWithBounds(entity, positionChange); - for (auto& collider : Game::collisionHandler->getColliders(GroupLabel::MAPTILES)) { - intersectionsY |= CollisionHandler::getIntersection(entity, collider->entity, Vector2D(0, positionChange.y), Vector2D(0, 0)); - } - for (auto& collider : Game::collisionHandler->getColliders(GroupLabel::COLLIDERS)) { - intersectionsY |= CollisionHandler::getIntersection(entity, collider->entity, Vector2D(0, positionChange.y), Vector2D(0, 0)); - } - - if (intersectionsX.test((size_t) direction::LEFT) && positionChange.x < 0) - positionChange.x = 0; - - if (intersectionsX.test((size_t) direction::RIGHT) && positionChange.x > 0) - positionChange.x = 0; - - if (intersectionsY.test((size_t) direction::UP) && positionChange.y < 0) - positionChange.y = 0; - - if (intersectionsY.test((size_t) direction::DOWN) && positionChange.y > 0) - positionChange.y = 0; position += positionChange; - }; \ No newline at end of file From f538e95623520f8901581a0dc5a2bf30e95fe456 Mon Sep 17 00:00:00 2001 From: Benedikt Galbavy Date: Sun, 28 Jan 2024 15:19:27 +0100 Subject: [PATCH 3/9] Hotfix collision to the right breaks sliding --- src/CollisionHandler.cpp | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/src/CollisionHandler.cpp b/src/CollisionHandler.cpp index a96f54a..b809f31 100644 --- a/src/CollisionHandler.cpp +++ b/src/CollisionHandler.cpp @@ -8,6 +8,7 @@ #include #include +#include IntersectionBitSet CollisionHandler::getIntersection(Entity* entityA, Entity* entityB) { @@ -31,17 +32,25 @@ IntersectionBitSet CollisionHandler::getIntersection(Entity* entityA, Entity* en std::bitset intersections; // checks all 4 directions to allow checking full overlap - if (colliderA->x + posModA.x < colliderB->x + colliderB->w + posModB.x) - intersections.set((size_t) direction::LEFT) = 1; + if (colliderA->x + posModA.x < colliderB->x + colliderB->w + posModB.x && + colliderA->x + posModA.x > colliderB->x + posModB.x) { + printf("%zu left\n", (size_t) direction::LEFT); + intersections.set((size_t) direction::LEFT); + } - if (colliderA->x + colliderA->w + posModA.x > colliderB->x + posModB.x) - intersections.set((size_t) direction::RIGHT) = 1; + if (colliderA->x + colliderA->w + posModA.x < colliderB->x + colliderB->w + posModB.x && + colliderA->x + colliderA->w + posModA.x > colliderB->x + posModB.x) { + printf("%zu right\n", (size_t) direction::RIGHT); + intersections.set((size_t) direction::RIGHT); + } - if (colliderA->y + posModA.y < colliderB->y + colliderB->h + posModB.y) - intersections.set((size_t) direction::UP) = 1; + if (colliderA->y + posModA.y < colliderB->y + colliderB->h + posModB.y && + colliderA->y + posModA.y > colliderB->y + posModB.y) + intersections.set((size_t) direction::UP); - if (colliderA->y + colliderA->h + posModA.y > colliderB->y + posModB.y) - intersections.set((size_t) direction::DOWN) = 1; + if (colliderA->y + colliderA->h + posModA.y < colliderB->y + colliderB->h + posModB.y && + colliderA->y + colliderA->h + posModA.y > colliderB->y + posModB.y) + intersections.set((size_t) direction::DOWN); return intersections; } From 9f2df7c6c55c6ee70777d76668ab79ccb32a183f Mon Sep 17 00:00:00 2001 From: Benedikt Galbavy Date: Sun, 28 Jan 2024 16:59:59 +0100 Subject: [PATCH 4/9] fixed sliding --- include/CollisionHandler.h | 1 + src/CollisionHandler.cpp | 33 +++++++++++++++++++-------------- 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/include/CollisionHandler.h b/include/CollisionHandler.h index 2783925..cf90905 100644 --- a/include/CollisionHandler.h +++ b/include/CollisionHandler.h @@ -1,6 +1,7 @@ #pragma once #include "Entity.h" +#include "SDL_rect.h" #include "SpriteComponent.h" #include "Vector2D.h" #include diff --git a/src/CollisionHandler.cpp b/src/CollisionHandler.cpp index b809f31..f8975d1 100644 --- a/src/CollisionHandler.cpp +++ b/src/CollisionHandler.cpp @@ -9,6 +9,7 @@ #include #include #include +#include IntersectionBitSet CollisionHandler::getIntersection(Entity* entityA, Entity* entityB) { @@ -21,35 +22,39 @@ IntersectionBitSet CollisionHandler::getIntersection(Entity* entityA, Entity* en !entityB->hasComponent()) return std::bitset(); - SDL_Rect* colliderA = &entityA->getComponent().collider; - SDL_Rect* colliderB = &entityB->getComponent().collider; + SDL_Rect colliderA = entityA->getComponent().collider; + SDL_Rect colliderB = entityB->getComponent().collider; + + colliderA.x += posModA.x; + colliderA.y += posModA.y; + + colliderB.x += posModB.x; + colliderB.y += posModB.y; if (!SDL_HasIntersection( - colliderA, - colliderB)) + &colliderA, + &colliderB)) return std::bitset(); std::bitset intersections; // checks all 4 directions to allow checking full overlap - if (colliderA->x + posModA.x < colliderB->x + colliderB->w + posModB.x && - colliderA->x + posModA.x > colliderB->x + posModB.x) { - printf("%zu left\n", (size_t) direction::LEFT); + if (colliderA.x < colliderB.x + colliderB.w && + colliderA.x > colliderB.x) { intersections.set((size_t) direction::LEFT); } - if (colliderA->x + colliderA->w + posModA.x < colliderB->x + colliderB->w + posModB.x && - colliderA->x + colliderA->w + posModA.x > colliderB->x + posModB.x) { - printf("%zu right\n", (size_t) direction::RIGHT); + if (colliderA.x + colliderA.w < colliderB.x + colliderB.w && + colliderA.x + colliderA.w > colliderB.x) { intersections.set((size_t) direction::RIGHT); } - if (colliderA->y + posModA.y < colliderB->y + colliderB->h + posModB.y && - colliderA->y + posModA.y > colliderB->y + posModB.y) + if (colliderA.y < colliderB.y + colliderB.h && + colliderA.y > colliderB.y) intersections.set((size_t) direction::UP); - if (colliderA->y + colliderA->h + posModA.y < colliderB->y + colliderB->h + posModB.y && - colliderA->y + colliderA->h + posModA.y > colliderB->y + posModB.y) + if (colliderA.y + colliderA.h < colliderB.y + colliderB.h && + colliderA.y + colliderA.h > colliderB.y) intersections.set((size_t) direction::DOWN); return intersections; From 4755076c347cc56ed24ae11aaefcaaea13f7d2dd Mon Sep 17 00:00:00 2001 From: Benedikt Galbavy Date: Sun, 28 Jan 2024 17:01:18 +0100 Subject: [PATCH 5/9] removed debug print --- src/TransformComponent.cpp | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/TransformComponent.cpp b/src/TransformComponent.cpp index 3065e1f..12609dd 100644 --- a/src/TransformComponent.cpp +++ b/src/TransformComponent.cpp @@ -75,9 +75,6 @@ void TransformComponent::update() intersectionsY |= CollisionHandler::getIntersection(entity, collider->entity, Vector2D(0, positionChange.y), Vector2D(0, 0)); } - if (this->entity->hasGroup((size_t) GroupLabel::PLAYERS) && this->entity->getTeam() == TeamLabel::BLUE) - std::cout << intersectionsX << std::endl; - if (intersectionsX.test((size_t) direction::LEFT) && positionChange.x < 0) positionChange.x = 0; From 28af8c9389e2bfe696dd40bb4f68ff60e4be6619 Mon Sep 17 00:00:00 2001 From: Benedikt Galbavy Date: Sun, 28 Jan 2024 18:18:27 +0100 Subject: [PATCH 6/9] minor improvements --- include/CollisionHandler.h | 8 ++++++-- include/Entity.h | 1 + include/Manager.h | 2 ++ src/CollisionHandler.cpp | 11 +++++++++-- src/Entity.cpp | 5 +++++ src/Manager.cpp | 11 +++++++++++ src/TransformComponent.cpp | 20 +++++--------------- 7 files changed, 39 insertions(+), 19 deletions(-) diff --git a/include/CollisionHandler.h b/include/CollisionHandler.h index cf90905..b786c12 100644 --- a/include/CollisionHandler.h +++ b/include/CollisionHandler.h @@ -1,13 +1,17 @@ #pragma once +#include "Constants.h" #include "Entity.h" #include "SDL_rect.h" #include "SpriteComponent.h" #include "Vector2D.h" +#include "Manager.h" + +#include +#include #include class ColliderComponent; -class Manager; class Entity; constexpr uint8_t DIRECTION_C = 4; @@ -39,7 +43,7 @@ public: static IntersectionBitSet getIntersectionWithBounds(Entity* entity);// will fail if speed high enough to switch from no collision to full overlap in one tick static IntersectionBitSet getIntersectionWithBounds(Entity* entity, Vector2D posMod); - std::vector getColliders(GroupLabel groupLabel); // temporary function, remove once game.cpp cleaned up + std::vector getColliders(std::initializer_list const& groupLabels); // temporary function, remove once game.cpp cleaned up void update(); }; \ No newline at end of file diff --git a/include/Entity.h b/include/Entity.h index 630e2fe..afb871a 100644 --- a/include/Entity.h +++ b/include/Entity.h @@ -47,6 +47,7 @@ public: bool hasGroup(Group mGroup); void addGroup(Group mGroup); void delGroup(Group mGroup); + std::bitset getGroupBitSet(); void setTeam(TeamLabel teamLabel); TeamLabel getTeam(); diff --git a/include/Manager.h b/include/Manager.h index 0046c33..61a1c45 100644 --- a/include/Manager.h +++ b/include/Manager.h @@ -20,6 +20,8 @@ public: void addToTeam(Entity* mEntity, Team mTeam); std::vector& getTeam(Team mTeam); + std::vector getAll(); + Entity& addEntity(); private: diff --git a/src/CollisionHandler.cpp b/src/CollisionHandler.cpp index f8975d1..98173e2 100644 --- a/src/CollisionHandler.cpp +++ b/src/CollisionHandler.cpp @@ -94,11 +94,18 @@ IntersectionBitSet CollisionHandler::getIntersectionWithBounds(Entity* entity, V return intersections; } -std::vector CollisionHandler::getColliders(GroupLabel groupLabel) +std::vector CollisionHandler::getColliders(std::initializer_list const& groupLabels) { std::vector colliders; - for (auto& entity : manager.getGroup((size_t) groupLabel)) { + std::bitset groupBitSet; + for (auto& groupLabel : groupLabels) { + groupBitSet.set((size_t) groupLabel); + } + + for (auto& entity : manager.getAll()) { + if ((groupBitSet & entity->getGroupBitSet()).none()) + continue; if (!entity->hasComponent()) continue; colliders.emplace_back(&entity->getComponent()); diff --git a/src/Entity.cpp b/src/Entity.cpp index 6817071..bd9b79a 100644 --- a/src/Entity.cpp +++ b/src/Entity.cpp @@ -30,6 +30,11 @@ void Entity::delGroup(Group mGroup) groupBitSet[mGroup] = false; } +std::bitset Entity::getGroupBitSet() +{ + return groupBitSet; +} + void Entity::setTeam(TeamLabel teamLabel) { teamLabel = teamLabel; diff --git a/src/Manager.cpp b/src/Manager.cpp index 3759704..f3aa133 100644 --- a/src/Manager.cpp +++ b/src/Manager.cpp @@ -1,8 +1,10 @@ #include "Manager.h" #include +#include #include "Constants.h" +#include "Entity.h" void Manager::draw() { @@ -66,6 +68,15 @@ std::vector& Manager::getTeam(Team mTeam) return entitiesByTeam[mTeam]; } +std::vector Manager::getAll() +{ + std::vector entity_vec; + for (auto& entity_ptr : entities) { + entity_vec.emplace_back(entity_ptr.get()); + } + return entity_vec; +} + Entity& Manager::addEntity() { Entity* e = new Entity(*this); diff --git a/src/TransformComponent.cpp b/src/TransformComponent.cpp index 12609dd..b14ecb9 100644 --- a/src/TransformComponent.cpp +++ b/src/TransformComponent.cpp @@ -60,31 +60,21 @@ void TransformComponent::update() if (this->entity->hasGroup((size_t) GroupLabel::PLAYERS)) { IntersectionBitSet intersectionsX = CollisionHandler::getIntersectionWithBounds(entity, positionChange); - for (auto& collider : Game::collisionHandler->getColliders(GroupLabel::MAPTILES)) { - intersectionsX |= CollisionHandler::getIntersection(entity, collider->entity, Vector2D(positionChange.x, 0), Vector2D(0, 0)); - } - for (auto& collider : Game::collisionHandler->getColliders(GroupLabel::COLLIDERS)) { + for (auto& collider : Game::collisionHandler->getColliders({GroupLabel::MAPTILES, GroupLabel::COLLIDERS})) { intersectionsX |= CollisionHandler::getIntersection(entity, collider->entity, Vector2D(positionChange.x, 0), Vector2D(0, 0)); } IntersectionBitSet intersectionsY = CollisionHandler::getIntersectionWithBounds(entity, positionChange); - for (auto& collider : Game::collisionHandler->getColliders(GroupLabel::MAPTILES)) { - intersectionsY |= CollisionHandler::getIntersection(entity, collider->entity, Vector2D(0, positionChange.y), Vector2D(0, 0)); - } - for (auto& collider : Game::collisionHandler->getColliders(GroupLabel::COLLIDERS)) { + for (auto& collider : Game::collisionHandler->getColliders({GroupLabel::MAPTILES, GroupLabel::COLLIDERS})) { intersectionsY |= CollisionHandler::getIntersection(entity, collider->entity, Vector2D(0, positionChange.y), Vector2D(0, 0)); } - if (intersectionsX.test((size_t) direction::LEFT) && positionChange.x < 0) - positionChange.x = 0; + IntersectionBitSet intersections = (intersectionsX & IntersectionBitSet("0011")) | (intersectionsY & IntersectionBitSet("1100")); - if (intersectionsX.test((size_t) direction::RIGHT) && positionChange.x > 0) + if (intersections.test((size_t) direction::LEFT) || intersections.test((size_t) direction::RIGHT)) positionChange.x = 0; - if (intersectionsY.test((size_t) direction::UP) && positionChange.y < 0) - positionChange.y = 0; - - if (intersectionsY.test((size_t) direction::DOWN) && positionChange.y > 0) + if (intersections.test((size_t) direction::UP) || intersections.test((size_t) direction::DOWN)) positionChange.y = 0; } From 973114f351bb8b57f4be291b8baad08552e18642 Mon Sep 17 00:00:00 2001 From: Benedikt Galbavy Date: Mon, 29 Jan 2024 23:49:09 +0100 Subject: [PATCH 7/9] Added debug type to CMake --- CMakeLists.txt | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 7e5c48a..9ce25dd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -26,4 +26,10 @@ target_link_libraries(${PROJECT_NAME} PRIVATE SDL2_image::SDL2_image-static ) +if(CMAKE_BUILD_TYPE MATCHES "Debug") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -fsanitize=address -fno-omit-frame-pointer") + target_link_libraries(${PROJECT_NAME} PRIVATE "-fsanitize=address") +endif() + + file(COPY ${PROJECT_SOURCE_DIR}/assets DESTINATION ${PROJECT_BINARY_DIR}) \ No newline at end of file From fa4586f1dbeed8f73bf16a401e697f613ea03916 Mon Sep 17 00:00:00 2001 From: Benedikt Galbavy Date: Mon, 29 Jan 2024 23:49:58 +0100 Subject: [PATCH 8/9] Rewrote collision detection --- include/AssetManager.h | 4 +- include/CollisionHandler.h | 28 ++++++++++--- include/Defines.h | 3 +- include/Direction.h | 7 ++++ include/Entity.h | 8 +++- include/Game.h | 12 +++--- include/HealthComponent.h | 10 ++--- include/ProjectileComponent.h | 4 +- include/SpriteComponent.h | 9 +---- include/TransformComponent.h | 2 +- include/Vector2D.h | 7 +++- src/AssetManager.cpp | 4 +- src/ColliderComponent.cpp | 1 + src/CollisionHandler.cpp | 59 +++++++++++++++++++++------ src/Entity.cpp | 2 +- src/Game.cpp | 76 ++++++++++------------------------- src/HealthComponent.cpp | 32 +++++++++++---- src/KeyboardController.cpp | 24 +++++------ src/Manager.cpp | 8 ++-- src/ProjectileComponent.cpp | 33 ++++++++++++--- src/SpriteComponent.cpp | 5 ++- src/TransformComponent.cpp | 28 ++++++------- src/Vector2D.cpp | 9 +++++ 23 files changed, 229 insertions(+), 146 deletions(-) create mode 100644 include/Direction.h diff --git a/include/AssetManager.h b/include/AssetManager.h index 61b94d2..50ecabe 100644 --- a/include/AssetManager.h +++ b/include/AssetManager.h @@ -1,3 +1,5 @@ +#include "Entity.h" + #include #include #include @@ -12,7 +14,7 @@ public: AssetManager(Manager* manager); ~AssetManager(); - void createProjectile(Vector2D pos, Vector2D velocity, bool source, int scale, int range, int speed, const char* texturePath); + void createProjectile(Vector2D pos, Vector2D velocity, bool source, int scale, int range, int speed, const char* texturePath, TeamLabel teamLabel); //texture management void addTexture(std::string id, const char* path); diff --git a/include/CollisionHandler.h b/include/CollisionHandler.h index b786c12..8cdaad5 100644 --- a/include/CollisionHandler.h +++ b/include/CollisionHandler.h @@ -1,5 +1,6 @@ #pragma once +#include "ColliderComponent.h" #include "Constants.h" #include "Entity.h" #include "SDL_rect.h" @@ -9,6 +10,8 @@ #include #include +#include +#include #include class ColliderComponent; @@ -37,13 +40,28 @@ public: manager(mManager) { }; ~CollisionHandler(); + static IntersectionBitSet getIntersection( // intersections relative to entityA + Entity* entityA, + Entity* entityB, + Vector2D posModA = Vector2D(0,0), + Vector2D posModB = Vector2D(0,0)); + static IntersectionBitSet getIntersectionWithBounds( // will fail to determine direction if speed high enough to switch from no collision to full overlap in one tick + Entity* entity, + Vector2D posMod = Vector2D(0,0)); - static IntersectionBitSet getIntersection(Entity* entityA, Entity* entityB); // intersections relative to entityA - static IntersectionBitSet getIntersection(Entity* entityA, Entity* entityB, Vector2D posModA, Vector2D posModB); - static IntersectionBitSet getIntersectionWithBounds(Entity* entity);// will fail if speed high enough to switch from no collision to full overlap in one tick - static IntersectionBitSet getIntersectionWithBounds(Entity* entity, Vector2D posMod); + // temporary function, remove once game.cpp cleaned up + std::vector getColliders( + std::initializer_list const& groupLabels, + std::initializer_list const& teamLabels = {}, + bool negateTeam = false); - std::vector getColliders(std::initializer_list const& groupLabels); // temporary function, remove once game.cpp cleaned up + template + T getAnyIntersection( + Entity* entity, + Vector2D posMod = {}, + std::initializer_list const& groupLabels = {}, + std::initializer_list const& teamLabels = {}, + bool negateTeam = false); void update(); }; \ No newline at end of file diff --git a/include/Defines.h b/include/Defines.h index 3f59c93..7b9637e 100644 --- a/include/Defines.h +++ b/include/Defines.h @@ -1,2 +1 @@ -#pragma once - +#pragma once \ No newline at end of file diff --git a/include/Direction.h b/include/Direction.h new file mode 100644 index 0000000..a971704 --- /dev/null +++ b/include/Direction.h @@ -0,0 +1,7 @@ +#pragma once + +enum class Direction +{ + LEFT, + RIGHT +}; \ No newline at end of file diff --git a/include/Entity.h b/include/Entity.h index afb871a..9d768b5 100644 --- a/include/Entity.h +++ b/include/Entity.h @@ -5,6 +5,7 @@ #include #include +#include "ColliderComponent.h" #include "ECS.h" #include "Constants.h" @@ -42,7 +43,12 @@ public: void draw() const; bool isActive() const { return this->active; } - void destroy() { this->active = false; } + void destroy() { + this->active = false; + if (this->hasComponent()) { + this->getComponent().removeCollision(); + } + } bool hasGroup(Group mGroup); void addGroup(Group mGroup); diff --git a/include/Game.h b/include/Game.h index 64fa511..86a38c8 100644 --- a/include/Game.h +++ b/include/Game.h @@ -6,6 +6,7 @@ class AssetManager; class CollisionHandler; +enum class TeamLabel; class Game { @@ -21,19 +22,18 @@ public: void clean(); bool running() const; - static void addTile(int id, int x, int y); + static void addTile(unsigned long id, int x, int y); static SDL_Renderer* renderer; static SDL_Event event; static CollisionHandler* collisionHandler; static AssetManager* assets; - - bool getWinner(); private: + void setWinner(TeamLabel winningTeam); + TeamLabel getWinner(); + int counter = 0; bool isRunning = false; SDL_Window* window; - - //true for player1 win / false for player2 win; - bool winner; + TeamLabel winner; }; diff --git a/include/HealthComponent.h b/include/HealthComponent.h index 7cfd2e7..6daf17b 100644 --- a/include/HealthComponent.h +++ b/include/HealthComponent.h @@ -1,5 +1,6 @@ #pragma once +#include "Direction.h" #include "Component.h" class Manager; @@ -8,22 +9,21 @@ class HealthComponent : public Component { public: - HealthComponent(int health, Manager* manager, bool player) : health(health), manager(manager), player(player) {} + HealthComponent(int health, Direction side) : health(health), side(side) {} ~HealthComponent() {} - void getDamage() { this->health--; } + void modifyHealth(int health = -1); int getHealth() { return this->health; } void init() override; - void createAllHearts(); + void resetHearts(); void createHeartComponents(int x); - private: int health; - Manager* manager; + Direction side; bool player; //true if player1 / false if player2 }; \ No newline at end of file diff --git a/include/ProjectileComponent.h b/include/ProjectileComponent.h index 73088c1..8fa949c 100644 --- a/include/ProjectileComponent.h +++ b/include/ProjectileComponent.h @@ -10,7 +10,7 @@ class ProjectileComponent : public Component //can maybe be split in separate .cpp file public: - ProjectileComponent(int range, int speed, Vector2D velocity, bool source) : range(range), speed(speed), velocity(velocity), source(source) {} + ProjectileComponent(int range, int speed, Vector2D direction, bool source) : range(range), speed(speed), direction(direction), source(source) {} ~ProjectileComponent() {} void init() override; @@ -27,5 +27,5 @@ private: const bool source; //true if from player1 / false if from player2 - Vector2D velocity; + Vector2D direction; }; \ No newline at end of file diff --git a/include/SpriteComponent.h b/include/SpriteComponent.h index 109d824..a11865f 100644 --- a/include/SpriteComponent.h +++ b/include/SpriteComponent.h @@ -5,15 +5,10 @@ #include "AnimationHandler.h" #include "Component.h" +#include "Direction.h" class TransformComponent; -enum SpriteDirection -{ - LEFT, - RIGHT -}; - class SpriteComponent : public Component { public: @@ -43,5 +38,5 @@ public: void update() override; void draw() override; void playAnimation(AnimationType type); - void setDirection(SpriteDirection direction); + void setDirection(Direction direction); }; diff --git a/include/TransformComponent.h b/include/TransformComponent.h index 04b193f..deed7f9 100644 --- a/include/TransformComponent.h +++ b/include/TransformComponent.h @@ -7,7 +7,7 @@ class TransformComponent : public Component { public: Vector2D position; - Vector2D velocity; + Vector2D direction; int height = 32; int width = 32; diff --git a/include/Vector2D.h b/include/Vector2D.h index 11704a7..bc2d652 100644 --- a/include/Vector2D.h +++ b/include/Vector2D.h @@ -1,5 +1,8 @@ #pragma once +#include +#include + class Vector2D { public: @@ -17,4 +20,6 @@ public: Vector2D& operator*(const int& i); Vector2D& zero(); -}; \ No newline at end of file +}; + +SDL_Rect operator+(const SDL_Rect& rect, const Vector2D& vector2D); \ No newline at end of file diff --git a/src/AssetManager.cpp b/src/AssetManager.cpp index 766fe27..d30e7de 100644 --- a/src/AssetManager.cpp +++ b/src/AssetManager.cpp @@ -1,5 +1,6 @@ #include "AssetManager.h" +#include "Entity.h" #include "TextureManager.h" #include "Components.h" @@ -15,7 +16,7 @@ SDL_Texture* AssetManager::getTexture(std::string id) { return textures.at(id); } -void AssetManager::createProjectile(Vector2D pos, Vector2D velocity, bool source, int scale, int range, int speed, const char* texturePath) { +void AssetManager::createProjectile(Vector2D pos, Vector2D velocity, bool source, int scale, int range, int speed, const char* texturePath, TeamLabel teamLabel) { auto& projectile(man->addEntity()); projectile.addComponent(pos.x, pos.y, 32, 32, scale); //32x32 is standard size for objects @@ -23,4 +24,5 @@ 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); + projectile.setTeam(teamLabel); } \ No newline at end of file diff --git a/src/ColliderComponent.cpp b/src/ColliderComponent.cpp index d5ea1e5..a186d9d 100644 --- a/src/ColliderComponent.cpp +++ b/src/ColliderComponent.cpp @@ -28,6 +28,7 @@ void ColliderComponent::init() transform = &entity->getComponent(); //Game::collisionHandler->add(this); + this->update(); } void ColliderComponent::update() diff --git a/src/CollisionHandler.cpp b/src/CollisionHandler.cpp index 98173e2..fc9ac42 100644 --- a/src/CollisionHandler.cpp +++ b/src/CollisionHandler.cpp @@ -11,11 +11,6 @@ #include #include -IntersectionBitSet CollisionHandler::getIntersection(Entity* entityA, Entity* entityB) -{ - return getIntersection(entityA, entityB, Vector2D(0,0), Vector2D(0,0)); -} - IntersectionBitSet CollisionHandler::getIntersection(Entity* entityA, Entity* entityB, Vector2D posModA, Vector2D posModB) { if (!entityA->hasComponent() || @@ -60,11 +55,6 @@ IntersectionBitSet CollisionHandler::getIntersection(Entity* entityA, Entity* en return intersections; } -IntersectionBitSet CollisionHandler::getIntersectionWithBounds(Entity* entity) -{ - return getIntersectionWithBounds(entity, Vector2D(0,0)); -} - IntersectionBitSet CollisionHandler::getIntersectionWithBounds(Entity* entity, Vector2D posMod) { if (!entity->hasComponent()) @@ -76,8 +66,9 @@ IntersectionBitSet CollisionHandler::getIntersectionWithBounds(Entity* entity, V // all 4 directions and both sides to allow checking for fully out of bounds if (collider->x + posMod.x < 0 || - collider->x + posMod.x > SCREEN_SIZE_WIDTH) + collider->x + posMod.x > SCREEN_SIZE_WIDTH) { intersections.set((size_t) direction::LEFT); + } if (collider->x + collider->w + posMod.x < 0 || collider->x + collider->w + posMod.x > SCREEN_SIZE_WIDTH) @@ -94,7 +85,10 @@ IntersectionBitSet CollisionHandler::getIntersectionWithBounds(Entity* entity, V return intersections; } -std::vector CollisionHandler::getColliders(std::initializer_list const& groupLabels) +std::vector CollisionHandler::getColliders( + std::initializer_list const& groupLabels, + std::initializer_list const& teamLabels, + bool negateTeam) { std::vector colliders; @@ -103,13 +97,52 @@ std::vector CollisionHandler::getColliders(std::initializer_ groupBitSet.set((size_t) groupLabel); } + std::bitset teamBitSet; + for (auto& teamLabel : teamLabels) { + teamBitSet.set((size_t) teamLabel); + } + for (auto& entity : manager.getAll()) { if ((groupBitSet & entity->getGroupBitSet()).none()) continue; + if (teamBitSet.any() && negateTeam != (teamBitSet.test((size_t) entity->getTeam()))) + continue; if (!entity->hasComponent()) continue; colliders.emplace_back(&entity->getComponent()); } return colliders; -} \ No newline at end of file +} + +template<> +IntersectionBitSet CollisionHandler::getAnyIntersection( + Entity* entity, + Vector2D posMod, + std::initializer_list const& groupLabels, + std::initializer_list const& teamLabels, + bool negateTeam) +{ + IntersectionBitSet intersections; + for (auto& collider : getColliders(groupLabels, teamLabels)) { + intersections |= getIntersection(entity, collider->entity, posMod); + } + return intersections; +}; + +template<> +Entity* CollisionHandler::getAnyIntersection( + Entity* entity, + Vector2D posMod, + std::initializer_list const& groupLabels, + std::initializer_list const& teamLabels, + bool negateTeam) +{ + for (auto& collider : getColliders(groupLabels, teamLabels)) { + SDL_Rect rect = entity->getComponent().collider + posMod; + if (SDL_HasIntersection(&rect, &collider->collider)) { + return collider->entity; + } + } + return nullptr; +}; \ No newline at end of file diff --git a/src/Entity.cpp b/src/Entity.cpp index bd9b79a..0f523ea 100644 --- a/src/Entity.cpp +++ b/src/Entity.cpp @@ -37,7 +37,7 @@ std::bitset Entity::getGroupBitSet() void Entity::setTeam(TeamLabel teamLabel) { - teamLabel = teamLabel; + this->teamLabel = teamLabel; manager.addToTeam(this, (size_t) teamLabel); } diff --git a/src/Game.cpp b/src/Game.cpp index 0874e1a..3fc3498 100644 --- a/src/Game.cpp +++ b/src/Game.cpp @@ -4,7 +4,9 @@ #include "Components.h" #include "AssetManager.h" +#include "Direction.h" #include "Entity.h" +#include "HealthComponent.h" #include "Map.h" #include "TextureManager.h" @@ -113,27 +115,28 @@ void Game::init(const char* title, int xpos, int ypos, int width, int height, bo //ecs implementation + + player1.setTeam(TeamLabel::BLUE); player1.addComponent(80,80,2); //posx, posy, scale player1.addComponent("assets/chicken_knight_spritesheet.png", true); //adds sprite (32x32px), path needed player1.addComponent(SDL_SCANCODE_W, SDL_SCANCODE_S, SDL_SCANCODE_A, SDL_SCANCODE_D, SDL_SCANCODE_E, Vector2D(1, 0));//custom keycontrols can be added player1.addComponent("player", 0.8f); //adds tag (for further use, reference tag) - player1.addComponent(5, &manager, true); + player1.addComponent(5, Direction::LEFT); player1.addGroup((size_t) GroupLabel::PLAYERS); //tell programm what group it belongs to for rendering order - player1.setTeam(TeamLabel::BLUE); + + player2.setTeam(TeamLabel::RED); player2.addComponent(600, 500, 2); player2.addComponent("assets/chicken_spritesheet.png", true); player2.addComponent(SDL_SCANCODE_UP, SDL_SCANCODE_DOWN, SDL_SCANCODE_LEFT, SDL_SCANCODE_RIGHT, SDL_SCANCODE_RCTRL, Vector2D(-1, 0)); player2.addComponent("enemy", 0.8f); - player2.addComponent(5, &manager, false); + player2.addComponent(5, Direction::RIGHT); player2.addGroup((size_t) GroupLabel::PLAYERS); - player2.setTeam(TeamLabel::RED); } auto& tiles(manager.getGroup((size_t)GroupLabel::MAPTILES)); auto& players(manager.getGroup((size_t)GroupLabel::PLAYERS)); -auto& enemies(manager.getGroup((size_t)GroupLabel::PLAYERS)); auto& projectiles(manager.getGroup((size_t)GroupLabel::PROJECTILE)); auto& hearts(manager.getGroup((size_t)GroupLabel::HEARTS)); @@ -159,50 +162,10 @@ void Game::update() manager.refresh(); manager.update(); - //checking if projectiles hit player1 or player2 - for (auto& p : projectiles) { - if(SDL_HasIntersection(&player2.getComponent().collider, &p->getComponent().collider) - && (p->getComponent().hasCollision) && !p->getComponent().getSource()) { - //std::cout << "Enemy hit!"; - p->getComponent().removeCollision(); - p->destroy(); - - player2.getComponent().getDamage(); - - //display updated health | pretty scuffed but works ig - for(auto h : hearts) - h->destroy(); - - player1.getComponent().createAllHearts(); - player2.getComponent().createAllHearts(); - - if(player2.getComponent().getHealth() < 1) { - std::cout << "Player1 wins!" << std::endl; - winner = true; - isRunning = false; - } - } - - if(SDL_HasIntersection(&player1.getComponent().collider, &p->getComponent().collider) - && (p->getComponent().hasCollision) && p->getComponent().getSource()) { - //std::cout << "Player hit!"; - p->getComponent().removeCollision(); - p->destroy(); - - player1.getComponent().getDamage(); - - //display updated health - for(auto h : hearts) - h->destroy(); - - player1.getComponent().createAllHearts(); - player2.getComponent().createAllHearts(); - - if(player1.getComponent().getHealth() < 1) { - std::cout << "Player2 wins!" << std::endl; - winner = false; - isRunning = false; - } + // needs to be in game.cpp to have access to internal functions + for (auto& player : manager.getGroup((size_t) GroupLabel::PLAYERS)) { + if (player->getComponent().getHealth() <= 0) { + this->setWinner(player->getTeam()); } } } @@ -218,10 +181,6 @@ void Game::render() { p->draw(); } - for (auto& e : enemies) - { - e->draw(); - } for (auto& p : projectiles) p->draw(); @@ -239,7 +198,7 @@ void Game::clean() std::cout << "Game Cleaned!" << std::endl; } -void Game::addTile(int id, int x, int y) +void Game::addTile(unsigned long id, int x, int y) { auto& tile(manager.addEntity()); tile.addComponent(x, y, TILE_SIZE, TILE_SIZE, id); @@ -252,6 +211,13 @@ bool Game::running() const return isRunning; } -bool Game::getWinner() { +void Game::setWinner(TeamLabel winningTeam) +{ + this->winner = winningTeam; + this->isRunning = false; +} + +TeamLabel Game::getWinner() +{ return this->winner; } \ No newline at end of file diff --git a/src/HealthComponent.cpp b/src/HealthComponent.cpp index 67b2070..4f9132b 100644 --- a/src/HealthComponent.cpp +++ b/src/HealthComponent.cpp @@ -1,26 +1,43 @@ #include "HealthComponent.h" #include "Components.h" +#include "Direction.h" +#include "Entity.h" +#include "Game.h" +#include void HealthComponent::init() { - createAllHearts(); + resetHearts(); } -void HealthComponent::createAllHearts() +void HealthComponent::modifyHealth(int health) { + this->health += health; + this->resetHearts(); +} + +void HealthComponent::resetHearts() +{ + // clear hearts if exist + for (auto& heart : this->entity->getManager().getGroup((size_t) GroupLabel::HEARTS)) { + if (heart->getTeam() == this->entity->getTeam()) { + heart->destroy(); + } + } + int x; //starting position for first health icon - if(player) { + if(side == Direction::LEFT) { x = 10; } else { x = 730; } for(int i = 0; i < health; i++) { - + //checks for player side - if(player) { + if (side == Direction::LEFT) { createHeartComponents(x); x += 50; continue; @@ -31,10 +48,11 @@ void HealthComponent::createAllHearts() } } - void HealthComponent::createHeartComponents(int x) +void HealthComponent::createHeartComponents(int x) { - auto& heart(manager->addEntity()); + auto& heart(this->entity->getManager().addEntity()); heart.addComponent(x,5,2); heart.addComponent("assets/heart.png"); heart.addGroup((size_t)GroupLabel::HEARTS); + heart.setTeam(this->entity->getTeam()); } \ No newline at end of file diff --git a/src/KeyboardController.cpp b/src/KeyboardController.cpp index 2880fe0..507a44e 100644 --- a/src/KeyboardController.cpp +++ b/src/KeyboardController.cpp @@ -23,27 +23,27 @@ void KeyboardController::init() void KeyboardController::update() { - transform->velocity.x = 0; - transform->velocity.y = 0; + transform->direction.x = 0; + transform->direction.y = 0; sprite->playAnimation(IDLE); if (keystates[this->up]) { - transform->velocity.y = -1; + transform->direction.y = -1; sprite->playAnimation(WALK); } if (keystates[this->left]) { - transform->velocity.x = -1; + transform->direction.x = -1; sprite->playAnimation(WALK); - sprite->setDirection(LEFT); + sprite->setDirection(Direction::LEFT); } if (keystates[this->down]) { - transform->velocity.y = 1; + transform->direction.y = 1; sprite->playAnimation(WALK); } if (keystates[this->right]) { - transform->velocity.x = 1; + transform->direction.x = 1; sprite->playAnimation(WALK); - sprite->setDirection(RIGHT); + sprite->setDirection(Direction::RIGHT); } if (keystates[this->fire]) { @@ -57,14 +57,14 @@ void KeyboardController::update() //checks player source via the firing velocity //TODO: adding actual projectile textures if (fireVelocity.x > 0) { - sprite->setDirection(RIGHT); + sprite->setDirection(Direction::RIGHT); Game::assets->createProjectile(Vector2D(player->position.x, player->position.y), fireVelocity, - false, 1, 180, 1, "assets/egg.png"); + false, 1, 180, 1, "assets/egg.png", this->entity->getTeam()); } else { - sprite->setDirection(LEFT); + sprite->setDirection(Direction::LEFT); Game::assets->createProjectile(Vector2D(player->position.x, player->position.y), fireVelocity, - true, 1, 180, 1, "assets/egg.png"); + true, 1, 180, 1, "assets/egg.png", this->entity->getTeam()); } lastFireTime = currentTicks; diff --git a/src/Manager.cpp b/src/Manager.cpp index f3aa133..f98ebdc 100644 --- a/src/Manager.cpp +++ b/src/Manager.cpp @@ -50,22 +50,22 @@ void Manager::update() void Manager::addToGroup(Entity* mEntity, Group mGroup) { - entitiesByGroup[mGroup].emplace_back(mEntity); + entitiesByGroup.at(mGroup).emplace_back(mEntity); } std::vector& Manager::getGroup(Group mGroup) { - return entitiesByGroup[mGroup]; + return entitiesByGroup.at(mGroup); } void Manager::addToTeam(Entity* mEntity, Team mTeam) { - entitiesByTeam[mTeam].emplace_back(mEntity); + entitiesByTeam.at(mTeam).emplace_back(mEntity); // } std::vector& Manager::getTeam(Team mTeam) { - return entitiesByTeam[mTeam]; + return entitiesByTeam.at(mTeam); } std::vector Manager::getAll() diff --git a/src/ProjectileComponent.cpp b/src/ProjectileComponent.cpp index ad50e39..de90155 100644 --- a/src/ProjectileComponent.cpp +++ b/src/ProjectileComponent.cpp @@ -1,21 +1,44 @@ #include "ProjectileComponent.h" +#include "CollisionHandler.h" #include "Components.h" +#include "Entity.h" +#include "Game.h" +#include "HealthComponent.h" +#include "Vector2D.h" +#include +#include void ProjectileComponent::init() { transformComponent = &entity->getComponent(); + transformComponent->direction = direction; } void ProjectileComponent::update() { - transformComponent->velocity = velocity; - distance += speed; + IntersectionBitSet boundsIntersection = Game::collisionHandler->getIntersectionWithBounds(entity); + + if ((boundsIntersection | IntersectionBitSet("1100")).all() || (boundsIntersection | IntersectionBitSet("0011")).all()) { + this->entity->destroy(); + std::cout << "out of bounds" << std::endl; + } + if (distance > range) { - entity->destroy(); - entity->getComponent().removeCollision(); - //std::cout << "out of range" << std::endl; + this->entity->destroy(); + std::cout << "out of range" << std::endl; + } + + Entity* player; + if ((player = Game::collisionHandler->getAnyIntersection( + entity, + Vector2D(0,0), + {GroupLabel::PLAYERS}, + {entity->getTeam()}, + true)) != nullptr) { + player->getComponent().modifyHealth(); + this->entity->destroy(); } } \ No newline at end of file diff --git a/src/SpriteComponent.cpp b/src/SpriteComponent.cpp index 6af7d6b..58e2295 100644 --- a/src/SpriteComponent.cpp +++ b/src/SpriteComponent.cpp @@ -2,6 +2,7 @@ #include +#include "Direction.h" #include "TextureManager.h" #include "Entity.h" #include "TransformComponent.h" @@ -71,9 +72,9 @@ void SpriteComponent::playAnimation(AnimationType type) this->speed = animations.at(type)->speed; } -void SpriteComponent::setDirection(SpriteDirection direction) +void SpriteComponent::setDirection(Direction direction) { - if (direction == RIGHT) { + if (direction == Direction::RIGHT) { this->flipped = true; return; } diff --git a/src/TransformComponent.cpp b/src/TransformComponent.cpp index b14ecb9..f6ab5d1 100644 --- a/src/TransformComponent.cpp +++ b/src/TransformComponent.cpp @@ -6,6 +6,8 @@ #include "Entity.h" #include "Game.h" #include "Vector2D.h" +#include +#include #include TransformComponent::TransformComponent() @@ -43,33 +45,29 @@ TransformComponent::TransformComponent(float x, float y, int w, int h, int scale void TransformComponent::init() { - velocity.zero(); + direction.zero(); } void TransformComponent::update() { // if(velocity.x != 0 && velocity.y != 0) - float multiplier = velocity.x != 0 && velocity.y != 0 ? 0.707 : 1; //normalizes vector + float multiplier = direction.x != 0 && direction.y != 0 ? 0.707 : 1; // normalizes vector; only works if directions are in increments of 45° Vector2D positionChange( - velocity.x * speed * multiplier, - velocity.y * speed * multiplier + direction.x * speed * multiplier, + direction.y * speed * multiplier ); // TODO: move to separate functions if (this->entity->hasGroup((size_t) GroupLabel::PLAYERS)) { - IntersectionBitSet intersectionsX = CollisionHandler::getIntersectionWithBounds(entity, positionChange); - for (auto& collider : Game::collisionHandler->getColliders({GroupLabel::MAPTILES, GroupLabel::COLLIDERS})) { - intersectionsX |= CollisionHandler::getIntersection(entity, collider->entity, Vector2D(positionChange.x, 0), Vector2D(0, 0)); - } - - IntersectionBitSet intersectionsY = CollisionHandler::getIntersectionWithBounds(entity, positionChange); - for (auto& collider : Game::collisionHandler->getColliders({GroupLabel::MAPTILES, GroupLabel::COLLIDERS})) { - intersectionsY |= CollisionHandler::getIntersection(entity, collider->entity, Vector2D(0, positionChange.y), Vector2D(0, 0)); - } - - IntersectionBitSet intersections = (intersectionsX & IntersectionBitSet("0011")) | (intersectionsY & IntersectionBitSet("1100")); + IntersectionBitSet intersections = + (CollisionHandler::getIntersectionWithBounds(entity, Vector2D(positionChange.x, 0)) | + (Game::collisionHandler->getAnyIntersection(entity, Vector2D(positionChange.x, 0), {GroupLabel::MAPTILES, GroupLabel::COLLIDERS})) & + IntersectionBitSet("0011")) | + (CollisionHandler::getIntersectionWithBounds(entity, Vector2D(0, positionChange.y)) | + (Game::collisionHandler->getAnyIntersection(entity, Vector2D(0, positionChange.y), {GroupLabel::MAPTILES, GroupLabel::COLLIDERS})) & + IntersectionBitSet("1100")); if (intersections.test((size_t) direction::LEFT) || intersections.test((size_t) direction::RIGHT)) positionChange.x = 0; diff --git a/src/Vector2D.cpp b/src/Vector2D.cpp index 71d2848..6368ff3 100644 --- a/src/Vector2D.cpp +++ b/src/Vector2D.cpp @@ -1,4 +1,5 @@ #include "Vector2D.h" +#include "SDL_rect.h" Vector2D::Vector2D() { @@ -56,3 +57,11 @@ Vector2D& Vector2D::zero() return *this; } + +SDL_Rect operator+(const SDL_Rect& rect, const Vector2D& vector2D) +{ + SDL_Rect newRect = rect; + newRect.x += vector2D.x; + newRect.y += vector2D.y; + return newRect; +} \ No newline at end of file From e979b4aa884471f8a624b461e26e6d8a98187007 Mon Sep 17 00:00:00 2001 From: Benedikt Galbavy Date: Tue, 30 Jan 2024 03:27:11 +0100 Subject: [PATCH 9/9] Cleanup of legacy code booleans --- include/AssetManager.h | 2 +- include/HealthComponent.h | 4 +--- include/ProjectileComponent.h | 6 +----- src/AssetManager.cpp | 4 ++-- src/HealthComponent.cpp | 6 +++--- src/KeyboardController.cpp | 4 ++-- 6 files changed, 10 insertions(+), 16 deletions(-) diff --git a/include/AssetManager.h b/include/AssetManager.h index 37eb645..d87dc63 100644 --- a/include/AssetManager.h +++ b/include/AssetManager.h @@ -15,7 +15,7 @@ public: AssetManager(Manager* manager); ~AssetManager(); - void createProjectile(Vector2D pos, Vector2D velocity, bool source, 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, TeamLabel teamLabel); //texture management void addTexture(std::string id, const char* path); diff --git a/include/HealthComponent.h b/include/HealthComponent.h index 6daf17b..78b9451 100644 --- a/include/HealthComponent.h +++ b/include/HealthComponent.h @@ -17,13 +17,11 @@ public: void init() override; - void resetHearts(); + void refreshHearts(); void createHeartComponents(int x); private: int health; Direction side; - bool player; //true if player1 / false if player2 - }; \ No newline at end of file diff --git a/include/ProjectileComponent.h b/include/ProjectileComponent.h index 8fa949c..76a5e7b 100644 --- a/include/ProjectileComponent.h +++ b/include/ProjectileComponent.h @@ -10,14 +10,12 @@ class ProjectileComponent : public Component //can maybe be split in separate .cpp file public: - ProjectileComponent(int range, int speed, Vector2D direction, bool source) : range(range), speed(speed), direction(direction), source(source) {} + ProjectileComponent(int range, int speed, Vector2D direction) : range(range), speed(speed), direction(direction) {} ~ProjectileComponent() {} void init() override; void update() override; - bool getSource() { return this->source; } - private: TransformComponent* transformComponent; @@ -25,7 +23,5 @@ private: int speed = 0; int distance = 0; - const bool source; //true if from player1 / false if from player2 - Vector2D direction; }; \ No newline at end of file diff --git a/src/AssetManager.cpp b/src/AssetManager.cpp index 9c4e909..b6988dd 100644 --- a/src/AssetManager.cpp +++ b/src/AssetManager.cpp @@ -26,12 +26,12 @@ Mix_Chunk* AssetManager::getSound(std::string id) { return soundEffects.at(id); } -void AssetManager::createProjectile(Vector2D pos, Vector2D velocity, bool source, 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, 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, source); + projectile.addComponent(range, speed, velocity); projectile.addComponent("projectile", 0.6f); projectile.addGroup((size_t)GroupLabel::PROJECTILE); projectile.setTeam(teamLabel); diff --git a/src/HealthComponent.cpp b/src/HealthComponent.cpp index 4f9132b..77c9e0e 100644 --- a/src/HealthComponent.cpp +++ b/src/HealthComponent.cpp @@ -8,16 +8,16 @@ void HealthComponent::init() { - resetHearts(); + refreshHearts(); } void HealthComponent::modifyHealth(int health) { this->health += health; - this->resetHearts(); + this->refreshHearts(); } -void HealthComponent::resetHearts() +void HealthComponent::refreshHearts() { // clear hearts if exist for (auto& heart : this->entity->getManager().getGroup((size_t) GroupLabel::HEARTS)) { diff --git a/src/KeyboardController.cpp b/src/KeyboardController.cpp index a892083..c699ae3 100644 --- a/src/KeyboardController.cpp +++ b/src/KeyboardController.cpp @@ -63,12 +63,12 @@ void KeyboardController::update() if (fireVelocity.x > 0) { sprite->setDirection(Direction::RIGHT); Game::assets->createProjectile(Vector2D(player->position.x, player->position.y), fireVelocity, - false, 1, 180, 1, "assets/egg.png", this->entity->getTeam()); + 1, 180, 1, "assets/egg.png", this->entity->getTeam()); } else { sprite->setDirection(Direction::LEFT); Game::assets->createProjectile(Vector2D(player->position.x, player->position.y), fireVelocity, - true, 1, 180, 1, "assets/egg.png", this->entity->getTeam()); + 1, 180, 1, "assets/egg.png", this->entity->getTeam()); } lastFireTime = currentTicks;