Skip to content

Commit

Permalink
Events
Browse files Browse the repository at this point in the history
- Added an event system, first even kills entities on collision
  • Loading branch information
deanblackborough committed Jun 1, 2022
1 parent 38ce4c6 commit f21b78c
Show file tree
Hide file tree
Showing 11 changed files with 168 additions and 8 deletions.
2 changes: 0 additions & 2 deletions Sandbox/src/Engine/Engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ void Prune::Engine::Up()
Engine::SDLInit();
Engine::SDLCreateWindow();
Engine::SDLCreateRenderer();

entt::registry m_Registry;
}

void Prune::Engine::Down()
Expand Down
4 changes: 1 addition & 3 deletions Sandbox/src/Engine/Engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ namespace Prune {

SDL_Window* m_Window = nullptr;
SDL_Renderer* m_Renderer = nullptr;

entt::registry m_Registry;


void SDLCreateRenderer();
void SDLCreateWindow();
void SDLInit();
Expand Down
20 changes: 20 additions & 0 deletions Sandbox/src/Engine/Event/CollisionEvent.h
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:

};
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#include "BoxCollider2DCollisionSystem.h"
#include "../../Component/TransformComponent.h"
#include "../../Component/BoxCollider2DComponent.h"
#include "../../Event/CollisionEvent.h"
#include "../../../Log/Log.h"

void Prune::BoxCollider2DCollisionSystem::Update(entt::registry& registry)
void Prune::BoxCollider2DCollisionSystem::Update(entt::registry& registry, std::unique_ptr<EventBus>& eventBus)
{
auto outer_view = registry.view<TransformComponent, BoxCollider2DComponent>();

Expand Down Expand Up @@ -36,6 +37,7 @@ void Prune::BoxCollider2DCollisionSystem::Update(entt::registry& registry)

if (collision)
{
eventBus->EmitEvent<CollisionEvent>(registry, inner_entity, outer_entity);
PRUNE_LOG_INFO("There has been a collision!!!");
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <entt.hpp>
#include <glm.hpp>
#include "../../../Event/EventBus.h"

namespace Prune
{
Expand All @@ -13,7 +14,7 @@ namespace Prune
BoxCollider2DCollisionSystem() = default;
~BoxCollider2DCollisionSystem() = default;

void Update(entt::registry& registry);
void Update(entt::registry& registry, std::unique_ptr<EventBus>& eventBus);

private:
bool checkForCollision(
Expand Down
15 changes: 15 additions & 0 deletions Sandbox/src/Engine/System/Update/DamageSystem.cpp
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");
}
21 changes: 21 additions & 0 deletions Sandbox/src/Engine/System/Update/DamageSystem.h
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:
};
}
10 changes: 10 additions & 0 deletions Sandbox/src/Event/Event.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#pragma once

namespace Prune
{
class Event
{
public:
Event() = default;
};
}
86 changes: 86 additions & 0 deletions Sandbox/src/Event/EventBus.h
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;
};
}
9 changes: 8 additions & 1 deletion Sandbox/src/Game/Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "../Engine/Component/VelocityComponent.h"
#include "../Engine/System/Update/AnimatedSpriteSystem.h"
#include "../Engine/System/Update/BoxCollider2DCollisionSystem.h"
#include "../Engine/System/Update/DamageSystem.h"
#include "../Engine/System/Update/MovementSystem.h"
#include "../Engine/System/Render/BoxCollider2DRenderSystem.h"
#include "../Engine/System/Render/SpriteRenderSystem.h"
Expand All @@ -15,6 +16,7 @@ Prune::Game::Game()
InitECS();

m_SpriteLibrary = SpriteLibrary();
m_EventBus = std::make_unique<EventBus>();
m_IsRunning = true;
}

Expand All @@ -38,6 +40,8 @@ void Prune::Game::Run()

double deltaTime = (frameStartTime - frameEndTime) / 1000.f;

m_EventBus->Reset();

RunSystems(deltaTime);

Render();
Expand Down Expand Up @@ -115,14 +119,17 @@ void Prune::Game::AddSpritesToLibrary()

void Prune::Game::RunSystems(double delta)
{
DamageSystem damageSystem = DamageSystem();
damageSystem.SubscribeToEvents(m_EventBus);

MovementSystem movementSystem = MovementSystem();
movementSystem.Update(m_Registry, delta);

AnimatedSpriteSystem animatedSpriteSystem = AnimatedSpriteSystem();
animatedSpriteSystem.Update(m_Registry);

BoxCollider2DCollisionSystem boxCollider2DCollisionSystem = BoxCollider2DCollisionSystem();
boxCollider2DCollisionSystem.Update(m_Registry);
boxCollider2DCollisionSystem.Update(m_Registry, m_EventBus);
}

void Prune::Game::RenderEntities()
Expand Down
2 changes: 2 additions & 0 deletions Sandbox/src/Game/Game.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <SDL.h>
#include <entt.hpp>
#include "../Event/EventBus.h"
#include "../Library/SpriteLibrary.h"

namespace Prune
Expand All @@ -19,6 +20,7 @@ namespace Prune
private:
SDL_Renderer* m_Renderer = nullptr;
entt::registry m_Registry;
std::unique_ptr<EventBus> m_EventBus;
SpriteLibrary m_SpriteLibrary;

bool m_ShowBoxColliders2D = false;
Expand Down

0 comments on commit f21b78c

Please sign in to comment.