-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
115 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters