DarkflameServer/dGame/dComponents/VehiclePhysicsComponent.cpp

113 lines
2.6 KiB
C++
Raw Normal View History

#include "VehiclePhysicsComponent.h"
#include "EntityManager.h"
2022-07-28 13:39:57 +00:00
VehiclePhysicsComponent::VehiclePhysicsComponent(Entity* parent) : Component(parent) {
m_Position = NiPoint3::ZERO;
m_Rotation = NiQuaternion::IDENTITY;
m_Velocity = NiPoint3::ZERO;
m_AngularVelocity = NiPoint3::ZERO;
m_IsOnGround = true;
m_IsOnRail = false;
m_DirtyPosition = true;
m_DirtyVelocity = true;
m_DirtyAngularVelocity = true;
m_EndBehavior = GeneralUtils::GenerateRandomNumber<uint32_t>(0, 7);
}
2022-07-28 13:39:57 +00:00
VehiclePhysicsComponent::~VehiclePhysicsComponent() {
}
2022-07-28 13:39:57 +00:00
void VehiclePhysicsComponent::SetPosition(const NiPoint3& pos) {
m_Position = pos;
}
2022-07-28 13:39:57 +00:00
void VehiclePhysicsComponent::SetRotation(const NiQuaternion& rot) {
m_DirtyPosition = true;
2022-07-28 13:39:57 +00:00
m_Rotation = rot;
}
2022-07-28 13:39:57 +00:00
void VehiclePhysicsComponent::SetVelocity(const NiPoint3& vel) {
m_DirtyPosition = true;
2022-07-28 13:39:57 +00:00
m_Velocity = vel;
}
2022-07-28 13:39:57 +00:00
void VehiclePhysicsComponent::SetAngularVelocity(const NiPoint3& vel) {
m_DirtyPosition = true;
2022-07-28 13:39:57 +00:00
m_AngularVelocity = vel;
}
2022-07-28 13:39:57 +00:00
void VehiclePhysicsComponent::SetIsOnGround(bool val) {
m_DirtyPosition = true;
2022-07-28 13:39:57 +00:00
m_IsOnGround = val;
}
2022-07-28 13:39:57 +00:00
void VehiclePhysicsComponent::SetIsOnRail(bool val) {
m_DirtyPosition = true;
2022-07-28 13:39:57 +00:00
m_IsOnRail = val;
}
2022-07-28 13:39:57 +00:00
void VehiclePhysicsComponent::SetDirtyPosition(bool val) {
m_DirtyPosition = val;
}
2022-07-28 13:39:57 +00:00
void VehiclePhysicsComponent::SetDirtyVelocity(bool val) {
m_DirtyVelocity = val;
}
2022-07-28 13:39:57 +00:00
void VehiclePhysicsComponent::SetDirtyAngularVelocity(bool val) {
m_DirtyAngularVelocity = val;
}
2022-07-28 13:39:57 +00:00
void VehiclePhysicsComponent::Serialize(RakNet::BitStream* outBitStream, bool bIsInitialUpdate, unsigned int& flags) {
outBitStream->Write(bIsInitialUpdate || m_DirtyPosition);
if (bIsInitialUpdate || m_DirtyPosition) {
outBitStream->Write(m_Position);
2022-07-28 13:39:57 +00:00
outBitStream->Write(m_Rotation);
2022-07-28 13:39:57 +00:00
outBitStream->Write(m_IsOnGround);
outBitStream->Write(m_IsOnRail);
2022-07-28 13:39:57 +00:00
outBitStream->Write(bIsInitialUpdate || m_DirtyVelocity);
2022-07-28 13:39:57 +00:00
if (bIsInitialUpdate || m_DirtyVelocity) {
outBitStream->Write(m_Velocity);
}
2022-07-28 13:39:57 +00:00
outBitStream->Write(bIsInitialUpdate || m_DirtyAngularVelocity);
2022-07-28 13:39:57 +00:00
if (bIsInitialUpdate || m_DirtyAngularVelocity) {
outBitStream->Write(m_AngularVelocity);
}
2022-07-28 13:39:57 +00:00
outBitStream->Write0();
outBitStream->Write0();
outBitStream->Write(0.0f);
2022-07-28 13:39:57 +00:00
if (!bIsInitialUpdate) {
outBitStream->Write0();
}
}
2022-07-28 13:39:57 +00:00
if (bIsInitialUpdate) {
outBitStream->Write<uint8_t>(m_EndBehavior);
2022-07-28 13:39:57 +00:00
outBitStream->Write1();
}
2022-07-28 13:39:57 +00:00
outBitStream->Write0();
}
2022-07-28 13:39:57 +00:00
void VehiclePhysicsComponent::Update(float deltaTime) {
if (m_SoftUpdate > 5) {
EntityManager::Instance()->SerializeEntity(m_OwningEntity);
2022-07-28 13:39:57 +00:00
m_SoftUpdate = 0;
} else {
m_SoftUpdate += deltaTime;
}
}