1
This commit is contained in:
parent
03e289906e
commit
f91828045e
@ -17,7 +17,16 @@ void Trigger::Init()
|
||||
|
||||
void Trigger::UnInit()
|
||||
{
|
||||
|
||||
for (auto& pair : listeners_hash_) {
|
||||
while (!list_empty(&pair.second)) {
|
||||
EventHandler* e = list_first_entry(&pair.second,
|
||||
EventHandler,
|
||||
entry);
|
||||
e->ptr->data = nullptr;
|
||||
list_del_init(&e->entry);
|
||||
delete e;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Trigger::TakeonWeapon(Weapon* old_weapon, Weapon* new_weapon)
|
||||
@ -371,14 +380,31 @@ void Trigger::RemoveBuffs(int cond, std::vector<int>& buffids)
|
||||
}
|
||||
}
|
||||
|
||||
std::weak_ptr<EventHandler> Trigger::AddListener(int event_id, std::function<void(const a8::XParams&)> cb)
|
||||
std::weak_ptr<EventHandlerPtr> Trigger::AddListener(int event_id, std::function<void(const a8::XParams&)> cb)
|
||||
{
|
||||
|
||||
auto itr = listeners_hash_.find(event_id);
|
||||
if (itr != listeners_hash_.end()) {
|
||||
listeners_hash_[event_id] = list_head();
|
||||
itr = listeners_hash_.find(event_id);
|
||||
INIT_LIST_HEAD(&itr->second);
|
||||
}
|
||||
EventHandler* p = new EventHandler();
|
||||
p->cb = cb;
|
||||
p->ptr = std::make_shared<EventHandlerPtr>();
|
||||
p->ptr->data = p;
|
||||
list_add_tail(&p->entry, &itr->second);
|
||||
return p->ptr;
|
||||
}
|
||||
|
||||
void Trigger::RemoveEventHandler(std::weak_ptr<EventHandler> handler)
|
||||
void Trigger::RemoveEventHandler(std::weak_ptr<EventHandlerPtr> handler)
|
||||
{
|
||||
|
||||
if (!handler.expired()) {
|
||||
auto p = handler.lock()->data;
|
||||
EventHandler* e = list_entry(&p->entry, EventHandler, entry);
|
||||
e->ptr->data = nullptr;
|
||||
list_del_init(&e->entry);
|
||||
delete e;
|
||||
}
|
||||
}
|
||||
|
||||
void Trigger::DispatchEvent(int event_id, const a8::XParams& param)
|
||||
|
@ -6,10 +6,16 @@ namespace MetaData
|
||||
struct Equip;
|
||||
};
|
||||
|
||||
struct EventHandlerPtr
|
||||
{
|
||||
struct EventHandler* data = nullptr;
|
||||
};
|
||||
|
||||
struct EventHandler
|
||||
{
|
||||
std::function<void(const a8::XParams&)> cb;
|
||||
list_head entry;
|
||||
std::shared_ptr<EventHandlerPtr> ptr;
|
||||
};
|
||||
|
||||
class Weapon;
|
||||
@ -34,8 +40,8 @@ public:
|
||||
void Die();
|
||||
void ActiveBuff(MetaData::Buff* buff_meta);
|
||||
void DeactiveBuff(MetaData::Buff* buff_meta);
|
||||
std::weak_ptr<EventHandler> AddListener(int event_id, std::function<void(const a8::XParams&)> cb);
|
||||
void RemoveEventHandler(std::weak_ptr<EventHandler> handler);
|
||||
std::weak_ptr<EventHandlerPtr> AddListener(int event_id, std::function<void(const a8::XParams&)> cb);
|
||||
void RemoveEventHandler(std::weak_ptr<EventHandlerPtr> handler_ptr);
|
||||
|
||||
private:
|
||||
void TraverseCondBuffs(int cond, std::function<void (Buff*, bool&)> func);
|
||||
|
Loading…
x
Reference in New Issue
Block a user