2021-12-05 17:54:36 +00:00
|
|
|
#include "SwitchBehavior.h"
|
|
|
|
#include "BehaviorBranchContext.h"
|
|
|
|
#include "EntityManager.h"
|
2023-10-21 23:31:55 +00:00
|
|
|
#include "Logger.h"
|
2021-12-05 17:54:36 +00:00
|
|
|
#include "DestroyableComponent.h"
|
|
|
|
#include "BehaviorContext.h"
|
|
|
|
#include "BuffComponent.h"
|
|
|
|
|
2024-02-27 07:25:44 +00:00
|
|
|
void SwitchBehavior::Handle(BehaviorContext* context, RakNet::BitStream& bitStream, const BehaviorBranchContext branch) {
|
2024-03-24 19:01:12 +00:00
|
|
|
bool state = true;
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2024-03-24 19:01:12 +00:00
|
|
|
if (m_imagination > 0 || m_targetHasBuff > 0 || m_Distance > -1.0f) {
|
2024-02-27 07:25:44 +00:00
|
|
|
if (!bitStream.Read(state)) {
|
|
|
|
LOG("Unable to read state from bitStream, aborting Handle! %i", bitStream.GetNumberOfUnreadBits());
|
2022-12-16 21:23:02 +00:00
|
|
|
return;
|
|
|
|
};
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
2023-07-15 20:56:33 +00:00
|
|
|
auto* entity = Game::entityManager->GetEntity(context->originator);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2024-03-24 19:01:12 +00:00
|
|
|
if (!entity) return;
|
2021-12-05 17:54:36 +00:00
|
|
|
|
|
|
|
auto* destroyableComponent = entity->GetComponent<DestroyableComponent>();
|
2022-07-25 02:26:51 +00:00
|
|
|
|
2024-03-24 19:01:12 +00:00
|
|
|
if (destroyableComponent) {
|
|
|
|
if (m_isEnemyFaction) {
|
|
|
|
auto* target = Game::entityManager->GetEntity(branch.target);
|
|
|
|
if (target) state = destroyableComponent->IsEnemy(target);
|
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2024-03-24 19:01:12 +00:00
|
|
|
LOG_DEBUG("[%i] State: (%d), imagination: (%i) / (%f)", entity->GetLOT(), state, destroyableComponent->GetImagination(), destroyableComponent->GetMaxImagination());
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
2024-03-24 19:01:12 +00:00
|
|
|
|
|
|
|
auto* behaviorToCall = state ? m_actionTrue : m_actionFalse;
|
|
|
|
behaviorToCall->Handle(context, bitStream, branch);
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
2024-02-27 07:25:44 +00:00
|
|
|
void SwitchBehavior::Calculate(BehaviorContext* context, RakNet::BitStream& bitStream, BehaviorBranchContext branch) {
|
2024-03-24 19:01:12 +00:00
|
|
|
bool state = true;
|
|
|
|
if (m_imagination > 0 || m_targetHasBuff > 0 || m_Distance > -1.0f) {
|
2023-07-15 20:56:33 +00:00
|
|
|
auto* entity = Game::entityManager->GetEntity(branch.target);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
|
|
|
state = entity != nullptr;
|
|
|
|
|
2024-03-24 19:01:12 +00:00
|
|
|
if (state) {
|
|
|
|
if (m_targetHasBuff != 0) {
|
|
|
|
auto* buffComponent = entity->GetComponent<BuffComponent>();
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2024-03-24 19:01:12 +00:00
|
|
|
if (buffComponent != nullptr && !buffComponent->HasBuff(m_targetHasBuff)) {
|
|
|
|
state = false;
|
|
|
|
}
|
|
|
|
} else if (m_imagination > 0) {
|
|
|
|
auto* destroyableComponent = entity->GetComponent<DestroyableComponent>();
|
|
|
|
|
|
|
|
if (destroyableComponent && destroyableComponent->GetImagination() < m_imagination) {
|
|
|
|
state = false;
|
|
|
|
}
|
|
|
|
} else if (m_Distance > -1.0f) {
|
|
|
|
auto* originator = Game::entityManager->GetEntity(context->originator);
|
|
|
|
|
|
|
|
if (originator) {
|
|
|
|
const auto distance = (originator->GetPosition() - entity->GetPosition()).Length();
|
|
|
|
|
|
|
|
state = distance <= m_Distance;
|
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-27 07:25:44 +00:00
|
|
|
bitStream.Write(state);
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
2024-03-24 19:01:12 +00:00
|
|
|
auto* behaviorToCall = state ? m_actionTrue : m_actionFalse;
|
|
|
|
behaviorToCall->Calculate(context, bitStream, branch);
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SwitchBehavior::Load() {
|
|
|
|
this->m_actionTrue = GetAction("action_true");
|
2022-07-25 02:26:51 +00:00
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
this->m_actionFalse = GetAction("action_false");
|
2022-07-25 02:26:51 +00:00
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
this->m_imagination = GetInt("imagination");
|
2022-07-25 02:26:51 +00:00
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
this->m_isEnemyFaction = GetBoolean("isEnemyFaction");
|
|
|
|
|
2024-03-24 19:01:12 +00:00
|
|
|
this->m_targetHasBuff = GetInt("target_has_buff", -1);
|
|
|
|
|
|
|
|
this->m_Distance = GetFloat("distance", -1.0f);
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|