2021-12-05 17:54:36 +00:00
|
|
|
|
#include "SpawnBehavior.h"
|
|
|
|
|
|
|
|
|
|
#include "BehaviorContext.h"
|
|
|
|
|
#include "BehaviorBranchContext.h"
|
|
|
|
|
#include "EntityManager.h"
|
|
|
|
|
#include "Game.h"
|
|
|
|
|
#include "dLogger.h"
|
|
|
|
|
#include "DestroyableComponent.h"
|
|
|
|
|
#include "RebuildComponent.h"
|
|
|
|
|
|
|
|
|
|
void SpawnBehavior::Handle(BehaviorContext* context, RakNet::BitStream* bitStream, BehaviorBranchContext branch)
|
|
|
|
|
{
|
|
|
|
|
auto* origin = EntityManager::Instance()->GetEntity(context->originator);
|
|
|
|
|
|
|
|
|
|
if (origin == nullptr)
|
|
|
|
|
{
|
2022-07-25 02:26:51 +00:00
|
|
|
|
Game::logger->Log("SpawnBehavior", "Failed to find self entity (%llu)!", context->originator);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (branch.isProjectile)
|
|
|
|
|
{
|
|
|
|
|
auto* target = EntityManager::Instance()->GetEntity(branch.target);
|
|
|
|
|
|
|
|
|
|
if (target != nullptr)
|
|
|
|
|
{
|
|
|
|
|
origin = target;
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-07-25 02:26:51 +00:00
|
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
|
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 = EntityManager::Instance()->CreateEntity(
|
|
|
|
|
info,
|
|
|
|
|
nullptr,
|
|
|
|
|
EntityManager::Instance()->GetEntity(context->originator)
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (entity == nullptr)
|
|
|
|
|
{
|
2022-07-25 02:26:51 +00:00
|
|
|
|
Game::logger->Log("SpawnBehavior", "Failed to spawn entity (%i)!", this->m_lot);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
entity->SetOwnerOverride(context->originator);
|
|
|
|
|
|
|
|
|
|
// Unset the flag to reposition the player, this makes it harder to glitch out of the map
|
|
|
|
|
auto* rebuildComponent = entity->GetComponent<RebuildComponent>();
|
|
|
|
|
|
|
|
|
|
if (rebuildComponent != nullptr)
|
|
|
|
|
{
|
|
|
|
|
rebuildComponent->SetRepositionPlayer(false);
|
|
|
|
|
}
|
2022-07-25 02:26:51 +00:00
|
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
|
EntityManager::Instance()->ConstructEntity(entity);
|
|
|
|
|
|
|
|
|
|
if (branch.duration > 0)
|
|
|
|
|
{
|
|
|
|
|
context->RegisterTimerBehavior(this, branch, entity->GetObjectID());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (branch.start != 0)
|
|
|
|
|
{
|
|
|
|
|
context->RegisterEndBehavior(this, branch, entity->GetObjectID());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
entity->AddCallbackTimer(60, [entity] () {
|
|
|
|
|
entity->Smash();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-25 02:26:51 +00:00
|
|
|
|
void SpawnBehavior::Calculate(BehaviorContext* context, RakNet::BitStream* bitStream, BehaviorBranchContext branch)
|
2021-12-05 17:54:36 +00:00
|
|
|
|
{
|
|
|
|
|
Handle(context, bitStream, branch);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SpawnBehavior::Timer(BehaviorContext* context, const BehaviorBranchContext branch, const LWOOBJID second)
|
|
|
|
|
{
|
|
|
|
|
auto* entity = EntityManager::Instance()->GetEntity(second);
|
|
|
|
|
|
|
|
|
|
if (entity == nullptr)
|
|
|
|
|
{
|
2022-07-25 02:26:51 +00:00
|
|
|
|
Game::logger->Log("SpawnBehavior", "Failed to find spawned entity (%llu)!", second);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto* destroyable = static_cast<DestroyableComponent*>(entity->GetComponent(COMPONENT_TYPE_DESTROYABLE));
|
|
|
|
|
|
|
|
|
|
if (destroyable == nullptr)
|
|
|
|
|
{
|
|
|
|
|
entity->Smash(context->originator);
|
2022-07-25 02:26:51 +00:00
|
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
destroyable->Smash(second);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SpawnBehavior::End(BehaviorContext* context, const BehaviorBranchContext branch, const LWOOBJID second)
|
|
|
|
|
{
|
|
|
|
|
Timer(context, branch, second);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void SpawnBehavior::Load()
|
|
|
|
|
{
|
|
|
|
|
this->m_lot = GetInt("LOT_ID");
|
|
|
|
|
this->m_Distance = GetFloat("distance");
|
|
|
|
|
}
|