DarkflameServer/dGame/dBehaviors/SwitchBehavior.cpp

77 lines
2.0 KiB
C++
Raw Normal View History

2022-08-06 03:01:59 +00:00
#include "SwitchBehavior.h"
#include "BehaviorBranchContext.h"
#include "EntityManager.h"
#include "Logger.h"
#include "DestroyableComponent.h"
#include "BehaviorContext.h"
#include "BuffComponent.h"
void SwitchBehavior::Handle(BehaviorContext* context, RakNet::BitStream& bitStream, const BehaviorBranchContext branch) {
auto state = true;
2022-07-28 13:39:57 +00:00
if (this->m_imagination > 0 || !this->m_isEnemyFaction) {
if (!bitStream.Read(state)) {
LOG("Unable to read state from bitStream, aborting Handle! %i", bitStream.GetNumberOfUnreadBits());
return;
};
}
auto* entity = Game::entityManager->GetEntity(context->originator);
2022-07-28 13:39:57 +00:00
if (entity == nullptr) {
return;
}
2022-07-28 13:39:57 +00:00
auto* destroyableComponent = entity->GetComponent<DestroyableComponent>();
2022-07-28 13:39:57 +00:00
if (destroyableComponent == nullptr) {
return;
}
LOG_DEBUG("[%i] State: (%d), imagination: (%i) / (%f)", entity->GetLOT(), state, destroyableComponent->GetImagination(), destroyableComponent->GetMaxImagination());
if (state) {
this->m_actionTrue->Handle(context, bitStream, branch);
2022-07-28 13:39:57 +00:00
} else {
this->m_actionFalse->Handle(context, bitStream, branch);
}
}
void SwitchBehavior::Calculate(BehaviorContext* context, RakNet::BitStream& bitStream, BehaviorBranchContext branch) {
auto state = true;
2022-07-28 13:39:57 +00:00
if (this->m_imagination > 0 || !this->m_isEnemyFaction) {
auto* entity = Game::entityManager->GetEntity(branch.target);
state = entity != nullptr;
2022-07-28 13:39:57 +00:00
if (state && m_targetHasBuff != 0) {
auto* buffComponent = entity->GetComponent<BuffComponent>();
2022-07-28 13:39:57 +00:00
if (buffComponent != nullptr && !buffComponent->HasBuff(m_targetHasBuff)) {
state = false;
}
}
bitStream.Write(state);
}
2022-07-28 13:39:57 +00:00
if (state) {
this->m_actionTrue->Calculate(context, bitStream, branch);
2022-07-28 13:39:57 +00:00
} else {
this->m_actionFalse->Calculate(context, bitStream, branch);
}
}
2022-07-28 13:39:57 +00:00
void SwitchBehavior::Load() {
this->m_actionTrue = GetAction("action_true");
this->m_actionFalse = GetAction("action_false");
this->m_imagination = GetInt("imagination");
this->m_isEnemyFaction = GetBoolean("isEnemyFaction");
this->m_targetHasBuff = GetInt("target_has_buff");
}