DarkflameServer/dGame/dComponents/MovingPlatformComponent.cpp

354 lines
10 KiB
C++
Raw Normal View History

/*
* Darkflame Universe
* Copyright 2019
*/
#include "MovingPlatformComponent.h"
#include "BitStream.h"
#include "GeneralUtils.h"
#include "dZoneManager.h"
#include "EntityManager.h"
#include "dLogger.h"
#include "GameMessages.h"
#include "CppScripts.h"
#include "SimplePhysicsComponent.h"
#include "Zone.h"
MoverSubComponent::MoverSubComponent(const NiPoint3& startPos) {
2022-07-28 13:39:57 +00:00
mPosition = {};
mState = eMovementPlatformState::Stopped;
2022-07-28 13:39:57 +00:00
mDesiredWaypointIndex = 0; // -1;
mInReverse = false;
mShouldStopAtDesiredWaypoint = false;
2022-07-28 13:39:57 +00:00
mPercentBetweenPoints = 0.0f;
2022-07-28 13:39:57 +00:00
mCurrentWaypointIndex = 0;
mNextWaypointIndex = 0; //mCurrentWaypointIndex + 1;
2022-07-28 13:39:57 +00:00
mIdleTimeElapsed = 0.0f;
}
MoverSubComponent::~MoverSubComponent() = default;
void MoverSubComponent::Serialize(RakNet::BitStream* outBitStream, bool bIsInitialUpdate, unsigned int& flags) const {
2022-07-28 13:39:57 +00:00
outBitStream->Write<bool>(true);
2022-07-28 13:39:57 +00:00
outBitStream->Write<uint32_t>(static_cast<uint32_t>(mState));
outBitStream->Write<int32_t>(mDesiredWaypointIndex);
outBitStream->Write(mShouldStopAtDesiredWaypoint);
outBitStream->Write(mInReverse);
2022-07-28 13:39:57 +00:00
outBitStream->Write<float_t>(mPercentBetweenPoints);
2022-07-28 13:39:57 +00:00
outBitStream->Write<float_t>(mPosition.x);
outBitStream->Write<float_t>(mPosition.y);
outBitStream->Write<float_t>(mPosition.z);
2022-07-28 13:39:57 +00:00
outBitStream->Write<uint32_t>(mCurrentWaypointIndex);
outBitStream->Write<uint32_t>(mNextWaypointIndex);
2022-07-28 13:39:57 +00:00
outBitStream->Write<float_t>(mIdleTimeElapsed);
outBitStream->Write<float_t>(0.0f); // Move time elapsed
}
//------------- MovingPlatformComponent below --------------
MovingPlatformComponent::MovingPlatformComponent(Entity* parent, const std::string& pathName) : Component(parent) {
2022-07-28 13:39:57 +00:00
m_MoverSubComponentType = eMoverSubComponentType::mover;
m_MoverSubComponent = new MoverSubComponent(m_OwningEntity->GetDefaultPosition());
2022-07-28 13:39:57 +00:00
m_PathName = GeneralUtils::ASCIIToUTF16(pathName);
m_Path = dZoneManager::Instance()->GetZone()->GetPath(pathName);
m_NoAutoStart = false;
if (m_Path == nullptr) {
Game::logger->Log("MovingPlatformComponent", "Path not found: %s", pathName.c_str());
}
}
MovingPlatformComponent::~MovingPlatformComponent() {
2022-07-28 13:39:57 +00:00
delete static_cast<MoverSubComponent*>(m_MoverSubComponent);
}
void MovingPlatformComponent::Serialize(RakNet::BitStream* outBitStream, bool bIsInitialUpdate, unsigned int& flags) {
2022-07-28 13:39:57 +00:00
// Here we don't serialize the moving platform to let the client simulate the movement
2022-07-28 13:39:57 +00:00
if (!m_Serialize) {
outBitStream->Write<bool>(false);
outBitStream->Write<bool>(false);
2022-07-28 13:39:57 +00:00
return;
}
2022-07-28 13:39:57 +00:00
outBitStream->Write<bool>(true);
2022-07-28 13:39:57 +00:00
auto hasPath = !m_PathingStopped && !m_PathName.empty();
outBitStream->Write(hasPath);
2022-07-28 13:39:57 +00:00
if (hasPath) {
// Is on rail
outBitStream->Write1();
2022-07-28 13:39:57 +00:00
outBitStream->Write(static_cast<uint16_t>(m_PathName.size()));
for (const auto& c : m_PathName) {
outBitStream->Write(static_cast<uint16_t>(c));
}
2022-07-28 13:39:57 +00:00
// Starting point
outBitStream->Write<uint32_t>(0);
2022-07-28 13:39:57 +00:00
// Reverse
outBitStream->Write<bool>(false);
}
2022-07-28 13:39:57 +00:00
const auto hasPlatform = m_MoverSubComponent != nullptr;
outBitStream->Write<bool>(hasPlatform);
2022-07-28 13:39:57 +00:00
if (hasPlatform) {
auto* mover = static_cast<MoverSubComponent*>(m_MoverSubComponent);
outBitStream->Write<uint32_t>(static_cast<uint32_t>(m_MoverSubComponentType));
2022-07-28 13:39:57 +00:00
if (m_MoverSubComponentType == eMoverSubComponentType::simpleMover) {
// TODO
} else {
mover->Serialize(outBitStream, bIsInitialUpdate, flags);
}
}
}
void MovingPlatformComponent::OnRebuildInitilized() {
2022-07-28 13:39:57 +00:00
StopPathing();
}
void MovingPlatformComponent::OnCompleteRebuild() {
2022-07-28 13:39:57 +00:00
if (m_NoAutoStart)
return;
2022-07-28 13:39:57 +00:00
StartPathing();
}
void MovingPlatformComponent::SetMovementState(eMovementPlatformState value) {
2022-07-28 13:39:57 +00:00
auto* subComponent = static_cast<MoverSubComponent*>(m_MoverSubComponent);
2022-07-28 13:39:57 +00:00
subComponent->mState = value;
EntityManager::Instance()->SerializeEntity(m_OwningEntity);
}
2022-07-28 13:39:57 +00:00
void MovingPlatformComponent::GotoWaypoint(uint32_t index, bool stopAtWaypoint) {
auto* subComponent = static_cast<MoverSubComponent*>(m_MoverSubComponent);
2022-07-28 13:39:57 +00:00
subComponent->mDesiredWaypointIndex = index;
subComponent->mNextWaypointIndex = index;
subComponent->mShouldStopAtDesiredWaypoint = stopAtWaypoint;
2022-07-28 13:39:57 +00:00
StartPathing();
}
2022-07-28 13:39:57 +00:00
void MovingPlatformComponent::StartPathing() {
//GameMessages::SendStartPathing(m_OwningEntity);
2022-07-28 13:39:57 +00:00
m_PathingStopped = false;
2022-07-28 13:39:57 +00:00
auto* subComponent = static_cast<MoverSubComponent*>(m_MoverSubComponent);
2022-07-28 13:39:57 +00:00
subComponent->mShouldStopAtDesiredWaypoint = true;
subComponent->mState = eMovementPlatformState::Stationary;
2022-07-28 13:39:57 +00:00
NiPoint3 targetPosition;
2022-07-28 13:39:57 +00:00
if (m_Path != nullptr) {
const auto& currentWaypoint = m_Path->pathWaypoints[subComponent->mCurrentWaypointIndex];
const auto& nextWaypoint = m_Path->pathWaypoints[subComponent->mNextWaypointIndex];
2022-07-28 13:39:57 +00:00
subComponent->mPosition = currentWaypoint.position;
subComponent->mSpeed = currentWaypoint.movingPlatform.speed;
subComponent->mWaitTime = currentWaypoint.movingPlatform.wait;
2022-07-28 13:39:57 +00:00
targetPosition = nextWaypoint.position;
} else {
subComponent->mPosition = m_OwningEntity->GetPosition();
2022-07-28 13:39:57 +00:00
subComponent->mSpeed = 1.0f;
subComponent->mWaitTime = 2.0f;
targetPosition = m_OwningEntity->GetPosition() + NiPoint3(0.0f, 10.0f, 0.0f);
2022-07-28 13:39:57 +00:00
}
m_OwningEntity->AddCallbackTimer(subComponent->mWaitTime, [this] {
SetMovementState(eMovementPlatformState::Moving);
2022-07-28 13:39:57 +00:00
});
2022-07-28 13:39:57 +00:00
const auto travelTime = Vector3::Distance(targetPosition, subComponent->mPosition) / subComponent->mSpeed + 1.5f;
2022-07-28 13:39:57 +00:00
const auto travelNext = subComponent->mWaitTime + travelTime;
m_OwningEntity->AddCallbackTimer(travelTime, [subComponent, this] {
for (CppScripts::Script* script : CppScripts::GetEntityScripts(m_OwningEntity)) {
script->OnWaypointReached(m_OwningEntity, subComponent->mNextWaypointIndex);
2022-07-28 13:39:57 +00:00
}
});
m_OwningEntity->AddCallbackTimer(travelNext, [this] {
2022-07-28 13:39:57 +00:00
ContinuePathing();
});
//GameMessages::SendPlatformResync(m_OwningEntity, UNASSIGNED_SYSTEM_ADDRESS);
EntityManager::Instance()->SerializeEntity(m_OwningEntity);
}
2022-07-28 13:39:57 +00:00
void MovingPlatformComponent::ContinuePathing() {
auto* subComponent = static_cast<MoverSubComponent*>(m_MoverSubComponent);
subComponent->mState = eMovementPlatformState::Stationary;
2022-07-28 13:39:57 +00:00
subComponent->mCurrentWaypointIndex = subComponent->mNextWaypointIndex;
NiPoint3 targetPosition;
uint32_t pathSize;
PathBehavior behavior;
if (m_Path != nullptr) {
const auto& currentWaypoint = m_Path->pathWaypoints[subComponent->mCurrentWaypointIndex];
const auto& nextWaypoint = m_Path->pathWaypoints[subComponent->mNextWaypointIndex];
subComponent->mPosition = currentWaypoint.position;
subComponent->mSpeed = currentWaypoint.movingPlatform.speed;
subComponent->mWaitTime = currentWaypoint.movingPlatform.wait; // + 2;
pathSize = m_Path->pathWaypoints.size() - 1;
behavior = static_cast<PathBehavior>(m_Path->pathBehavior);
targetPosition = nextWaypoint.position;
} else {
subComponent->mPosition = m_OwningEntity->GetPosition();
2022-07-28 13:39:57 +00:00
subComponent->mSpeed = 1.0f;
subComponent->mWaitTime = 2.0f;
targetPosition = m_OwningEntity->GetPosition() + NiPoint3(0.0f, 10.0f, 0.0f);
2022-07-28 13:39:57 +00:00
pathSize = 1;
behavior = PathBehavior::Loop;
}
if (m_OwningEntity->GetLOT() == 9483) {
2022-07-28 13:39:57 +00:00
behavior = PathBehavior::Bounce;
} else {
return;
}
if (subComponent->mCurrentWaypointIndex >= pathSize) {
subComponent->mCurrentWaypointIndex = pathSize;
switch (behavior) {
case PathBehavior::Once:
EntityManager::Instance()->SerializeEntity(m_OwningEntity);
2022-07-28 13:39:57 +00:00
return;
case PathBehavior::Bounce:
subComponent->mInReverse = true;
break;
case PathBehavior::Loop:
subComponent->mNextWaypointIndex = 0;
break;
default:
break;
}
} else if (subComponent->mCurrentWaypointIndex == 0) {
subComponent->mInReverse = false;
}
if (subComponent->mInReverse) {
subComponent->mNextWaypointIndex = subComponent->mCurrentWaypointIndex - 1;
} else {
subComponent->mNextWaypointIndex = subComponent->mCurrentWaypointIndex + 1;
}
/*
subComponent->mNextWaypointIndex = 0;
subComponent->mCurrentWaypointIndex = 1;
*/
//GameMessages::SendPlatformResync(m_OwningEntity, UNASSIGNED_SYSTEM_ADDRESS);
2022-07-28 13:39:57 +00:00
if (subComponent->mCurrentWaypointIndex == subComponent->mDesiredWaypointIndex) {
// TODO: Send event?
StopPathing();
return;
}
m_OwningEntity->CancelCallbackTimers();
2022-07-28 13:39:57 +00:00
m_OwningEntity->AddCallbackTimer(subComponent->mWaitTime, [this] {
SetMovementState(eMovementPlatformState::Moving);
2022-07-28 13:39:57 +00:00
});
auto travelTime = Vector3::Distance(targetPosition, subComponent->mPosition) / subComponent->mSpeed + 1.5;
if (m_OwningEntity->GetLOT() == 9483) {
2022-07-28 13:39:57 +00:00
travelTime += 20;
}
const auto travelNext = subComponent->mWaitTime + travelTime;
m_OwningEntity->AddCallbackTimer(travelTime, [subComponent, this] {
for (CppScripts::Script* script : CppScripts::GetEntityScripts(m_OwningEntity)) {
script->OnWaypointReached(m_OwningEntity, subComponent->mNextWaypointIndex);
2022-07-28 13:39:57 +00:00
}
});
m_OwningEntity->AddCallbackTimer(travelNext, [this] {
2022-07-28 13:39:57 +00:00
ContinuePathing();
});
EntityManager::Instance()->SerializeEntity(m_OwningEntity);
}
2022-07-28 13:39:57 +00:00
void MovingPlatformComponent::StopPathing() {
//m_OwningEntity->CancelCallbackTimers();
2022-07-28 13:39:57 +00:00
auto* subComponent = static_cast<MoverSubComponent*>(m_MoverSubComponent);
2022-07-28 13:39:57 +00:00
m_PathingStopped = true;
subComponent->mState = eMovementPlatformState::Stopped;
2022-07-28 13:39:57 +00:00
subComponent->mDesiredWaypointIndex = -1;
subComponent->mShouldStopAtDesiredWaypoint = false;
EntityManager::Instance()->SerializeEntity(m_OwningEntity);
//GameMessages::SendPlatformResync(m_OwningEntity, UNASSIGNED_SYSTEM_ADDRESS);
}
2022-07-28 13:39:57 +00:00
void MovingPlatformComponent::SetSerialized(bool value) {
m_Serialize = value;
}
2022-07-28 13:39:57 +00:00
bool MovingPlatformComponent::GetNoAutoStart() const {
return m_NoAutoStart;
}
2022-07-28 13:39:57 +00:00
void MovingPlatformComponent::SetNoAutoStart(const bool value) {
m_NoAutoStart = value;
}
2022-07-28 13:39:57 +00:00
void MovingPlatformComponent::WarpToWaypoint(size_t index) {
const auto& waypoint = m_Path->pathWaypoints[index];
m_OwningEntity->SetPosition(waypoint.position);
m_OwningEntity->SetRotation(waypoint.rotation);
EntityManager::Instance()->SerializeEntity(m_OwningEntity);
}
2022-07-28 13:39:57 +00:00
size_t MovingPlatformComponent::GetLastWaypointIndex() const {
return m_Path->pathWaypoints.size() - 1;
}
2022-07-28 13:39:57 +00:00
MoverSubComponent* MovingPlatformComponent::GetMoverSubComponent() const {
return static_cast<MoverSubComponent*>(m_MoverSubComponent);
}