mirror of
https://github.com/araxiaonline/mod-mythic-plus.git
synced 2026-06-13 03:02:24 -04:00
Adding event system for override necessary to balance higher levels
This commit is contained in:
81
src/CreatureHooks.cpp
Normal file
81
src/CreatureHooks.cpp
Normal file
@@ -0,0 +1,81 @@
|
||||
#include <functional>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
#include <memory>
|
||||
#include "Creature.h"
|
||||
|
||||
|
||||
class CreatureHooks {
|
||||
private:
|
||||
// Define type aliases for cleaner code
|
||||
using EventCallback = std::function<void(Creature*)>;
|
||||
using EventList = std::vector<EventCallback>;
|
||||
using EventMap = std::unordered_map<uint32, EventList>;
|
||||
|
||||
// Constructor
|
||||
CreatureHooks() = default;
|
||||
|
||||
// Use the type aliases for readability
|
||||
std::unique_ptr<EventMap> _spawnedEvents = std::make_unique<EventMap>();
|
||||
std::unique_ptr<EventMap> _isDeadEvents = std::make_unique<EventMap>();
|
||||
std::unique_ptr<std::unordered_map<uint32, std::unordered_map<uint8, EventList>>> _healthPercentEvents
|
||||
= std::make_unique<std::unordered_map<uint32, std::unordered_map<uint8, EventList>>>();
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
0
src/MpEventHandler.cpp
Normal file
0
src/MpEventHandler.cpp
Normal file
24
src/MpEventHandler.h
Normal file
24
src/MpEventHandler.h
Normal file
@@ -0,0 +1,24 @@
|
||||
#ifndef MP_EVENT_HANDLER_H
|
||||
#define MP_EVENT_HANDLER_H
|
||||
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
|
||||
// Template class for handling different function signatures
|
||||
template<typename... Args>
|
||||
class MpEventHandler
|
||||
{
|
||||
public:
|
||||
using EventCallback = std::function<void(Args...)>;
|
||||
|
||||
// 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
|
||||
Reference in New Issue
Block a user