DarkflameServer/dGame/dBehaviors/StunBehavior.cpp

106 lines
2.5 KiB
C++
Raw Normal View History

2022-08-06 03:01:59 +00:00
#include "StunBehavior.h"
#include "BaseCombatAIComponent.h"
#include "BehaviorBranchContext.h"
#include "BehaviorContext.h"
#include "EntityManager.h"
#include "Game.h"
#include "dLogger.h"
#include "DestroyableComponent.h"
#include "eReplicaComponentType.h"
2022-07-28 13:39:57 +00:00
void StunBehavior::Handle(BehaviorContext* context, RakNet::BitStream* bitStream, const BehaviorBranchContext branch) {
if (this->m_stunCaster || branch.target == context->originator) {
return;
}
bool blocked{};
if (!bitStream->Read(blocked)) {
Game::logger->Log("StunBehavior", "Unable to read blocked from bitStream, aborting Handle! %i", bitStream->GetNumberOfUnreadBits());
return;
};
auto* target = EntityManager::Instance()->GetEntity(branch.target);
2022-07-28 13:39:57 +00:00
if (target == nullptr) {
Game::logger->Log("StunBehavior", "Failed to find target (%llu)!", branch.target);
return;
}
/*
* If our target is an enemy we can go ahead and stun it.
*/
auto* combatAiComponent = target->GetComponent<BaseCombatAIComponent>();
2022-07-28 13:39:57 +00:00
if (combatAiComponent == nullptr) {
return;
}
combatAiComponent->Stun(branch.duration);
}
2022-07-28 13:39:57 +00:00
void StunBehavior::Calculate(BehaviorContext* context, RakNet::BitStream* bitStream, const BehaviorBranchContext branch) {
if (this->m_stunCaster || branch.target == context->originator) {
auto* self = EntityManager::Instance()->GetEntity(context->originator);
2022-07-28 13:39:57 +00:00
if (self == nullptr) {
Game::logger->Log("StunBehavior", "Invalid self entity (%llu)!", context->originator);
return;
}
/*
* See if we can stun ourselves
*/
auto* combatAiComponent = self->GetComponent<BaseCombatAIComponent>();
2022-07-28 13:39:57 +00:00
if (combatAiComponent == nullptr) {
return;
}
combatAiComponent->Stun(branch.duration);
return;
}
bool blocked = false;
auto* target = EntityManager::Instance()->GetEntity(branch.target);
2022-07-28 13:39:57 +00:00
if (target != nullptr) {
auto* destroyableComponent = target->GetComponent<DestroyableComponent>();
2022-07-28 13:39:57 +00:00
if (destroyableComponent != nullptr) {
blocked = destroyableComponent->IsKnockbackImmune();
}
}
bitStream->Write(blocked);
2022-07-28 13:39:57 +00:00
if (target == nullptr) {
Game::logger->Log("StunBehavior", "Failed to find target (%llu)!", branch.target);
return;
}
/*
* If our target is an enemy we can go ahead and stun it.
*/
auto* combatAiComponent = target->GetComponent<BaseCombatAIComponent>();
2022-07-28 13:39:57 +00:00
if (combatAiComponent == nullptr) {
return;
}
combatAiComponent->Stun(branch.duration);
}
2022-07-28 13:39:57 +00:00
void StunBehavior::Load() {
this->m_stunCaster = GetBoolean("stun_caster");
}