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.
This commit is contained in:
David Markowitz 2025-06-10 22:51:24 -07:00
parent 263c329932
commit 0fb028e129
5 changed files with 93 additions and 25 deletions

View File

@ -2010,6 +2010,22 @@ void Entity::SetVelocity(const NiPoint3& velocity) {
Game::entityManager->SerializeEntity(this); Game::entityManager->SerializeEntity(this);
} }
const NiPoint3& Entity::GetVelocity() const {
auto* controllable = GetComponent<ControllablePhysicsComponent>();
if (controllable != nullptr) {
return controllable->GetVelocity();
}
auto* simple = GetComponent<SimplePhysicsComponent>();
if (simple != nullptr) {
return simple->GetVelocity();
}
return NiPoint3Constant::ZERO;
}
bool Entity::GetBoolean(const std::u16string& name) const { bool Entity::GetBoolean(const std::u16string& name) const {
return GetVar<bool>(name); return GetVar<bool>(name);
} }

View File

@ -124,6 +124,8 @@ public:
// then return the collision group from that. // then return the collision group from that.
int32_t GetCollisionGroup() const; int32_t GetCollisionGroup() const;
const NiPoint3& GetVelocity() const;
/** /**
* Setters * Setters
*/ */

View File

@ -209,3 +209,31 @@ void ModelComponent::RemoveUnSmash() {
LOG_DEBUG("Removing UnSmash %i", m_NumActiveUnSmash); LOG_DEBUG("Removing UnSmash %i", m_NumActiveUnSmash);
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);
}

View File

@ -130,6 +130,14 @@ public:
bool IsUnSmashing() const { return m_NumActiveUnSmash != 0; } bool IsUnSmashing() const { return m_NumActiveUnSmash != 0; }
void Resume(); 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: private:
// Number of Actions that are awaiting an UnSmash to finish. // Number of Actions that are awaiting an UnSmash to finish.
uint32_t m_NumActiveUnSmash{}; uint32_t m_NumActiveUnSmash{};

View File

@ -136,30 +136,31 @@ void Strip::ProcNormalAction(float deltaTime, ModelComponent& modelComponent) {
// TODO replace with switch case and nextActionType with enum // TODO replace with switch case and nextActionType with enum
/* BEGIN Move */ /* BEGIN Move */
if (nextActionType == "FlyUp" || nextActionType == "FlyDown") { if (nextActionType == "MoveRight" || nextActionType == "MoveLeft") {
bool isFlyDown = nextActionType == "FlyDown"; // X axis
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") {
bool isMoveLeft = nextActionType == "MoveLeft"; 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_PreviousFramePosition = entity.GetPosition();
m_InActionMove.x = isMoveLeft ? -number : number; 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") { } 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"; 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_PreviousFramePosition = entity.GetPosition();
m_InActionMove.z = isMoveBackward ? -number : number; 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 */ /* 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. // 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 // If one is being done, then one of the move_ variables will be non-zero
bool moveFinished = true; bool moveFinished = true;
NiPoint3 finalPositionAdjustment = NiPoint3Constant::ZERO;
if (moveX != 0.0f) { if (moveX != 0.0f) {
m_InActionMove.x -= diff.x; m_InActionMove.x -= diff.x;
// If the sign bit is different between the two numbers, then we have finished our move. // 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); moveFinished = std::signbit(m_InActionMove.x) != std::signbit(moveX);
finalPositionAdjustment.x = m_InActionMove.x;
} else if (moveY != 0.0f) { } else if (moveY != 0.0f) {
m_InActionMove.y -= diff.y; m_InActionMove.y -= diff.y;
// If the sign bit is different between the two numbers, then we have finished our move. // 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); moveFinished = std::signbit(m_InActionMove.y) != std::signbit(moveY);
finalPositionAdjustment.y = m_InActionMove.y;
} else if (moveZ != 0.0f) { } else if (moveZ != 0.0f) {
m_InActionMove.z -= diff.z; m_InActionMove.z -= diff.z;
// If the sign bit is different between the two numbers, then we have finished our move. // 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); moveFinished = std::signbit(m_InActionMove.z) != std::signbit(moveZ);
finalPositionAdjustment.z = m_InActionMove.z;
} }
// once done, set the in action move & velocity to zero // Once done, set the in action move & velocity to zero
if (moveFinished) { if (moveFinished && m_InActionMove != NiPoint3Constant::ZERO) {
entity.SetVelocity(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; 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. // Strips are also designed to have 2 actions or more to run.
if (!HasMinimumActions()) return; if (!HasMinimumActions()) return;
// Return if this strip has an active movement action
if (!CheckMovement(deltaTime, modelComponent)) return; if (!CheckMovement(deltaTime, modelComponent)) return;
// Don't run this strip if we're paused. // Don't run this strip if we're paused.