DarkflameServer/dGame/dBehaviors/SpawnBehavior.cpp

110 lines
2.7 KiB
C++
Raw Normal View History

2022-08-06 03:01:59 +00:00
#include "SpawnBehavior.h"
#include "BehaviorContext.h"
#include "BehaviorBranchContext.h"
#include "EntityManager.h"
#include "Game.h"
#include "Logger.h"
#include "DestroyableComponent.h"
#include "QuickBuildComponent.h"
#include "Entity.h"
#include "EntityInfo.h"
#include "eReplicaComponentType.h"
2022-07-28 13:39:57 +00:00
void SpawnBehavior::Handle(BehaviorContext* context, RakNet::BitStream* bitStream, BehaviorBranchContext branch) {
auto* origin = Game::entityManager->GetEntity(context->originator);
2022-07-28 13:39:57 +00:00
if (origin == nullptr) {
LOG("Failed to find self entity (%llu)!", context->originator);
return;
}
2022-07-28 13:39:57 +00:00
if (branch.isProjectile) {
auto* target = Game::entityManager->GetEntity(branch.target);
2022-07-28 13:39:57 +00:00
if (target != nullptr) {
origin = target;
}
}
EntityInfo info;
info.lot = this->m_lot;
info.pos = origin->GetPosition();
info.rot = origin->GetRotation();
info.scale = 1;
info.spawner = nullptr;
info.spawnerID = context->originator;
info.spawnerNodeID = 0;
info.pos = info.pos + (info.rot.GetForwardVector() * m_Distance);
auto* entity = Game::entityManager->CreateEntity(
info,
nullptr,
Game::entityManager->GetEntity(context->originator)
);
2022-07-28 13:39:57 +00:00
if (entity == nullptr) {
LOG("Failed to spawn entity (%i)!", this->m_lot);
return;
}
entity->SetOwnerOverride(context->originator);
// Unset the flag to reposition the player, this makes it harder to glitch out of the map
auto* quickBuildComponent = entity->GetComponent<QuickBuildComponent>();
if (quickBuildComponent != nullptr) {
quickBuildComponent->SetRepositionPlayer(false);
}
Game::entityManager->ConstructEntity(entity);
2022-07-28 13:39:57 +00:00
if (branch.duration > 0) {
context->RegisterTimerBehavior(this, branch, entity->GetObjectID());
}
2022-07-28 13:39:57 +00:00
if (branch.start != 0) {
context->RegisterEndBehavior(this, branch, entity->GetObjectID());
}
2022-07-28 13:39:57 +00:00
entity->AddCallbackTimer(60, [entity]() {
entity->Smash();
2022-07-28 13:39:57 +00:00
});
}
2022-07-28 13:39:57 +00:00
void SpawnBehavior::Calculate(BehaviorContext* context, RakNet::BitStream* bitStream, BehaviorBranchContext branch) {
Handle(context, bitStream, branch);
}
2022-07-28 13:39:57 +00:00
void SpawnBehavior::Timer(BehaviorContext* context, const BehaviorBranchContext branch, const LWOOBJID second) {
auto* entity = Game::entityManager->GetEntity(second);
2022-07-28 13:39:57 +00:00
if (entity == nullptr) {
LOG("Failed to find spawned entity (%llu)!", second);
return;
}
auto* destroyable = static_cast<DestroyableComponent*>(entity->GetComponent(eReplicaComponentType::DESTROYABLE));
2022-07-28 13:39:57 +00:00
if (destroyable == nullptr) {
entity->Smash(context->originator);
return;
}
destroyable->Smash(second);
}
2022-07-28 13:39:57 +00:00
void SpawnBehavior::End(BehaviorContext* context, const BehaviorBranchContext branch, const LWOOBJID second) {
Timer(context, branch, second);
}
2022-07-28 13:39:57 +00:00
void SpawnBehavior::Load() {
this->m_lot = GetInt("LOT_ID");
this->m_Distance = GetFloat("distance");
}