mirror of
https://github.com/Nimac0/SDL_Minigame
synced 2026-01-12 13:43:41 +00:00
refactor (working version)
This commit is contained in:
parent
091ffb4a33
commit
5d4512133c
@ -1,7 +1,8 @@
|
||||
#pragma once
|
||||
#include <string>
|
||||
#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<TransformComponent>())
|
||||
{
|
||||
entity->addComponent<TransformComponent>();
|
||||
}
|
||||
transform = &entity->getComponent<TransformComponent>();
|
||||
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;
|
||||
};
|
||||
15
include/Component.h
Normal file
15
include/Component.h
Normal file
@ -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;
|
||||
};
|
||||
@ -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"
|
||||
|
||||
18
include/Constants.h
Normal file
18
include/Constants.h
Normal file
@ -0,0 +1,18 @@
|
||||
#pragma once
|
||||
#include <iostream>
|
||||
|
||||
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;
|
||||
|
||||
@ -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
|
||||
|
||||
|
||||
145
include/ECS.h
145
include/ECS.h
@ -6,6 +6,7 @@
|
||||
#include <algorithm>
|
||||
#include <bitset>
|
||||
#include <array>
|
||||
#include "Constants.h"
|
||||
|
||||
class Component;
|
||||
class Entity;
|
||||
@ -25,147 +26,3 @@ template <typename T> 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<maxComponents>;
|
||||
using GroupBitSet = std::bitset<maxGroups>;
|
||||
using ComponentArray = std::array<Component*, maxComponents>;
|
||||
|
||||
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 <typename T> bool hasComponent() const
|
||||
{
|
||||
return componentBitSet[getComponentTypeID<T>()];
|
||||
}
|
||||
|
||||
template <typename T, typename...TArgs>
|
||||
T& addComponent(TArgs&&...mArgs)
|
||||
{
|
||||
T* c(new T(std::forward<TArgs>(mArgs)...));
|
||||
c->entity = this;
|
||||
std::unique_ptr<Component> uPtr{ c };
|
||||
this->components.emplace_back(std::move(uPtr));
|
||||
|
||||
componentArray[getComponentTypeID<T>()] = c;
|
||||
componentBitSet[getComponentTypeID<T>()] = true;
|
||||
|
||||
c->init();
|
||||
return *c;
|
||||
};
|
||||
|
||||
template<typename T> T& getComponent() const
|
||||
{
|
||||
auto ptr(componentArray[getComponentTypeID<T>()]);
|
||||
return *static_cast<T*>(ptr);
|
||||
}
|
||||
|
||||
|
||||
private:
|
||||
Manager& manager;
|
||||
bool active = true;
|
||||
std::vector<std::unique_ptr<Component>> 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<Entity>& mEntity)
|
||||
{
|
||||
return !mEntity->isActive();
|
||||
}),
|
||||
std::end(entities));
|
||||
}
|
||||
|
||||
void addToGroup(Entity* mEntity, Group mGroup)
|
||||
{
|
||||
groupedEntities[mGroup].emplace_back(mEntity);
|
||||
}
|
||||
|
||||
std::vector<Entity*>& getGroup(Group mGroup)
|
||||
{
|
||||
return groupedEntities[mGroup];
|
||||
}
|
||||
|
||||
Entity& addEntity()
|
||||
{
|
||||
Entity* e = new Entity(*this);
|
||||
std::unique_ptr<Entity> uPtr{ e };
|
||||
entities.emplace_back(std::move(uPtr));
|
||||
return *e;
|
||||
}
|
||||
|
||||
private:
|
||||
std::vector<std::unique_ptr<Entity>> entities;
|
||||
std::array<std::vector<Entity*>, maxGroups> groupedEntities;
|
||||
};
|
||||
|
||||
65
include/Entity.h
Normal file
65
include/Entity.h
Normal file
@ -0,0 +1,65 @@
|
||||
#pragma once
|
||||
#include <array>
|
||||
#include <vector>
|
||||
#include <bitset>
|
||||
#include "Constants.h"
|
||||
#include "Component.h"
|
||||
#include "SpriteComponent.h"
|
||||
#include "ECS.h"
|
||||
|
||||
class Manager;
|
||||
//class Component;
|
||||
|
||||
using ComponentBitSet = std::bitset<MAX_COMPONENTS>;
|
||||
using GroupBitSet = std::bitset<MAX_GROUPS>;
|
||||
using ComponentArray = std::array<Component*, MAX_COMPONENTS>;
|
||||
|
||||
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 <typename T> bool hasComponent() const
|
||||
{
|
||||
return componentBitSet[getComponentTypeID<T>()];
|
||||
}
|
||||
|
||||
template <typename T, typename...TArgs> T& addComponent(TArgs&&...mArgs)
|
||||
{
|
||||
T* c(new T(std::forward<TArgs>(mArgs)...));
|
||||
c->entity = this;
|
||||
std::unique_ptr<Component> uPtr{ c };
|
||||
this->components.emplace_back(std::move(uPtr));
|
||||
|
||||
componentArray[getComponentTypeID<T>()] = c;
|
||||
componentBitSet[getComponentTypeID<T>()] = true;
|
||||
|
||||
c->init();
|
||||
return *c;
|
||||
};
|
||||
|
||||
template <typename T> T& getComponent() const
|
||||
{
|
||||
auto ptr(componentArray[getComponentTypeID<T>()]);
|
||||
return *static_cast<T*>(ptr);
|
||||
}
|
||||
|
||||
private:
|
||||
Manager& manager;
|
||||
bool active = true;
|
||||
std::vector<std::unique_ptr<Component>> components;
|
||||
|
||||
ComponentArray componentArray;
|
||||
ComponentBitSet componentBitSet;
|
||||
GroupBitSet groupBitSet;
|
||||
};
|
||||
@ -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;
|
||||
};
|
||||
|
||||
|
||||
@ -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;
|
||||
};
|
||||
|
||||
|
||||
@ -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;
|
||||
};
|
||||
|
||||
24
include/Manager.h
Normal file
24
include/Manager.h
Normal file
@ -0,0 +1,24 @@
|
||||
#pragma once
|
||||
#include <iostream>
|
||||
#include <array>
|
||||
#include <vector>
|
||||
#include "Constants.h"
|
||||
|
||||
class Entity;
|
||||
|
||||
class Manager
|
||||
{
|
||||
public:
|
||||
void update();
|
||||
void draw();
|
||||
void refresh();
|
||||
|
||||
void addToGroup(Entity* mEntity, Group mGroup);
|
||||
std::vector<Entity*>& getGroup(Group mGroup);
|
||||
|
||||
Entity& addEntity();
|
||||
|
||||
private:
|
||||
std::vector<std::unique_ptr<Entity>> entities;
|
||||
std::array<std::vector<Entity*>, MAX_GROUPS> groupedEntities;
|
||||
};
|
||||
@ -1,17 +1,10 @@
|
||||
#pragma once
|
||||
#include "Defines.h"
|
||||
#include <string>
|
||||
|
||||
class Map
|
||||
{
|
||||
public:
|
||||
Map();
|
||||
~Map();
|
||||
public:
|
||||
Map() = default;
|
||||
~Map() = default;
|
||||
|
||||
static void loadMap(const char* path, int sizeX, int sizeY);
|
||||
|
||||
private:
|
||||
|
||||
|
||||
};
|
||||
|
||||
|
||||
@ -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<TransformComponent>();
|
||||
|
||||
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;
|
||||
};
|
||||
|
||||
@ -1,6 +1,5 @@
|
||||
#pragma once
|
||||
|
||||
#include "Game.h"
|
||||
#include "SDL_render.h"
|
||||
#include <map>
|
||||
|
||||
|
||||
@ -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<TransformComponent>(this->tileRect.x, this->tileRect.y, this->tileRect.w, this->tileRect.h, 1);
|
||||
this->transform = &entity->getComponent<TransformComponent>();
|
||||
|
||||
this->entity->addComponent<SpriteComponent>(this->path);
|
||||
this->sprite = &entity->getComponent<SpriteComponent>();
|
||||
}
|
||||
void init() override;
|
||||
};
|
||||
@ -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;
|
||||
};
|
||||
|
||||
28
src/ColliderComponent.cpp
Normal file
28
src/ColliderComponent.cpp
Normal file
@ -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<TransformComponent>())
|
||||
{
|
||||
entity->addComponent<TransformComponent>();
|
||||
}
|
||||
transform = &entity->getComponent<TransformComponent>();
|
||||
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;
|
||||
}
|
||||
@ -1,7 +1,2 @@
|
||||
#include "ECS.h"
|
||||
|
||||
void Entity::addGroup(Group mGroup)
|
||||
{
|
||||
groupBitSet[mGroup] = true;
|
||||
manager.addToGroup(this, mGroup);
|
||||
}
|
||||
56
src/Entity.cpp
Normal file
56
src/Entity.cpp
Normal file
@ -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 <typename T> bool Entity::hasComponent() const
|
||||
//{
|
||||
// return componentBitSet[getComponentTypeID<T>()];
|
||||
//}
|
||||
|
||||
//template <typename T, typename...TArgs> T& Entity::addComponent(TArgs&&...mArgs)
|
||||
//{
|
||||
// T* c(new T(std::forward<TArgs>(mArgs)...));
|
||||
// c->entity = this;
|
||||
// std::unique_ptr<Component> uPtr{ c };
|
||||
// this->components.emplace_back(std::move(uPtr));
|
||||
//
|
||||
// componentArray[getComponentTypeID<T>()] = c;
|
||||
// componentBitSet[getComponentTypeID<T>()] = true;
|
||||
//
|
||||
// c->init();
|
||||
// return *c;
|
||||
//};
|
||||
|
||||
//template <typename T> T& Entity::getComponent() const
|
||||
//{
|
||||
// auto ptr(componentArray[getComponentTypeID<T>()]);
|
||||
// return *static_cast<T*>(ptr);
|
||||
//}
|
||||
65
src/Game.cpp
65
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<SpriteComponent>("assets/chicken_neutral_knight.png"); //adds sprite (32x32px), path needed
|
||||
player.addComponent<KeyboardController>(SDL_SCANCODE_W, SDL_SCANCODE_S, SDL_SCANCODE_A, SDL_SCANCODE_D);//custom keycontrols can be added
|
||||
player.addComponent<ColliderComponent>("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<TransformComponent>(600, 500, 2);
|
||||
enemy.addComponent<SpriteComponent>("assets/chicken_neutral.png");
|
||||
enemy.addComponent<KeyboardController>(SDL_SCANCODE_UP, SDL_SCANCODE_DOWN, SDL_SCANCODE_LEFT, SDL_SCANCODE_RIGHT);
|
||||
enemy.addComponent<ColliderComponent>("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<TransformComponent>().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<TileComponent>(x, y, TILE_SIZE, TILE_SIZE, id);
|
||||
if (id == 1) tile.addComponent<ColliderComponent>("water");
|
||||
tile.addGroup(GROUP_MAP);
|
||||
if (id == 1)
|
||||
{
|
||||
tile.addComponent<ColliderComponent>("water");
|
||||
}
|
||||
tile.addGroup((size_t)GroupLabel::MAP);
|
||||
}
|
||||
|
||||
bool Game::running()
|
||||
bool Game::running() const
|
||||
{
|
||||
return isRunning;
|
||||
}
|
||||
@ -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++;
|
||||
|
||||
@ -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<TransformComponent>();
|
||||
|
||||
51
src/Manager.cpp
Normal file
51
src/Manager.cpp
Normal file
@ -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<Entity>& mEntity)
|
||||
{
|
||||
return !mEntity->isActive();
|
||||
}),
|
||||
std::end(entities));
|
||||
}
|
||||
|
||||
void Manager::addToGroup(Entity* mEntity, Group mGroup)
|
||||
{
|
||||
groupedEntities[mGroup].emplace_back(mEntity);
|
||||
}
|
||||
|
||||
std::vector<Entity*>& Manager::getGroup(Group mGroup)
|
||||
{
|
||||
return groupedEntities[mGroup];
|
||||
}
|
||||
|
||||
Entity& Manager::addEntity()
|
||||
{
|
||||
Entity* e = new Entity(*this);
|
||||
std::unique_ptr<Entity> uPtr{ e };
|
||||
entities.emplace_back(std::move(uPtr));
|
||||
return *e;
|
||||
}
|
||||
12
src/Map.cpp
12
src/Map.cpp
@ -1,15 +1,7 @@
|
||||
#include "Map.h"
|
||||
#include "Game.h"
|
||||
#include <fstream>
|
||||
|
||||
Map::Map()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
Map::~Map()
|
||||
{
|
||||
}
|
||||
#include "Constants.h"
|
||||
#include "Game.h"
|
||||
|
||||
void Map::loadMap(const char* path, int sizeX, int sizeY)
|
||||
{
|
||||
|
||||
43
src/SpriteComponent.cpp
Normal file
43
src/SpriteComponent.cpp
Normal file
@ -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<TransformComponent>();
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
#include "TextureManager.h"
|
||||
#include <cstdio>
|
||||
#include "Game.h"
|
||||
|
||||
SDL_Texture* TextureManager::loadTexture(const char* fileName)
|
||||
{
|
||||
|
||||
32
src/TileComponent.cpp
Normal file
32
src/TileComponent.cpp
Normal file
@ -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<TransformComponent>(this->tileRect.x, this->tileRect.y, this->tileRect.w, this->tileRect.h, 1);
|
||||
this->transform = &entity->getComponent<TransformComponent>();
|
||||
|
||||
this->entity->addComponent<SpriteComponent>(this->path);
|
||||
this->sprite = &entity->getComponent<SpriteComponent>();
|
||||
}
|
||||
|
||||
47
src/TransformComponent.cpp
Normal file
47
src/TransformComponent.cpp
Normal file
@ -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;
|
||||
}
|
||||
@ -1,5 +1,5 @@
|
||||
#include "Game.h"
|
||||
#include "Defines.h"
|
||||
#include "Constants.h"
|
||||
|
||||
Game* game = nullptr;
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user