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

ref(teams): removed teams from engine

This commit is contained in:
Nimac0 2024-06-24 20:39:38 +02:00
parent a8e046ce07
commit 6bf460d483
18 changed files with 195 additions and 219 deletions

View File

@ -24,7 +24,7 @@ public:
AssetManager(Manager* manager);
~AssetManager();
void createProjectile(Vector2D pos, Vector2D velocity, int scale, int range, int speed, const char* texturePath, Entity::TeamLabel teamLabel);
void createProjectile(Vector2D pos, Vector2D velocity, int scale, int range, int speed, const char* texturePath, Entity* owner);
void createPowerup(Vector2D pos, std::function<void (Entity*)> pickupFunc, std::string texturePath);
Vector2D calculateSpawnPosition();

View File

@ -44,16 +44,14 @@ public:
// temporary function, remove once game.cpp cleaned up
std::vector<ColliderComponent*> getColliders(
std::initializer_list<Entity::GroupLabel> const& groupLabels,
std::initializer_list<Entity::TeamLabel> const& teamLabels = {},
bool negateTeam = false);
std::initializer_list<Entity*> const& excludedEntities = {});
template<typename T>
T getAnyIntersection(
Entity* entity,
Vector2D posMod = {},
std::initializer_list<Entity::GroupLabel> const& groupLabels = {},
std::initializer_list<Entity::TeamLabel> const& teamLabels = {},
bool negateTeam = false);
std::initializer_list<Entity*> const& excludedEntities = {});
void update();
};

View File

@ -3,7 +3,6 @@
#include <cstddef>
using Group = std::size_t;
using Team = std::size_t;
constexpr int CHARACTER_COUNT = 4;

View File

@ -39,12 +39,12 @@ public:
POWERUPS
};
enum class TeamLabel
{
NONE, //!< No team
BLUE, //!< Team blue
RED //!< Team red
};
//enum class TeamLabel
//{
// NONE, //!< No team
// BLUE, //!< Team blue
// RED //!< Team red
//};
explicit Entity(Manager& mManager) :
manager(mManager) { };
@ -65,8 +65,8 @@ public:
void delGroup(Group mGroup);
std::bitset<MAX_GROUPS> getGroupBitSet();
void setTeam(TeamLabel teamLabel);
TeamLabel getTeam();
//void setTeam(TeamLabel teamLabel);
//TeamLabel getTeam();
Manager& getManager() { return manager; };
@ -103,5 +103,5 @@ private:
ComponentArray componentArray = {};
ComponentBitSet componentBitSet;
GroupBitSet groupBitSet;
TeamLabel teamLabel;
//TeamLabel teamLabel;
};

View File

@ -26,14 +26,15 @@ public:
~GameInternal();
void init(const char* title, int xpos, int ypos, int width, int height, bool fullscreen);
void selectCharacters(const char* &playerSprite, const char* &enemySprite);
//void selectCharacters(const char* &playerSprite, const char* &enemySprite);
void handleEvents();
void update();
void render();
void clean();
bool isRunning() const;
void setRunning(bool running);
void setRunning(bool running); // TODO: should be private/not accesible for game dev
void stopGame();
/* static */ SDL_Renderer* renderer = nullptr;
/* static */ SDL_Event event;
@ -54,15 +55,15 @@ public:
// end moved globals
void refreshPlayers();
Entity::TeamLabel getWinner() const;
void setWinner(Entity::TeamLabel winningTeam);
//Entity::TeamLabel getWinner() const;
//void setWinner(Entity::TeamLabel winningTeam);
private:
Game* gameInstance;
int counter = 0;
bool running = false;
bool running = true;
SDL_Window* window;
Entity::TeamLabel winner;
//Entity::TeamLabel winner;
};

View File

@ -1,6 +1,8 @@
#pragma once
#include <string>
#include <vector>
#include "Direction.h"
#include "Component.h"
@ -28,4 +30,6 @@ private:
int health;
Direction side;
std::string healthTexture;
std::vector<Entity*> heartElements;
};

View File

@ -22,8 +22,8 @@ public:
void addToGroup(Entity* mEntity, Group mGroup);
std::vector<Entity*>& getGroup(Group mGroup);
void addToTeam(Entity* mEntity, Team mTeam);
std::vector<Entity*>& getTeam(Team mTeam);
//void addToTeam(Entity* mEntity, Team mTeam);
//std::vector<Entity*>& getTeam(Team mTeam);
std::vector<Entity*> getAll();
@ -35,5 +35,5 @@ private:
GameInternal* game;
std::vector<std::unique_ptr<Entity>> entities;
std::array<std::vector<Entity*>, MAX_GROUPS> entitiesByGroup;
std::array<std::vector<Entity*>, MAX_TEAMS> entitiesByTeam;
//std::array<std::vector<Entity*>, MAX_TEAMS> entitiesByTeam;
};

View File

@ -5,6 +5,8 @@
#include "Entity.h"
/*
class GameInternal;
class PopupWindow {
@ -25,4 +27,6 @@ private:
SDL_Window* window;
SDL_Texture* texture;
bool continueGame;
};
};
*/

View File

@ -11,7 +11,8 @@ class ProjectileComponent : public Component
//can maybe be split in separate .cpp file
public:
ProjectileComponent(int range, int speed, Vector2D direction) : range(range), speed(speed), direction(direction) {}
ProjectileComponent(int range, int speed, Vector2D direction, Entity* owner)
: range(range), speed(speed), direction(direction), owner(owner) {}
~ProjectileComponent() {}
void init() override;
@ -24,5 +25,7 @@ private:
int speed = 0;
int distance = 0;
Entity* owner = nullptr;
Vector2D direction;
};

View File

@ -36,15 +36,14 @@ Mix_Chunk* AssetManager::getSound(std::string id) {
return soundEffects.at(id);
}
void AssetManager::createProjectile(Vector2D pos, Vector2D velocity, int scale, int range, int speed, const char* texturePath, Entity::TeamLabel teamLabel) {
void AssetManager::createProjectile(Vector2D pos, Vector2D velocity, int scale, int range, int speed, const char* texturePath, Entity* owner) {
auto& projectile(man->addEntity());
projectile.addComponent<TransformComponent>(pos.x, pos.y, 32, 32, scale); //32x32 is standard size for objects
projectile.addComponent<SpriteComponent>(texturePath);
projectile.addComponent<ProjectileComponent>(range, speed, velocity);
projectile.addComponent<ProjectileComponent>(range, speed, velocity, owner);
projectile.addComponent<ColliderComponent>("projectile", 0.6f);
projectile.addGroup((size_t)Entity::GroupLabel::PROJECTILE);
projectile.setTeam(teamLabel);
}
void AssetManager::createPowerup(Vector2D pos, std::function<void (Entity*)> pickupFunc, std::string texturePath) {

View File

@ -87,8 +87,7 @@ IntersectionBitSet CollisionHandler::getIntersectionWithBounds(Entity* entity, V
std::vector<ColliderComponent*> CollisionHandler::getColliders(
std::initializer_list<Entity::GroupLabel> const& groupLabels,
std::initializer_list<Entity::TeamLabel> const& teamLabels,
bool negateTeam)
std::initializer_list<Entity*> const& excludedEntities)
{
std::vector<ColliderComponent*> colliders;
@ -97,16 +96,11 @@ std::vector<ColliderComponent*> CollisionHandler::getColliders(
groupBitSet.set((size_t) groupLabel);
}
std::bitset<MAX_TEAMS> teamBitSet;
for (auto& teamLabel : teamLabels) {
teamBitSet.set((size_t) teamLabel);
}
for (auto& entity : manager.getAll()) {
if ((groupBitSet & entity->getGroupBitSet()).none())
continue;
if (teamBitSet.any() && negateTeam != (teamBitSet.test((size_t) entity->getTeam())))
continue;
if (std::ranges::find(excludedEntities, entity) != excludedEntities.end())
continue;
if (!entity->hasComponent<ColliderComponent>())
continue;
colliders.emplace_back(&entity->getComponent<ColliderComponent>());
@ -120,11 +114,10 @@ IntersectionBitSet CollisionHandler::getAnyIntersection<IntersectionBitSet>(
Entity* entity,
Vector2D posMod,
std::initializer_list<Entity::GroupLabel> const& groupLabels,
std::initializer_list<Entity::TeamLabel> const& teamLabels,
bool negateTeam)
std::initializer_list<Entity*> const& excludedEntities)
{
IntersectionBitSet intersections;
for (auto& collider : getColliders(groupLabels, teamLabels)) {
for (auto& collider : getColliders(groupLabels, excludedEntities)) {
intersections |= getIntersection(entity, collider->entity, posMod);
}
return intersections;
@ -135,10 +128,9 @@ Entity* CollisionHandler::getAnyIntersection<Entity*>(
Entity* entity,
Vector2D posMod,
std::initializer_list<Entity::GroupLabel> const& groupLabels,
std::initializer_list<Entity::TeamLabel> const& teamLabels,
bool negateTeam)
std::initializer_list<Entity*> const& excludedEntities)
{
for (auto& collider : getColliders(groupLabels, teamLabels)) {
for (auto& collider : getColliders(groupLabels, excludedEntities)) {
SDL_Rect rect = entity->getComponent<ColliderComponent>().collider + posMod;
if (SDL_HasIntersection(&rect, &collider->collider)) {
return collider->entity;

View File

@ -35,13 +35,13 @@ std::bitset<MAX_GROUPS> Entity::getGroupBitSet()
return groupBitSet;
}
void Entity::setTeam(Entity::TeamLabel teamLabel)
{
this->teamLabel = teamLabel;
manager.addToTeam(this, (size_t) teamLabel);
}
Entity::TeamLabel Entity::getTeam()
{
return teamLabel;
}
//void Entity::setTeam(Entity::TeamLabel teamLabel)
//{
// this->teamLabel = teamLabel;
// manager.addToTeam(this, (size_t) teamLabel);
//}
//
//Entity::TeamLabel Entity::getTeam()
//{
// return teamLabel;
//}

View File

@ -135,146 +135,117 @@ void GameInternal::init(const char* title, int xpos, int ypos, int width, int he
// engine::init(); // temporarily moved down to access groups at engine init call
// character selection
const char* player1Sprite;
const char* player2Sprite;
//const char* player1Sprite;
//const char* player2Sprite;
selectCharacters(player1Sprite, player2Sprite);
//selectCharacters(player1Sprite, player2Sprite);
if (this->isRunning() == false) return;
map = new Map();
//adding textures to the library in AssetManager
/*
assets->addTexture("player1", "assets/chicken_neutral_knight.png");
assets->addTexture("player2", "assets/chicken_neutral.png");
assets->addTexture("egg", "assets/egg.png");
*/
// loading sounds
assets->addSoundEffect("throw_egg", "assets/sound/throw_egg.wav");
assets->addSoundEffect("steps", "assets/sound/steps.wav");
//ecs implementation
// player1.setTeam(Entity::TeamLabel::BLUE);
// player1.addComponent<TransformComponent>(80,80,2); //posx, posy, scale
// 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<ColliderComponent>("player", 0.8f); //adds tag (for further use, reference tag)
// player1.addComponent<HealthComponent>(5, Direction::LEFT, "assets/heart.png");
// player1.addComponent<StatEffectsComponent>();
// player1.addGroup((size_t) Entity::GroupLabel::PLAYERS); //tell programm what group it belongs to for rendering order
// player2.setTeam(Entity::TeamLabel::RED);
// player2.addComponent<TransformComponent>(600, 500, 2);
// 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<ColliderComponent>("enemy", 0.8f);
// player2.addComponent<HealthComponent>(5, Direction::RIGHT, "assets/heart.png");
// player2.addComponent<StatEffectsComponent>();
// player2.addGroup((size_t) Entity::GroupLabel::PLAYERS);
this->gameInstance = GameFactory::instance().create(this);
this->gameInstance->init();
}
void GameInternal::selectCharacters(const char* &playerSprite, const char* &enemySprite)
{
// TODO: move this whereever it makes sense (maybe game as a member)
std::map<int, std::pair<const char*, const char*>> characterSprites;
characterSprites[0] = std::make_pair("assets/chicken_neutral_knight.png", "assets/chicken_knight_spritesheet.png");
characterSprites[1] = std::make_pair("assets/chicken_neutral.png", "assets/chicken_spritesheet.png");
characterSprites[2] = std::make_pair("assets/chicken_neutral_wizard.png", "assets/chicken_wizard_spritesheet.png");
characterSprites[3] = std::make_pair("assets/chicken_neutral_mlady.png", "assets/chicken_mlady_spritesheet.png");
SDL_Rect playerCharacterRects[CHARACTER_COUNT];
SDL_Rect enemyCharacterRects[CHARACTER_COUNT];
SDL_Texture* characterTextures[CHARACTER_COUNT];
int playerSelection = 0;
int enemySelection = 0;
// load textures
for (int i = 0; i < CHARACTER_COUNT; ++i)
{
characterTextures[i] = IMG_LoadTexture(renderer, characterSprites.find(i)->second.first);
}
// set up initial positions for character rects
for (int i = 0; i < CHARACTER_COUNT; ++i)
{
playerCharacterRects[i] = { 134 + (i % 2) * 118, 272 + ((i >= 2) ? 114 : 0), 64, 64 };
enemyCharacterRects[i] = { 485 + (i % 2) * 118, 273 + ((i >= 2) ? 114 : 0), 64, 64 };
}
bool hasQuit = false;
while (!hasQuit)
{
SDL_PollEvent(&event);
if (event.type == SDL_QUIT)
{
hasQuit = true;
}
if (event.type == SDL_KEYDOWN)
{
if (event.key.keysym.scancode == SDL_SCANCODE_RETURN)
{
break;
}
switch (event.key.keysym.scancode)
{
case SDL_SCANCODE_A:
playerSelection = (playerSelection - 1 + CHARACTER_COUNT) % CHARACTER_COUNT;
break;
case SDL_SCANCODE_D:
playerSelection = (playerSelection + 1) % CHARACTER_COUNT;
break;
case SDL_SCANCODE_LEFT:
enemySelection = (enemySelection - 1 + CHARACTER_COUNT) % CHARACTER_COUNT;
break;
case SDL_SCANCODE_RIGHT:
enemySelection = (enemySelection + 1) % CHARACTER_COUNT;
break;
default:
break;
}
}
SDL_Texture* backgroundTexture = GameInternal::textureManager->loadTexture("assets/characterSelection.png");
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, backgroundTexture, NULL, NULL);
for (int i = 0; i < CHARACTER_COUNT; ++i)
{
SDL_RenderCopy(renderer, characterTextures[i], nullptr, &playerCharacterRects[i]);
SDL_RenderCopy(renderer, characterTextures[i], nullptr, &enemyCharacterRects[i]);
}
SDL_SetRenderDrawColor(renderer, 0, 255, 0, 255);
SDL_RenderDrawRect(renderer, &playerCharacterRects[playerSelection]);
SDL_RenderDrawRect(renderer, &enemyCharacterRects[enemySelection]);
SDL_RenderPresent(renderer);
}
if (hasQuit)
{
this->setRunning(false);
return;
}
playerSprite = characterSprites.find(playerSelection)->second.second;
enemySprite = characterSprites.find(enemySelection)->second.second;
this->setRunning(true);
}
//void GameInternal::selectCharacters(const char* &playerSprite, const char* &enemySprite)
//{
// // TODO: move this whereever it makes sense (maybe game as a member)
// std::map<int, std::pair<const char*, const char*>> characterSprites;
// characterSprites[0] = std::make_pair("assets/chicken_neutral_knight.png", "assets/chicken_knight_spritesheet.png");
// characterSprites[1] = std::make_pair("assets/chicken_neutral.png", "assets/chicken_spritesheet.png");
// characterSprites[2] = std::make_pair("assets/chicken_neutral_wizard.png", "assets/chicken_wizard_spritesheet.png");
// characterSprites[3] = std::make_pair("assets/chicken_neutral_mlady.png", "assets/chicken_mlady_spritesheet.png");
//
// SDL_Rect playerCharacterRects[CHARACTER_COUNT];
// SDL_Rect enemyCharacterRects[CHARACTER_COUNT];
// SDL_Texture* characterTextures[CHARACTER_COUNT];
//
// int playerSelection = 0;
// int enemySelection = 0;
//
// // load textures
// for (int i = 0; i < CHARACTER_COUNT; ++i)
// {
// characterTextures[i] = IMG_LoadTexture(renderer, characterSprites.find(i)->second.first);
// }
//
// // set up initial positions for character rects
// for (int i = 0; i < CHARACTER_COUNT; ++i)
// {
// playerCharacterRects[i] = { 134 + (i % 2) * 118, 272 + ((i >= 2) ? 114 : 0), 64, 64 };
// enemyCharacterRects[i] = { 485 + (i % 2) * 118, 273 + ((i >= 2) ? 114 : 0), 64, 64 };
// }
//
// bool hasQuit = false;
//
// while (!hasQuit)
// {
// SDL_PollEvent(&event);
//
// if (event.type == SDL_QUIT)
// {
// hasQuit = true;
// }
//
// if (event.type == SDL_KEYDOWN)
// {
// if (event.key.keysym.scancode == SDL_SCANCODE_RETURN)
// {
// break;
// }
//
// switch (event.key.keysym.scancode)
// {
// case SDL_SCANCODE_A:
// playerSelection = (playerSelection - 1 + CHARACTER_COUNT) % CHARACTER_COUNT;
// break;
// case SDL_SCANCODE_D:
// playerSelection = (playerSelection + 1) % CHARACTER_COUNT;
// break;
//
// case SDL_SCANCODE_LEFT:
// enemySelection = (enemySelection - 1 + CHARACTER_COUNT) % CHARACTER_COUNT;
// break;
// case SDL_SCANCODE_RIGHT:
// enemySelection = (enemySelection + 1) % CHARACTER_COUNT;
// break;
//
// default:
// break;
// }
// }
//
// SDL_Texture* backgroundTexture = GameInternal::textureManager->loadTexture("assets/characterSelection.png");
// SDL_RenderClear(renderer);
// SDL_RenderCopy(renderer, backgroundTexture, NULL, NULL);
//
// for (int i = 0; i < CHARACTER_COUNT; ++i)
// {
// SDL_RenderCopy(renderer, characterTextures[i], nullptr, &playerCharacterRects[i]);
// SDL_RenderCopy(renderer, characterTextures[i], nullptr, &enemyCharacterRects[i]);
// }
//
// SDL_SetRenderDrawColor(renderer, 0, 255, 0, 255);
// SDL_RenderDrawRect(renderer, &playerCharacterRects[playerSelection]);
// SDL_RenderDrawRect(renderer, &enemyCharacterRects[enemySelection]);
//
// SDL_RenderPresent(renderer);
// }
//
// if (hasQuit)
// {
// this->setRunning(false);
// return;
// }
//
// playerSprite = characterSprites.find(playerSelection)->second.second;
// enemySprite = characterSprites.find(enemySelection)->second.second;
// this->setRunning(true);
//}
void GameInternal::handleEvents()
{
@ -333,21 +304,25 @@ bool GameInternal::isRunning() const
return running;
}
void GameInternal::setRunning(bool running)
void GameInternal::setRunning(bool running) //TODO: might be depracted
{
this->running = running;
}
void GameInternal::setWinner(Entity::TeamLabel winningTeam)
void GameInternal::stopGame()
{
this->winner = winningTeam;
this->setRunning(false);
}
Entity::TeamLabel GameInternal::getWinner() const
{
return this->winner;
this->running = false;
}
//void GameInternal::setWinner(Entity::TeamLabel winningTeam)
//{
// this->winner = winningTeam;
// this->setRunning(false);
//}
//
//Entity::TeamLabel GameInternal::getWinner() const
//{
// return this->winner;
//}
//void Game::refreshPlayers() {
//

View File

@ -25,12 +25,12 @@ void HealthComponent::setHealth(int health)
void HealthComponent::refreshHearts()
{
// clear hearts if exist
for (auto& heart : this->entity->getManager().getGroup((size_t) Entity::GroupLabel::HEARTS)) {
if (heart->getTeam() == this->entity->getTeam()) {
heart->destroy();
}
for (auto& heart : this->heartElements) {
heart->destroy();
}
this->heartElements.clear();
int x; //starting position for first health icon
if(side == Direction::LEFT) {
@ -59,5 +59,5 @@ void HealthComponent::createHeartComponents(int x)
heart.addComponent<TransformComponent>(x,5,2);
heart.addComponent<SpriteComponent>(this->healthTexture.data());
heart.addGroup((size_t)Entity::GroupLabel::HEARTS);
heart.setTeam(this->entity->getTeam());
this->heartElements.emplace_back(&heart);
}

View File

@ -24,16 +24,16 @@ void Manager::refresh()
}), std::end(v));
}
for (auto i(0u); i < MAX_TEAMS; i++)
{
auto& v(entitiesByTeam[i]);
v.erase(
std::remove_if(std::begin(v), std::end(v),
[i](Entity* mEntity)
{
return !mEntity->isActive() || (size_t)(mEntity->getTeam()) != i;
}), std::end(v));
}
//for (auto i(0u); i < MAX_TEAMS; i++)
//{
// auto& v(entitiesByTeam[i]);
// v.erase(
// std::remove_if(std::begin(v), std::end(v),
// [i](Entity* mEntity)
// {
// return !mEntity->isActive() || (size_t)(mEntity->getTeam()) != i;
// }), std::end(v));
//}
entities.erase(std::remove_if(std::begin(entities), std::end(entities),
[](const std::unique_ptr<Entity>& mEntity)
@ -58,15 +58,15 @@ std::vector<Entity*>& Manager::getGroup(Group mGroup)
return entitiesByGroup.at(mGroup);
}
void Manager::addToTeam(Entity* mEntity, Team mTeam)
{
entitiesByTeam.at(mTeam).emplace_back(mEntity); //
}
std::vector<Entity*>& Manager::getTeam(Team mTeam)
{
return entitiesByTeam.at(mTeam);
}
//void Manager::addToTeam(Entity* mEntity, Team mTeam)
//{
// entitiesByTeam.at(mTeam).emplace_back(mEntity); //
//}
//
//std::vector<Entity*>& Manager::getTeam(Team mTeam)
//{
// return entitiesByTeam.at(mTeam);
//}
std::vector<Entity*> Manager::getAll()
{

View File

@ -6,6 +6,8 @@
#include "TextureManager.h"
#include "GameInternal.h"
/*
PopupWindow::PopupWindow(const char* title, const std::string &message) :
continueGame(false), interacted(false) {
this->window = SDL_CreateWindow(title, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 400, 250, 0);
@ -83,3 +85,5 @@ void PopupWindow::renderWinnerPopup(Entity::TeamLabel winner) {
}
}
*/

View File

@ -18,9 +18,7 @@ void PowerupComponent::update()
if ((player = this->entity->getManager().getGame()->collisionHandler->getAnyIntersection<Entity*>(
entity,
Vector2D(0, 0),
{ Entity::GroupLabel::PLAYERS },
{},
true)) != nullptr)
{ Entity::GroupLabel::PLAYERS })) != nullptr)
{
(this->pickupFunc)(player);
this->entity->destroy();

View File

@ -36,8 +36,7 @@ void ProjectileComponent::update()
entity,
Vector2D(0,0),
{Entity::GroupLabel::PLAYERS},
{entity->getTeam()},
true)) != nullptr) {
{this->owner})) != nullptr) {
player->getComponent<HealthComponent>().modifyHealth();
this->entity->destroy();
}