2021-12-05 17:54:36 +00:00
|
|
|
#include "SpeedBehavior.h"
|
|
|
|
|
|
|
|
#include "ControllablePhysicsComponent.h"
|
|
|
|
#include "BehaviorContext.h"
|
|
|
|
#include "BehaviorBranchContext.h"
|
|
|
|
#include "dLogger.h"
|
|
|
|
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
void SpeedBehavior::Handle(BehaviorContext* context, RakNet::BitStream* bitStream, BehaviorBranchContext branch) {
|
|
|
|
if (m_AffectsCaster) {
|
|
|
|
branch.target = context->caster;
|
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
|
|
|
|
auto* target = EntityManager::Instance()->GetEntity(branch.target);
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
if (target == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
auto* controllablePhysicsComponent = target->GetComponent<ControllablePhysicsComponent>();
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
if (controllablePhysicsComponent == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
const auto current = controllablePhysicsComponent->GetSpeedMultiplier();
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
controllablePhysicsComponent->SetSpeedMultiplier(current + ((m_RunSpeed - 500.0f) / 500.0f));
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
EntityManager::Instance()->SerializeEntity(target);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
if (branch.duration > 0.0f) {
|
|
|
|
context->RegisterTimerBehavior(this, branch);
|
|
|
|
} else if (branch.start > 0) {
|
|
|
|
controllablePhysicsComponent->SetIgnoreMultipliers(true);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
context->RegisterEndBehavior(this, branch);
|
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
void SpeedBehavior::Timer(BehaviorContext* context, BehaviorBranchContext branch, LWOOBJID second) {
|
|
|
|
auto* target = EntityManager::Instance()->GetEntity(branch.target);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
if (target == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
auto* controllablePhysicsComponent = target->GetComponent<ControllablePhysicsComponent>();
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
if (controllablePhysicsComponent == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
const auto current = controllablePhysicsComponent->GetSpeedMultiplier();
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
controllablePhysicsComponent->SetSpeedMultiplier(current - ((m_RunSpeed - 500.0f) / 500.0f));
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
EntityManager::Instance()->SerializeEntity(target);
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
void SpeedBehavior::End(BehaviorContext* context, BehaviorBranchContext branch, LWOOBJID second) {
|
|
|
|
auto* target = EntityManager::Instance()->GetEntity(branch.target);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
if (target == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
auto* controllablePhysicsComponent = target->GetComponent<ControllablePhysicsComponent>();
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
if (controllablePhysicsComponent == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
const auto current = controllablePhysicsComponent->GetSpeedMultiplier();
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
controllablePhysicsComponent->SetIgnoreMultipliers(false);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
controllablePhysicsComponent->SetSpeedMultiplier(current - ((m_RunSpeed - 500.0f) / 500.0f));
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
EntityManager::Instance()->SerializeEntity(target);
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
void SpeedBehavior::Load() {
|
|
|
|
m_RunSpeed = GetFloat("run_speed");
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
if (m_RunSpeed < 500.0f) {
|
|
|
|
m_RunSpeed = 500.0f;
|
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
m_AffectsCaster = GetBoolean("affects_caster");
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|