diff --git a/dGame/Entity.cpp b/dGame/Entity.cpp index 35cd10fb..c51c6152 100644 --- a/dGame/Entity.cpp +++ b/dGame/Entity.cpp @@ -1994,6 +1994,38 @@ 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); +} + +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 700672c3..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 */ @@ -148,6 +150,8 @@ public: void SetRespawnRot(const NiQuaternion& rotation); + void SetVelocity(const NiPoint3& velocity); + /** * Component management */ @@ -329,7 +333,7 @@ public: * @brief The observable for player entity position updates. */ static Observable OnPlayerPositionUpdate; - + protected: LWOOBJID m_ObjectID; @@ -435,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 730aefa2..d1e19cc8 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; } @@ -203,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 e63a7f0e..8d430d2e 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 @@ -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/dComponents/PropertyManagementComponent.cpp b/dGame/dComponents/PropertyManagementComponent.cpp index 25a7d036..5dba7c19 100644 --- a/dGame/dComponents/PropertyManagementComponent.cpp +++ b/dGame/dComponents/PropertyManagementComponent.cpp @@ -704,8 +704,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 8f5a6aaa..9ceae743 100644 --- a/dGame/dPropertyBehaviors/Strip.cpp +++ b/dGame/dPropertyBehaviors/Strip.cpp @@ -99,6 +99,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() { @@ -131,19 +133,39 @@ 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 == "MoveRight" || nextActionType == "MoveLeft") { + // X axis + bool isMoveLeft = nextActionType == "MoveLeft"; + // Default velocity is 3 units per second. + 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"; + // Default velocity is 3 units per second. + 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 */ + + /* BEGIN Action */ + else if (nextActionType == "Smash") { if (!modelComponent.IsUnSmashing()) { GameMessages::Smash smash{}; smash.target = entity.GetObjectID(); @@ -166,7 +188,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()); @@ -190,11 +229,60 @@ 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; + 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 && 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; + } + + return moveFinished; +} + void Strip::Update(float deltaTime, ModelComponent& modelComponent) { // No point in running a strip with only one action. // 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. m_PausedTime -= deltaTime; if (m_PausedTime > 0.0f) return; @@ -209,7 +297,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 6e4aa66d..a0085f9e 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); @@ -40,7 +44,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()). @@ -51,6 +56,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__