diff --git a/src/CreatureHooks.cpp b/src/CreatureHooks.cpp new file mode 100644 index 0000000..89549b1 --- /dev/null +++ b/src/CreatureHooks.cpp @@ -0,0 +1,81 @@ +#include +#include +#include +#include +#include "Creature.h" + + +class CreatureHooks { +private: + // Define type aliases for cleaner code + using EventCallback = std::function; + using EventList = std::vector; + using EventMap = std::unordered_map; + + // Constructor + CreatureHooks() = default; + + // Use the type aliases for readability + std::unique_ptr _spawnedEvents = std::make_unique(); + std::unique_ptr _isDeadEvents = std::make_unique(); + std::unique_ptr>> _healthPercentEvents + = std::make_unique>>(); + +public: + // Singleton instance access method + static CreatureHooks* instance() { + static CreatureHooks instance; + return &instance; + } + + // Register events for health at a certain percentage + void RegisterHealthAtPercentHook(uint32 entry, uint8 percent, EventCallback callback) { + (*_healthPercentEvents)[entry][percent].push_back(callback); + } + + // Register "Is Dead" events + void RegisterIsDeadHook(uint32 entry, EventCallback callback) { + (*_isDeadEvents)[entry].push_back(callback); + } + + // Register "Spawned" events + void RegisterSpawnedHook(uint32 entry, EventCallback callback) { + (*_spawnedEvents)[entry].push_back(callback); + } + + // Call health events if the creature's health is at or below the percentage + void CheckHealthEvents(Creature* creature) { + uint32 entry = creature->GetEntry(); + uint32 currentHealthPct = creature->GetHealthPct(); + + if (_healthPercentEvents->contains(entry)) { + for (const auto& [percent, callbacks] : _healthPercentEvents->at(entry)) { + if (currentHealthPct <= percent) { + for (auto& callback : callbacks) { + callback(creature); // Trigger custom behavior + } + } + } + } + } + + // Call "Is Dead" events if the creature is dead + void CheckIsDeadEvent(Creature* creature) { + uint32 entry = creature->GetEntry(); + if (!creature->IsAlive() && _isDeadEvents->contains(entry)) { + for (auto& callback : _isDeadEvents->at(entry)) { + callback(creature); // Trigger custom behavior + } + } + } + + // Call "Spawned" events when the creature spawns + void CheckSpawnedEvent(Creature* creature) { + uint32 entry = creature->GetEntry(); + if (_spawnedEvents->contains(entry)) { + for (auto& callback : _spawnedEvents->at(entry)) { + callback(creature); // Trigger custom behavior + } + } + } +}; diff --git a/src/MpEventHandler.cpp b/src/MpEventHandler.cpp new file mode 100644 index 0000000..e69de29 diff --git a/src/MpEventHandler.h b/src/MpEventHandler.h new file mode 100644 index 0000000..0852349 --- /dev/null +++ b/src/MpEventHandler.h @@ -0,0 +1,24 @@ +#ifndef MP_EVENT_HANDLER_H +#define MP_EVENT_HANDLER_H + +#include +#include + +// Template class for handling different function signatures +template +class MpEventHandler +{ +public: + using EventCallback = std::function; + + // Constructor to initialize with a callback function + EventCallback(CallbackFunction func); + + // Method to call the stored function with arguments + void Call(Args... args); + +private: + CallbackFunction _func; +}; + +#endif // MP_EVENT_HANDLER_H