DarkflameServer/dGame/dBehaviors/ProjectileAttackBehavior.cpp

152 lines
4.3 KiB
C++
Raw Permalink Normal View History

2022-08-06 03:01:59 +00:00
#include "ProjectileAttackBehavior.h"
#include "BehaviorBranchContext.h"
#include "BehaviorContext.h"
#include "EntityManager.h"
#include "Game.h"
#include "dLogger.h"
#include "SkillComponent.h"
#include "../dWorldServer/ObjectIDManager.h"
2022-07-28 13:39:57 +00:00
void ProjectileAttackBehavior::Handle(BehaviorContext* context, RakNet::BitStream* bitStream, BehaviorBranchContext branch) {
LWOOBJID target;
bitStream->Read(target);
auto* entity = EntityManager::Instance()->GetEntity(context->originator);
2022-07-28 13:39:57 +00:00
if (entity == nullptr) {
Game::logger->Log("ProjectileAttackBehavior", "Failed to find originator (%llu)!", context->originator);
return;
}
auto* skillComponent = entity->GetComponent<SkillComponent>();
2022-07-28 13:39:57 +00:00
if (skillComponent == nullptr) {
Game::logger->Log("ProjectileAttackBehavior", "Failed to find skill component for (%llu)!", -context->originator);
return;
}
2022-07-28 13:39:57 +00:00
if (m_useMouseposit) {
NiPoint3 targetPosition = NiPoint3::ZERO;
bitStream->Read(targetPosition);
}
auto* targetEntity = EntityManager::Instance()->GetEntity(target);
2022-07-28 13:39:57 +00:00
for (auto i = 0u; i < this->m_projectileCount; ++i) {
LWOOBJID projectileId;
bitStream->Read(projectileId);
branch.target = target;
branch.isProjectile = true;
branch.referencePosition = targetEntity == nullptr ? entity->GetPosition() : targetEntity->GetPosition();
skillComponent->RegisterPlayerProjectile(projectileId, context, branch, this->m_lot);
}
}
2022-07-28 13:39:57 +00:00
void ProjectileAttackBehavior::Calculate(BehaviorContext* context, RakNet::BitStream* bitStream, BehaviorBranchContext branch) {
bitStream->Write(branch.target);
auto* entity = EntityManager::Instance()->GetEntity(context->originator);
2022-07-28 13:39:57 +00:00
if (entity == nullptr) {
Game::logger->Log("ProjectileAttackBehavior", "Failed to find originator (%llu)!", context->originator);
return;
}
auto* skillComponent = entity->GetComponent<SkillComponent>();
2022-07-28 13:39:57 +00:00
if (skillComponent == nullptr) {
Game::logger->Log("ProjectileAttackBehavior", "Failed to find skill component for (%llu)!", context->originator);
return;
}
auto* other = EntityManager::Instance()->GetEntity(branch.target);
2022-07-28 13:39:57 +00:00
if (other == nullptr) {
Game::logger->Log("ProjectileAttackBehavior", "Invalid projectile target (%llu)!", branch.target);
return;
}
const auto position = entity->GetPosition() + this->m_offset;
2022-09-04 10:47:53 +00:00
const auto distance = NiPoint3::Distance(position, other->GetPosition());
const auto time = distance / this->m_projectileSpeed;
const auto rotation = NiQuaternion::LookAtUnlocked(position, other->GetPosition());
const auto targetPosition = other->GetPosition();
//entity->SetRotation(rotation);
const auto angleDelta = this->m_spreadAngle;
const auto angleStep = angleDelta / this->m_projectileCount;
auto angle = -angleStep;
const auto maxTime = this->m_maxDistance / this->m_projectileSpeed;
2022-07-28 13:39:57 +00:00
for (auto i = 0u; i < this->m_projectileCount; ++i) {
auto id = static_cast<LWOOBJID>(ObjectIDManager::Instance()->GenerateObjectID());
id = GeneralUtils::SetBit(id, OBJECT_BIT_CLIENT);
bitStream->Write(id);
auto eulerAngles = rotation.GetEulerAngles();
eulerAngles.y += angle * (3.14 / 180);
const auto angledRotation = NiQuaternion::FromEulerAngles(eulerAngles);
const auto direction = angledRotation.GetForwardVector();
const auto destination = position + direction * distance;
branch.isProjectile = true;
branch.referencePosition = destination;
skillComponent->RegisterCalculatedProjectile(id, context, branch, this->m_lot, maxTime, position, direction * this->m_projectileSpeed, this->m_trackTarget, this->m_trackRadius);
// No idea how to calculate this properly
2022-07-28 13:39:57 +00:00
if (this->m_projectileCount == 2) {
angle += angleDelta;
2022-07-28 13:39:57 +00:00
} else if (this->m_projectileCount == 3) {
angle += angleStep;
}
}
}
2022-07-28 13:39:57 +00:00
void ProjectileAttackBehavior::Load() {
this->m_lot = GetInt("LOT_ID");
this->m_projectileCount = GetInt("spread_count");
2022-07-28 13:39:57 +00:00
if (this->m_projectileCount == 0) {
this->m_projectileCount = 1;
}
this->m_maxDistance = GetFloat("max_distance");
this->m_projectileSpeed = GetFloat("projectile_speed");
this->m_spreadAngle = GetFloat("spread_angle");
this->m_offset = { GetFloat("offset_x"), GetFloat("offset_y"), GetFloat("offset_z") };
this->m_trackTarget = GetBoolean("track_target");
this->m_trackRadius = GetFloat("track_radius");
this->m_useMouseposit = GetBoolean("use_mouseposit");
}