DarkflameServer/dGame/dBehaviors/BuffBehavior.cpp

85 lines
2.3 KiB
C++
Raw Normal View History

2022-08-06 03:01:59 +00:00
#include "BuffBehavior.h"
#include "BehaviorContext.h"
#include "BehaviorBranchContext.h"
#include "EntityManager.h"
#include "Game.h"
#include "Logger.h"
#include "DestroyableComponent.h"
void BuffBehavior::Handle(BehaviorContext* context, RakNet::BitStream& bitStream, BehaviorBranchContext branch) {
const auto target = branch.target != LWOOBJID_EMPTY ? branch.target : context->originator;
auto* entity = Game::entityManager->GetEntity(target);
2022-07-28 13:39:57 +00:00
if (entity == nullptr) {
2024-03-13 02:10:01 +00:00
Log::Warn("Invalid target ({})!", target);
return;
}
auto* component = entity->GetComponent<DestroyableComponent>();
2022-07-28 13:39:57 +00:00
if (component == nullptr) {
2024-03-13 02:10:01 +00:00
Log::Warn("Invalid target, no destroyable component ({})!", target);
return;
}
component->SetMaxHealth(component->GetMaxHealth() + this->m_health);
component->SetMaxArmor(component->GetMaxArmor() + this->m_armor);
component->SetMaxImagination(component->GetMaxImagination() + this->m_imagination);
Game::entityManager->SerializeEntity(entity);
2022-07-28 13:39:57 +00:00
if (!context->unmanaged) {
if (branch.duration > 0) {
context->RegisterTimerBehavior(this, branch);
2022-07-28 13:39:57 +00:00
} else if (branch.start > 0) {
context->RegisterEndBehavior(this, branch);
}
}
}
2022-07-28 13:39:57 +00:00
void BuffBehavior::UnCast(BehaviorContext* context, BehaviorBranchContext branch) {
const auto target = branch.target != LWOOBJID_EMPTY ? branch.target : context->originator;
auto* entity = Game::entityManager->GetEntity(target);
2022-07-28 13:39:57 +00:00
if (entity == nullptr) {
2024-03-13 02:10:01 +00:00
Log::Warn("Invalid target ({})!", target);
return;
}
auto* component = entity->GetComponent<DestroyableComponent>();
2022-07-28 13:39:57 +00:00
if (component == nullptr) {
2024-03-13 02:10:01 +00:00
Log::Warn("Invalid target, no destroyable component ({})!", target);
return;
}
component->SetMaxHealth(component->GetMaxHealth() - this->m_health);
component->SetMaxArmor(component->GetMaxArmor() - this->m_armor);
component->SetMaxImagination(component->GetMaxImagination() - this->m_imagination);
Game::entityManager->SerializeEntity(entity);
}
2022-07-28 13:39:57 +00:00
void BuffBehavior::Timer(BehaviorContext* context, const BehaviorBranchContext branch, LWOOBJID second) {
UnCast(context, branch);
}
2022-07-28 13:39:57 +00:00
void BuffBehavior::End(BehaviorContext* context, const BehaviorBranchContext branch, LWOOBJID second) {
UnCast(context, branch);
}
2022-07-28 13:39:57 +00:00
void BuffBehavior::Load() {
this->m_health = GetInt("life");
this->m_armor = GetInt("armor");
this->m_imagination = GetInt("imag");
}