-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Added an event system, first even kills entities on collision
- Loading branch information
1 parent
38ce4c6
commit f21b78c
Showing
11 changed files
with
168 additions
and
8 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
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,20 @@ | ||
#pragma once | ||
|
||
#include <entt.hpp> | ||
#include "../../../Event/Event.h" | ||
|
||
namespace Prune | ||
{ | ||
class CollisionEvent : public Event | ||
{ | ||
public: | ||
entt::entity a; | ||
entt::entity b; | ||
entt::registry& registry; | ||
|
||
CollisionEvent(entt::registry& registry, entt::entity a, entt::entity b) : registry(registry), a(a), b(b) {} | ||
|
||
private: | ||
|
||
}; | ||
} |
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
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,15 @@ | ||
#include "DamageSystem.h" | ||
#include "../../../Log/Log.h" | ||
|
||
void Prune::DamageSystem::SubscribeToEvents(std::unique_ptr<EventBus>& eventBus) | ||
{ | ||
eventBus->SubscribeToEvent<CollisionEvent>(this, &DamageSystem::OnCollision); | ||
} | ||
|
||
void Prune::DamageSystem::OnCollision(CollisionEvent& event) | ||
{ | ||
event.registry.destroy(event.a); | ||
event.registry.destroy(event.b); | ||
|
||
PRUNE_LOG_INFO("Called the OnCollision callback method and killed the entities"); | ||
} |
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,21 @@ | ||
#pragma once | ||
|
||
#include <entt.hpp> | ||
#include "../../../Event/EventBus.h" | ||
#include "../../Event/CollisionEvent.h" | ||
|
||
namespace Prune | ||
{ | ||
class DamageSystem | ||
{ | ||
public: | ||
DamageSystem() = default; | ||
~DamageSystem() = default; | ||
|
||
void SubscribeToEvents(std::unique_ptr<EventBus>& eventBus); | ||
|
||
void OnCollision(CollisionEvent& event); | ||
|
||
private: | ||
}; | ||
} |
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,10 @@ | ||
#pragma once | ||
|
||
namespace Prune | ||
{ | ||
class Event | ||
{ | ||
public: | ||
Event() = default; | ||
}; | ||
} |
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,86 @@ | ||
#pragma once | ||
|
||
#include <map> | ||
#include <typeindex> | ||
#include <functional> | ||
#include <list> | ||
#include "Event.h" | ||
|
||
namespace Prune | ||
{ | ||
class IEventCallback | ||
{ | ||
public: | ||
virtual ~IEventCallback() = default; | ||
|
||
void Execute(Event& e) { | ||
Call(e); | ||
} | ||
|
||
private: | ||
virtual void Call(Event& e) = 0; | ||
|
||
}; | ||
|
||
template <typename TOwner, typename TEvent> | ||
class EventCallback : public IEventCallback | ||
{ | ||
private: | ||
typedef void (TOwner::* CallbackFunction)(TEvent&); | ||
|
||
TOwner* ownerInstance; | ||
CallbackFunction callbackFunction; | ||
|
||
virtual void Call(Event& e) override { | ||
std::invoke(callbackFunction, ownerInstance, static_cast<TEvent&>(e)); | ||
} | ||
|
||
public: | ||
EventCallback(TOwner* ownerInstance, CallbackFunction callbackFunction) | ||
{ | ||
this->ownerInstance = ownerInstance; | ||
this->callbackFunction = callbackFunction; | ||
} | ||
|
||
virtual ~EventCallback() override = default; | ||
}; | ||
|
||
typedef std::list<std::unique_ptr<IEventCallback>> HandlerList; | ||
class EventBus | ||
{ | ||
public: | ||
EventBus() = default; | ||
~EventBus() = default; | ||
|
||
void Reset() | ||
{ | ||
subscribers.clear(); | ||
} | ||
|
||
template <typename TEvent, typename TOwner> | ||
void SubscribeToEvent(TOwner* ownerInstance, void (TOwner::* callbackFunction)(TEvent&)) | ||
{ | ||
if (!subscribers[typeid(TEvent)].get()) { | ||
subscribers[typeid(TEvent)] = std::make_unique<HandlerList>(); | ||
} | ||
auto subscriber = std::make_unique<EventCallback<TOwner, TEvent>>(ownerInstance, callbackFunction); | ||
subscribers[typeid(TEvent)]->push_back(std::move(subscriber)); | ||
} | ||
|
||
template <typename TEvent, typename ...TArgs> | ||
void EmitEvent(TArgs&& ...args) { | ||
auto handlers = subscribers[typeid(TEvent)].get(); | ||
if (handlers) | ||
{ | ||
for (auto it = handlers->begin(); it != handlers->end(); it++) | ||
{ | ||
auto handler = it->get(); | ||
TEvent event(std::forward<TArgs>(args)...); | ||
handler->Execute(event); | ||
} | ||
} | ||
} | ||
private: | ||
std::map<std::type_index, std::unique_ptr<HandlerList>> subscribers; | ||
}; | ||
} |
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