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();