From 5d4512133cb422d0670e4cf6ba675e9ab807c93b Mon Sep 17 00:00:00 2001 From: sara Date: Tue, 23 Jan 2024 18:50:21 +0100 Subject: [PATCH] refactor (working version) --- include/ColliderComponent.h | 30 ++------ include/Component.h | 15 ++++ include/Components.h | 3 + include/Constants.h | 18 +++++ include/Defines.h | 10 --- include/ECS.h | 145 +---------------------------------- include/Entity.h | 65 ++++++++++++++++ include/Game.h | 3 +- include/GameObject.h | 7 +- include/KeyboardController.h | 13 ++-- include/Manager.h | 24 ++++++ include/Map.h | 15 +--- include/SpriteComponent.h | 51 +++--------- include/TextureManager.h | 43 +++++------ include/TileComponent.h | 39 ++-------- include/TransformComponent.h | 54 ++----------- src/ColliderComponent.cpp | 28 +++++++ src/ECS.cpp | 5 -- src/Entity.cpp | 56 ++++++++++++++ src/Game.cpp | 67 ++++++++-------- src/GameObject.cpp | 6 +- src/KeyboardController.cpp | 12 +-- src/Manager.cpp | 51 ++++++++++++ src/Map.cpp | 12 +-- src/SpriteComponent.cpp | 43 +++++++++++ src/TextureManager.cpp | 1 + src/TileComponent.cpp | 32 ++++++++ src/TransformComponent.cpp | 47 ++++++++++++ src/main.cpp | 2 +- 29 files changed, 493 insertions(+), 404 deletions(-) create mode 100644 include/Component.h create mode 100644 include/Constants.h create mode 100644 include/Entity.h create mode 100644 include/Manager.h create mode 100644 src/ColliderComponent.cpp create mode 100644 src/Entity.cpp create mode 100644 src/Manager.cpp create mode 100644 src/SpriteComponent.cpp create mode 100644 src/TileComponent.cpp create mode 100644 src/TransformComponent.cpp diff --git a/include/ColliderComponent.h b/include/ColliderComponent.h index 2e24cf0..b4674f3 100644 --- a/include/ColliderComponent.h +++ b/include/ColliderComponent.h @@ -1,7 +1,8 @@ #pragma once -#include #include "SDL.h" -#include "Components.h" +#include "Component.h" + +class TransformComponent; class ColliderComponent : public Component { @@ -10,27 +11,8 @@ public: const char* tag; TransformComponent* transform; - ColliderComponent(const char* tag) - { - this->tag = tag; - } + ColliderComponent(const char* tag); - void init() override - { - if (!entity->hasComponent()) - { - entity->addComponent(); - } - transform = &entity->getComponent(); - Game::colliders.push_back(this); - } - - void update() override - { - collider.x = transform->position.x; - collider.y = transform->position.y; - - collider.w = transform->width * transform->scale; - collider.h = transform->height * transform->scale; - } + void init() override; + void update() override; }; \ No newline at end of file diff --git a/include/Component.h b/include/Component.h new file mode 100644 index 0000000..095b370 --- /dev/null +++ b/include/Component.h @@ -0,0 +1,15 @@ +#pragma once + +class Entity; + +class Component +{ +public: + Entity* entity; + + virtual void init() {} + virtual void update() {} + virtual void draw() {} + + virtual ~Component() = default; +}; \ No newline at end of file diff --git a/include/Components.h b/include/Components.h index 607d792..4c6c63b 100644 --- a/include/Components.h +++ b/include/Components.h @@ -1,5 +1,8 @@ #pragma once #include "ECS.h" +#include "Component.h" +#include "Manager.h" +#include "Entity.h" #include "TransformComponent.h" #include "SpriteComponent.h" #include "KeyboardController.h" diff --git a/include/Constants.h b/include/Constants.h new file mode 100644 index 0000000..1e8720f --- /dev/null +++ b/include/Constants.h @@ -0,0 +1,18 @@ +#pragma once +#include + +using Group = std::size_t; + +constexpr std::size_t MAX_COMPONENTS = 32; +constexpr std::size_t MAX_GROUPS = 32; + +constexpr int SCREEN_SIZE_HEIGHT = 640; +constexpr int SCREEN_SIZE_WIDTH = 800; + +constexpr int FPS = 60; + +constexpr int TILE_SIZE = 32; + +constexpr int MAP_SIZE_X = 25; +constexpr int MAP_SIZE_Y = 20; + diff --git a/include/Defines.h b/include/Defines.h index da72770..3f59c93 100644 --- a/include/Defines.h +++ b/include/Defines.h @@ -1,12 +1,2 @@ #pragma once -#define SCREEN_SIZE_HEIGHT 640 -#define SCREEN_SIZE_WIDTH 800 - -#define FPS 60 - -#define TILE_SIZE 32 - -#define MAP_SIZE_X 25 -#define MAP_SIZE_Y 20 - diff --git a/include/ECS.h b/include/ECS.h index 9b50830..86cff1d 100644 --- a/include/ECS.h +++ b/include/ECS.h @@ -6,6 +6,7 @@ #include #include #include +#include "Constants.h" class Component; class Entity; @@ -25,147 +26,3 @@ template inline ComponentID getComponentTypeID() noexcept static ComponentID typeID = getNewComponentTypeID(); return typeID; } - -constexpr std::size_t maxComponents = 32; -constexpr std::size_t maxGroups = 32; - -using ComponentBitSet = std::bitset; -using GroupBitSet = std::bitset; -using ComponentArray = std::array; - -class Component -{ - public: - Entity* entity; - - virtual void init() {} - virtual void update() {} - virtual void draw() {} - - virtual ~Component() {} -}; - -class Entity -{ - public: - Entity(Manager& mManager) : manager(mManager) - { - - } - void update() - { - for (auto& c : components) c->update(); - } - - void draw() - { - for (auto& c : components) c->draw(); - } - - bool isActive() const { return this->active; } - void destroy() { this->active = false; } - - bool hasGroup(Group mGroup) - { - return groupBitSet[mGroup]; - } - void addGroup(Group mGroup); - void delGroup(Group mGroup) - { - groupBitSet[mGroup] = false; - } - - - template bool hasComponent() const - { - return componentBitSet[getComponentTypeID()]; - } - - template - T& addComponent(TArgs&&...mArgs) - { - T* c(new T(std::forward(mArgs)...)); - c->entity = this; - std::unique_ptr uPtr{ c }; - this->components.emplace_back(std::move(uPtr)); - - componentArray[getComponentTypeID()] = c; - componentBitSet[getComponentTypeID()] = true; - - c->init(); - return *c; - }; - - template T& getComponent() const - { - auto ptr(componentArray[getComponentTypeID()]); - return *static_cast(ptr); - } - - - private: - Manager& manager; - bool active = true; - std::vector> components; - - ComponentArray componentArray; - ComponentBitSet componentBitSet; - GroupBitSet groupBitSet; -}; - -class Manager -{ - public: - void update() - { - for (auto& e : entities) e->update(); - } - - void draw() - { - for (auto& e : entities) e->draw(); - } - - void refresh() - { - for (auto i(0u); i < maxGroups; i++) - { - auto& v(groupedEntities[i]); - v.erase( - std::remove_if(std::begin(v), std::end(v), - [i](Entity* mEntity) - { - return !mEntity->isActive() || !mEntity->hasGroup(i); - }),std::end(v)); - } - - entities.erase(std::remove_if(std::begin(entities), std::end(entities), - [](const std::unique_ptr& mEntity) - { - return !mEntity->isActive(); - }), - std::end(entities)); - } - - void addToGroup(Entity* mEntity, Group mGroup) - { - groupedEntities[mGroup].emplace_back(mEntity); - } - - std::vector& getGroup(Group mGroup) - { - return groupedEntities[mGroup]; - } - - Entity& addEntity() - { - Entity* e = new Entity(*this); - std::unique_ptr uPtr{ e }; - entities.emplace_back(std::move(uPtr)); - return *e; - } - - private: - std::vector> entities; - std::array, maxGroups> groupedEntities; -}; diff --git a/include/Entity.h b/include/Entity.h new file mode 100644 index 0000000..a85a80f --- /dev/null +++ b/include/Entity.h @@ -0,0 +1,65 @@ +#pragma once +#include +#include +#include +#include "Constants.h" +#include "Component.h" +#include "SpriteComponent.h" +#include "ECS.h" + +class Manager; +//class Component; + +using ComponentBitSet = std::bitset; +using GroupBitSet = std::bitset; +using ComponentArray = std::array; + +class Entity +{ +public: + explicit Entity(Manager& mManager) : manager(mManager) { } + + void update() const; + void draw() const; + + bool isActive() const { return this->active; } + void destroy() { this->active = false; } + + bool hasGroup(Group mGroup); + void addGroup(Group mGroup); + void delGroup(Group mGroup); + + template bool hasComponent() const + { + return componentBitSet[getComponentTypeID()]; + } + + template T& addComponent(TArgs&&...mArgs) + { + T* c(new T(std::forward(mArgs)...)); + c->entity = this; + std::unique_ptr uPtr{ c }; + this->components.emplace_back(std::move(uPtr)); + + componentArray[getComponentTypeID()] = c; + componentBitSet[getComponentTypeID()] = true; + + c->init(); + return *c; + }; + + template T& getComponent() const + { + auto ptr(componentArray[getComponentTypeID()]); + return *static_cast(ptr); + } + +private: + Manager& manager; + bool active = true; + std::vector> components; + + ComponentArray componentArray; + ComponentBitSet componentBitSet; + GroupBitSet groupBitSet; +}; \ No newline at end of file diff --git a/include/Game.h b/include/Game.h index 838f66a..870fe2d 100644 --- a/include/Game.h +++ b/include/Game.h @@ -20,7 +20,7 @@ class Game void update(); void render(); void clean(); - bool running(); + bool running() const; static void addTile(int id, int x, int y); static SDL_Renderer* renderer; @@ -32,4 +32,3 @@ class Game bool isRunning = false; SDL_Window* window; }; - diff --git a/include/GameObject.h b/include/GameObject.h index d8093d5..ca8d779 100644 --- a/include/GameObject.h +++ b/include/GameObject.h @@ -1,11 +1,11 @@ #pragma once -#include "Game.h" +#include "SDL.h" class GameObject { public: GameObject(const char* texturesheet, int x, int y); - ~GameObject(); + ~GameObject() = default; void update(); void render(); @@ -15,6 +15,7 @@ class GameObject int yPos; SDL_Texture* objTexture; - SDL_Rect srcRect, destRect; + SDL_Rect srcRect; + SDL_Rect destRect; }; diff --git a/include/KeyboardController.h b/include/KeyboardController.h index dbd75ff..e6d0c30 100644 --- a/include/KeyboardController.h +++ b/include/KeyboardController.h @@ -1,7 +1,8 @@ #pragma once -#include "Game.h" -#include "ECS.h" -#include "Components.h" +#include "SDL.h" +#include "Component.h" + +class TransformComponent; class KeyboardController : public Component { @@ -14,12 +15,10 @@ public: SDL_Scancode right; //SDL_Scancode action; - KeyboardController(); + KeyboardController() = default; KeyboardController(SDL_Scancode up, SDL_Scancode down, SDL_Scancode left, SDL_Scancode right/*, SDL_Scancode action*/); - - ~KeyboardController(); + ~KeyboardController() = default; void init() override; - void update() override; }; diff --git a/include/Manager.h b/include/Manager.h new file mode 100644 index 0000000..c5db8f5 --- /dev/null +++ b/include/Manager.h @@ -0,0 +1,24 @@ +#pragma once +#include +#include +#include +#include "Constants.h" + +class Entity; + +class Manager +{ +public: + void update(); + void draw(); + void refresh(); + + void addToGroup(Entity* mEntity, Group mGroup); + std::vector& getGroup(Group mGroup); + + Entity& addEntity(); + +private: + std::vector> entities; + std::array, MAX_GROUPS> groupedEntities; +}; \ No newline at end of file diff --git a/include/Map.h b/include/Map.h index ee85c71..3ab48e6 100644 --- a/include/Map.h +++ b/include/Map.h @@ -1,17 +1,10 @@ #pragma once -#include "Defines.h" -#include class Map { - public: - Map(); - ~Map(); - - static void loadMap(const char* path, int sizeX, int sizeY); - - private: - +public: + Map() = default; + ~Map() = default; + static void loadMap(const char* path, int sizeX, int sizeY); }; - diff --git a/include/SpriteComponent.h b/include/SpriteComponent.h index 9a5667c..3d10a34 100644 --- a/include/SpriteComponent.h +++ b/include/SpriteComponent.h @@ -1,54 +1,25 @@ #pragma once -#include "Components.h" #include "SDL.h" -#include "TextureManager.h" +#include "Component.h" + +class TransformComponent; class SpriteComponent : public Component { public: SpriteComponent() = default; - SpriteComponent(const char* path) - { - setTexture(path); - } + SpriteComponent(const char* path); + ~SpriteComponent(); - ~SpriteComponent() - { - SDL_DestroyTexture(this->texture); - } + void setTexture(const char* path); - void setTexture(const char* path) - { - this->texture = TextureManager::get().loadTexture(path); - } - - void init() override - { - this->transform = &entity->getComponent(); - - this->srcRect.x = this->srcRect.y = 0; - this->srcRect.w = transform->width; - this->srcRect.h = transform->height; -; - - } - - void update() override - { - this->destRect.x = this->transform->position.x; - this->destRect.y = this->transform->position.y; - this->destRect.w = transform->width * transform->scale; - this->destRect.h = transform->height * transform->scale; - } - - void draw() override - { - TextureManager::get().draw(this->texture, this->srcRect, this->destRect); - } + void init() override; + void update() override; + void draw() override; private: TransformComponent* transform; SDL_Texture* texture; - SDL_Rect srcRect, destRect; - + SDL_Rect srcRect; + SDL_Rect destRect; }; diff --git a/include/TextureManager.h b/include/TextureManager.h index 85e6bbb..a90c103 100644 --- a/include/TextureManager.h +++ b/include/TextureManager.h @@ -1,38 +1,37 @@ #pragma once -#include "Game.h" #include "SDL_render.h" #include struct cmp_str { - bool operator()(char const *a, char const *b) const { - return strcmp(a, b) < 0; - } + bool operator()(char const *a, char const *b) const { + return strcmp(a, b) < 0; + } }; class TextureManager { - public: - static TextureManager& get() - { - static TextureManager instance; - return instance; - } - - private: - TextureManager() {} - ~TextureManager() { - for (auto& it : this->texture_cache) { - SDL_DestroyTexture(it.second); - } - } + public: + static TextureManager& get() + { + static TextureManager instance; + return instance; + } + + private: + TextureManager() {} + ~TextureManager() { + for (auto& it : this->texture_cache) { + SDL_DestroyTexture(it.second); + } + } - public: - TextureManager(TextureManager const&) = delete; - void operator=(TextureManager const&) = delete; + public: + TextureManager(TextureManager const&) = delete; + void operator=(TextureManager const&) = delete; - std::map texture_cache; + std::map texture_cache; SDL_Texture* loadTexture(const char* fileName); static void draw(SDL_Texture* texture, SDL_Rect src, SDL_Rect dest); diff --git a/include/TileComponent.h b/include/TileComponent.h index d8be964..8f570cd 100644 --- a/include/TileComponent.h +++ b/include/TileComponent.h @@ -1,50 +1,25 @@ #pragma once -#include "ECS.h" -#include "TransformComponent.h" #include "SDL.h" +#include "Component.h" #include "TextureDict.h" +class SpriteComponent; +class TransformComponent; + class TileComponent : public Component { public: TransformComponent* transform; SpriteComponent* sprite; + TextureDict textureDict; SDL_Rect tileRect; int tileID; const char* path; - TextureDict textureDict; - TileComponent() = default; - - TileComponent(int x, int y, int w, int h, int id) - { - this->tileRect.x = x; - this->tileRect.y = y; - this->tileRect.w = w; - this->tileRect.h = h; - tileID = id; - - auto it = textureDict.textureDictionary.find(tileID); //every id has its own distinct texture (in texturedict.h) - if (it == textureDict.textureDictionary.end()) - { - std::cout << "it end" << std::endl; - return; - } - bool test = it == textureDict.textureDictionary.end(); - std::cout << it->second.data() << std::endl; - this->path = it->second.data(); - } - + TileComponent(int x, int y, int w, int h, int id); ~TileComponent() = default; - void init() override - { - this->entity->addComponent(this->tileRect.x, this->tileRect.y, this->tileRect.w, this->tileRect.h, 1); - this->transform = &entity->getComponent(); - - this->entity->addComponent(this->path); - this->sprite = &entity->getComponent(); - } + void init() override; }; \ No newline at end of file diff --git a/include/TransformComponent.h b/include/TransformComponent.h index 1c33aed..9404277 100644 --- a/include/TransformComponent.h +++ b/include/TransformComponent.h @@ -1,6 +1,6 @@ #pragma once -#include "Components.h" #include "Vector2D.h" +#include "Component.h" class TransformComponent : public Component { @@ -15,50 +15,12 @@ public: int speed = 3; + TransformComponent(); + TransformComponent(int scale); + TransformComponent(float x, float y); + TransformComponent(float x, float y, int scale); + TransformComponent(float x, float y, int w, int h, int scale); - TransformComponent() - { - position.zero(); - } - - TransformComponent(int scale) - { - position.zero(); - this->scale = scale; - } - - TransformComponent(float x, float y) - { - this->position.x = x; - this->position.y = y; - } - - TransformComponent(float x, float y, int scale) - { - this->position.x = x; - this->position.y = y; - this->scale = scale; - } - - TransformComponent(float x, float y, int w, int h, int scale) - { - this->position.x = x; - this->position.y = y; - this->width = w; - this->height = h; - this->scale = scale; - } - - void init() override - { - velocity.zero(); - } - - void update() override - { - // if(velocity.x != 0 && velocity.y != 0) - double multiplier = velocity.x != 0 && velocity.y != 0 ? 0.707 : 1; //normalizes vector - position.x += velocity.x * speed * multiplier; - position.y += velocity.y * speed * multiplier; - } + void init() override; + void update() override; }; diff --git a/src/ColliderComponent.cpp b/src/ColliderComponent.cpp new file mode 100644 index 0000000..f708978 --- /dev/null +++ b/src/ColliderComponent.cpp @@ -0,0 +1,28 @@ +#include "ColliderComponent.h" +#include "TransformComponent.h" +#include "Entity.h" +#include "Game.h" + +ColliderComponent::ColliderComponent(const char* tag) +{ + this->tag = tag; +} + +void ColliderComponent::init() +{ + if (!entity->hasComponent()) + { + entity->addComponent(); + } + transform = &entity->getComponent(); + Game::colliders.push_back(this); +} + +void ColliderComponent::update() +{ + collider.x = transform->position.x; + collider.y = transform->position.y; + + collider.w = transform->width * transform->scale; + collider.h = transform->height * transform->scale; +} \ No newline at end of file diff --git a/src/ECS.cpp b/src/ECS.cpp index 58484d7..c41b4eb 100644 --- a/src/ECS.cpp +++ b/src/ECS.cpp @@ -1,7 +1,2 @@ #include "ECS.h" -void Entity::addGroup(Group mGroup) -{ - groupBitSet[mGroup] = true; - manager.addToGroup(this, mGroup); -} \ No newline at end of file diff --git a/src/Entity.cpp b/src/Entity.cpp new file mode 100644 index 0000000..799445e --- /dev/null +++ b/src/Entity.cpp @@ -0,0 +1,56 @@ +#pragma once +#include "Entity.h" +#include "Manager.h" +#include "Component.h" +//#include "ECS.h" + +void Entity::update() const +{ + for (auto const& c : components) c->update(); +} + +void Entity::draw() const +{ + for (auto const& c : components) c->draw(); +} + +bool Entity::hasGroup(Group mGroup) +{ + return groupBitSet[mGroup]; +} + +void Entity::addGroup(Group mGroup) +{ + groupBitSet[mGroup] = true; + manager.addToGroup(this, mGroup); +} + +void Entity::delGroup(Group mGroup) +{ + groupBitSet[mGroup] = false; +} + +//template bool Entity::hasComponent() const +//{ +// return componentBitSet[getComponentTypeID()]; +//} + +//template T& Entity::addComponent(TArgs&&...mArgs) +//{ +// T* c(new T(std::forward(mArgs)...)); +// c->entity = this; +// std::unique_ptr uPtr{ c }; +// this->components.emplace_back(std::move(uPtr)); +// +// componentArray[getComponentTypeID()] = c; +// componentBitSet[getComponentTypeID()] = true; +// +// c->init(); +// return *c; +//}; + +//template T& Entity::getComponent() const +//{ +// auto ptr(componentArray[getComponentTypeID()]); +// return *static_cast(ptr); +//} \ No newline at end of file diff --git a/src/Game.cpp b/src/Game.cpp index 622f521..aeb3894 100644 --- a/src/Game.cpp +++ b/src/Game.cpp @@ -1,10 +1,14 @@ #include "Game.h" #include "TextureManager.h" +#include "Manager.h" #include "Map.h" -#include "ECS.h" -#include "Components.h" -#include "Vector2D.h" - +#include "Entity.h" +#include "Component.h" +#include "TransformComponent.h" +#include "TileComponent.h" +#include "ColliderComponent.h" +#include "SpriteComponent.h" +#include "KeyboardController.h" Map* map; Manager manager; @@ -19,44 +23,42 @@ auto& player(manager.addEntity()); auto& enemy(manager.addEntity()); auto& wall(manager.addEntity()); -enum GroupLabel +enum class GroupLabel { - GROUP_MAP, - GROUP_PLAYERS, - GROUP_ENEMIES, - GROUP_COLLIDERS + MAP, + PLAYERS, + ENEMIES, + COLLIDERS }; -Game::Game() -{ +Game::Game() = default; -} - -Game::~Game() -{ - -} +Game::~Game() = default; void Game::init(const char* title, int xpos, int ypos, int width, int height, bool fullscreen) { int flags = 0; - if (fullscreen) { + if (fullscreen) + { flags = SDL_WINDOW_FULLSCREEN; } - if (SDL_Init(SDL_INIT_EVERYTHING) != 0) { + if (SDL_Init(SDL_INIT_EVERYTHING) != 0) + { std::cout << "ERROR. Subsystem couldnt be initialized!" << std::endl; return; } window = SDL_CreateWindow(title, xpos, ypos, width, height, flags); - if (!window) { + if (!window) + { std::cout << "ERROR: Window couldnt be created!" << std::endl; return; } renderer = SDL_CreateRenderer(window, -1, 0); - if (!renderer) { + if (!renderer) + { std::cout << "ERROR: Renderer couldnt be created!" << std::endl; return; } @@ -72,13 +74,13 @@ void Game::init(const char* title, int xpos, int ypos, int width, int height, bo player.addComponent("assets/chicken_neutral_knight.png"); //adds sprite (32x32px), path needed player.addComponent(SDL_SCANCODE_W, SDL_SCANCODE_S, SDL_SCANCODE_A, SDL_SCANCODE_D);//custom keycontrols can be added player.addComponent("player"); //adds tag (for further use, reference tag) - player.addGroup(GROUP_PLAYERS); //tell programm what group it belongs to for rendering order + player.addGroup((size_t)GroupLabel::PLAYERS); //tell programm what group it belongs to for rendering order enemy.addComponent(600, 500, 2); enemy.addComponent("assets/chicken_neutral.png"); enemy.addComponent(SDL_SCANCODE_UP, SDL_SCANCODE_DOWN, SDL_SCANCODE_LEFT, SDL_SCANCODE_RIGHT); enemy.addComponent("enemy"); - enemy.addGroup(GROUP_ENEMIES); + enemy.addGroup((size_t)GroupLabel::ENEMIES); } @@ -94,7 +96,6 @@ void Game::handleEvents() default: break; } - } void Game::update() @@ -116,12 +117,11 @@ void Game::update() enemy.getComponent().position = enemyPos; } } - } -auto& tiles(manager.getGroup(GROUP_MAP)); -auto& players(manager.getGroup(GROUP_PLAYERS)); -auto& enemies(manager.getGroup(GROUP_ENEMIES)); +auto& tiles(manager.getGroup((size_t)GroupLabel::MAP)); +auto& players(manager.getGroup((size_t)GroupLabel::PLAYERS)); +auto& enemies(manager.getGroup((size_t)GroupLabel::ENEMIES)); void Game::render() { @@ -153,11 +153,14 @@ void Game::addTile(int id, int x, int y) { auto& tile(manager.addEntity()); tile.addComponent(x, y, TILE_SIZE, TILE_SIZE, id); - if (id == 1) tile.addComponent("water"); - tile.addGroup(GROUP_MAP); + if (id == 1) + { + tile.addComponent("water"); + } + tile.addGroup((size_t)GroupLabel::MAP); } -bool Game::running() +bool Game::running() const { return isRunning; -} \ No newline at end of file +} diff --git a/src/GameObject.cpp b/src/GameObject.cpp index 8d61ac7..4a5f59c 100644 --- a/src/GameObject.cpp +++ b/src/GameObject.cpp @@ -1,5 +1,6 @@ #include "GameObject.h" #include "TextureManager.h" +#include "Game.h" GameObject::GameObject(const char* texturesheet, int x, int y) { @@ -8,11 +9,6 @@ GameObject::GameObject(const char* texturesheet, int x, int y) this->yPos = y; } -GameObject::~GameObject() -{ - -} - void GameObject::update() { xPos++; diff --git a/src/KeyboardController.cpp b/src/KeyboardController.cpp index 1d71e02..d6d4a79 100644 --- a/src/KeyboardController.cpp +++ b/src/KeyboardController.cpp @@ -1,9 +1,6 @@ #include "KeyboardController.h" - -KeyboardController::KeyboardController() -{ - -} +#include "TransformComponent.h" +#include "Entity.h" KeyboardController::KeyboardController(SDL_Scancode up, SDL_Scancode down, SDL_Scancode left, SDL_Scancode right) { @@ -13,11 +10,6 @@ KeyboardController::KeyboardController(SDL_Scancode up, SDL_Scancode down, SDL_S this->right = right; } -KeyboardController::~KeyboardController() -{ - -} - void KeyboardController::init() { transform = &entity->getComponent(); diff --git a/src/Manager.cpp b/src/Manager.cpp new file mode 100644 index 0000000..f247a7f --- /dev/null +++ b/src/Manager.cpp @@ -0,0 +1,51 @@ +#include "Manager.h" +#include "Entity.h" + +void Manager::update() +{ + for (auto& e : entities) e->update(); +} + +void Manager::draw() +{ + for (auto& e : entities) e->draw(); +} + +void Manager::refresh() +{ + for (auto i(0u); i < MAX_GROUPS; i++) + { + auto& v(groupedEntities[i]); + v.erase( + std::remove_if(std::begin(v), std::end(v), + [i](Entity* mEntity) + { + return !mEntity->isActive() || !mEntity->hasGroup(i); + }), std::end(v)); + } + + entities.erase(std::remove_if(std::begin(entities), std::end(entities), + [](const std::unique_ptr& mEntity) + { + return !mEntity->isActive(); + }), + std::end(entities)); +} + +void Manager::addToGroup(Entity* mEntity, Group mGroup) +{ + groupedEntities[mGroup].emplace_back(mEntity); +} + +std::vector& Manager::getGroup(Group mGroup) +{ + return groupedEntities[mGroup]; +} + +Entity& Manager::addEntity() +{ + Entity* e = new Entity(*this); + std::unique_ptr uPtr{ e }; + entities.emplace_back(std::move(uPtr)); + return *e; +} diff --git a/src/Map.cpp b/src/Map.cpp index 5b728cd..0c8327c 100644 --- a/src/Map.cpp +++ b/src/Map.cpp @@ -1,15 +1,7 @@ #include "Map.h" -#include "Game.h" #include - -Map::Map() -{ - -} - -Map::~Map() -{ -} +#include "Constants.h" +#include "Game.h" void Map::loadMap(const char* path, int sizeX, int sizeY) { diff --git a/src/SpriteComponent.cpp b/src/SpriteComponent.cpp new file mode 100644 index 0000000..a81b6c4 --- /dev/null +++ b/src/SpriteComponent.cpp @@ -0,0 +1,43 @@ +#include "TransformComponent.h" +#include "SpriteComponent.h" +#include "TextureManager.h" +#include "Entity.h" + +SpriteComponent::SpriteComponent(const char* path) +{ + setTexture(path); +} + +SpriteComponent::~SpriteComponent() +{ + SDL_DestroyTexture(this->texture); +} + +void SpriteComponent::setTexture(const char* path) +{ + this->texture = TextureManager::get().loadTexture(path); +} + +void SpriteComponent::init() +{ + this->transform = &entity->getComponent(); + + this->srcRect.x = this->srcRect.y = 0; + this->srcRect.w = transform->width; + this->srcRect.h = transform->height; +} + +void SpriteComponent::update() +{ + this->destRect.x = this->transform->position.x; + this->destRect.y = this->transform->position.y; + this->destRect.w = transform->width * transform->scale; + this->destRect.h = transform->height * transform->scale; +} + +void SpriteComponent::draw() +{ + TextureManager::get().draw(this->texture, this->srcRect, this->destRect); +} + + diff --git a/src/TextureManager.cpp b/src/TextureManager.cpp index a3e0d85..ae597e0 100644 --- a/src/TextureManager.cpp +++ b/src/TextureManager.cpp @@ -1,5 +1,6 @@ #include "TextureManager.h" #include +#include "Game.h" SDL_Texture* TextureManager::loadTexture(const char* fileName) { diff --git a/src/TileComponent.cpp b/src/TileComponent.cpp new file mode 100644 index 0000000..fc4b83e --- /dev/null +++ b/src/TileComponent.cpp @@ -0,0 +1,32 @@ +#include "TileComponent.h" +#include "Entity.h" +#include "TransformComponent.h" + +TileComponent::TileComponent(int x, int y, int w, int h, int id) +{ + this->tileRect.x = x; + this->tileRect.y = y; + this->tileRect.w = w; + this->tileRect.h = h; + tileID = id; + + auto it = textureDict.textureDictionary.find(tileID); //every id has its own distinct texture (in texturedict.h) + if (it == textureDict.textureDictionary.end()) + { + std::cout << "it end" << std::endl; + return; + } + bool test = it == textureDict.textureDictionary.end(); + std::cout << it->second.data() << std::endl; + this->path = it->second.data(); +} + +void TileComponent::init() +{ + this->entity->addComponent(this->tileRect.x, this->tileRect.y, this->tileRect.w, this->tileRect.h, 1); + this->transform = &entity->getComponent(); + + this->entity->addComponent(this->path); + this->sprite = &entity->getComponent(); +} + diff --git a/src/TransformComponent.cpp b/src/TransformComponent.cpp new file mode 100644 index 0000000..1f71846 --- /dev/null +++ b/src/TransformComponent.cpp @@ -0,0 +1,47 @@ +#include "TransformComponent.h" + +TransformComponent::TransformComponent() +{ + position.zero(); +} + +TransformComponent::TransformComponent(int scale) +{ + position.zero(); + this->scale = scale; +} + +TransformComponent::TransformComponent(float x, float y) +{ + this->position.x = x; + this->position.y = y; +} + +TransformComponent::TransformComponent(float x, float y, int scale) +{ + this->position.x = x; + this->position.y = y; + this->scale = scale; +} + +TransformComponent::TransformComponent(float x, float y, int w, int h, int scale) +{ + this->position.x = x; + this->position.y = y; + this->width = w; + this->height = h; + this->scale = scale; +} + +void TransformComponent::init() +{ + velocity.zero(); +} + +void TransformComponent::update() +{ + // if(velocity.x != 0 && velocity.y != 0) + double multiplier = velocity.x != 0 && velocity.y != 0 ? 0.707 : 1; //normalizes vector + position.x += velocity.x * speed * multiplier; + position.y += velocity.y * speed * multiplier; +} diff --git a/src/main.cpp b/src/main.cpp index 05c2f04..c8ee4e3 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,5 +1,5 @@ #include "Game.h" -#include "Defines.h" +#include "Constants.h" Game* game = nullptr;