Skip to content

Commit

Permalink
Added behaviours
Browse files Browse the repository at this point in the history
  • Loading branch information
RiscadoA committed Feb 27, 2021
1 parent 3201dfd commit 1e4cd1e
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ set (SOURCE_FILES

"src/vpg/ecs/transform.hpp"
"src/vpg/ecs/transform.cpp"
"src/vpg/ecs/behaviour.hpp"
"src/vpg/ecs/behaviour.cpp"
)

add_executable(vpg ${SOURCE_FILES})
Expand Down
35 changes: 35 additions & 0 deletions src/vpg/ecs/behaviour.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include <vpg/ecs/behaviour.hpp>

using namespace vpg::ecs;

vpg::ecs::Behaviour::Behaviour() {
this->behaviour = nullptr;
}

Behaviour::Behaviour(Behaviour&& rhs) noexcept {
this->behaviour = rhs.behaviour;
rhs.behaviour = nullptr;
}

Behaviour::~Behaviour() {
if (this->behaviour != nullptr) {
delete this->behaviour;
}
}

void vpg::ecs::Behaviour::update(float dt) {
if (this->behaviour != nullptr) {
this->behaviour->update(dt);
}
}

BehaviourSystem::BehaviourSystem() {
this->signature.set(Coordinator::get_component_type<Behaviour>());
}

void BehaviourSystem::update(float dt) {
for (auto& entity : this->entities) {
auto& behaviour = *Coordinator::get_component<Behaviour>(entity);
behaviour.update(dt);
}
}
42 changes: 42 additions & 0 deletions src/vpg/ecs/behaviour.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#pragma once

#include <vpg/ecs/coordinator.hpp>

namespace vpg::ecs {
class IBehaviour {
public:
virtual ~IBehaviour() = default;

inline virtual void update(float dt) {}
};

class Behaviour {
public:
Behaviour();
template<typename T, typename ... TArgs>
static Behaviour create(Entity entity, TArgs... args);
Behaviour(Behaviour&& rhs) noexcept;
Behaviour(const Behaviour&) = delete;
~Behaviour();

void update(float dt);

private:
IBehaviour* behaviour;
};

template<typename T, typename ...TArgs>
inline static Behaviour Behaviour::create(Entity entity, TArgs ...args) {
static_assert(std::is_base_of<IBehaviour, T>::value);
Behaviour ret;
ret.behaviour = new T(entity, args...);
return std::move(ret);
}

class BehaviourSystem : public System {
public:
BehaviourSystem();

void update(float dt);
};
}
6 changes: 4 additions & 2 deletions src/vpg/ecs/component_array.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ namespace vpg::ecs {
abort();
}

this->components[this->count] = std::move(component);
this->components[this->count].~T();
new (&this->components[this->count]) T(std::move(component));
this->index_to_entity.emplace(this->count, entity);
this->entity_to_index.emplace(entity, this->count);
this->count += 1;
Expand All @@ -62,7 +63,8 @@ namespace vpg::ecs {
this->entity_to_index.erase(it);

this->count -= 1;
this->components[index] = std::move(this->components[this->count]);
this->components[index].~T();
new (&this->components[this->count]) T(std::move(this->components[this->count]));
auto last_entity = this->index_to_entity[this->count];
this->index_to_entity.erase(this->count);
this->entity_to_index[last_entity] = index;
Expand Down
32 changes: 32 additions & 0 deletions src/vpg/entry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include <vpg/data/model.hpp>

#include <vpg/ecs/coordinator.hpp>
#include <vpg/ecs/transform.hpp>
#include <vpg/ecs/behaviour.hpp>

#include <glm/glm.hpp>
#include <gl/glew.h>
Expand All @@ -14,6 +16,21 @@

using namespace vpg;

class MyBehaviour : public ecs::IBehaviour {
public:
MyBehaviour(ecs::Entity entity) {
std::cout << "Initialized MyBehaviour on entity " << entity << '\n';
}

~MyBehaviour() {
std::cout << "Destroyed MyBehaviour\n";
}

virtual void update(float dt) override {
std::cout << "Updated MyBehaviour with dt " << dt << '\n';
}
};

int main(int argc, char** argv) {
Config::load(argc, argv);

Expand Down Expand Up @@ -62,14 +79,29 @@ int main(int argc, char** argv) {
auto shader = data::Manager::load<data::Shader>("shader.mesh");
auto model = data::Manager::load<data::Model>("model.chr_knight");

// Init Entity-Component System
ecs::Coordinator::register_component<ecs::Transform>();
ecs::Coordinator::register_component<ecs::Behaviour>();
auto behaviour_sys = ecs::Coordinator::register_system<ecs::BehaviourSystem>();

auto last_time = (float)glfwGetTime();
auto delta_time = 0.0f;
while (!glfwWindowShouldClose(window)) {
glfwPollEvents();

// Update behaviours
behaviour_sys->update(delta_time);

// Render here
lighting->get_shader().bind();
glDrawArrays(GL_TRIANGLES, 0, 6);

glfwSwapBuffers(window);

// Calculate delta time
auto new_time = (float)glfwGetTime();
delta_time = new_time - last_time;
last_time = new_time;
}

// Unload assets
Expand Down

0 comments on commit 1e4cd1e

Please sign in to comment.