0
0
mirror of https://github.com/Nimac0/SDL_Minigame synced 2026-01-12 12:33:43 +00:00

Merge branch 'ref/nicole' into separation-refactor

This commit is contained in:
Nimac0 2024-06-14 00:46:19 +02:00
commit 58e2bb0d30
14 changed files with 90 additions and 72 deletions

View File

@ -16,11 +16,5 @@ struct Animation
} }
}; };
enum AnimationType //TODO enum class
{
IDLE = 0,
WALK = 1
};

View File

@ -24,7 +24,3 @@ constexpr int MAP_SIZE_Y = 20;
constexpr int SPAWN_ATTEMPTS = 20; constexpr int SPAWN_ATTEMPTS = 20;
constexpr int BUFF_DURATION = 240;
constexpr int BUFF_VALUE = 1;

View File

@ -32,7 +32,8 @@ public:
void update(); void update();
void render(); void render();
void clean(); void clean();
bool running() const; bool isRunning() const;
void setRunning(bool running);
/* static */ SDL_Renderer* renderer = nullptr; /* static */ SDL_Renderer* renderer = nullptr;
/* static */ SDL_Event event; /* static */ SDL_Event event;
@ -45,11 +46,6 @@ public:
Manager manager; Manager manager;
Map* map; // game specific, might not be needed for all types of games Map* map; // game specific, might not be needed for all types of games
Entity& player1;
Entity& player2;
Entity& wall;
std::vector<Entity*>& tiles; std::vector<Entity*>& tiles;
std::vector<Entity*>& players; std::vector<Entity*>& players;
std::vector<Entity*>& projectiles; std::vector<Entity*>& projectiles;
@ -66,7 +62,7 @@ private:
Game* gameInstance; Game* gameInstance;
int counter = 0; int counter = 0;
bool isRunning = false; bool running = false;
SDL_Window* window; SDL_Window* window;
Entity::TeamLabel winner; Entity::TeamLabel winner;
}; };

View File

@ -1,5 +1,6 @@
#pragma once #pragma once
#include <string>
#include "Direction.h" #include "Direction.h"
#include "Component.h" #include "Component.h"
@ -9,7 +10,7 @@ class HealthComponent : public Component
{ {
public: 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() {} ~HealthComponent() {}
void modifyHealth(int health = -1); void modifyHealth(int health = -1);
@ -26,4 +27,5 @@ private:
int health; int health;
Direction side; Direction side;
std::string healthTexture;
}; };

View File

@ -3,6 +3,7 @@
#include <map> #include <map>
#include <SDL_render.h> #include <SDL_render.h>
#include <memory> #include <memory>
#include <string>
#include "AnimationHandler.h" #include "AnimationHandler.h"
#include "Component.h" #include "Component.h"
@ -15,7 +16,7 @@ class SpriteComponent : public Component
public: public:
int animationIndex = 0; int animationIndex = 0;
std::map<AnimationType, std::unique_ptr<Animation>> animations; std::map<std::string, std::unique_ptr<Animation>>* animations = nullptr;
private: private:
TransformComponent* transform; TransformComponent* transform;
@ -32,7 +33,11 @@ private:
public: public:
SpriteComponent() = default; SpriteComponent() = default;
SpriteComponent(const char* path); SpriteComponent(const char* path);
SpriteComponent(const char* path, bool isAnimated); SpriteComponent(
const char* path,
bool isAnimated,
std::map<std::string, std::unique_ptr<Animation>>* animationList,
std::string defaultAnimation);
~SpriteComponent(); ~SpriteComponent();
void setTexture(const char* path); void setTexture(const char* path);
@ -40,6 +45,6 @@ public:
void init() override; void init() override;
void update() override; void update() override;
void draw() override; void draw() override;
void playAnimation(AnimationType type); void playAnimation(std::string type);
void setDirection(Direction direction); void setDirection(Direction direction);
}; };

View File

@ -18,9 +18,10 @@ public:
void init() override; void init() override;
void update() 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 modifyStatValue(Stats stat, int modifier);
void resetStatValue(Stats stat);
private: private:
std::array<int, MAX_STATS> buffs = { 0 }; std::array<int, MAX_STATS> buffs = { 0 };

View File

@ -14,7 +14,8 @@ public:
int width = 32; int width = 32;
int scale = 1; int scale = 1;
int speed = 3; int getSpeed() { return speed + speedMod; };
void resetSpeedMod() { speedMod = 0; };
TransformComponent(); TransformComponent();
explicit TransformComponent(int scale); explicit TransformComponent(int scale);
@ -26,4 +27,8 @@ public:
void update() override; void update() override;
void setPositionAfterCollision(Vector2D& positionChange); void setPositionAfterCollision(Vector2D& positionChange);
void modifySpeed(int8_t modifier); void modifySpeed(int8_t modifier);
private:
int speed = 3;
int speedMod = 0;
}; };

View File

@ -23,10 +23,9 @@ GameInternal::GameInternal() :
players(manager.getGroup((size_t)Entity::GroupLabel::PLAYERS)), players(manager.getGroup((size_t)Entity::GroupLabel::PLAYERS)),
projectiles(manager.getGroup((size_t)Entity::GroupLabel::PROJECTILE)), projectiles(manager.getGroup((size_t)Entity::GroupLabel::PROJECTILE)),
hearts(manager.getGroup((size_t)Entity::GroupLabel::HEARTS)), hearts(manager.getGroup((size_t)Entity::GroupLabel::HEARTS)),
powerups(manager.getGroup((size_t)Entity::GroupLabel::POWERUPS)), powerups(manager.getGroup((size_t)Entity::GroupLabel::POWERUPS))
player1(manager.addEntity()), //player1(manager.addEntity()),
player2(manager.addEntity()), //player2(manager.addEntity())
wall(manager.addEntity())
{}; {};
GameInternal::~GameInternal() = default; GameInternal::~GameInternal() = default;
@ -129,7 +128,7 @@ void GameInternal::init(const char* title, int xpos, int ypos, int width, int he
if (hasQuit) if (hasQuit)
{ {
this->isRunning = false; this->setRunning(false);
return; return;
} }
@ -140,7 +139,7 @@ void GameInternal::init(const char* title, int xpos, int ypos, int width, int he
const char* player2Sprite; const char* player2Sprite;
selectCharacters(player1Sprite, player2Sprite); selectCharacters(player1Sprite, player2Sprite);
if (this->isRunning == false) return; if (this->isRunning() == false) return;
map = new Map(); map = new Map();
@ -158,26 +157,24 @@ void GameInternal::init(const char* title, int xpos, int ypos, int width, int he
//ecs implementation //ecs implementation
player1.setTeam(Entity::TeamLabel::BLUE); // player1.setTeam(Entity::TeamLabel::BLUE);
player1.addComponent<TransformComponent>(80,80,2); //posx, posy, scale // player1.addComponent<TransformComponent>(80,80,2); //posx, posy, scale
player1.addComponent<SpriteComponent>(player1Sprite, true); //adds sprite (32x32px), path needed // player1.addComponent<SpriteComponent>(player1Sprite, true); //adds sprite (32x32px), path needed
// player1.addComponent<KeyboardController>(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<KeyboardController>(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<InputComponent>(); // player1.addComponent<ColliderComponent>("player", 0.8f); //adds tag (for further use, reference tag)
player1.addComponent<ColliderComponent>("player", 0.8f); //adds tag (for further use, reference tag) // player1.addComponent<HealthComponent>(5, Direction::LEFT, "assets/heart.png");
player1.addComponent<HealthComponent>(5, Direction::LEFT); // player1.addComponent<StatEffectsComponent>();
player1.addComponent<StatEffectsComponent>(); // player1.addGroup((size_t) Entity::GroupLabel::PLAYERS); //tell programm what group it belongs to for rendering order
player1.addGroup((size_t) Entity::GroupLabel::PLAYERS); //tell programm what group it belongs to for rendering order
player2.setTeam(Entity::TeamLabel::RED); // player2.setTeam(Entity::TeamLabel::RED);
player2.addComponent<TransformComponent>(600, 500, 2); // player2.addComponent<TransformComponent>(600, 500, 2);
player2.addComponent<SpriteComponent>(player2Sprite, true); // player2.addComponent<SpriteComponent>(player2Sprite, true);
// player2.addComponent<KeyboardController>(SDL_SCANCODE_UP, SDL_SCANCODE_DOWN, SDL_SCANCODE_LEFT, SDL_SCANCODE_RIGHT, SDL_SCANCODE_RCTRL, Vector2D(-2, 0)); // player2.addComponent<KeyboardController>(SDL_SCANCODE_UP, SDL_SCANCODE_DOWN, SDL_SCANCODE_LEFT, SDL_SCANCODE_RIGHT, SDL_SCANCODE_RCTRL, Vector2D(-2, 0));
player2.addComponent<InputComponent>(); // player2.addComponent<ColliderComponent>("enemy", 0.8f);
player2.addComponent<ColliderComponent>("enemy", 0.8f); // player2.addComponent<HealthComponent>(5, Direction::RIGHT, "assets/heart.png");
player2.addComponent<HealthComponent>(5, Direction::RIGHT); // player2.addComponent<StatEffectsComponent>();
player2.addComponent<StatEffectsComponent>(); // player2.addGroup((size_t) Entity::GroupLabel::PLAYERS);
player2.addGroup((size_t) Entity::GroupLabel::PLAYERS);
this->gameInstance = GameFactory::instance().create("Chickengame", this); //!< \todo Should be specified via a config file this->gameInstance = GameFactory::instance().create("Chickengame", this); //!< \todo Should be specified via a config file
this->gameInstance->init(); this->gameInstance->init();
@ -270,13 +267,13 @@ void GameInternal::selectCharacters(const char* &playerSprite, const char* &enem
if (hasQuit) if (hasQuit)
{ {
this->isRunning = false; this->setRunning(false);
return; return;
} }
playerSprite = characterSprites.find(playerSelection)->second.second; playerSprite = characterSprites.find(playerSelection)->second.second;
enemySprite = characterSprites.find(enemySelection)->second.second; enemySprite = characterSprites.find(enemySelection)->second.second;
this->isRunning = true; this->setRunning(true);
} }
void GameInternal::handleEvents() void GameInternal::handleEvents()
@ -285,7 +282,7 @@ void GameInternal::handleEvents()
switch (event.type) switch (event.type)
{ {
case SDL_QUIT: this->isRunning = false; case SDL_QUIT: this->setRunning(false);
break; break;
default: default:
@ -331,15 +328,20 @@ void GameInternal::clean()
std::cout << "Game Cleaned!" << std::endl; std::cout << "Game Cleaned!" << std::endl;
} }
bool GameInternal::running() const bool GameInternal::isRunning() const
{ {
return isRunning; return running;
}
void GameInternal::setRunning(bool running)
{
this->running = running;
} }
void GameInternal::setWinner(Entity::TeamLabel winningTeam) void GameInternal::setWinner(Entity::TeamLabel winningTeam)
{ {
this->winner = winningTeam; this->winner = winningTeam;
this->isRunning = false; this->setRunning(false);
} }
Entity::TeamLabel GameInternal::getWinner() const Entity::TeamLabel GameInternal::getWinner() const

View File

@ -57,7 +57,7 @@ void HealthComponent::createHeartComponents(int x)
{ {
auto& heart(this->entity->getManager().addEntity()); auto& heart(this->entity->getManager().addEntity());
heart.addComponent<TransformComponent>(x,5,2); heart.addComponent<TransformComponent>(x,5,2);
heart.addComponent<SpriteComponent>("assets/heart.png"); heart.addComponent<SpriteComponent>(this->healthTexture.data());
heart.addGroup((size_t)Entity::GroupLabel::HEARTS); heart.addGroup((size_t)Entity::GroupLabel::HEARTS);
heart.setTeam(this->entity->getTeam()); heart.setTeam(this->entity->getTeam());
} }

View File

@ -17,14 +17,17 @@ SpriteComponent::SpriteComponent(const char* path)
this->texturePath = path; this->texturePath = path;
} }
SpriteComponent::SpriteComponent(const char* path, bool isAnimated) SpriteComponent::SpriteComponent(
const char* path,
bool isAnimated,
std::map<std::string, std::unique_ptr<Animation>>* animationMap,
std::string defaultAnimation)
{ {
animated = isAnimated; animated = isAnimated;
animations.emplace(IDLE, std::make_unique<Animation>((uint8_t)AnimationType::IDLE, 2, 200)); animations = animationMap;
animations.emplace(WALK, std::make_unique<Animation>((uint8_t)AnimationType::WALK, 2, 200));
playAnimation(IDLE); playAnimation(defaultAnimation);
this->texturePath = path; this->texturePath = path;
} }
@ -71,11 +74,11 @@ void SpriteComponent::draw()
this->entity->getManager().getGame()->textureManager->draw(this->entity->getManager().getGame()->renderer, this->texture, this->srcRect, this->destRect, this->animated && this->flipped); 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->animationIndex = animations->at(type)->index;
this->frames = animations.at(type)->frames; this->frames = animations->at(type)->frames;
this->speed = animations.at(type)->speed; this->speed = animations->at(type)->speed;
} }
void SpriteComponent::setDirection(Direction direction) void SpriteComponent::setDirection(Direction direction)

View File

@ -15,15 +15,15 @@ void StatEffectsComponent::update()
if (this->buffs.at(i) == 0) continue; if (this->buffs.at(i) == 0) continue;
if (this->buffs.at(i) - 1 == 0) if (this->buffs.at(i) - 1 == 0)
{ {
this->modifyStatValue((Stats)i, BUFF_VALUE * -1); this->resetStatValue((Stats)i);
} }
this->buffs.at(i) -= 1; 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; this->buffs.at((uint8_t)stat) += duration;
} }
@ -40,3 +40,17 @@ void StatEffectsComponent::modifyStatValue(Stats stat, int modifier) //modifier
default: break; default: break;
} }
} }
void StatEffectsComponent::resetStatValue(Stats stat)
{
switch (stat)
{
case Stats::MOVEMENT_SPEED:
this->entity->getComponent<TransformComponent>().resetSpeedMod();
break;
case Stats::ATTACK_SPEED:
// this->entity->getComponent<KeyboardController>().resetAtkSpeedMod();
break;
default: break;
}
}

View File

@ -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° float multiplier = direction.x != 0 && direction.y != 0 ? 0.707 : 1; // normalizes vector; only works if directions are in increments of 45°
Vector2D positionChange( Vector2D positionChange(
direction.x * speed * multiplier, direction.x * this->getSpeed() * multiplier,
direction.y * speed * multiplier direction.y * this->getSpeed() * multiplier
); );
if (this->entity->hasGroup((size_t)Entity::GroupLabel::PLAYERS)){ if (this->entity->hasGroup((size_t)Entity::GroupLabel::PLAYERS)){
@ -69,7 +69,7 @@ void TransformComponent::update()
void TransformComponent::modifySpeed(int8_t modifier) void TransformComponent::modifySpeed(int8_t modifier)
{ {
this->speed += modifier; this->speedMod += modifier;
} }
void TransformComponent::setPositionAfterCollision(Vector2D& positionChange) void TransformComponent::setPositionAfterCollision(Vector2D& positionChange)

View File

@ -21,7 +21,7 @@ int main(int argc, char* argv[])
game = new GameInternal(); game = new GameInternal();
game->init("No_Name_Chicken_Game", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, SCREEN_SIZE_WIDTH, SCREEN_SIZE_HEIGHT, false); 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(); frameStart = SDL_GetTicks();
game->handleEvents(); game->handleEvents();