Core/Misc: Added lambda support to EventProcessor

Closes #22909

(cherry picked from commit 813f693768)
This commit is contained in:
Shauren
2019-02-15 21:32:07 +01:00
parent ed5ba915f1
commit c14cf5a892
2 changed files with 37 additions and 18 deletions

View File

@@ -20,8 +20,9 @@
#include "Define.h"
#include "Duration.h"
#include <map>
#include "Random.h"
#include "advstd.h"
#include <map>
class EventProcessor;
@@ -69,6 +70,26 @@ class TC_COMMON_API BasicEvent
uint64 m_execTime; // planned time of next execution, filled by event handler
};
template<typename T>
class LambdaBasicEvent : public BasicEvent
{
public:
LambdaBasicEvent(T&& callback) : BasicEvent(), _callback(std::move(callback)) { }
bool Execute(uint64, uint32) override
{
_callback();
return true;
}
private:
T _callback;
};
template<typename T>
using is_lambda_event = std::enable_if_t<!advstd::is_base_of_v<BasicEvent, std::remove_pointer_t<advstd::remove_cvref_t<T>>>>;
class TC_COMMON_API EventProcessor
{
public:
@@ -78,8 +99,14 @@ class TC_COMMON_API EventProcessor
void Update(uint32 p_time);
void KillAllEvents(bool force);
void AddEvent(BasicEvent* event, uint64 e_time, bool set_addtime = true);
template<typename T>
is_lambda_event<T> AddEvent(T&& event, uint64 e_time, bool set_addtime = true) { AddEvent(new LambdaBasicEvent<T>(std::move(event)), e_time, set_addtime); }
void AddEventAtOffset(BasicEvent* event, Milliseconds offset) { AddEvent(event, CalculateTime(offset.count())); }
void AddEventAtOffset(BasicEvent* event, Milliseconds offset, Milliseconds offset2) { AddEvent(event, CalculateTime(urand(offset.count(), offset2.count()))); }
template<typename T>
is_lambda_event<T> AddEventAtOffset(T&& event, Milliseconds offset) { AddEventAtOffset(new LambdaBasicEvent<T>(std::move(event)), offset); }
template<typename T>
is_lambda_event<T> AddEventAtOffset(T&& event, Milliseconds offset, Milliseconds offset2) { AddEventAtOffset(new LambdaBasicEvent<T>(std::move(event)), offset, offset2); }
void ModifyEventTime(BasicEvent* event, uint64 newTime);
uint64 CalculateTime(uint64 t_offset) const { return m_time + t_offset; }
std::multimap<uint64, BasicEvent*> const& GetEvents() const { return m_events; }

View File

@@ -127,22 +127,6 @@ enum Phases
PHASE_OUTRO = 3
};
class GravityElapseKnockupEvent : public BasicEvent
{
public:
GravityElapseKnockupEvent(Unit* caster, uint32 difficultySpellId) : _caster(caster), _difficultySpellId(difficultySpellId) { }
bool Execute(uint64 /*time*/, uint32 /*diff*/) override
{
_caster->CastSpell(_caster, _difficultySpellId);
_caster->CastSpell(_caster, SPELL_GRAVITY_LAPSE_FLY);
return true;
}
private:
Unit* _caster;
uint32 _difficultySpellId;
};
struct boss_felblood_kaelthas : public BossAI
{
boss_felblood_kaelthas(Creature* creature) : BossAI(creature, DATA_KAELTHAS_SUNSTRIDER)
@@ -238,10 +222,18 @@ struct boss_felblood_kaelthas : public BossAI
switch (spell->Id)
{
case SPELL_GRAVITY_LAPSE_INITIAL:
{
DoCast(target, gravityLapseTeleportSpells[_gravityLapseTargetCount], true);
target->m_Events.AddEventAtOffset(new GravityElapseKnockupEvent(target, SPELL_GRAVITY_LAPSE_DAMAGE), 400ms);
uint32 gravityLapseDamageSpell = SPELL_GRAVITY_LAPSE_DAMAGE;
target->m_Events.AddEventAtOffset([target, gravityLapseDamageSpell]()
{
target->CastSpell(target, gravityLapseDamageSpell);
target->CastSpell(target, SPELL_GRAVITY_LAPSE_FLY);
}, 400ms);
_gravityLapseTargetCount++;
break;
}
case SPELL_CLEAR_FLIGHT:
target->RemoveAurasDueToSpell(SPELL_GRAVITY_LAPSE_FLY);
target->RemoveAurasDueToSpell(SPELL_GRAVITY_LAPSE_DAMAGE);