mirror of
https://github.com/Nimac0/SDL_Minigame
synced 2026-01-12 15:53:42 +00:00
refactor (working version)
This commit is contained in:
parent
091ffb4a33
commit
5d4512133c
@ -1,7 +1,8 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include <string>
|
|
||||||
#include "SDL.h"
|
#include "SDL.h"
|
||||||
#include "Components.h"
|
#include "Component.h"
|
||||||
|
|
||||||
|
class TransformComponent;
|
||||||
|
|
||||||
class ColliderComponent : public Component
|
class ColliderComponent : public Component
|
||||||
{
|
{
|
||||||
@ -10,27 +11,8 @@ public:
|
|||||||
const char* tag;
|
const char* tag;
|
||||||
TransformComponent* transform;
|
TransformComponent* transform;
|
||||||
|
|
||||||
ColliderComponent(const char* tag)
|
ColliderComponent(const char* tag);
|
||||||
{
|
|
||||||
this->tag = tag;
|
|
||||||
}
|
|
||||||
|
|
||||||
void init() override
|
void init() override;
|
||||||
{
|
void update() 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;
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
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
|
#pragma once
|
||||||
#include "ECS.h"
|
#include "ECS.h"
|
||||||
|
#include "Component.h"
|
||||||
|
#include "Manager.h"
|
||||||
|
#include "Entity.h"
|
||||||
#include "TransformComponent.h"
|
#include "TransformComponent.h"
|
||||||
#include "SpriteComponent.h"
|
#include "SpriteComponent.h"
|
||||||
#include "KeyboardController.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
|
#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 <algorithm>
|
||||||
#include <bitset>
|
#include <bitset>
|
||||||
#include <array>
|
#include <array>
|
||||||
|
#include "Constants.h"
|
||||||
|
|
||||||
class Component;
|
class Component;
|
||||||
class Entity;
|
class Entity;
|
||||||
@ -25,147 +26,3 @@ template <typename T> inline ComponentID getComponentTypeID() noexcept
|
|||||||
static ComponentID typeID = getNewComponentTypeID();
|
static ComponentID typeID = getNewComponentTypeID();
|
||||||
return typeID;
|
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 update();
|
||||||
void render();
|
void render();
|
||||||
void clean();
|
void clean();
|
||||||
bool running();
|
bool running() const;
|
||||||
|
|
||||||
static void addTile(int id, int x, int y);
|
static void addTile(int id, int x, int y);
|
||||||
static SDL_Renderer* renderer;
|
static SDL_Renderer* renderer;
|
||||||
@ -32,4 +32,3 @@ class Game
|
|||||||
bool isRunning = false;
|
bool isRunning = false;
|
||||||
SDL_Window* window;
|
SDL_Window* window;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@ -1,11 +1,11 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include "Game.h"
|
#include "SDL.h"
|
||||||
|
|
||||||
class GameObject
|
class GameObject
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
GameObject(const char* texturesheet, int x, int y);
|
GameObject(const char* texturesheet, int x, int y);
|
||||||
~GameObject();
|
~GameObject() = default;
|
||||||
|
|
||||||
void update();
|
void update();
|
||||||
void render();
|
void render();
|
||||||
@ -15,6 +15,7 @@ class GameObject
|
|||||||
int yPos;
|
int yPos;
|
||||||
|
|
||||||
SDL_Texture* objTexture;
|
SDL_Texture* objTexture;
|
||||||
SDL_Rect srcRect, destRect;
|
SDL_Rect srcRect;
|
||||||
|
SDL_Rect destRect;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@ -1,7 +1,8 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include "Game.h"
|
#include "SDL.h"
|
||||||
#include "ECS.h"
|
#include "Component.h"
|
||||||
#include "Components.h"
|
|
||||||
|
class TransformComponent;
|
||||||
|
|
||||||
class KeyboardController : public Component
|
class KeyboardController : public Component
|
||||||
{
|
{
|
||||||
@ -14,12 +15,10 @@ public:
|
|||||||
SDL_Scancode right;
|
SDL_Scancode right;
|
||||||
//SDL_Scancode action;
|
//SDL_Scancode action;
|
||||||
|
|
||||||
KeyboardController();
|
KeyboardController() = default;
|
||||||
KeyboardController(SDL_Scancode up, SDL_Scancode down, SDL_Scancode left, SDL_Scancode right/*, SDL_Scancode action*/);
|
KeyboardController(SDL_Scancode up, SDL_Scancode down, SDL_Scancode left, SDL_Scancode right/*, SDL_Scancode action*/);
|
||||||
|
~KeyboardController() = default;
|
||||||
~KeyboardController();
|
|
||||||
|
|
||||||
void init() override;
|
void init() override;
|
||||||
|
|
||||||
void update() 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
|
#pragma once
|
||||||
#include "Defines.h"
|
|
||||||
#include <string>
|
|
||||||
|
|
||||||
class Map
|
class Map
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Map();
|
Map() = default;
|
||||||
~Map();
|
~Map() = default;
|
||||||
|
|
||||||
static void loadMap(const char* path, int sizeX, int sizeY);
|
|
||||||
|
|
||||||
private:
|
|
||||||
|
|
||||||
|
|
||||||
|
static void loadMap(const char* path, int sizeX, int sizeY);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@ -1,54 +1,25 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include "Components.h"
|
|
||||||
#include "SDL.h"
|
#include "SDL.h"
|
||||||
#include "TextureManager.h"
|
#include "Component.h"
|
||||||
|
|
||||||
|
class TransformComponent;
|
||||||
|
|
||||||
class SpriteComponent : public Component
|
class SpriteComponent : public Component
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
SpriteComponent() = default;
|
SpriteComponent() = default;
|
||||||
SpriteComponent(const char* path)
|
SpriteComponent(const char* path);
|
||||||
{
|
~SpriteComponent();
|
||||||
setTexture(path);
|
|
||||||
}
|
|
||||||
|
|
||||||
~SpriteComponent()
|
void setTexture(const char* path);
|
||||||
{
|
|
||||||
SDL_DestroyTexture(this->texture);
|
|
||||||
}
|
|
||||||
|
|
||||||
void setTexture(const char* path)
|
void init() override;
|
||||||
{
|
void update() override;
|
||||||
this->texture = TextureManager::get().loadTexture(path);
|
void draw() override;
|
||||||
}
|
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
TransformComponent* transform;
|
TransformComponent* transform;
|
||||||
SDL_Texture* texture;
|
SDL_Texture* texture;
|
||||||
SDL_Rect srcRect, destRect;
|
SDL_Rect srcRect;
|
||||||
|
SDL_Rect destRect;
|
||||||
};
|
};
|
||||||
|
|||||||
@ -1,38 +1,37 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "Game.h"
|
|
||||||
#include "SDL_render.h"
|
#include "SDL_render.h"
|
||||||
#include <map>
|
#include <map>
|
||||||
|
|
||||||
struct cmp_str
|
struct cmp_str
|
||||||
{
|
{
|
||||||
bool operator()(char const *a, char const *b) const {
|
bool operator()(char const *a, char const *b) const {
|
||||||
return strcmp(a, b) < 0;
|
return strcmp(a, b) < 0;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
class TextureManager
|
class TextureManager
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
static TextureManager& get()
|
static TextureManager& get()
|
||||||
{
|
{
|
||||||
static TextureManager instance;
|
static TextureManager instance;
|
||||||
return instance;
|
return instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
TextureManager() {}
|
TextureManager() {}
|
||||||
~TextureManager() {
|
~TextureManager() {
|
||||||
for (auto& it : this->texture_cache) {
|
for (auto& it : this->texture_cache) {
|
||||||
SDL_DestroyTexture(it.second);
|
SDL_DestroyTexture(it.second);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
TextureManager(TextureManager const&) = delete;
|
TextureManager(TextureManager const&) = delete;
|
||||||
void operator=(TextureManager const&) = delete;
|
void operator=(TextureManager const&) = delete;
|
||||||
|
|
||||||
std::map<const char*, SDL_Texture*, cmp_str> texture_cache;
|
std::map<const char*, SDL_Texture*, cmp_str> texture_cache;
|
||||||
|
|
||||||
SDL_Texture* loadTexture(const char* fileName);
|
SDL_Texture* loadTexture(const char* fileName);
|
||||||
static void draw(SDL_Texture* texture, SDL_Rect src, SDL_Rect dest);
|
static void draw(SDL_Texture* texture, SDL_Rect src, SDL_Rect dest);
|
||||||
|
|||||||
@ -1,50 +1,25 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include "ECS.h"
|
|
||||||
#include "TransformComponent.h"
|
|
||||||
#include "SDL.h"
|
#include "SDL.h"
|
||||||
|
#include "Component.h"
|
||||||
#include "TextureDict.h"
|
#include "TextureDict.h"
|
||||||
|
|
||||||
|
class SpriteComponent;
|
||||||
|
class TransformComponent;
|
||||||
|
|
||||||
class TileComponent : public Component
|
class TileComponent : public Component
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
TransformComponent* transform;
|
TransformComponent* transform;
|
||||||
SpriteComponent* sprite;
|
SpriteComponent* sprite;
|
||||||
|
TextureDict textureDict;
|
||||||
|
|
||||||
SDL_Rect tileRect;
|
SDL_Rect tileRect;
|
||||||
int tileID;
|
int tileID;
|
||||||
const char* path;
|
const char* path;
|
||||||
|
|
||||||
TextureDict textureDict;
|
|
||||||
|
|
||||||
TileComponent() = default;
|
TileComponent() = default;
|
||||||
|
TileComponent(int x, int y, int w, int h, int id);
|
||||||
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() = default;
|
~TileComponent() = default;
|
||||||
|
|
||||||
void init() override
|
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>();
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
@ -1,6 +1,6 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include "Components.h"
|
|
||||||
#include "Vector2D.h"
|
#include "Vector2D.h"
|
||||||
|
#include "Component.h"
|
||||||
|
|
||||||
class TransformComponent : public Component
|
class TransformComponent : public Component
|
||||||
{
|
{
|
||||||
@ -15,50 +15,12 @@ public:
|
|||||||
|
|
||||||
int speed = 3;
|
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()
|
void init() override;
|
||||||
{
|
void update() override;
|
||||||
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;
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|||||||
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"
|
#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 "Game.h"
|
||||||
#include "TextureManager.h"
|
#include "TextureManager.h"
|
||||||
|
#include "Manager.h"
|
||||||
#include "Map.h"
|
#include "Map.h"
|
||||||
#include "ECS.h"
|
#include "Entity.h"
|
||||||
#include "Components.h"
|
#include "Component.h"
|
||||||
#include "Vector2D.h"
|
#include "TransformComponent.h"
|
||||||
|
#include "TileComponent.h"
|
||||||
|
#include "ColliderComponent.h"
|
||||||
|
#include "SpriteComponent.h"
|
||||||
|
#include "KeyboardController.h"
|
||||||
|
|
||||||
Map* map;
|
Map* map;
|
||||||
Manager manager;
|
Manager manager;
|
||||||
@ -19,44 +23,42 @@ auto& player(manager.addEntity());
|
|||||||
auto& enemy(manager.addEntity());
|
auto& enemy(manager.addEntity());
|
||||||
auto& wall(manager.addEntity());
|
auto& wall(manager.addEntity());
|
||||||
|
|
||||||
enum GroupLabel
|
enum class GroupLabel
|
||||||
{
|
{
|
||||||
GROUP_MAP,
|
MAP,
|
||||||
GROUP_PLAYERS,
|
PLAYERS,
|
||||||
GROUP_ENEMIES,
|
ENEMIES,
|
||||||
GROUP_COLLIDERS
|
COLLIDERS
|
||||||
};
|
};
|
||||||
|
|
||||||
Game::Game()
|
Game::Game() = default;
|
||||||
{
|
|
||||||
|
|
||||||
}
|
Game::~Game() = default;
|
||||||
|
|
||||||
Game::~Game()
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
void Game::init(const char* title, int xpos, int ypos, int width, int height, bool fullscreen)
|
void Game::init(const char* title, int xpos, int ypos, int width, int height, bool fullscreen)
|
||||||
{
|
{
|
||||||
int flags = 0;
|
int flags = 0;
|
||||||
if (fullscreen) {
|
if (fullscreen)
|
||||||
|
{
|
||||||
flags = SDL_WINDOW_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;
|
std::cout << "ERROR. Subsystem couldnt be initialized!" << std::endl;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
window = SDL_CreateWindow(title, xpos, ypos, width, height, flags);
|
window = SDL_CreateWindow(title, xpos, ypos, width, height, flags);
|
||||||
if (!window) {
|
if (!window)
|
||||||
|
{
|
||||||
std::cout << "ERROR: Window couldnt be created!" << std::endl;
|
std::cout << "ERROR: Window couldnt be created!" << std::endl;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
renderer = SDL_CreateRenderer(window, -1, 0);
|
renderer = SDL_CreateRenderer(window, -1, 0);
|
||||||
if (!renderer) {
|
if (!renderer)
|
||||||
|
{
|
||||||
std::cout << "ERROR: Renderer couldnt be created!" << std::endl;
|
std::cout << "ERROR: Renderer couldnt be created!" << std::endl;
|
||||||
return;
|
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<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<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.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<TransformComponent>(600, 500, 2);
|
||||||
enemy.addComponent<SpriteComponent>("assets/chicken_neutral.png");
|
enemy.addComponent<SpriteComponent>("assets/chicken_neutral.png");
|
||||||
enemy.addComponent<KeyboardController>(SDL_SCANCODE_UP, SDL_SCANCODE_DOWN, SDL_SCANCODE_LEFT, SDL_SCANCODE_RIGHT);
|
enemy.addComponent<KeyboardController>(SDL_SCANCODE_UP, SDL_SCANCODE_DOWN, SDL_SCANCODE_LEFT, SDL_SCANCODE_RIGHT);
|
||||||
enemy.addComponent<ColliderComponent>("enemy");
|
enemy.addComponent<ColliderComponent>("enemy");
|
||||||
enemy.addGroup(GROUP_ENEMIES);
|
enemy.addGroup((size_t)GroupLabel::ENEMIES);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -94,7 +96,6 @@ void Game::handleEvents()
|
|||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Game::update()
|
void Game::update()
|
||||||
@ -116,12 +117,11 @@ void Game::update()
|
|||||||
enemy.getComponent<TransformComponent>().position = enemyPos;
|
enemy.getComponent<TransformComponent>().position = enemyPos;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
auto& tiles(manager.getGroup(GROUP_MAP));
|
auto& tiles(manager.getGroup((size_t)GroupLabel::MAP));
|
||||||
auto& players(manager.getGroup(GROUP_PLAYERS));
|
auto& players(manager.getGroup((size_t)GroupLabel::PLAYERS));
|
||||||
auto& enemies(manager.getGroup(GROUP_ENEMIES));
|
auto& enemies(manager.getGroup((size_t)GroupLabel::ENEMIES));
|
||||||
|
|
||||||
void Game::render()
|
void Game::render()
|
||||||
{
|
{
|
||||||
@ -153,11 +153,14 @@ void Game::addTile(int id, int x, int y)
|
|||||||
{
|
{
|
||||||
auto& tile(manager.addEntity());
|
auto& tile(manager.addEntity());
|
||||||
tile.addComponent<TileComponent>(x, y, TILE_SIZE, TILE_SIZE, id);
|
tile.addComponent<TileComponent>(x, y, TILE_SIZE, TILE_SIZE, id);
|
||||||
if (id == 1) tile.addComponent<ColliderComponent>("water");
|
if (id == 1)
|
||||||
tile.addGroup(GROUP_MAP);
|
{
|
||||||
|
tile.addComponent<ColliderComponent>("water");
|
||||||
|
}
|
||||||
|
tile.addGroup((size_t)GroupLabel::MAP);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Game::running()
|
bool Game::running() const
|
||||||
{
|
{
|
||||||
return isRunning;
|
return isRunning;
|
||||||
}
|
}
|
||||||
@ -1,5 +1,6 @@
|
|||||||
#include "GameObject.h"
|
#include "GameObject.h"
|
||||||
#include "TextureManager.h"
|
#include "TextureManager.h"
|
||||||
|
#include "Game.h"
|
||||||
|
|
||||||
GameObject::GameObject(const char* texturesheet, int x, int y)
|
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;
|
this->yPos = y;
|
||||||
}
|
}
|
||||||
|
|
||||||
GameObject::~GameObject()
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
void GameObject::update()
|
void GameObject::update()
|
||||||
{
|
{
|
||||||
xPos++;
|
xPos++;
|
||||||
|
|||||||
@ -1,9 +1,6 @@
|
|||||||
#include "KeyboardController.h"
|
#include "KeyboardController.h"
|
||||||
|
#include "TransformComponent.h"
|
||||||
KeyboardController::KeyboardController()
|
#include "Entity.h"
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
KeyboardController::KeyboardController(SDL_Scancode up, SDL_Scancode down, SDL_Scancode left, SDL_Scancode right)
|
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;
|
this->right = right;
|
||||||
}
|
}
|
||||||
|
|
||||||
KeyboardController::~KeyboardController()
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
void KeyboardController::init()
|
void KeyboardController::init()
|
||||||
{
|
{
|
||||||
transform = &entity->getComponent<TransformComponent>();
|
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 "Map.h"
|
||||||
#include "Game.h"
|
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
|
#include "Constants.h"
|
||||||
Map::Map()
|
#include "Game.h"
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
Map::~Map()
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
void Map::loadMap(const char* path, int sizeX, int sizeY)
|
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 "TextureManager.h"
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
|
#include "Game.h"
|
||||||
|
|
||||||
SDL_Texture* TextureManager::loadTexture(const char* fileName)
|
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 "Game.h"
|
||||||
#include "Defines.h"
|
#include "Constants.h"
|
||||||
|
|
||||||
Game* game = nullptr;
|
Game* game = nullptr;
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user