yupplemayham/YuppleMayham/utility/events.h
Ethan Adams 7e7dfd97b7 Removed UNIX (..) from headers
Cleaned up the weapon class
moved mousestate.h out of gameplay into utility
2024-06-22 00:08:59 -04:00

71 lines
No EOL
1.8 KiB
C++

#ifndef _H_EVENTS_H
#define _H_EVENTS_H
#include <string>
#include <memory>
#include <functional>
#include <unordered_map>
#include "utility/direction.h"
class Bullet;
struct PhysicsComponent;
class Event {
public:
virtual ~Event() { };
virtual std::string getType() const = 0;
};
class BulletFiredEvent : public Event {
public:
BulletFiredEvent(const std::shared_ptr<Bullet>& bullet) : bullet(bullet) {};
std::string getType() const override { return "OnBulletFired"; }
std::shared_ptr<Bullet> bullet;
};
class BulletDiedEvent : public Event {
public:
BulletDiedEvent(const std::shared_ptr<PhysicsComponent>& physObj) : physObj(physObj) {};
std::string getType() const override { return "OnBulletDied"; }
std::shared_ptr<PhysicsComponent> physObj;
};
class BulletCollideEvent : public Event {
public:
BulletCollideEvent(const unsigned int ownerID, const unsigned int victimID) : ownerID(ownerID), victimID(victimID) {}
std::string getType() const override { return "OnBulletCollide"; }
unsigned int ownerID;
unsigned int victimID;
};
class DirectionChangeEvent : public Event {
public:
DirectionChangeEvent(const int actorid, Direction direction) : actorid(actorid), direction(direction) {}
std::string getType() const override { return "OnDirectionChange"; }
Direction direction;
int actorid;
};
class EventManager {
public:
using EventCallback = std::function<void(std::shared_ptr<Event>)>;
void subscribe(std::string eventType, EventCallback callback) {
listeners[eventType].push_back(callback);
}
void notify(const std::shared_ptr<Event>& event) {
auto iterator = listeners.find(event->getType());
if (iterator != listeners.end())
{
for (auto& callback : iterator->second)
{
callback(event);
}
}
}
private:
std::unordered_map <std::string, std::vector <EventCallback>> listeners;
};
#endif //_H_EVENTS_H