feat: add attackers handling
This commit is contained in:
@@ -6,6 +6,7 @@
|
||||
#include "Domain/Events/HeroCreatedEvent.h"
|
||||
#include "Domain/Events/HeroDeletedEvent.h"
|
||||
#include "Domain/Events/CreatureDiedEvent.h"
|
||||
#include "Domain/Events/AttackedEvent.h"
|
||||
#include "../GameStructs/NetworkHandlerWrapper.h"
|
||||
#include "Domain/Services/ServiceLocator.h"
|
||||
|
||||
@@ -32,6 +33,16 @@ namespace Interlude
|
||||
else
|
||||
{
|
||||
m_Factory.Update(m_Hero, hero);
|
||||
const auto attackers = std::map<uint32_t, uint32_t>(m_Hero->GetAttackerIds());
|
||||
for (const auto kvp : attackers)
|
||||
{
|
||||
const auto attacker = m_NetworkHandler.GetUser(kvp.first);
|
||||
// try to remove creature out of sight from the attackers
|
||||
if (attacker == nullptr)
|
||||
{
|
||||
m_Hero->RemoveAttacker(kvp.first);
|
||||
}
|
||||
}
|
||||
}
|
||||
result[hero->objectId] = m_Hero;
|
||||
}
|
||||
@@ -53,16 +64,53 @@ namespace Interlude
|
||||
void Init() override
|
||||
{
|
||||
Services::ServiceLocator::GetInstance().GetEventDispatcher()->Subscribe(Events::CreatureDiedEvent::name, [this](const Events::Event& evt) {
|
||||
std::unique_lock<std::shared_timed_mutex>(m_Mutex);
|
||||
if (evt.GetName() == Events::CreatureDiedEvent::name)
|
||||
OnCreatureDied(evt);
|
||||
});
|
||||
Services::ServiceLocator::GetInstance().GetEventDispatcher()->Subscribe(Events::AttackedEvent::name, [this](const Events::Event& evt) {
|
||||
OnAttacked(evt);
|
||||
});
|
||||
}
|
||||
|
||||
void OnCreatureDied(const Events::Event& evt)
|
||||
{
|
||||
std::shared_lock<std::shared_timed_mutex>(m_Mutex);
|
||||
if (evt.GetName() == Events::CreatureDiedEvent::name)
|
||||
{
|
||||
const auto casted = static_cast<const Events::CreatureDiedEvent&>(evt);
|
||||
if (m_Hero)
|
||||
{
|
||||
const auto casted = static_cast<const Events::CreatureDiedEvent&>(evt);
|
||||
if (m_Hero && m_Hero->GetId() == casted.GetCreatureId())
|
||||
if (m_Hero->GetId() == casted.GetCreatureId())
|
||||
{
|
||||
Services::ServiceLocator::GetInstance().GetLogger()->App(L"{} died", m_Hero->GetFullName().GetNickname());
|
||||
}
|
||||
else
|
||||
{
|
||||
// try to remove dead creature from the attackers
|
||||
m_Hero->RemoveAttacker(casted.GetCreatureId());
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
void OnAttacked(const Events::Event& evt)
|
||||
{
|
||||
std::shared_lock<std::shared_timed_mutex>(m_Mutex);
|
||||
if (evt.GetName() == Events::AttackedEvent::name)
|
||||
{
|
||||
const auto casted = static_cast<const Events::AttackedEvent&>(evt);
|
||||
if (m_Hero && m_Hero->GetId() != casted.GetAttackerId())
|
||||
{
|
||||
if (m_Hero->GetId() == casted.GetTargetId())
|
||||
{
|
||||
m_Hero->AddAttacker(casted.GetAttackerId());
|
||||
}
|
||||
else
|
||||
{
|
||||
// try to remove creature that is attacking another target from the attackers
|
||||
m_Hero->RemoveAttacker(casted.GetAttackerId());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
HeroRepository(const NetworkHandlerWrapper& networkHandler, const HeroFactory& factory) :
|
||||
|
Reference in New Issue
Block a user