Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ecs #5

Merged
merged 4 commits into from
Nov 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,5 @@ build/
cmake-build*/
bin/
.idea/
.cache/
.config/
13 changes: 13 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
set(SOURCE_FILES
main.cpp
engine/systems/Systems.hpp
engine/systems/MovementSystem.hpp
engine/systems/MovementSystem.cpp
engine/Components.hpp
engine/Entity.hpp
engine/Entity.cpp
engine/GameContainer.hpp
engine/GameContainer.cpp
common/vec.hpp
)

if (WIN32)
Expand All @@ -9,6 +18,10 @@ else ()
endif ()

add_executable(${PROJECT_NAME} ${SOURCE_FILES})
target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_SOURCE_DIR}/include)
target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_SOURCE_DIR}/src/engine/systems)
target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_SOURCE_DIR}/src/engine)
target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_SOURCE_DIR}/src/common)

if (WIN32)
# TODO Add library linking under windows
Expand Down
102 changes: 102 additions & 0 deletions src/common/vec.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
//
// Created by Thomas Burgaud on 12/11/2018.
//

#pragma once

#include <cmath>

template<typename T>
class vec3d {
public:
vec3d() = default;
vec3d(T value1, T value2 , T value3) : x(value1),
y(value2),
z(value3)
{}
~vec3d() = default;
bool operator==(const vec3d &other) const {
return x == other.x && y == other.y && z == other.z;
}

bool operator!=(const vec3d &other) const {
return !(other == *this);
}

vec3d operator+(vec3d &v) {
return vec3d(x + v.x, y + v.y, z + v.z);
}

vec3d operator-(vec3d &v) {
return vec3d(x - v.x, y - v.y, z - v.z);
}

vec3d& operator+=(vec3d& v) {
x += v.x;
y += v.y;
z += v.z;
return *this;
}

vec3d& operator-=(vec3d& v) {
x -= v.x;
y -= v.y;
z -= v.z;
return *this;
}

T x;
T y;
T z;
};

template<typename T>
class vec2d {
public:
vec2d() = default;
vec2d(T value1, T value2) : x(value1), y(value2)
{}
~vec2d() = default;
bool operator==(const vec2d &other) const {
return x == other.x && y == other.y;
}

bool operator!=(const vec2d &other) const {
return !(other == *this);
}

vec2d operator+(const vec2d &v) {
return vec2d(x + v.x, y + v.y);
}

vec2d operator-(const vec2d &v) {
return vec2d(x - v.x, y - v.y);
}

vec2d &operator+=(const vec2d &v) {
x += v.x;
y += v.y;
return *this;
}

vec2d &operator-=(const vec2d &v) {
x -= v.x;
y -= v.y;
return *this;
}

T x;
T y;
};

template<class T>
vec2d<T> roundPos(const vec2d<float> &pos) {
return {static_cast<T>(std::lround(pos.x)),
static_cast<T>(std::lround(pos.y))};
}

template<class T>
vec2d<T> roundPos(float x, float y) {
return {static_cast<T>(std::lround(x)),
static_cast<T>(std::lround(y))};
}
51 changes: 51 additions & 0 deletions src/engine/Components.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
** EPITECH PROJECT, 2018
** rtype
** File description:
** rtype
*/

#pragma once

#include <memory>
#include <chrono>

namespace ecs {

enum comp_e : std::size_t {
COMP_ORIENTATION,
COMP_VELOCITY,
COMP_POSITION,
MAX_COMPONENTS
};

struct Component {

};

struct Velocity : public Component {
Velocity(float w, float v) : x(w), y(v)
{};
~Velocity() = default;
static comp_e const type = COMP_VELOCITY;
float x;
float y;
};

struct Orientation : public Component{
explicit Orientation(float ori) : orientation(ori)
{};
~Orientation() = default;
static comp_e const type = COMP_ORIENTATION;
float orientation;
};

struct Position : public Component {
Position(float d, float v) : x(d), y(v)
{};
~Position() = default;
static comp_e const type = COMP_POSITION;
float x;
float y;
};
}
10 changes: 10 additions & 0 deletions src/engine/Entity.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*
** EPITECH PROJECT, 2018
** cpp_indie_studio
** File description:
** Entity
*/

#include "Entity.hpp"

std::size_t ecs::Entity::nextId = 0;
70 changes: 70 additions & 0 deletions src/engine/Entity.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
** EPITECH PROJECT, 2018
** rtype
** File description:
** Entities
*/

#pragma once

#include <bitset>
#include <vector>
#include <array>
#include "Components.hpp"

namespace ecs {

using entityId = unsigned long;

class Entity {
public:
Entity() : bit{0}, id{nextId++}, componentArray{nullptr} {};
Entity(Entity &) = delete;
Entity &operator=(Entity &) = delete;
Entity(Entity &&) = default;
Entity &operator=(Entity &&) = default;

template<class T>
T &getComponent() {
static_assert(std::is_base_of<Component, T>(), "T is not a component");
if (!bit[T::type])
throw std::runtime_error("Entity has no T component");
return static_cast<T&>(*componentArray[T::type]);
}

template<class T>
const T &getComponent() const {
static_assert(std::is_base_of<Component, T>(), "T is not a component");
if (!bit[T::type])
throw std::runtime_error("Entity has no T component");
return static_cast<T&>(*componentArray[T::type]);
}

template<class T>
bool hasComponent() const {
static_assert(std::is_base_of<Component, T>(), "T is not a component");
return bit[T::type];
}

template <typename T, typename... Args>
void addComponent(Args&&... args) {
static_assert(std::is_base_of<Component, T>(), "T is not a component");
bit[T::type] = true;
componentArray[T::type] = std::unique_ptr<T>(new T{std::forward<Args>(args)...});
}

template<class T>
void removeComponent() {
static_assert(std::is_base_of<Component, T>(), "T is not a component");
bit[T::type] = false;
componentArray[T::type].reset(nullptr);
}

public:
std::bitset<MAX_COMPONENTS> bit;
std::size_t id;
private:
static std::size_t nextId;
std::array<std::unique_ptr<Component>, MAX_COMPONENTS> componentArray;
};
}
12 changes: 12 additions & 0 deletions src/engine/GameContainer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
** EPITECH PROJECT, 2017
** cpp_indie_studio
** File description:
** Created by armandmgt,
*/

#include "GameContainer.hpp"

void ids::GameContainer::start()
{
}
23 changes: 23 additions & 0 deletions src/engine/GameContainer.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
** EPITECH PROJECT, 2018
** cpp_indie_studio
** File description:
** GameContainer
*/

#pragma once

#include <stack>

namespace ids {
class GameContainer {
public:
GameContainer() = default;
GameContainer(GameContainer &) = delete;
GameContainer &operator=(GameContainer &) = delete;

void start();

private:
};
}
49 changes: 49 additions & 0 deletions src/engine/systems/MovementSystem.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
** EPITECH PROJECT, 2018
** cpp_indie_studio
** File description:
** MovementSystem
*/

#include <iostream>
#include "vec.hpp"
#include "MovementSystem.hpp"

namespace ecs {
MovementSystem::MovementSystem(entityVector allEntities)
: System(allEntities)
{
}

bool MovementSystem::_isValidPosition(float x, float y)
{
auto entities = getEntities(COMP_POSITION);

for (auto &entitie : entities) {
auto &posE = entitie->getComponent<Position>();
auto posP = roundPos<int>(x, y);
auto posRounded = roundPos<int>(posE.x, posE.y);
if (posP == posRounded)
return false;
}
return true;
}

void MovementSystem::update(double delta[[maybe_unused]]) {
auto entities = getEntities(COMP_POSITION, COMP_VELOCITY);

for (auto e : entities) {
auto &position = e->getComponent<Position>();
auto &velocity = e->getComponent<Velocity>();
if (velocity.x == 0.0f && velocity.y == 0.0f) {
continue;
}
if (_isValidPosition(position.x + velocity.x, position.y))
position.x += velocity.x;
if (_isValidPosition(position.x, position.y + velocity.y))
position.y += velocity.y;
velocity.x = 0;
velocity.y = 0;
}
}
};
21 changes: 21 additions & 0 deletions src/engine/systems/MovementSystem.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
** EPITECH PROJECT, 2018
** cpp_indie_studio
** File description:
** MovementSystem
*/

#pragma once

#include "Systems.hpp"

namespace ecs {
class MovementSystem : public System {
public:
MovementSystem(entityVector);

void update(double delta) override;
private:
bool _isValidPosition(float x, float y);
};
};
Loading