From a4a91f1e737f310393eec8c738497fc8cf1e07a3 Mon Sep 17 00:00:00 2001 From: David Markowitz Date: Tue, 20 May 2025 18:49:48 -0700 Subject: [PATCH] Move in all directions is functional --- dGame/Entity.cpp | 16 +++ dGame/Entity.h | 2 + dGame/dComponents/ModelComponent.cpp | 6 + dGame/dComponents/ModelComponent.h | 4 +- .../PropertyManagementComponent.cpp | 5 +- dGame/dComponents/SimplePhysicsComponent.cpp | 7 ++ dGame/dComponents/SimplePhysicsComponent.h | 2 + dGame/dPropertyBehaviors/Strip.cpp | 103 +++++++++++++++--- dGame/dPropertyBehaviors/Strip.h | 14 ++- 9 files changed, 139 insertions(+), 20 deletions(-) diff --git a/dGame/Entity.cpp b/dGame/Entity.cpp index 35cd10fb..6bd4e569 100644 --- a/dGame/Entity.cpp +++ b/dGame/Entity.cpp @@ -1994,6 +1994,22 @@ void Entity::SetRotation(const NiQuaternion& rotation) { Game::entityManager->SerializeEntity(this); } +void Entity::SetVelocity(const NiPoint3& velocity) { + auto* controllable = GetComponent(); + + if (controllable != nullptr) { + controllable->SetVelocity(velocity); + } + + auto* simple = GetComponent(); + + if (simple != nullptr) { + simple->SetVelocity(velocity); + } + + Game::entityManager->SerializeEntity(this); +} + bool Entity::GetBoolean(const std::u16string& name) const { return GetVar(name); } diff --git a/dGame/Entity.h b/dGame/Entity.h index 700672c3..ab61f27b 100644 --- a/dGame/Entity.h +++ b/dGame/Entity.h @@ -148,6 +148,8 @@ public: void SetRespawnRot(const NiQuaternion& rotation); + void SetVelocity(const NiPoint3& velocity); + /** * Component management */ diff --git a/dGame/dComponents/ModelComponent.cpp b/dGame/dComponents/ModelComponent.cpp index 730aefa2..90fabb27 100644 --- a/dGame/dComponents/ModelComponent.cpp +++ b/dGame/dComponents/ModelComponent.cpp @@ -30,10 +30,16 @@ bool ModelComponent::OnResetModelToDefaults(GameMessages::GameMsg& msg) { unsmash.target = GetParent()->GetObjectID(); unsmash.duration = 0.0f; unsmash.Send(UNASSIGNED_SYSTEM_ADDRESS); + + m_Parent->SetPosition(m_OriginalPosition); + m_Parent->SetRotation(m_OriginalRotation); + m_Parent->SetVelocity(NiPoint3Constant::ZERO); + m_NumListeningInteract = 0; m_NumActiveUnSmash = 0; m_Dirty = true; Game::entityManager->SerializeEntity(GetParent()); + return true; } diff --git a/dGame/dComponents/ModelComponent.h b/dGame/dComponents/ModelComponent.h index e63a7f0e..e8111e59 100644 --- a/dGame/dComponents/ModelComponent.h +++ b/dGame/dComponents/ModelComponent.h @@ -41,7 +41,7 @@ public: * Returns the original position of the model * @return the original position of the model */ - const NiPoint3& GetPosition() { return m_OriginalPosition; } + const NiPoint3& GetOriginalPosition() { return m_OriginalPosition; } /** * Sets the original position of the model @@ -53,7 +53,7 @@ public: * Returns the original rotation of the model * @return the original rotation of the model */ - const NiQuaternion& GetRotation() { return m_OriginalRotation; } + const NiQuaternion& GetOriginalRotation() { return m_OriginalRotation; } /** * Sets the original rotation of the model diff --git a/dGame/dComponents/PropertyManagementComponent.cpp b/dGame/dComponents/PropertyManagementComponent.cpp index f20a2886..2dd7f0d2 100644 --- a/dGame/dComponents/PropertyManagementComponent.cpp +++ b/dGame/dComponents/PropertyManagementComponent.cpp @@ -696,8 +696,9 @@ void PropertyManagementComponent::Save() { Database::Get()->AddBehavior(info); } - const auto position = entity->GetPosition(); - const auto rotation = entity->GetRotation(); + // Always save the original position so we can move the model freely + const auto& position = modelComponent->GetOriginalPosition(); + const auto& rotation = modelComponent->GetOriginalRotation(); if (std::find(present.begin(), present.end(), id) == present.end()) { IPropertyContents::Model model; diff --git a/dGame/dComponents/SimplePhysicsComponent.cpp b/dGame/dComponents/SimplePhysicsComponent.cpp index 825570f2..64034412 100644 --- a/dGame/dComponents/SimplePhysicsComponent.cpp +++ b/dGame/dComponents/SimplePhysicsComponent.cpp @@ -33,6 +33,13 @@ SimplePhysicsComponent::SimplePhysicsComponent(Entity* parent, int32_t component SimplePhysicsComponent::~SimplePhysicsComponent() { } +void SimplePhysicsComponent::Update(const float deltaTime) { + if (m_Velocity == NiPoint3Constant::ZERO) return; + m_Position += m_Velocity * deltaTime; + m_DirtyPosition = true; + Game::entityManager->SerializeEntity(m_Parent); +} + void SimplePhysicsComponent::Serialize(RakNet::BitStream& outBitStream, bool bIsInitialUpdate) { if (bIsInitialUpdate) { outBitStream.Write(m_ClimbableType != eClimbableType::CLIMBABLE_TYPE_NOT); diff --git a/dGame/dComponents/SimplePhysicsComponent.h b/dGame/dComponents/SimplePhysicsComponent.h index 61362712..e669dea8 100644 --- a/dGame/dComponents/SimplePhysicsComponent.h +++ b/dGame/dComponents/SimplePhysicsComponent.h @@ -33,6 +33,8 @@ public: SimplePhysicsComponent(Entity* parent, int32_t componentID); ~SimplePhysicsComponent() override; + void Update(const float deltaTime) override; + void Serialize(RakNet::BitStream& outBitStream, bool bIsInitialUpdate) override; /** diff --git a/dGame/dPropertyBehaviors/Strip.cpp b/dGame/dPropertyBehaviors/Strip.cpp index b1dd94eb..a483e842 100644 --- a/dGame/dPropertyBehaviors/Strip.cpp +++ b/dGame/dPropertyBehaviors/Strip.cpp @@ -97,6 +97,8 @@ void Strip::HandleMsg(GameMessages::ResetModelToDefaults& msg) { m_WaitingForAction = false; m_PausedTime = 0.0f; m_NextActionIndex = 0; + m_InActionMove = NiPoint3Constant::ZERO; + m_PreviousFramePosition = NiPoint3Constant::ZERO; } void Strip::IncrementAction() { @@ -127,19 +129,37 @@ void Strip::ProcNormalAction(float deltaTime, ModelComponent& modelComponent) { auto number = nextAction.GetValueParameterDouble(); auto numberAsInt = static_cast(number); auto nextActionType = GetNextAction().GetType(); - if (nextActionType == "SpawnStromling") { - Spawn(10495, entity); // Stromling property - } else if (nextActionType == "SpawnPirate") { - Spawn(10497, entity); // Maelstrom Pirate property - } else if (nextActionType == "SpawnRonin") { - Spawn(10498, entity); // Dark Ronin property - } else if (nextActionType == "DropImagination") { - for (; numberAsInt > 0; numberAsInt--) SpawnDrop(935, entity); // 1 Imagination powerup - } else if (nextActionType == "DropHealth") { - for (; numberAsInt > 0; numberAsInt--) SpawnDrop(177, entity); // 1 Life powerup - } else if (nextActionType == "DropArmor") { - for (; numberAsInt > 0; numberAsInt--) SpawnDrop(6431, entity); // 1 Armor powerup - } else if (nextActionType == "Smash") { + + // TODO replace with switch case and nextActionType with enum + /* BEGIN Move */ + if (nextActionType == "FlyUp" || nextActionType == "FlyDown") { + bool isFlyDown = nextActionType == "FlyDown"; + m_PreviousFramePosition = entity.GetPosition(); + m_InActionMove.y = isFlyDown ? -number : number; + + // Default velocity is 3 units per second. + entity.SetVelocity(NiPoint3{0.0f, isFlyDown ? -3.0f : 3.0f, 0.0f}); + } + else if (nextActionType == "MoveRight" || nextActionType == "MoveLeft") { + bool isMoveLeft = nextActionType == "MoveLeft"; + m_PreviousFramePosition = entity.GetPosition(); + m_InActionMove.x = isMoveLeft ? -number : number; + + // Default velocity is 3 units per second. + entity.SetVelocity(NiPoint3{isMoveLeft ? -3.0f : 3.0f, 0.0f, 0.0f}); + } + else if (nextActionType == "MoveForward" || nextActionType == "MoveBackward") { + bool isMoveBackward = nextActionType == "MoveBackward"; + m_PreviousFramePosition = entity.GetPosition(); + m_InActionMove.z = isMoveBackward ? -number : number; + + // Default velocity is 3 units per second. + entity.SetVelocity(NiPoint3{0.0f, 0.0f, isMoveBackward ? -3.0f : 3.0f}); + } + /* END Move */ + + /* BEGIN Action */ + else if (nextActionType == "Smash") { if (!modelComponent.IsUnSmashing()) { GameMessages::Smash smash{}; smash.target = entity.GetObjectID(); @@ -162,7 +182,24 @@ void Strip::ProcNormalAction(float deltaTime, ModelComponent& modelComponent) { sound.target = modelComponent.GetParent()->GetObjectID(); sound.soundID = numberAsInt; sound.Send(UNASSIGNED_SYSTEM_ADDRESS); - } else { + } + /* END Action */ + /* BEGIN Gameplay */ + else if (nextActionType == "SpawnStromling") { + Spawn(10495, entity); // Stromling property + } else if (nextActionType == "SpawnPirate") { + Spawn(10497, entity); // Maelstrom Pirate property + } else if (nextActionType == "SpawnRonin") { + Spawn(10498, entity); // Dark Ronin property + } else if (nextActionType == "DropImagination") { + for (; numberAsInt > 0; numberAsInt--) SpawnDrop(935, entity); // 1 Imagination powerup + } else if (nextActionType == "DropHealth") { + for (; numberAsInt > 0; numberAsInt--) SpawnDrop(177, entity); // 1 Life powerup + } else if (nextActionType == "DropArmor") { + for (; numberAsInt > 0; numberAsInt--) SpawnDrop(6431, entity); // 1 Armor powerup + } + /* END Gameplay */ + else { static std::set g_WarnedActions; if (!g_WarnedActions.contains(nextActionType.data())) { LOG("Tried to play action (%s) which is not supported.", nextActionType.data()); @@ -187,7 +224,43 @@ void Strip::RemoveStates(ModelComponent& modelComponent) const { } } +bool Strip::CheckMovement(float deltaTime, ModelComponent& modelComponent) { + auto& entity = *modelComponent.GetParent(); + const auto& currentPos = entity.GetPosition(); + const auto diff = currentPos - m_PreviousFramePosition; + const auto [moveX, moveY, moveZ] = m_InActionMove; + m_PreviousFramePosition = currentPos; + + // Only want to subtract from the move if one is being performed. + // Starts at true because we may not be doing a move at all. + // If one is being done, then one of the move_ variables will be non-zero + bool moveFinished = true; + if (moveX != 0.0f) { + m_InActionMove.x -= diff.x; + // If the sign bit is different between the two numbers, then we have finished our move. + moveFinished = std::signbit(m_InActionMove.x) != std::signbit(moveX); + } else if (moveY != 0.0f) { + m_InActionMove.y -= diff.y; + // If the sign bit is different between the two numbers, then we have finished our move. + moveFinished = std::signbit(m_InActionMove.y) != std::signbit(moveY); + } else if (moveZ != 0.0f) { + m_InActionMove.z -= diff.z; + // If the sign bit is different between the two numbers, then we have finished our move. + moveFinished = std::signbit(m_InActionMove.z) != std::signbit(moveZ); + } + + // once done, set the in action move & velocity to zero + if (moveFinished) { + entity.SetVelocity(NiPoint3Constant::ZERO); + m_InActionMove = NiPoint3Constant::ZERO; + } + + return moveFinished; +} + void Strip::Update(float deltaTime, ModelComponent& modelComponent) { + if (!CheckMovement(deltaTime, modelComponent)) return; + m_PausedTime -= deltaTime; if (m_PausedTime > 0.0f) return; @@ -200,7 +273,7 @@ void Strip::Update(float deltaTime, ModelComponent& modelComponent) { RemoveStates(modelComponent); - // Check for starting blocks and if not a starting block proc this blocks action + // Check for trigger blocks and if not a trigger block proc this blocks action if (m_NextActionIndex == 0) { if (nextAction.GetType() == "OnInteract") { modelComponent.AddInteract(); diff --git a/dGame/dPropertyBehaviors/Strip.h b/dGame/dPropertyBehaviors/Strip.h index 3f929a7d..b84bbd38 100644 --- a/dGame/dPropertyBehaviors/Strip.h +++ b/dGame/dPropertyBehaviors/Strip.h @@ -29,6 +29,10 @@ public: void IncrementAction(); void Spawn(LOT object, Entity& entity); + + // Checks the movement logic for whether or not to proceed + // Returns true if the movement can continue, false if it needs to wait more. + bool CheckMovement(float deltaTime, ModelComponent& modelComponent); void Update(float deltaTime, ModelComponent& modelComponent); void SpawnDrop(LOT dropLOT, Entity& entity); void ProcNormalAction(float deltaTime, ModelComponent& modelComponent); @@ -37,7 +41,8 @@ private: // Indicates this Strip is waiting for an action to be taken upon it to progress to its actions bool m_WaitingForAction{ false }; - // The amount of time this strip is paused for. Any interactions with this strip should be bounced if this is greater than 0. + // The amount of time this strip is paused for. Any interactions with this strip should be bounced if this is greater than 0. + // Actions that do not use time do not use this (ex. positions). float m_PausedTime{ 0.0f }; // The index of the next action to be played. This should always be within range of [0, m_Actions.size()). @@ -48,6 +53,13 @@ private: // The location of this strip on the UGBehaviorEditor UI StripUiPosition m_Position; + + // The current actions remaining distance to the target + // Only 1 of these vertexs' will be active at once for any given strip. + NiPoint3 m_InActionMove{}; + + // The position of the parent model on the previous frame + NiPoint3 m_PreviousFramePosition{}; }; #endif //!__STRIP__H__