From 0fb028e129018b65b32c9ebb947cc0da1fac0753 Mon Sep 17 00:00:00 2001 From: David Markowitz Date: Tue, 10 Jun 2025 22:51:24 -0700 Subject: [PATCH] feat: add movement behaviors the following behaviors will function MoveRight MoveLeft FlyUp FlyDown MoveForward MoveBackward The behavior of the behaviors is once a move in an axis is active, that behavior must finish its movement before another one on that axis can do another movement on it. --- dGame/Entity.cpp | 16 ++++++++ dGame/Entity.h | 6 ++- dGame/dComponents/ModelComponent.cpp | 28 +++++++++++++ dGame/dComponents/ModelComponent.h | 8 ++++ dGame/dPropertyBehaviors/Strip.cpp | 60 +++++++++++++++++----------- 5 files changed, 93 insertions(+), 25 deletions(-) diff --git a/dGame/Entity.cpp b/dGame/Entity.cpp index 6bd4e569..c51c6152 100644 --- a/dGame/Entity.cpp +++ b/dGame/Entity.cpp @@ -2010,6 +2010,22 @@ void Entity::SetVelocity(const NiPoint3& velocity) { Game::entityManager->SerializeEntity(this); } +const NiPoint3& Entity::GetVelocity() const { + auto* controllable = GetComponent(); + + if (controllable != nullptr) { + return controllable->GetVelocity(); + } + + auto* simple = GetComponent(); + + if (simple != nullptr) { + return simple->GetVelocity(); + } + + return NiPoint3Constant::ZERO; +} + bool Entity::GetBoolean(const std::u16string& name) const { return GetVar(name); } diff --git a/dGame/Entity.h b/dGame/Entity.h index ab61f27b..310d6b39 100644 --- a/dGame/Entity.h +++ b/dGame/Entity.h @@ -124,6 +124,8 @@ public: // then return the collision group from that. int32_t GetCollisionGroup() const; + const NiPoint3& GetVelocity() const; + /** * Setters */ @@ -331,7 +333,7 @@ public: * @brief The observable for player entity position updates. */ static Observable OnPlayerPositionUpdate; - + protected: LWOOBJID m_ObjectID; @@ -437,7 +439,7 @@ const T& Entity::GetVar(const std::u16string& name) const { template T Entity::GetVarAs(const std::u16string& name) const { const auto data = GetVarAsString(name); - + return GeneralUtils::TryParse(data).value_or(LDFData::Default); } diff --git a/dGame/dComponents/ModelComponent.cpp b/dGame/dComponents/ModelComponent.cpp index 90fabb27..d1e19cc8 100644 --- a/dGame/dComponents/ModelComponent.cpp +++ b/dGame/dComponents/ModelComponent.cpp @@ -209,3 +209,31 @@ void ModelComponent::RemoveUnSmash() { LOG_DEBUG("Removing UnSmash %i", m_NumActiveUnSmash); m_NumActiveUnSmash--; } + +bool ModelComponent::TrySetVelocity(const NiPoint3& velocity) const { + auto currentVelocity = m_Parent->GetVelocity(); + + // If we're currently moving on an axis, prevent the move so only 1 behavior can have control over an axis + if (velocity != NiPoint3Constant::ZERO) { + const auto [x, y, z] = velocity; + if (x != 0.0f) { + if (currentVelocity.x != 0.0f) return false; + currentVelocity.x = x; + } else if (y != 0.0f) { + if (currentVelocity.y != 0.0f) return false; + currentVelocity.y = y; + } else if (z != 0.0f) { + if (currentVelocity.z != 0.0f) return false; + currentVelocity.z = z; + } + } else { + currentVelocity = velocity; + } + + m_Parent->SetVelocity(currentVelocity); + return true; +} + +void ModelComponent::SetVelocity(const NiPoint3& velocity) const { + m_Parent->SetVelocity(velocity); +} diff --git a/dGame/dComponents/ModelComponent.h b/dGame/dComponents/ModelComponent.h index e8111e59..8d430d2e 100644 --- a/dGame/dComponents/ModelComponent.h +++ b/dGame/dComponents/ModelComponent.h @@ -130,6 +130,14 @@ public: bool IsUnSmashing() const { return m_NumActiveUnSmash != 0; } void Resume(); + + // Attempts to set the velocity of an axis for movement. + // If the axis currently has a velocity of zero, returns true. + // If the axis is currently controlled by a behavior, returns false. + bool TrySetVelocity(const NiPoint3& velocity) const; + + // Force sets the velocity to a value. + void SetVelocity(const NiPoint3& velocity) const; private: // Number of Actions that are awaiting an UnSmash to finish. uint32_t m_NumActiveUnSmash{}; diff --git a/dGame/dPropertyBehaviors/Strip.cpp b/dGame/dPropertyBehaviors/Strip.cpp index a2f06e55..9ceae743 100644 --- a/dGame/dPropertyBehaviors/Strip.cpp +++ b/dGame/dPropertyBehaviors/Strip.cpp @@ -136,30 +136,31 @@ void Strip::ProcNormalAction(float deltaTime, ModelComponent& modelComponent) { // 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}); - modelComponent.SetVelocity(NiPoint3{0.0f, isFlyDown ? -3.0f : 3.0f, 0.0f}); - } - else if (nextActionType == "MoveRight" || nextActionType == "MoveLeft") { + if (nextActionType == "MoveRight" || nextActionType == "MoveLeft") { + // X axis 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") { + if (modelComponent.TrySetVelocity(NiPoint3{ isMoveLeft ? -3.0f : 3.0f, 0.0f, 0.0f })) { + m_PreviousFramePosition = entity.GetPosition(); + m_InActionMove.x = isMoveLeft ? -number : number; + } + } else if (nextActionType == "FlyUp" || nextActionType == "FlyDown") { + // Y axis + bool isFlyDown = nextActionType == "FlyDown"; + // Default velocity is 3 units per second. + if (modelComponent.TrySetVelocity(NiPoint3{ 0.0f, isFlyDown ? -3.0f : 3.0f, 0.0f })) { + m_PreviousFramePosition = entity.GetPosition(); + m_InActionMove.y = isFlyDown ? -number : number; + } + + } else if (nextActionType == "MoveForward" || nextActionType == "MoveBackward") { + // Z axis 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}); + if (modelComponent.TrySetVelocity(NiPoint3{ 0.0f, 0.0f, isMoveBackward ? -3.0f : 3.0f })) { + m_PreviousFramePosition = entity.GetPosition(); + m_InActionMove.z = isMoveBackward ? -number : number; + } } /* END Move */ @@ -239,23 +240,35 @@ bool Strip::CheckMovement(float deltaTime, ModelComponent& modelComponent) { // 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; + NiPoint3 finalPositionAdjustment = NiPoint3Constant::ZERO; 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); + finalPositionAdjustment.x = m_InActionMove.x; } 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); + finalPositionAdjustment.y = m_InActionMove.y; } 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); + finalPositionAdjustment.z = m_InActionMove.z; } - // once done, set the in action move & velocity to zero - if (moveFinished) { - entity.SetVelocity(NiPoint3Constant::ZERO); + // Once done, set the in action move & velocity to zero + if (moveFinished && m_InActionMove != NiPoint3Constant::ZERO) { + auto entityVelocity = entity.GetVelocity(); + // Zero out only the velocity that was acted on + if (moveX != 0.0f) entityVelocity.x = 0.0f; + else if (moveY != 0.0f) entityVelocity.y = 0.0f; + else if (moveZ != 0.0f) entityVelocity.z = 0.0f; + modelComponent.SetVelocity(entityVelocity); + + // Do the final adjustment so we will have moved exactly the requested units + entity.SetPosition(entity.GetPosition() + finalPositionAdjustment); m_InActionMove = NiPoint3Constant::ZERO; } @@ -267,6 +280,7 @@ void Strip::Update(float deltaTime, ModelComponent& modelComponent) { // Strips are also designed to have 2 actions or more to run. if (!HasMinimumActions()) return; + // Return if this strip has an active movement action if (!CheckMovement(deltaTime, modelComponent)) return; // Don't run this strip if we're paused.