From 5dc61e62309d557aa1ff5f7aa6e825e3b8b7252b Mon Sep 17 00:00:00 2001 From: Sara Varga Date: Wed, 1 May 2024 13:07:48 +0200 Subject: [PATCH 1/8] refactor: moved input polling from KeyboardController to input class --- include/Input.h | 101 ++++++++++++++++++++++++++++++ include/KeyboardController.h | 44 ++++++-------- src/Input.cpp | 115 +++++++++++++++++++++++++++++++++++ src/KeyboardController.cpp | 100 +++++++++++++++--------------- 4 files changed, 288 insertions(+), 72 deletions(-) create mode 100644 include/Input.h create mode 100644 src/Input.cpp diff --git a/include/Input.h b/include/Input.h new file mode 100644 index 0000000..3fc6f80 --- /dev/null +++ b/include/Input.h @@ -0,0 +1,101 @@ +#include +#include + +class Input +{ +public: + Input(); + ~Input(); + + void pollEvents(); + bool isKeyDown(Key key); + +private: + const Uint8* m_keyStates; + SDL_Scancode mapKeyToSDL(Key key); + std::map m_keyMappings; + void InitKeyMappings(); +}; + +enum class Key +{ + UP, + DOWN, + LEFT, + RIGHT, + FIRE, + ENTER, + ESCAPE, + TAB, + BACKSPACE, + DELETE, + HOME, + END, + PAGE_UP, + PAGE_DOWN, + INSERT, + CAPS_LOCK, + LEFT_SHIFT, + RIGHT_SHIFT, + LEFT_CTRL, + RIGHT_CTRL, + LEFT_ALT, + RIGHT_ALT, + F1, + F2, + F3, + F4, + F5, + F6, + F7, + F8, + F9, + F10, + F11, + F12, + A, + B, + C, + D, + E, + F, + G, + H, + I, + J, + K, + L, + M, + N, + O, + P, + Q, + R, + S, + T, + U, + V, + W, + X, + Y, + Z, + NUM_0, + NUM_1, + NUM_2, + NUM_3, + NUM_4, + NUM_5, + NUM_6, + NUM_7, + NUM_8, + NUM_9, + LEFT_BRACKET, + RIGHT_BRACKET, + SEMICOLON, + APOSTROPHE, + COMMA, + PERIOD, + SLASH, + BACKSLASH, + GRAVE +}; diff --git a/include/KeyboardController.h b/include/KeyboardController.h index 9079b73..1d1fbd3 100644 --- a/include/KeyboardController.h +++ b/include/KeyboardController.h @@ -3,8 +3,9 @@ #include "Component.h" #include "Vector2D.h" -#include "Constants.h" +#include "Constants.h" // TODO: change so gamespecific constants are in own file #include "SoundManager.h" +#include "Input.h" class TransformComponent; class SpriteComponent; @@ -12,33 +13,28 @@ class SpriteComponent; class KeyboardController : public Component { public: - TransformComponent* transform; - const uint8_t* keystates = SDL_GetKeyboardState(NULL); - SDL_Scancode up; - SDL_Scancode down; - SDL_Scancode left; - SDL_Scancode right; - SDL_Scancode fire; - - SpriteComponent* sprite; - - //for attack cooldown in between shots - uint32_t lastFireTime = 0; - uint32_t fireCooldown = 1000; //in ms can be adjusted to change possible attack-speed - - KeyboardController() = default; - KeyboardController(SDL_Scancode up, SDL_Scancode down, SDL_Scancode left, SDL_Scancode right, SDL_Scancode fire, Vector2D fireVelocity); + KeyboardController(Input* input, Key up, Key down, Key left, Key right, Key fire, Vector2D fireVelocity); ~KeyboardController() = default; - + void init() override; void update() override; - void modifyAtkSpeed(int8_t modifier); private: - //for creation of projectiles - TransformComponent* player; //for starting position of projectile - Vector2D fireVelocity; //decide source of projectile and flying direction - // SoundManager* soundEffect = Game::assets->getSound; - //SoundManager* soundEffect = new SoundManager(); + Input* m_input; + Key m_up; + Key m_down; + Key m_left; + Key m_right; + Key m_fire; + + TransformComponent* m_transform; + SpriteComponent* m_sprite; + + TransformComponent* m_player; //for starting position of projectile + Vector2D m_fireVelocity; //decide source of projectile and flying direction + + //for attack cooldown in between shots + uint32_t m_lastFireTime = 0; + uint32_t m_fireCooldown = 1000; //in ms can be adjusted to change possible attack-speed }; diff --git a/src/Input.cpp b/src/Input.cpp new file mode 100644 index 0000000..737c976 --- /dev/null +++ b/src/Input.cpp @@ -0,0 +1,115 @@ +#include "Input.h" + +Input::Input() +{ + m_keyStates = SDL_GetKeyboardState(NULL); + InitKeyMappings(); +} + +Input::~Input() = default; + +void Input::pollEvents() +{ + SDL_PumpEvents(); +} + +bool Input::isKeyDown(Key key) +{ + return m_keyStates[mapKeyToSDL(key)]; +} + +SDL_Scancode Input::mapKeyToSDL(Key key) +{ + auto it = m_keyMappings.find(key); + if (it == m_keyMappings.end()) + { + return SDL_SCANCODE_UNKNOWN; + } + return it->second; +} + +void Input::InitKeyMappings() +{ + m_keyMappings = + { + {Key::UP, SDL_SCANCODE_UP}, + {Key::DOWN, SDL_SCANCODE_DOWN}, + {Key::LEFT, SDL_SCANCODE_LEFT}, + {Key::RIGHT, SDL_SCANCODE_RIGHT}, + {Key::FIRE, SDL_SCANCODE_SPACE}, + {Key::ENTER, SDL_SCANCODE_RETURN}, + {Key::ESCAPE, SDL_SCANCODE_ESCAPE}, + {Key::TAB, SDL_SCANCODE_TAB}, + {Key::BACKSPACE, SDL_SCANCODE_BACKSPACE}, + {Key::DELETE, SDL_SCANCODE_DELETE}, + {Key::HOME, SDL_SCANCODE_HOME}, + {Key::END, SDL_SCANCODE_END}, + {Key::PAGE_UP, SDL_SCANCODE_PAGEUP}, + {Key::PAGE_DOWN, SDL_SCANCODE_PAGEDOWN}, + {Key::INSERT, SDL_SCANCODE_INSERT}, + {Key::CAPS_LOCK, SDL_SCANCODE_CAPSLOCK}, + {Key::LEFT_SHIFT, SDL_SCANCODE_LSHIFT}, + {Key::RIGHT_SHIFT, SDL_SCANCODE_RSHIFT}, + {Key::LEFT_CTRL, SDL_SCANCODE_LCTRL}, + {Key::RIGHT_CTRL, SDL_SCANCODE_RCTRL}, + {Key::LEFT_ALT, SDL_SCANCODE_LALT}, + {Key::RIGHT_ALT, SDL_SCANCODE_RALT}, + {Key::F1, SDL_SCANCODE_F1}, + {Key::F2, SDL_SCANCODE_F2}, + {Key::F3, SDL_SCANCODE_F3}, + {Key::F4, SDL_SCANCODE_F4}, + {Key::F5, SDL_SCANCODE_F5}, + {Key::F6, SDL_SCANCODE_F6}, + {Key::F7, SDL_SCANCODE_F7}, + {Key::F8, SDL_SCANCODE_F8}, + {Key::F9, SDL_SCANCODE_F9}, + {Key::F10, SDL_SCANCODE_F10}, + {Key::F11, SDL_SCANCODE_F11}, + {Key::F12, SDL_SCANCODE_F12}, + {Key::A, SDL_SCANCODE_A}, + {Key::B, SDL_SCANCODE_B}, + {Key::C, SDL_SCANCODE_C}, + {Key::D, SDL_SCANCODE_D}, + {Key::E, SDL_SCANCODE_E}, + {Key::F, SDL_SCANCODE_F}, + {Key::G, SDL_SCANCODE_G}, + {Key::H, SDL_SCANCODE_H}, + {Key::I, SDL_SCANCODE_I}, + {Key::J, SDL_SCANCODE_J}, + {Key::K, SDL_SCANCODE_K}, + {Key::L, SDL_SCANCODE_L}, + {Key::M, SDL_SCANCODE_M}, + {Key::N, SDL_SCANCODE_N}, + {Key::O, SDL_SCANCODE_O}, + {Key::P, SDL_SCANCODE_P}, + {Key::Q, SDL_SCANCODE_Q}, + {Key::R, SDL_SCANCODE_R}, + {Key::S, SDL_SCANCODE_S}, + {Key::T, SDL_SCANCODE_T}, + {Key::U, SDL_SCANCODE_U}, + {Key::V, SDL_SCANCODE_V}, + {Key::W, SDL_SCANCODE_W}, + {Key::X, SDL_SCANCODE_X}, + {Key::Y, SDL_SCANCODE_Y}, + {Key::Z, SDL_SCANCODE_Z}, + {Key::NUM_0, SDL_SCANCODE_0}, + {Key::NUM_1, SDL_SCANCODE_1}, + {Key::NUM_2, SDL_SCANCODE_2}, + {Key::NUM_3, SDL_SCANCODE_3}, + {Key::NUM_4, SDL_SCANCODE_4}, + {Key::NUM_5, SDL_SCANCODE_5}, + {Key::NUM_6, SDL_SCANCODE_6}, + {Key::NUM_7, SDL_SCANCODE_7}, + {Key::NUM_8, SDL_SCANCODE_8}, + {Key::NUM_9, SDL_SCANCODE_9}, + {Key::LEFT_BRACKET, SDL_SCANCODE_LEFTBRACKET}, + {Key::RIGHT_BRACKET, SDL_SCANCODE_RIGHTBRACKET}, + {Key::SEMICOLON, SDL_SCANCODE_SEMICOLON}, + {Key::APOSTROPHE, SDL_SCANCODE_APOSTROPHE}, + {Key::COMMA, SDL_SCANCODE_COMMA}, + {Key::PERIOD, SDL_SCANCODE_PERIOD}, + {Key::SLASH, SDL_SCANCODE_SLASH}, + {Key::BACKSLASH, SDL_SCANCODE_BACKSLASH}, + {Key::GRAVE, SDL_SCANCODE_GRAVE} + }; +} \ No newline at end of file diff --git a/src/KeyboardController.cpp b/src/KeyboardController.cpp index df1d193..48d2b82 100644 --- a/src/KeyboardController.cpp +++ b/src/KeyboardController.cpp @@ -1,82 +1,86 @@ #include "KeyboardController.h" #include "Game.h" -#include "Components.h" +#include "TransformComponent.h" #include "AssetManager.h" #include "SpriteComponent.h" -KeyboardController::KeyboardController(SDL_Scancode up, SDL_Scancode down, SDL_Scancode left, SDL_Scancode right, SDL_Scancode fire, Vector2D fireVelocity) -{ - this->up = up; - this->down = down; - this->left = left; - this->right = right; - this->fire = fire; - this->fireVelocity = fireVelocity; -} +KeyboardController::KeyboardController(Input* input, Key up, Key down, Key left, Key right, Key fire, Vector2D fireVelocity) + : m_input(input), m_up(up), m_down(down), m_left(left), m_right(right), m_fire(fire) {} void KeyboardController::init() { - sprite = &entity->getComponent(); - transform = &entity->getComponent(); + m_sprite = &entity->getComponent(); + m_transform = &entity->getComponent(); } void KeyboardController::update() { - transform->direction.x = 0; - transform->direction.y = 0; - sprite->playAnimation(IDLE); + m_transform->direction.x = 0; + m_transform->direction.y = 0; + m_sprite->playAnimation(IDLE); - if (keystates[this->up]) { - transform->direction.y = -1; - sprite->playAnimation(WALK); - SoundManager::playSound(this->entity->getManager().getGame(), STEPS); - } - if (keystates[this->left]) { - transform->direction.x = -1; - sprite->playAnimation(WALK); - sprite->setDirection(Direction::LEFT); - SoundManager::playSound(this->entity->getManager().getGame(), STEPS); - } - if (keystates[this->down]) { - transform->direction.y = 1; - sprite->playAnimation(WALK); - SoundManager::playSound(this->entity->getManager().getGame(), STEPS); - } - if (keystates[this->right]) { - transform->direction.x = 1; - sprite->playAnimation(WALK); - sprite->setDirection(Direction::RIGHT); + if (m_input->isKeyDown(m_left)) + { + m_transform->direction.x = -1; + m_sprite->playAnimation(WALK); + m_sprite->setDirection(Direction::LEFT); SoundManager::playSound(this->entity->getManager().getGame(), STEPS); } - if (keystates[this->fire]) { + if (m_input->isKeyDown(m_right)) + { + m_transform->direction.x = 1; + m_sprite->playAnimation(WALK); + m_sprite->setDirection(Direction::RIGHT); + SoundManager::playSound(this->entity->getManager().getGame(), STEPS); + } + if (m_input->isKeyDown(m_up)) + { + m_transform->direction.y = -1; + m_sprite->playAnimation(WALK); + SoundManager::playSound(this->entity->getManager().getGame(), STEPS); + } + + if (m_input->isKeyDown(m_down)) + { + m_transform->direction.y = 1; + m_sprite->playAnimation(WALK); + SoundManager::playSound(this->entity->getManager().getGame(), STEPS); + } + + if (m_input->isKeyDown(m_fire)) + { Uint32 currentTicks = SDL_GetTicks(); - if (currentTicks - lastFireTime >= fireCooldown) { + if (currentTicks - m_lastFireTime >= m_fireCooldown) + { - player = &entity->getComponent(); + m_player = &entity->getComponent(); //checks player source via the firing velocity //TODO: adding actual projectile textures - if (fireVelocity.x > 0) { - sprite->setDirection(Direction::RIGHT); - this->entity->getManager().getGame()->assets->createProjectile(Vector2D(player->position.x, player->position.y), fireVelocity, - 1, 180, 2, "assets/egg.png", this->entity->getTeam()); - } - else { - sprite->setDirection(Direction::LEFT); - this->entity->getManager().getGame()->assets->createProjectile(Vector2D(player->position.x, player->position.y), fireVelocity, + if (m_fireVelocity.x > 0) + { + m_sprite->setDirection(Direction::RIGHT); + this->entity->getManager().getGame()->assets->createProjectile(Vector2D(m_player->position.x, m_player->position.y), m_fireVelocity, 1, 180, 2, "assets/egg.png", this->entity->getTeam()); } - lastFireTime = currentTicks; + else + { + m_sprite->setDirection(Direction::LEFT); + this->entity->getManager().getGame()->assets->createProjectile(Vector2D(m_player->position.x, m_player->position.y), m_fireVelocity, + 1, 180, 2, "assets/egg.png", this->entity->getTeam()); + } + + m_lastFireTime = currentTicks; } } } void KeyboardController::modifyAtkSpeed(int8_t modifier) { - this->fireCooldown -= modifier * 400; + this->m_fireCooldown -= modifier * 400; } \ No newline at end of file From 2e7a1b45cfc6fcddf90f45b8fec1105a382e9c6b Mon Sep 17 00:00:00 2001 From: Sara Varga Date: Wed, 1 May 2024 13:47:42 +0200 Subject: [PATCH 2/8] refactor: removed Components.h file consisted of includes of all components -> components included when not necessary TODO: had to add 3 component includes in Entity.h because of template function; move if possible --- include/Components.h | 13 ------------- include/Entity.h | 6 ++++++ include/Input.h | 32 ++++++++++++++++---------------- src/AssetManager.cpp | 3 ++- src/Game.cpp | 4 +++- src/HealthComponent.cpp | 1 - src/ProjectileComponent.cpp | 3 ++- 7 files changed, 29 insertions(+), 33 deletions(-) delete mode 100644 include/Components.h diff --git a/include/Components.h b/include/Components.h deleted file mode 100644 index b6f506f..0000000 --- a/include/Components.h +++ /dev/null @@ -1,13 +0,0 @@ -#pragma once - -#include "ECS.h" -#include "Component.h" -#include "Manager.h" -#include "Entity.h" -#include "TransformComponent.h" -#include "SpriteComponent.h" -#include "KeyboardController.h" -#include "ColliderComponent.h" -#include "TileComponent.h" -#include "ProjectileComponent.h" -#include "HealthComponent.h" diff --git a/include/Entity.h b/include/Entity.h index 82ed6a5..7e1fa88 100644 --- a/include/Entity.h +++ b/include/Entity.h @@ -9,6 +9,12 @@ #include "ECS.h" #include "Constants.h" +// TODO: remove here if possible +// temporary fix: addComponent function template doesnt know TransformComponent -> error undefined type +#include "TransformComponent.h" +#include "KeyboardController.h" +#include "SpriteComponent.h" + class Manager; class Component; diff --git a/include/Input.h b/include/Input.h index 3fc6f80..b0cdb1d 100644 --- a/include/Input.h +++ b/include/Input.h @@ -1,22 +1,6 @@ #include #include -class Input -{ -public: - Input(); - ~Input(); - - void pollEvents(); - bool isKeyDown(Key key); - -private: - const Uint8* m_keyStates; - SDL_Scancode mapKeyToSDL(Key key); - std::map m_keyMappings; - void InitKeyMappings(); -}; - enum class Key { UP, @@ -99,3 +83,19 @@ enum class Key BACKSLASH, GRAVE }; + +class Input +{ +public: + Input(); + ~Input(); + + void pollEvents(); + bool isKeyDown(Key key); + +private: + const Uint8* m_keyStates; + SDL_Scancode mapKeyToSDL(Key key); + std::map m_keyMappings; + void InitKeyMappings(); +}; diff --git a/src/AssetManager.cpp b/src/AssetManager.cpp index f759b19..27ac965 100644 --- a/src/AssetManager.cpp +++ b/src/AssetManager.cpp @@ -2,8 +2,9 @@ #include "TextureManager.h" #include "SoundManager.h" -#include "Components.h" +#include "ProjectileComponent.h" #include "Game.h" +#include "TextureDict.h" #include "TransformComponent.h" diff --git a/src/Game.cpp b/src/Game.cpp index e7ac1d4..5da420f 100644 --- a/src/Game.cpp +++ b/src/Game.cpp @@ -3,8 +3,10 @@ #include #include "CollisionHandler.h" -#include "Components.h" #include "AssetManager.h" +#include "SoundManager.h" +#include "KeyboardController.h" +#include "TileComponent.h" #include "Direction.h" #include "Entity.h" #include "HealthComponent.h" diff --git a/src/HealthComponent.cpp b/src/HealthComponent.cpp index 7bbaa60..25dd3ff 100644 --- a/src/HealthComponent.cpp +++ b/src/HealthComponent.cpp @@ -1,6 +1,5 @@ #include "HealthComponent.h" -#include "Components.h" #include "Direction.h" #include "Entity.h" #include "Game.h" diff --git a/src/ProjectileComponent.cpp b/src/ProjectileComponent.cpp index 957fc66..deb588d 100644 --- a/src/ProjectileComponent.cpp +++ b/src/ProjectileComponent.cpp @@ -1,7 +1,8 @@ #include "ProjectileComponent.h" #include "CollisionHandler.h" -#include "Components.h" +#include "SoundManager.h" +#include "TransformComponent.h" #include "Entity.h" #include "Game.h" #include "HealthComponent.h" From 1f27d24de346891605c2ad1780f2ae42085f61f1 Mon Sep 17 00:00:00 2001 From: Nimac0 Date: Thu, 2 May 2024 23:50:28 +0200 Subject: [PATCH 3/8] ref(Healthcomp, Stateffects) --- include/Constants.h | 4 ---- include/HealthComponent.h | 4 +++- include/KeyboardController.h | 7 ++++++- include/StatEffectsComponent.h | 3 ++- include/TransformComponent.h | 7 ++++++- src/Game.cpp | 4 ++-- src/HealthComponent.cpp | 2 +- src/KeyboardController.cpp | 9 +++++++-- src/StatEffectsComponent.cpp | 20 +++++++++++++++++--- src/TransformComponent.cpp | 6 +++--- 10 files changed, 47 insertions(+), 19 deletions(-) diff --git a/include/Constants.h b/include/Constants.h index 20ccf44..8237a71 100644 --- a/include/Constants.h +++ b/include/Constants.h @@ -24,7 +24,3 @@ constexpr int MAP_SIZE_Y = 20; constexpr int SPAWN_ATTEMPTS = 20; -constexpr int BUFF_DURATION = 240; - -constexpr int BUFF_VALUE = 1; - diff --git a/include/HealthComponent.h b/include/HealthComponent.h index fad7a23..1d57330 100644 --- a/include/HealthComponent.h +++ b/include/HealthComponent.h @@ -1,5 +1,6 @@ #pragma once +#include #include "Direction.h" #include "Component.h" @@ -9,7 +10,7 @@ class HealthComponent : public Component { public: - HealthComponent(int health, Direction side) : health(health), side(side) {} + HealthComponent(int health, Direction side, std::string healthTexture) : health(health), side(side), healthTexture(healthTexture) {} ~HealthComponent() {} void modifyHealth(int health = -1); @@ -26,4 +27,5 @@ private: int health; Direction side; + std::string healthTexture; }; \ No newline at end of file diff --git a/include/KeyboardController.h b/include/KeyboardController.h index 9079b73..1514dd7 100644 --- a/include/KeyboardController.h +++ b/include/KeyboardController.h @@ -24,7 +24,6 @@ public: //for attack cooldown in between shots uint32_t lastFireTime = 0; - uint32_t fireCooldown = 1000; //in ms can be adjusted to change possible attack-speed KeyboardController() = default; KeyboardController(SDL_Scancode up, SDL_Scancode down, SDL_Scancode left, SDL_Scancode right, SDL_Scancode fire, Vector2D fireVelocity); @@ -33,7 +32,10 @@ public: void init() override; void update() override; + uint32_t getFireCooldown() { return this->fireCooldown + this->fireCooldownMod > 0 ? this->fireCooldown + this->fireCooldownMod : 0; }; + void modifyAtkSpeed(int8_t modifier); + void resetAtkSpeedMod(); private: //for creation of projectiles @@ -41,4 +43,7 @@ private: Vector2D fireVelocity; //decide source of projectile and flying direction // SoundManager* soundEffect = Game::assets->getSound; //SoundManager* soundEffect = new SoundManager(); + + uint32_t fireCooldown = 1000; //in ms can be adjusted to change possible attack-speed + int32_t fireCooldownMod = 0; }; diff --git a/include/StatEffectsComponent.h b/include/StatEffectsComponent.h index 9daa19d..bc93f2b 100644 --- a/include/StatEffectsComponent.h +++ b/include/StatEffectsComponent.h @@ -18,9 +18,10 @@ public: void init() override; void update() override; - void modifyStatDur(Stats stat, int duration); + void modifyStatDur(Stats stat, int duration, int value); void modifyStatValue(Stats stat, int modifier); + void resetStatValue(Stats stat); private: std::array buffs = { 0 }; diff --git a/include/TransformComponent.h b/include/TransformComponent.h index c88a81d..14afe87 100644 --- a/include/TransformComponent.h +++ b/include/TransformComponent.h @@ -14,7 +14,8 @@ public: int width = 32; int scale = 1; - int speed = 3; + int getSpeed() { return speed + speedMod; }; + void resetSpeedMod() { speedMod = 0; }; TransformComponent(); explicit TransformComponent(int scale); @@ -26,4 +27,8 @@ public: void update() override; void setPositionAfterCollision(Vector2D& positionChange); void modifySpeed(int8_t modifier); + +private: + int speed = 3; + int speedMod = 0; }; diff --git a/src/Game.cpp b/src/Game.cpp index 22bf7bf..c2b13a0 100644 --- a/src/Game.cpp +++ b/src/Game.cpp @@ -163,7 +163,7 @@ void Game::init(const char* title, int xpos, int ypos, int width, int height, bo 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(5, Direction::LEFT, "assets/heart.png"); player1.addComponent(); player1.addGroup((size_t) Entity::GroupLabel::PLAYERS); //tell programm what group it belongs to for rendering order @@ -173,7 +173,7 @@ void Game::init(const char* title, int xpos, int ypos, int width, int height, bo 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(5, Direction::RIGHT, "assets/heart.png"); player2.addComponent(); player2.addGroup((size_t) Entity::GroupLabel::PLAYERS); } diff --git a/src/HealthComponent.cpp b/src/HealthComponent.cpp index 7bbaa60..5a6a533 100644 --- a/src/HealthComponent.cpp +++ b/src/HealthComponent.cpp @@ -58,7 +58,7 @@ void HealthComponent::createHeartComponents(int x) { auto& heart(this->entity->getManager().addEntity()); heart.addComponent(x,5,2); - heart.addComponent("assets/heart.png"); + heart.addComponent(this->healthTexture.data()); heart.addGroup((size_t)Entity::GroupLabel::HEARTS); heart.setTeam(this->entity->getTeam()); } \ No newline at end of file diff --git a/src/KeyboardController.cpp b/src/KeyboardController.cpp index df1d193..e18070c 100644 --- a/src/KeyboardController.cpp +++ b/src/KeyboardController.cpp @@ -54,7 +54,7 @@ void KeyboardController::update() Uint32 currentTicks = SDL_GetTicks(); - if (currentTicks - lastFireTime >= fireCooldown) { + if (currentTicks - lastFireTime >= this->getFireCooldown()) { player = &entity->getComponent(); @@ -78,5 +78,10 @@ void KeyboardController::update() void KeyboardController::modifyAtkSpeed(int8_t modifier) { - this->fireCooldown -= modifier * 400; + this->fireCooldownMod -= modifier * 400; +} + +void KeyboardController::resetAtkSpeedMod() +{ + this->fireCooldownMod = 0; } \ No newline at end of file diff --git a/src/StatEffectsComponent.cpp b/src/StatEffectsComponent.cpp index 6c06263..557b6b7 100644 --- a/src/StatEffectsComponent.cpp +++ b/src/StatEffectsComponent.cpp @@ -15,15 +15,15 @@ void StatEffectsComponent::update() if (this->buffs.at(i) == 0) continue; if (this->buffs.at(i) - 1 == 0) { - this->modifyStatValue((Stats)i, BUFF_VALUE * -1); + this->resetStatValue((Stats)i); } this->buffs.at(i) -= 1; } } -void StatEffectsComponent::modifyStatDur(Stats stat, int duration) +void StatEffectsComponent::modifyStatDur(Stats stat, int duration, int value) { - if(this->buffs.at((uint8_t)stat) == 0) this->modifyStatValue(stat, BUFF_VALUE); + if(this->buffs.at((uint8_t)stat) == 0) this->modifyStatValue(stat, value); this->buffs.at((uint8_t)stat) += duration; } @@ -39,4 +39,18 @@ void StatEffectsComponent::modifyStatValue(Stats stat, int modifier) //modifier break; default: break; } +} + +void StatEffectsComponent::resetStatValue(Stats stat) +{ + switch (stat) + { + case Stats::MOVEMENT_SPEED: + this->entity->getComponent().resetSpeedMod(); + break; + case Stats::ATTACK_SPEED: + this->entity->getComponent().resetAtkSpeedMod(); + break; + default: break; + } } \ No newline at end of file diff --git a/src/TransformComponent.cpp b/src/TransformComponent.cpp index 3175968..1b64791 100644 --- a/src/TransformComponent.cpp +++ b/src/TransformComponent.cpp @@ -56,8 +56,8 @@ void TransformComponent::update() float multiplier = direction.x != 0 && direction.y != 0 ? 0.707 : 1; // normalizes vector; only works if directions are in increments of 45° Vector2D positionChange( - direction.x * speed * multiplier, - direction.y * speed * multiplier + direction.x * this->getSpeed() * multiplier, + direction.y * this->getSpeed() * multiplier ); if (this->entity->hasGroup((size_t)Entity::GroupLabel::PLAYERS)){ @@ -69,7 +69,7 @@ void TransformComponent::update() void TransformComponent::modifySpeed(int8_t modifier) { - this->speed += modifier; + this->speedMod += modifier; } void TransformComponent::setPositionAfterCollision(Vector2D& positionChange) From 31d7f42a31473cb84f004bb45a110d3ce12a0f48 Mon Sep 17 00:00:00 2001 From: Nimac0 Date: Mon, 20 May 2024 21:05:20 +0200 Subject: [PATCH 4/8] ref(Animations, ECS impl): dehardcoded Animations and moved ECS implementation + moved select screen function to chickengame --- include/AnimationHandler.h | 6 ---- include/Game.h | 11 +++--- include/Manager.h | 2 +- include/SpriteComponent.h | 11 ++++-- src/Entity.cpp | 2 +- src/Game.cpp | 70 ++++++++++++++++++++------------------ src/KeyboardController.cpp | 10 +++--- src/SpriteComponent.cpp | 21 +++++++----- src/main.cpp | 2 +- 9 files changed, 70 insertions(+), 65 deletions(-) diff --git a/include/AnimationHandler.h b/include/AnimationHandler.h index 069fc2f..b05a1d6 100644 --- a/include/AnimationHandler.h +++ b/include/AnimationHandler.h @@ -16,11 +16,5 @@ struct Animation } }; -enum AnimationType //TODO enum class -{ - IDLE = 0, - WALK = 1 -}; - diff --git a/include/Game.h b/include/Game.h index 1edbada..b0650bc 100644 --- a/include/Game.h +++ b/include/Game.h @@ -38,7 +38,8 @@ public: void update(); void render(); void clean(); - bool running() const; + bool isRunning() const; + void setRunning(bool running); /* static */ SDL_Renderer* renderer = nullptr; /* static */ SDL_Event event; @@ -51,10 +52,8 @@ public: Manager manager; Map* map; // game specific, might not be needed for all types of games - Entity& player1; - Entity& player2; - - Entity& wall; + //Entity& player1; + //Entity& player2; std::vector& tiles; std::vector& players; @@ -70,7 +69,7 @@ public: private: int counter = 0; - bool isRunning = false; + bool running = false; SDL_Window* window; Entity::TeamLabel winner; }; diff --git a/include/Manager.h b/include/Manager.h index 2497667..8de0983 100644 --- a/include/Manager.h +++ b/include/Manager.h @@ -14,7 +14,7 @@ class Manager { public: Manager(Game* game) : game(game) {}; - + void update(); void draw(); void refresh(); diff --git a/include/SpriteComponent.h b/include/SpriteComponent.h index a957ade..ed584a2 100644 --- a/include/SpriteComponent.h +++ b/include/SpriteComponent.h @@ -3,6 +3,7 @@ #include #include #include +#include #include "AnimationHandler.h" #include "Component.h" @@ -15,7 +16,7 @@ class SpriteComponent : public Component public: int animationIndex = 0; - std::map> animations; + std::map>* animations = nullptr; private: TransformComponent* transform; @@ -32,7 +33,11 @@ private: public: SpriteComponent() = default; SpriteComponent(const char* path); - SpriteComponent(const char* path, bool isAnimated); + SpriteComponent( + const char* path, + bool isAnimated, + std::map>* animationList, + std::string defaultAnimation); ~SpriteComponent(); void setTexture(const char* path); @@ -40,6 +45,6 @@ public: void init() override; void update() override; void draw() override; - void playAnimation(AnimationType type); + void playAnimation(std::string type); void setDirection(Direction direction); }; diff --git a/src/Entity.cpp b/src/Entity.cpp index 7ed2f32..2f2075f 100644 --- a/src/Entity.cpp +++ b/src/Entity.cpp @@ -30,7 +30,7 @@ void Entity::delGroup(Group mGroup) groupBitSet[mGroup] = false; } -std::bitset Entity::getGroupBitSet() +std::bitset Entity::getGroupBitSet() { return groupBitSet; } diff --git a/src/Game.cpp b/src/Game.cpp index c2b13a0..2afd41b 100644 --- a/src/Game.cpp +++ b/src/Game.cpp @@ -17,14 +17,13 @@ Game* engine::game = nullptr; // will be initialized in constructor Game::Game() : manager(this), - tiles(manager.getGroup((size_t)Entity::GroupLabel::MAPTILES)), + tiles(manager.getGroup((size_t)Entity::GroupLabel::MAPTILES)), players(manager.getGroup((size_t)Entity::GroupLabel::PLAYERS)), projectiles(manager.getGroup((size_t)Entity::GroupLabel::PROJECTILE)), hearts(manager.getGroup((size_t)Entity::GroupLabel::HEARTS)), - powerups(manager.getGroup((size_t)Entity::GroupLabel::POWERUPS)), - player1(manager.addEntity()), - player2(manager.addEntity()), - wall(manager.addEntity()) + powerups(manager.getGroup((size_t)Entity::GroupLabel::POWERUPS)) + //player1(manager.addEntity()), + //player2(manager.addEntity()) { engine::game = this; }; @@ -37,7 +36,7 @@ void Game::init(const char* title, int xpos, int ypos, int width, int height, bo Game::textureManager = new TextureManager(&manager); Game::soundManager = new SoundManager(); Game::collisionHandler = new CollisionHandler(manager); // why does this use a referrence, but AssetManager a pointer? - + int flags = 0; if (fullscreen) { @@ -104,7 +103,7 @@ void Game::init(const char* title, int xpos, int ypos, int width, int height, bo while (!hasQuit) { SDL_PollEvent(&event); - + if (event.type == SDL_QUIT) { hasQuit = true; @@ -129,7 +128,7 @@ void Game::init(const char* title, int xpos, int ypos, int width, int height, bo if (hasQuit) { - this->isRunning = false; + this->setRunning(false); return; } @@ -140,7 +139,7 @@ void Game::init(const char* title, int xpos, int ypos, int width, int height, bo const char* player2Sprite; selectCharacters(player1Sprite, player2Sprite); - if (this->isRunning == false) return; + if (this->isRunning() == false) return; map = new Map(); @@ -158,28 +157,28 @@ void Game::init(const char* title, int xpos, int ypos, int width, int height, bo //ecs implementation - 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, "assets/heart.png"); - player1.addComponent(); - player1.addGroup((size_t) Entity::GroupLabel::PLAYERS); //tell programm what group it belongs to for rendering order + // 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, "assets/heart.png"); + // player1.addComponent(); + // player1.addGroup((size_t) Entity::GroupLabel::PLAYERS); //tell programm what group it belongs to for rendering order - 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, "assets/heart.png"); - player2.addComponent(); - player2.addGroup((size_t) Entity::GroupLabel::PLAYERS); + // 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, "assets/heart.png"); + // player2.addComponent(); + // player2.addGroup((size_t) Entity::GroupLabel::PLAYERS); } void Game::selectCharacters(const char* &playerSprite, const char* &enemySprite) -{ +{ // TODO: move this whereever it makes sense (maybe game as a member) std::map> characterSprites; characterSprites[0] = std::make_pair("assets/chicken_neutral_knight.png", "assets/chicken_knight_spritesheet.png"); @@ -265,13 +264,13 @@ void Game::selectCharacters(const char* &playerSprite, const char* &enemySprite) if (hasQuit) { - this->isRunning = false; + this->setRunning(false); return; } playerSprite = characterSprites.find(playerSelection)->second.second; enemySprite = characterSprites.find(enemySelection)->second.second; - this->isRunning = true; + this->setRunning(true); } void Game::handleEvents() @@ -280,7 +279,7 @@ void Game::handleEvents() switch (event.type) { - case SDL_QUIT: this->isRunning = false; + case SDL_QUIT: this->setRunning(false); break; default: @@ -307,7 +306,7 @@ void Game::render() for (auto& p : players) p->draw(); - + for (auto& p : projectiles) p->draw(); @@ -326,15 +325,20 @@ void Game::clean() std::cout << "Game Cleaned!" << std::endl; } -bool Game::running() const +bool Game::isRunning() const { - return isRunning; + return running; +} + +void Game::setRunning(bool running) +{ + this->running = running; } void Game::setWinner(Entity::TeamLabel winningTeam) { this->winner = winningTeam; - this->isRunning = false; + this->setRunning(false); } Entity::TeamLabel Game::getWinner() const diff --git a/src/KeyboardController.cpp b/src/KeyboardController.cpp index e18070c..3f5c01a 100644 --- a/src/KeyboardController.cpp +++ b/src/KeyboardController.cpp @@ -25,27 +25,27 @@ void KeyboardController::update() { transform->direction.x = 0; transform->direction.y = 0; - sprite->playAnimation(IDLE); + sprite->playAnimation("IDLE"); if (keystates[this->up]) { transform->direction.y = -1; - sprite->playAnimation(WALK); + sprite->playAnimation("WALK"); SoundManager::playSound(this->entity->getManager().getGame(), STEPS); } if (keystates[this->left]) { transform->direction.x = -1; - sprite->playAnimation(WALK); + sprite->playAnimation("WALK"); sprite->setDirection(Direction::LEFT); SoundManager::playSound(this->entity->getManager().getGame(), STEPS); } if (keystates[this->down]) { transform->direction.y = 1; - sprite->playAnimation(WALK); + sprite->playAnimation("WALK"); SoundManager::playSound(this->entity->getManager().getGame(), STEPS); } if (keystates[this->right]) { transform->direction.x = 1; - sprite->playAnimation(WALK); + sprite->playAnimation("WALK"); sprite->setDirection(Direction::RIGHT); SoundManager::playSound(this->entity->getManager().getGame(), STEPS); } diff --git a/src/SpriteComponent.cpp b/src/SpriteComponent.cpp index ab40f25..9913e98 100644 --- a/src/SpriteComponent.cpp +++ b/src/SpriteComponent.cpp @@ -17,14 +17,17 @@ SpriteComponent::SpriteComponent(const char* path) this->texturePath = path; } -SpriteComponent::SpriteComponent(const char* path, bool isAnimated) +SpriteComponent::SpriteComponent( + const char* path, + bool isAnimated, + std::map>* animationMap, + std::string defaultAnimation) { animated = isAnimated; - animations.emplace(IDLE, std::make_unique((uint8_t)AnimationType::IDLE, 2, 200)); - animations.emplace(WALK, std::make_unique((uint8_t)AnimationType::WALK, 2, 200)); + animations = animationMap; - playAnimation(IDLE); + playAnimation(defaultAnimation); this->texturePath = path; } @@ -71,14 +74,14 @@ void SpriteComponent::draw() this->entity->getManager().getGame()->textureManager->draw(this->entity->getManager().getGame()->renderer, this->texture, this->srcRect, this->destRect, this->animated && this->flipped); } -void SpriteComponent::playAnimation(AnimationType type) +void SpriteComponent::playAnimation(std::string type) { - this->animationIndex = animations.at(type)->index; - this->frames = animations.at(type)->frames; - this->speed = animations.at(type)->speed; + this->animationIndex = animations->at(type)->index; + this->frames = animations->at(type)->frames; + this->speed = animations->at(type)->speed; } -void SpriteComponent::setDirection(Direction direction) +void SpriteComponent::setDirection(Direction direction) { this->flipped = direction == Direction::RIGHT; } \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index ce47941..4f2a48b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -21,7 +21,7 @@ int main(int argc, char* argv[]) game = new Game(); game->init("No_Name_Chicken_Game", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, SCREEN_SIZE_WIDTH, SCREEN_SIZE_HEIGHT, false); - while (game->running()) { + while (game->isRunning()) { frameStart = SDL_GetTicks(); game->handleEvents(); From aa83280b6a5c5d24bd327d2c4ce65ef4798e663b Mon Sep 17 00:00:00 2001 From: Sara Varga Date: Sun, 26 May 2024 22:54:50 +0200 Subject: [PATCH 5/8] #32 KeyboardController: movement working projectiles need adjustments --- include/Entity.h | 3 +- include/{Input.h => InputComponent.h} | 16 +++-- include/KeyboardController.h | 40 ------------- src/Game.cpp | 12 ++-- src/{Input.cpp => InputComponent.cpp} | 24 +++++--- src/KeyboardController.cpp | 86 --------------------------- src/StatEffectsComponent.cpp | 4 +- 7 files changed, 38 insertions(+), 147 deletions(-) rename include/{Input.h => InputComponent.h} (83%) delete mode 100644 include/KeyboardController.h rename src/{Input.cpp => InputComponent.cpp} (89%) delete mode 100644 src/KeyboardController.cpp diff --git a/include/Entity.h b/include/Entity.h index 7e1fa88..8f56b2f 100644 --- a/include/Entity.h +++ b/include/Entity.h @@ -11,8 +11,9 @@ // TODO: remove here if possible // temporary fix: addComponent function template doesnt know TransformComponent -> error undefined type +// #include "KeyboardController.h" +#include "InputComponent.h" #include "TransformComponent.h" -#include "KeyboardController.h" #include "SpriteComponent.h" class Manager; diff --git a/include/Input.h b/include/InputComponent.h similarity index 83% rename from include/Input.h rename to include/InputComponent.h index b0cdb1d..f9c235a 100644 --- a/include/Input.h +++ b/include/InputComponent.h @@ -1,13 +1,16 @@ +#pragma once #include #include +#include "Component.h" + enum class Key { UP, DOWN, LEFT, RIGHT, - FIRE, + SPACE, ENTER, ESCAPE, TAB, @@ -84,13 +87,16 @@ enum class Key GRAVE }; -class Input +class InputComponent : public Component { public: - Input(); - ~Input(); + InputComponent(); + ~InputComponent(); - void pollEvents(); + void init() override; + void update() override; + + // void pollEvents(); bool isKeyDown(Key key); private: diff --git a/include/KeyboardController.h b/include/KeyboardController.h deleted file mode 100644 index 1d1fbd3..0000000 --- a/include/KeyboardController.h +++ /dev/null @@ -1,40 +0,0 @@ -#pragma once -#include - -#include "Component.h" -#include "Vector2D.h" -#include "Constants.h" // TODO: change so gamespecific constants are in own file -#include "SoundManager.h" -#include "Input.h" - -class TransformComponent; -class SpriteComponent; - -class KeyboardController : public Component -{ -public: - KeyboardController(Input* input, Key up, Key down, Key left, Key right, Key fire, Vector2D fireVelocity); - ~KeyboardController() = default; - - void init() override; - void update() override; - void modifyAtkSpeed(int8_t modifier); - -private: - Input* m_input; - Key m_up; - Key m_down; - Key m_left; - Key m_right; - Key m_fire; - - TransformComponent* m_transform; - SpriteComponent* m_sprite; - - TransformComponent* m_player; //for starting position of projectile - Vector2D m_fireVelocity; //decide source of projectile and flying direction - - //for attack cooldown in between shots - uint32_t m_lastFireTime = 0; - uint32_t m_fireCooldown = 1000; //in ms can be adjusted to change possible attack-speed -}; diff --git a/src/Game.cpp b/src/Game.cpp index 5da420f..d4d1df5 100644 --- a/src/Game.cpp +++ b/src/Game.cpp @@ -5,7 +5,7 @@ #include "CollisionHandler.h" #include "AssetManager.h" #include "SoundManager.h" -#include "KeyboardController.h" +// #include "KeyboardController.h" #include "TileComponent.h" #include "Direction.h" #include "Entity.h" @@ -135,7 +135,7 @@ void Game::init(const char* title, int xpos, int ypos, int width, int height, bo return; } - engine::init(); + // engine::init(); // temporarily moved down to access groups at engine init call // character selection const char* player1Sprite; @@ -167,7 +167,8 @@ void Game::init(const char* title, int xpos, int ypos, int width, int height, bo 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(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(); player1.addComponent("player", 0.8f); //adds tag (for further use, reference tag) player1.addComponent(5, Direction::LEFT); player1.addComponent(); @@ -177,11 +178,14 @@ void Game::init(const char* title, int xpos, int ypos, int width, int height, bo 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(SDL_SCANCODE_UP, SDL_SCANCODE_DOWN, SDL_SCANCODE_LEFT, SDL_SCANCODE_RIGHT, SDL_SCANCODE_RCTRL, Vector2D(-2, 0)); + player2.addComponent(); player2.addComponent("enemy", 0.8f); player2.addComponent(5, Direction::RIGHT); player2.addComponent(); player2.addGroup((size_t) Entity::GroupLabel::PLAYERS); + + engine::init(); } void Game::selectCharacters(const char* &playerSprite, const char* &enemySprite) diff --git a/src/Input.cpp b/src/InputComponent.cpp similarity index 89% rename from src/Input.cpp rename to src/InputComponent.cpp index 737c976..538ad93 100644 --- a/src/Input.cpp +++ b/src/InputComponent.cpp @@ -1,24 +1,30 @@ -#include "Input.h" +#include "InputComponent.h" -Input::Input() +InputComponent::InputComponent() { m_keyStates = SDL_GetKeyboardState(NULL); InitKeyMappings(); } -Input::~Input() = default; +InputComponent::~InputComponent() = default; -void Input::pollEvents() +void InputComponent::init() +{ + // m_keyStates = SDL_GetKeyboardState(NULL); + // InitKeyMappings(); +} + +void InputComponent::update() { SDL_PumpEvents(); } -bool Input::isKeyDown(Key key) +bool InputComponent::isKeyDown(Key key) { return m_keyStates[mapKeyToSDL(key)]; } -SDL_Scancode Input::mapKeyToSDL(Key key) +SDL_Scancode InputComponent::mapKeyToSDL(Key key) { auto it = m_keyMappings.find(key); if (it == m_keyMappings.end()) @@ -28,7 +34,7 @@ SDL_Scancode Input::mapKeyToSDL(Key key) return it->second; } -void Input::InitKeyMappings() +void InputComponent::InitKeyMappings() { m_keyMappings = { @@ -36,7 +42,7 @@ void Input::InitKeyMappings() {Key::DOWN, SDL_SCANCODE_DOWN}, {Key::LEFT, SDL_SCANCODE_LEFT}, {Key::RIGHT, SDL_SCANCODE_RIGHT}, - {Key::FIRE, SDL_SCANCODE_SPACE}, + {Key::SPACE, SDL_SCANCODE_SPACE}, {Key::ENTER, SDL_SCANCODE_RETURN}, {Key::ESCAPE, SDL_SCANCODE_ESCAPE}, {Key::TAB, SDL_SCANCODE_TAB}, @@ -112,4 +118,4 @@ void Input::InitKeyMappings() {Key::BACKSLASH, SDL_SCANCODE_BACKSLASH}, {Key::GRAVE, SDL_SCANCODE_GRAVE} }; -} \ No newline at end of file +} diff --git a/src/KeyboardController.cpp b/src/KeyboardController.cpp deleted file mode 100644 index 48d2b82..0000000 --- a/src/KeyboardController.cpp +++ /dev/null @@ -1,86 +0,0 @@ -#include "KeyboardController.h" - -#include "Game.h" -#include "TransformComponent.h" -#include "AssetManager.h" -#include "SpriteComponent.h" - -KeyboardController::KeyboardController(Input* input, Key up, Key down, Key left, Key right, Key fire, Vector2D fireVelocity) - : m_input(input), m_up(up), m_down(down), m_left(left), m_right(right), m_fire(fire) {} - -void KeyboardController::init() -{ - m_sprite = &entity->getComponent(); - m_transform = &entity->getComponent(); -} - -void KeyboardController::update() -{ - m_transform->direction.x = 0; - m_transform->direction.y = 0; - m_sprite->playAnimation(IDLE); - - if (m_input->isKeyDown(m_left)) - { - m_transform->direction.x = -1; - m_sprite->playAnimation(WALK); - m_sprite->setDirection(Direction::LEFT); - SoundManager::playSound(this->entity->getManager().getGame(), STEPS); - } - - if (m_input->isKeyDown(m_right)) - { - m_transform->direction.x = 1; - m_sprite->playAnimation(WALK); - m_sprite->setDirection(Direction::RIGHT); - SoundManager::playSound(this->entity->getManager().getGame(), STEPS); - } - - if (m_input->isKeyDown(m_up)) - { - m_transform->direction.y = -1; - m_sprite->playAnimation(WALK); - SoundManager::playSound(this->entity->getManager().getGame(), STEPS); - } - - if (m_input->isKeyDown(m_down)) - { - m_transform->direction.y = 1; - m_sprite->playAnimation(WALK); - SoundManager::playSound(this->entity->getManager().getGame(), STEPS); - } - - if (m_input->isKeyDown(m_fire)) - { - Uint32 currentTicks = SDL_GetTicks(); - - if (currentTicks - m_lastFireTime >= m_fireCooldown) - { - - m_player = &entity->getComponent(); - - //checks player source via the firing velocity - //TODO: adding actual projectile textures - if (m_fireVelocity.x > 0) - { - m_sprite->setDirection(Direction::RIGHT); - this->entity->getManager().getGame()->assets->createProjectile(Vector2D(m_player->position.x, m_player->position.y), m_fireVelocity, - 1, 180, 2, "assets/egg.png", this->entity->getTeam()); - } - - else - { - m_sprite->setDirection(Direction::LEFT); - this->entity->getManager().getGame()->assets->createProjectile(Vector2D(m_player->position.x, m_player->position.y), m_fireVelocity, - 1, 180, 2, "assets/egg.png", this->entity->getTeam()); - } - - m_lastFireTime = currentTicks; - } - } -} - -void KeyboardController::modifyAtkSpeed(int8_t modifier) -{ - this->m_fireCooldown -= modifier * 400; -} \ No newline at end of file diff --git a/src/StatEffectsComponent.cpp b/src/StatEffectsComponent.cpp index 6c06263..e113462 100644 --- a/src/StatEffectsComponent.cpp +++ b/src/StatEffectsComponent.cpp @@ -1,7 +1,7 @@ #include "StatEffectsComponent.h" #include "Entity.h" #include "TransformComponent.h" -#include "KeyboardController.h" +// #include "KeyboardController.h" #include #include @@ -35,7 +35,7 @@ void StatEffectsComponent::modifyStatValue(Stats stat, int modifier) //modifier this->entity->getComponent().modifySpeed(modifier); break; case Stats::ATTACK_SPEED: - this->entity->getComponent().modifyAtkSpeed(modifier); + // this->entity->getComponent().modifyAtkSpeed(modifier); break; default: break; } From 68a3e4813100f84305f1385089be77ef3d0cd1dd Mon Sep 17 00:00:00 2001 From: Benedikt Galbavy Date: Wed, 29 May 2024 09:02:08 +0200 Subject: [PATCH 6/8] rename Game->GameInternal --- include/{Game.h => GameInternal.h} | 8 +++---- include/Manager.h | 8 +++---- include/Map.h | 6 ++--- include/PopupWindow.h | 2 +- include/SoundManager.h | 4 ++-- src/AssetManager.cpp | 3 +-- src/ColliderComponent.cpp | 2 +- src/{Game.cpp => GameInternal.cpp} | 38 +++++++++++++++--------------- src/GameObject.cpp | 2 +- src/HealthComponent.cpp | 2 +- src/Map.cpp | 6 ++--- src/PopupWindow.cpp | 2 +- src/PowerupComponent.cpp | 2 +- src/ProjectileComponent.cpp | 2 +- src/SoundManager.cpp | 4 ++-- src/SpriteComponent.cpp | 2 +- src/TextureManager.cpp | 2 +- src/TransformComponent.cpp | 2 +- src/main.cpp | 6 ++--- 19 files changed, 51 insertions(+), 52 deletions(-) rename include/{Game.h => GameInternal.h} (88%) rename src/{Game.cpp => GameInternal.cpp} (88%) diff --git a/include/Game.h b/include/GameInternal.h similarity index 88% rename from include/Game.h rename to include/GameInternal.h index 1edbada..6821af9 100644 --- a/include/Game.h +++ b/include/GameInternal.h @@ -22,14 +22,14 @@ namespace engine { extern gamefunction init; extern gamefunction update; - extern Game* game; // this is a temporary fix to remove artifacts of chicken_game from the engine while the API is not yet finalized + extern GameInternal* game; // this is a temporary fix to remove artifacts of chicken_game from the engine while the API is not yet finalized } -class Game +class GameInternal { public: - Game(); - ~Game(); + GameInternal(); + ~GameInternal(); void init(const char* title, int xpos, int ypos, int width, int height, bool fullscreen); void selectCharacters(const char* &playerSprite, const char* &enemySprite); diff --git a/include/Manager.h b/include/Manager.h index 2497667..ea549db 100644 --- a/include/Manager.h +++ b/include/Manager.h @@ -9,11 +9,11 @@ #include "Constants.h" #include "Entity.h" -class Game; +class GameInternal; class Manager { public: - Manager(Game* game) : game(game) {}; + Manager(GameInternal* game) : game(game) {}; void update(); void draw(); @@ -29,10 +29,10 @@ public: Entity& addEntity(); - Game* getGame() { return this->game; }; + GameInternal* getGame() { return this->game; }; private: - Game* game; + GameInternal* game; std::vector> entities; std::array, MAX_GROUPS> entitiesByGroup; std::array, MAX_TEAMS> entitiesByTeam; diff --git a/include/Map.h b/include/Map.h index 09f46b6..4c1bc3b 100644 --- a/include/Map.h +++ b/include/Map.h @@ -3,7 +3,7 @@ #include #include -class Game; +class GameInternal; class Map { public: @@ -21,6 +21,6 @@ public: * \return Boolean for success * */ - static void loadMap(const char* path, int sizeX, int sizeY, Game* game, const std::map>* textureDict /* backreference */); - static void addTile(unsigned long id, int x, int y, Game* game, const std::map>* textureDict); + static void loadMap(const char* path, int sizeX, int sizeY, GameInternal* game, const std::map>* textureDict /* backreference */); + static void addTile(unsigned long id, int x, int y, GameInternal* game, const std::map>* textureDict); }; diff --git a/include/PopupWindow.h b/include/PopupWindow.h index ccd01f6..464ab28 100644 --- a/include/PopupWindow.h +++ b/include/PopupWindow.h @@ -5,7 +5,7 @@ #include "Entity.h" -class Game; +class GameInternal; class PopupWindow { diff --git a/include/SoundManager.h b/include/SoundManager.h index 6fa2d73..bcfc0ac 100644 --- a/include/SoundManager.h +++ b/include/SoundManager.h @@ -13,7 +13,7 @@ enum SoundTypes THROW_EGG, }; -class Game; +class GameInternal; class SoundManager { public: @@ -30,6 +30,6 @@ class SoundManager std::map sound_cache; Mix_Chunk* loadSound(const char* fileName); - static void playSound(Game* game, SoundTypes sound); + static void playSound(GameInternal* game, SoundTypes sound); private: }; \ No newline at end of file diff --git a/src/AssetManager.cpp b/src/AssetManager.cpp index 8a70be9..d737a5d 100644 --- a/src/AssetManager.cpp +++ b/src/AssetManager.cpp @@ -3,7 +3,7 @@ #include "TextureManager.h" #include "SoundManager.h" #include "ProjectileComponent.h" -#include "Game.h" +#include "GameInternal.h" #include "TransformComponent.h" @@ -11,7 +11,6 @@ #include "ColliderComponent.h" #include "Constants.h" #include "Entity.h" -#include "Game.h" #include "Vector2D.h" #include "PowerupComponent.h" #include diff --git a/src/ColliderComponent.cpp b/src/ColliderComponent.cpp index 08475d0..aeca234 100644 --- a/src/ColliderComponent.cpp +++ b/src/ColliderComponent.cpp @@ -2,7 +2,7 @@ #include "CollisionHandler.h" #include "Entity.h" -#include "Game.h" +#include "GameInternal.h" #include "TransformComponent.h" #include diff --git a/src/Game.cpp b/src/GameInternal.cpp similarity index 88% rename from src/Game.cpp rename to src/GameInternal.cpp index c38cc9c..024d8ad 100644 --- a/src/Game.cpp +++ b/src/GameInternal.cpp @@ -1,4 +1,4 @@ -#include "Game.h" +#include "GameInternal.h" #include @@ -15,9 +15,9 @@ #include "StatEffectsComponent.h" #include "Constants.h" -Game* engine::game = nullptr; // will be initialized in constructor +GameInternal* engine::game = nullptr; // will be initialized in constructor -Game::Game() : +GameInternal::GameInternal() : manager(this), tiles(manager.getGroup((size_t)Entity::GroupLabel::MAPTILES)), players(manager.getGroup((size_t)Entity::GroupLabel::PLAYERS)), @@ -31,14 +31,14 @@ Game::Game() : engine::game = this; }; -Game::~Game() = default; +GameInternal::~GameInternal() = default; -void Game::init(const char* title, int xpos, int ypos, int width, int height, bool fullscreen) +void GameInternal::init(const char* title, int xpos, int ypos, int width, int height, bool fullscreen) { - Game::assets = new AssetManager(&manager); - Game::textureManager = new TextureManager(&manager); - Game::soundManager = new SoundManager(); - Game::collisionHandler = new CollisionHandler(manager); // why does this use a referrence, but AssetManager a pointer? + GameInternal::assets = new AssetManager(&manager); + GameInternal::textureManager = new TextureManager(&manager); + GameInternal::soundManager = new SoundManager(); + GameInternal::collisionHandler = new CollisionHandler(manager); // why does this use a referrence, but AssetManager a pointer? int flags = 0; if (fullscreen) @@ -84,7 +84,7 @@ void Game::init(const char* title, int xpos, int ypos, int width, int height, bo } SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); - SDL_Texture* backgroundTexture = Game::textureManager->loadTexture("assets/startscreen.png"); + SDL_Texture* backgroundTexture = GameInternal::textureManager->loadTexture("assets/startscreen.png"); SDL_RenderClear(renderer); SDL_RenderCopy(renderer, backgroundTexture, NULL, NULL); @@ -184,7 +184,7 @@ void Game::init(const char* title, int xpos, int ypos, int width, int height, bo engine::init(); } -void Game::selectCharacters(const char* &playerSprite, const char* &enemySprite) +void GameInternal::selectCharacters(const char* &playerSprite, const char* &enemySprite) { // TODO: move this whereever it makes sense (maybe game as a member) std::map> characterSprites; @@ -252,7 +252,7 @@ void Game::selectCharacters(const char* &playerSprite, const char* &enemySprite) } } - SDL_Texture* backgroundTexture = Game::textureManager->loadTexture("assets/characterSelection.png"); + SDL_Texture* backgroundTexture = GameInternal::textureManager->loadTexture("assets/characterSelection.png"); SDL_RenderClear(renderer); SDL_RenderCopy(renderer, backgroundTexture, NULL, NULL); @@ -280,7 +280,7 @@ void Game::selectCharacters(const char* &playerSprite, const char* &enemySprite) this->isRunning = true; } -void Game::handleEvents() +void GameInternal::handleEvents() { SDL_PollEvent(&event); @@ -294,7 +294,7 @@ void Game::handleEvents() } } -void Game::update() +void GameInternal::update() { manager.refresh(); manager.update(); @@ -302,7 +302,7 @@ void Game::update() engine::update(); // TODO: this might have to be split up into two update functions, before and after manager... } -void Game::render() +void GameInternal::render() { SDL_RenderClear(renderer); for (auto& t : tiles) @@ -323,7 +323,7 @@ void Game::render() SDL_RenderPresent(renderer); } -void Game::clean() +void GameInternal::clean() { delete(textureManager); SDL_DestroyRenderer(renderer); @@ -332,18 +332,18 @@ void Game::clean() std::cout << "Game Cleaned!" << std::endl; } -bool Game::running() const +bool GameInternal::running() const { return isRunning; } -void Game::setWinner(Entity::TeamLabel winningTeam) +void GameInternal::setWinner(Entity::TeamLabel winningTeam) { this->winner = winningTeam; this->isRunning = false; } -Entity::TeamLabel Game::getWinner() const +Entity::TeamLabel GameInternal::getWinner() const { return this->winner; } diff --git a/src/GameObject.cpp b/src/GameObject.cpp index b94c03a..2b065c2 100644 --- a/src/GameObject.cpp +++ b/src/GameObject.cpp @@ -2,7 +2,7 @@ #include "SDL_error.h" #include "TextureManager.h" -#include "Game.h" +#include "GameInternal.h" GameObject::GameObject(const char* texturesheet, int x, int y) { diff --git a/src/HealthComponent.cpp b/src/HealthComponent.cpp index 25dd3ff..ead3412 100644 --- a/src/HealthComponent.cpp +++ b/src/HealthComponent.cpp @@ -2,7 +2,7 @@ #include "Direction.h" #include "Entity.h" -#include "Game.h" +#include "GameInternal.h" #include void HealthComponent::init() diff --git a/src/Map.cpp b/src/Map.cpp index 125f029..5689d9e 100644 --- a/src/Map.cpp +++ b/src/Map.cpp @@ -6,11 +6,11 @@ #include #include "Constants.h" -#include "Game.h" +#include "GameInternal.h" #include "SDL_error.h" #include "TileComponent.h" -void Map::loadMap(const char* path, int sizeX, int sizeY, Game* game, const std::map>* textureDict /* backreference */) +void Map::loadMap(const char* path, int sizeX, int sizeY, GameInternal* game, const std::map>* textureDict /* backreference */) { std::string tileIDstr; char singleChar = 0; @@ -56,7 +56,7 @@ void Map::loadMap(const char* path, int sizeX, int sizeY, Game* game, const std: mapFile.close(); } -void Map::addTile(unsigned long id, int x, int y, Game* game, const std::map>* textureDict) // tile entity +void Map::addTile(unsigned long id, int x, int y, GameInternal* game, const std::map>* textureDict) // tile entity { auto& tile(game->manager.addEntity()); tile.addComponent(x, y, TILE_SIZE, TILE_SIZE, id, textureDict); diff --git a/src/PopupWindow.cpp b/src/PopupWindow.cpp index 5912eaf..39362a9 100644 --- a/src/PopupWindow.cpp +++ b/src/PopupWindow.cpp @@ -4,7 +4,7 @@ #include "Entity.h" #include "PopupWindow.h" #include "TextureManager.h" -#include "Game.h" +#include "GameInternal.h" PopupWindow::PopupWindow(const char* title, const std::string &message) : continueGame(false), interacted(false) { diff --git a/src/PowerupComponent.cpp b/src/PowerupComponent.cpp index 48de1be..7d4ce0a 100644 --- a/src/PowerupComponent.cpp +++ b/src/PowerupComponent.cpp @@ -1,5 +1,5 @@ #include "PowerupComponent.h" -#include "Game.h" +#include "GameInternal.h" #include "CollisionHandler.h" #include "Entity.h" #include "HealthComponent.h" diff --git a/src/ProjectileComponent.cpp b/src/ProjectileComponent.cpp index deb588d..75c711b 100644 --- a/src/ProjectileComponent.cpp +++ b/src/ProjectileComponent.cpp @@ -4,7 +4,7 @@ #include "SoundManager.h" #include "TransformComponent.h" #include "Entity.h" -#include "Game.h" +#include "GameInternal.h" #include "HealthComponent.h" #include "Vector2D.h" #include diff --git a/src/SoundManager.cpp b/src/SoundManager.cpp index 2a890d7..b8575c8 100644 --- a/src/SoundManager.cpp +++ b/src/SoundManager.cpp @@ -4,7 +4,7 @@ #include #include -#include "Game.h" +#include "GameInternal.h" #include "AssetManager.h" Mix_Chunk* SoundManager::loadSound(const char* fileName) @@ -27,7 +27,7 @@ Mix_Chunk* SoundManager::loadSound(const char* fileName) return sound; } -void SoundManager::playSound(Game* game, SoundTypes sound) +void SoundManager::playSound(GameInternal* game, SoundTypes sound) { switch (sound) { diff --git a/src/SpriteComponent.cpp b/src/SpriteComponent.cpp index ab40f25..9dea93e 100644 --- a/src/SpriteComponent.cpp +++ b/src/SpriteComponent.cpp @@ -9,7 +9,7 @@ #include "TextureManager.h" #include "Entity.h" #include "TransformComponent.h" -#include "Game.h" +#include "GameInternal.h" #include "Manager.h" SpriteComponent::SpriteComponent(const char* path) diff --git a/src/TextureManager.cpp b/src/TextureManager.cpp index 6d56c35..3e46d46 100644 --- a/src/TextureManager.cpp +++ b/src/TextureManager.cpp @@ -4,7 +4,7 @@ #include #include -#include "Game.h" +#include "GameInternal.h" SDL_Texture* TextureManager::loadTexture(const char* fileName) { diff --git a/src/TransformComponent.cpp b/src/TransformComponent.cpp index 3175968..71b1302 100644 --- a/src/TransformComponent.cpp +++ b/src/TransformComponent.cpp @@ -4,7 +4,7 @@ #include "ColliderComponent.h" #include "Constants.h" #include "Entity.h" -#include "Game.h" +#include "GameInternal.h" #include "Vector2D.h" #include #include diff --git a/src/main.cpp b/src/main.cpp index ce47941..092afb4 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2,11 +2,11 @@ #include #include "Entity.h" -#include "Game.h" +#include "GameInternal.h" #include "Constants.h" #include "PopupWindow.h" -Game* game = nullptr; +GameInternal* game = nullptr; int main(int argc, char* argv[]) { @@ -18,7 +18,7 @@ int main(int argc, char* argv[]) Uint32 frameStart; int frameTime; - game = new Game(); + game = new GameInternal(); game->init("No_Name_Chicken_Game", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, SCREEN_SIZE_WIDTH, SCREEN_SIZE_HEIGHT, false); while (game->running()) { From 5182312887be697d91e3f0f9c6d3b5af63715197 Mon Sep 17 00:00:00 2001 From: Benedikt Galbavy Date: Wed, 29 May 2024 10:02:14 +0200 Subject: [PATCH 7/8] implemented GameFactory to instance an implementation of game --- include/Game.h | 18 +++++++++++ include/GameFactory.h | 70 ++++++++++++++++++++++++++++++++++++++++++ include/GameInternal.h | 10 ++---- src/GameInternal.cpp | 13 ++++---- 4 files changed, 97 insertions(+), 14 deletions(-) create mode 100644 include/Game.h create mode 100644 include/GameFactory.h diff --git a/include/Game.h b/include/Game.h new file mode 100644 index 0000000..c1b1aa3 --- /dev/null +++ b/include/Game.h @@ -0,0 +1,18 @@ +#pragma once + +class GameInternal; + +// TODO: add managers here +class Game { +public: + virtual ~Game() {} + + virtual void init() = 0; + virtual void update() = 0; + + GameInternal* gameInternal; //!< \deprecated +}; + + +// game factory include to simplify imports in implementation +#include "GameFactory.h" \ No newline at end of file diff --git a/include/GameFactory.h b/include/GameFactory.h new file mode 100644 index 0000000..24fcfcb --- /dev/null +++ b/include/GameFactory.h @@ -0,0 +1,70 @@ +#pragma once + +#include +#include +#include +#include +#include + +#include "Game.h" + +class GameInternal; + +class GameFactory { +public: + using CreateFunc = std::function; + + static GameFactory& instance() { + static GameFactory factory; + return factory; + } + + void registerClass(CreateFunc createFunc) { + this->creator = createFunc; + } + + Game* get() { + assert(this->gameInstance != nullptr); + return this->gameInstance; + } + + Game* create(GameInternal* gameInternal) { + Game* game = this->gameInstance == nullptr ? this->creator() : this->gameInstance; // TODO: error handling + game->gameInternal = gameInternal; + this->gameInstance = game; + return game; + } + + /* named game instances for future use + void registerClass(const std::string& className, CreateFunc createFunc) { + this->creators[className] = createFunc; + } + + Game* create(const std::string& className) { + auto it = this->creators.find(className); + if (it != creators.end()) { + return it->second(); + } + return nullptr; + } + */ + +private: + CreateFunc creator; + Game* gameInstance = nullptr; //!< \depricated + // std::map creators; +}; + +#define REGISTER_GAME(className) \ + static bool registered_##className = []() { \ + GameFactory::instance().registerClass([]() -> Game* { return new className; }); \ + return true; \ + }(); + +/* +#define REGISTER_GAME(className) \ + static bool registered_##className = []() { \ + GameFactory::instance().registerClass(#className, []() -> Game* { return new className; }); \ + return true; \ + }(); +*/ diff --git a/include/GameInternal.h b/include/GameInternal.h index 6821af9..8cd806c 100644 --- a/include/GameInternal.h +++ b/include/GameInternal.h @@ -17,13 +17,7 @@ class CollisionHandler; class TextureManager; class SoundManager; class Map; - -namespace engine { - extern gamefunction init; - extern gamefunction update; - - extern GameInternal* game; // this is a temporary fix to remove artifacts of chicken_game from the engine while the API is not yet finalized -} +class Game; class GameInternal { @@ -69,6 +63,8 @@ public: private: + Game* gameInstance; + int counter = 0; bool isRunning = false; SDL_Window* window; diff --git a/src/GameInternal.cpp b/src/GameInternal.cpp index 024d8ad..0491db5 100644 --- a/src/GameInternal.cpp +++ b/src/GameInternal.cpp @@ -14,8 +14,8 @@ #include "TextureManager.h" #include "StatEffectsComponent.h" #include "Constants.h" - -GameInternal* engine::game = nullptr; // will be initialized in constructor +#include "Game.h" +#include "GameFactory.h" GameInternal::GameInternal() : manager(this), @@ -27,9 +27,7 @@ GameInternal::GameInternal() : player1(manager.addEntity()), player2(manager.addEntity()), wall(manager.addEntity()) -{ - engine::game = this; -}; +{}; GameInternal::~GameInternal() = default; @@ -181,7 +179,8 @@ void GameInternal::init(const char* title, int xpos, int ypos, int width, int he player2.addComponent(); player2.addGroup((size_t) Entity::GroupLabel::PLAYERS); - engine::init(); + this->gameInstance = GameFactory::instance().create(this); + this->gameInstance->init(); } void GameInternal::selectCharacters(const char* &playerSprite, const char* &enemySprite) @@ -299,7 +298,7 @@ void GameInternal::update() manager.refresh(); manager.update(); - engine::update(); // TODO: this might have to be split up into two update functions, before and after manager... + this->gameInstance->update(); // TODO: this might have to be split up into two update functions, before and after manager... } void GameInternal::render() From 8c5c5c7215e835ea8d6e08c008b4b648fe86743d Mon Sep 17 00:00:00 2001 From: Benedikt Galbavy Date: Thu, 13 Jun 2024 23:22:21 +0200 Subject: [PATCH 8/8] removed define marco --- include/GameFactory.h | 29 +++++++++-------------------- include/GameRegistryHelper.h | 17 +++++++++++++++++ src/GameInternal.cpp | 2 +- 3 files changed, 27 insertions(+), 21 deletions(-) create mode 100644 include/GameRegistryHelper.h diff --git a/include/GameFactory.h b/include/GameFactory.h index 24fcfcb..443a2d6 100644 --- a/include/GameFactory.h +++ b/include/GameFactory.h @@ -19,48 +19,37 @@ public: return factory; } - void registerClass(CreateFunc createFunc) { - this->creator = createFunc; - } - - Game* get() { + /*Game* get() { assert(this->gameInstance != nullptr); return this->gameInstance; - } + }*/ - Game* create(GameInternal* gameInternal) { + /*Game* create(GameInternal* gameInternal) { Game* game = this->gameInstance == nullptr ? this->creator() : this->gameInstance; // TODO: error handling game->gameInternal = gameInternal; this->gameInstance = game; return game; - } + }*/ - /* named game instances for future use void registerClass(const std::string& className, CreateFunc createFunc) { this->creators[className] = createFunc; } - Game* create(const std::string& className) { + Game* create(const std::string& className, GameInternal* gameInternal) { auto it = this->creators.find(className); if (it != creators.end()) { - return it->second(); + Game* game = it->second(); + game->gameInternal = gameInternal; + return game; } return nullptr; } - */ private: CreateFunc creator; - Game* gameInstance = nullptr; //!< \depricated - // std::map creators; + std::map creators; }; -#define REGISTER_GAME(className) \ - static bool registered_##className = []() { \ - GameFactory::instance().registerClass([]() -> Game* { return new className; }); \ - return true; \ - }(); - /* #define REGISTER_GAME(className) \ static bool registered_##className = []() { \ diff --git a/include/GameRegistryHelper.h b/include/GameRegistryHelper.h new file mode 100644 index 0000000..b980d70 --- /dev/null +++ b/include/GameRegistryHelper.h @@ -0,0 +1,17 @@ +#pragma once + +#include "GameFactory.h" + +namespace vego { + template + class GameRegistryHelper { + public: + GameRegistryHelper(const std::string& className) { + static_assert(std::is_base_of::value, "Your class must inherit from Game"); + GameFactory::instance().registerClass( + className, + []() -> Game* { return new T; } + ); + }; + }; +} \ No newline at end of file diff --git a/src/GameInternal.cpp b/src/GameInternal.cpp index 0491db5..148a32e 100644 --- a/src/GameInternal.cpp +++ b/src/GameInternal.cpp @@ -179,7 +179,7 @@ void GameInternal::init(const char* title, int xpos, int ypos, int width, int he player2.addComponent(); player2.addGroup((size_t) Entity::GroupLabel::PLAYERS); - this->gameInstance = GameFactory::instance().create(this); + this->gameInstance = GameFactory::instance().create("Chickengame", this); //!< \todo Should be specified via a config file this->gameInstance->init(); }