DarkflameServer/tests/dGameTests/dComponentsTests/SimplePhysicsComponentTests.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

154 lines
5.6 KiB
C++
Raw Normal View History

#include "GameDependencies.h"
#include <gtest/gtest.h>
#include "BitStream.h"
#include "SimplePhysicsComponent.h"
#include "Entity.h"
#include "eReplicaComponentType.h"
#include "eStateChangeType.h"
class SimplePhysicsTest : public GameDependenciesTest {
protected:
std::unique_ptr<Entity> baseEntity;
SimplePhysicsComponent* simplePhysicsComponent;
CBITSTREAM;
void SetUp() override {
SetUpDependencies();
baseEntity = std::make_unique<Entity>(15, GameDependenciesTest::info);
feat: Add isolated and simplified path to add components (#1204) * Components: Make ComponentType inline Prevents the next commits ODR violation * Components: Add new components * Entity: Add headers inline script component ComponentType * Components: Flip constructor argument order Entity comes first always * Entity: Add generic AddComponent Allows for much easier adding of components and is error proof by not allowing the user to add more than 1 of a specific component type to an Entity. * Entity: Migrate all component constructors Move all to the new variadic templates AddComponent function to reduce clutter and ways the component map is modified. The new function makes no assumptions. Component is assumed to not exist and is checked for with operator[]. This will construct a null component which will then be newed if the component didnt exist, or it will just get the current component if it does already exist. No new component will be allocated or constructed if the component already exists and the already existing pointer is returned instead. * Entity: Add placement new For the case where the component may already exist, use a placement new to construct the component again, it would be constructed again, but would not need to go through the allocator. * Entity: Add comments on likely new code * Tests: Fix tests * Update Entity.cpp * Update SGCannon.cpp * Entity: call destructor when re-constructing * Update Entity.cpp Update Entity.cpp --------- Co-authored-by: Aaron Kimbrell <aronwk.aaron@gmail.com>
2023-10-23 01:08:49 +00:00
simplePhysicsComponent = baseEntity->AddComponent<SimplePhysicsComponent>(1);
simplePhysicsComponent->SetClimbableType(eClimbableType::CLIMBABLE_TYPE_WALL);
simplePhysicsComponent->SetPosition(NiPoint3(1.0f, 2.0f, 3.0f));
simplePhysicsComponent->SetRotation(NiQuaternion(1.0f, 2.0f, 3.0f, 4.0f));
simplePhysicsComponent->SetVelocity(NiPoint3(5.0f, 6.0f, 7.0f));
simplePhysicsComponent->SetAngularVelocity(NiPoint3(5.0f, 6.0f, 7.0f));
simplePhysicsComponent->SetPhysicsMotionState(2);
}
void TearDown() override {
TearDownDependencies();
}
};
TEST_F(SimplePhysicsTest, SimplePhysicsSerializeTest) {
simplePhysicsComponent->Serialize(bitStream, false);
constexpr uint32_t sizeOfStream = 3 + BYTES_TO_BITS(3 * sizeof(NiPoint3)) + BYTES_TO_BITS(1 * sizeof(NiQuaternion)) + 1 * BYTES_TO_BITS(sizeof(uint32_t));
ASSERT_EQ(bitStream.GetNumberOfBitsUsed(), sizeOfStream);
bool dirtyVelocityFlag;
bitStream.Read(dirtyVelocityFlag);
ASSERT_EQ(dirtyVelocityFlag, true);
NiPoint3 velocity;
bitStream.Read(velocity.x);
bitStream.Read(velocity.y);
bitStream.Read(velocity.z);
ASSERT_EQ(velocity, NiPoint3(5.0f, 6.0f, 7.0f));
NiPoint3 angularVelocity;
bitStream.Read(angularVelocity.x);
bitStream.Read(angularVelocity.y);
bitStream.Read(angularVelocity.z);
ASSERT_EQ(angularVelocity, NiPoint3(5.0f, 6.0f, 7.0f));
bool dirtyPhysicsMotionStateFlag;
bitStream.Read(dirtyPhysicsMotionStateFlag);
ASSERT_EQ(dirtyPhysicsMotionStateFlag, true);
uint32_t physicsMotionState;
bitStream.Read(physicsMotionState);
ASSERT_EQ(physicsMotionState, 2.0f);
bool dirtyPositionFlag;
bitStream.Read(dirtyPositionFlag);
ASSERT_EQ(dirtyPositionFlag, true);
NiPoint3 position;
bitStream.Read(position.x);
bitStream.Read(position.y);
bitStream.Read(position.z);
ASSERT_EQ(position, NiPoint3(1.0f, 2.0f, 3.0f));
NiQuaternion rotation;
bitStream.Read(rotation.x);
bitStream.Read(rotation.y);
bitStream.Read(rotation.z);
bitStream.Read(rotation.w);
ASSERT_EQ(rotation, NiQuaternion(1.0f, 2.0f, 3.0f, 4.0f));
}
TEST_F(SimplePhysicsTest, SimplePhysicsConstructionTest) {
simplePhysicsComponent->Serialize(bitStream, true);
constexpr uint32_t sizeOfStream = 4 + BYTES_TO_BITS(1 * sizeof(int32_t)) + BYTES_TO_BITS(3 * sizeof(NiPoint3)) + BYTES_TO_BITS(1 * sizeof(NiQuaternion)) + 1 * BYTES_TO_BITS(sizeof(uint32_t));
ASSERT_EQ(bitStream.GetNumberOfBitsUsed(), sizeOfStream);
bool dirtyClimbableTypeFlag;
bitStream.Read(dirtyClimbableTypeFlag);
ASSERT_EQ(dirtyClimbableTypeFlag, true);
int32_t climbableType;
bitStream.Read(climbableType);
ASSERT_EQ(climbableType, 2);
bool dirtyVelocityFlag;
bitStream.Read(dirtyVelocityFlag);
ASSERT_EQ(dirtyVelocityFlag, true);
NiPoint3 velocity;
bitStream.Read(velocity.x);
bitStream.Read(velocity.y);
bitStream.Read(velocity.z);
ASSERT_EQ(velocity, NiPoint3(5.0f, 6.0f, 7.0f));
NiPoint3 angularVelocity;
bitStream.Read(angularVelocity.x);
bitStream.Read(angularVelocity.y);
bitStream.Read(angularVelocity.z);
ASSERT_EQ(angularVelocity, NiPoint3(5.0f, 6.0f, 7.0f));
bool dirtyPhysicsMotionStateFlag;
bitStream.Read(dirtyPhysicsMotionStateFlag);
ASSERT_EQ(dirtyPhysicsMotionStateFlag, true);
uint32_t physicsMotionState;
bitStream.Read(physicsMotionState);
ASSERT_EQ(physicsMotionState, 2.0f);
bool dirtyPositionFlag;
bitStream.Read(dirtyPositionFlag);
ASSERT_EQ(dirtyPositionFlag, true);
NiPoint3 position;
bitStream.Read(position.x);
bitStream.Read(position.y);
bitStream.Read(position.z);
ASSERT_EQ(position, NiPoint3(1.0f, 2.0f, 3.0f));
NiQuaternion rotation;
bitStream.Read(rotation.x);
bitStream.Read(rotation.y);
bitStream.Read(rotation.z);
bitStream.Read(rotation.w);
ASSERT_EQ(rotation, NiQuaternion(1.0f, 2.0f, 3.0f, 4.0f));
}
TEST_F(SimplePhysicsTest, SimplePhysicsGettersAndSettersTest) {
ASSERT_EQ(simplePhysicsComponent->GetClimabbleType(), eClimbableType::CLIMBABLE_TYPE_WALL);
ASSERT_EQ(simplePhysicsComponent->GetPosition(), NiPoint3(1.0f, 2.0f, 3.0f));
ASSERT_EQ(simplePhysicsComponent->GetRotation(), NiQuaternion(1.0f, 2.0f, 3.0f, 4.0f));
ASSERT_EQ(simplePhysicsComponent->GetVelocity(), NiPoint3(5.0f, 6.0f, 7.0f));
ASSERT_EQ(simplePhysicsComponent->GetAngularVelocity(), NiPoint3(5.0f, 6.0f, 7.0f));
ASSERT_EQ(simplePhysicsComponent->GetPhysicsMotionState(), 2);
simplePhysicsComponent->SetClimbableType(eClimbableType::CLIMBABLE_TYPE_LADDER);
simplePhysicsComponent->SetPosition(NiPoint3(4.0f, 5.0f, 6.0f));
simplePhysicsComponent->SetRotation(NiQuaternion(4.0f, 5.0f, 6.0f, 7.0f));
simplePhysicsComponent->SetVelocity(NiPoint3(6.0f, 7.0f, 8.0f));
simplePhysicsComponent->SetAngularVelocity(NiPoint3(6.0f, 7.0f, 8.0f));
simplePhysicsComponent->SetPhysicsMotionState(3);
ASSERT_EQ(simplePhysicsComponent->GetClimabbleType(), eClimbableType::CLIMBABLE_TYPE_LADDER);
ASSERT_EQ(simplePhysicsComponent->GetPosition(), NiPoint3(4.0f, 5.0f, 6.0f));
ASSERT_EQ(simplePhysicsComponent->GetRotation(), NiQuaternion(4.0f, 5.0f, 6.0f, 7.0f));
ASSERT_EQ(simplePhysicsComponent->GetVelocity(), NiPoint3(6.0f, 7.0f, 8.0f));
ASSERT_EQ(simplePhysicsComponent->GetAngularVelocity(), NiPoint3(6.0f, 7.0f, 8.0f));
ASSERT_EQ(simplePhysicsComponent->GetPhysicsMotionState(), 3);
}