Skip to content

Commit

Permalink
Xxxx randomization updates part 2
Browse files Browse the repository at this point in the history
Diffs=
99d28e1ac Xxxx randomization updates part 2 (#7097)

Co-authored-by: Alex Gibson <[email protected]>
Co-authored-by: hernan <[email protected]>
  • Loading branch information
3 people committed Apr 20, 2024
1 parent 8a0f927 commit 6fcc4fa
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .rive_head
Original file line number Diff line number Diff line change
@@ -1 +1 @@
a0004fa72b5b3c4f040b7807b186560703334dc4
99d28e1ac65672076b08f3a044218ec60268c4ae
4 changes: 4 additions & 0 deletions include/rive/animation/state_machine_instance.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include <stddef.h>
#include <vector>
#include "rive/animation/linear_animation_instance.hpp"
#include "rive/animation/state_instance.hpp"
#include "rive/animation/state_transition.hpp"
#include "rive/core/field_types/core_callback_type.hpp"
#include "rive/hit_result.hpp"
#include "rive/listener_type.hpp"
Expand Down Expand Up @@ -55,6 +57,8 @@ class StateMachineInstance : public Scene
void notifyEventListeners(const std::vector<EventReport>& events, NestedArtboard* source);
void sortHitComponents();
double randomValue();
StateTransition* findRandomTransition(StateInstance* stateFromInstance, bool ignoreTriggers);
StateTransition* findAllowedTransition(StateInstance* stateFromInstance, bool ignoreTriggers);

public:
StateMachineInstance(const StateMachine* machine, ArtboardInstance* instance);
Expand Down
64 changes: 50 additions & 14 deletions src/animation/state_machine_instance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,15 +172,11 @@ class StateMachineLayerInstance
return true;
}

bool tryChangeState(StateInstance* stateFromInstance, bool ignoreTriggers)
StateTransition* findRandomTransition(StateInstance* stateFromInstance, bool ignoreTriggers)
{
if (stateFromInstance == nullptr)
{
return false;
}
auto stateFrom = stateFromInstance->state();
auto outState = m_currentState;
uint32_t totalWeight = 0;
auto stateFrom = stateFromInstance->state();
// printf("stateFrom->transitionCount(): %zu\n", stateFrom->transitionCount());
for (size_t i = 0, length = stateFrom->transitionCount(); i < length; i++)
{
auto transition = stateFrom->transition(i);
Expand All @@ -190,11 +186,6 @@ class StateMachineLayerInstance
{
transition->evaluatedRandomWeight(transition->randomWeight());
totalWeight += transition->randomWeight();
if ((static_cast<LayerStateFlags>(stateFromInstance->state()->flags()) &
LayerStateFlags::Random) != LayerStateFlags::Random)
{
break;
}
}
else
{
Expand All @@ -207,7 +198,6 @@ class StateMachineLayerInstance
}
if (totalWeight > 0)
{

double randomWeight = randomValue() * totalWeight * 1.0;
float currentWeight = 0;
size_t index = 0;
Expand All @@ -218,11 +208,57 @@ class StateMachineLayerInstance
auto transitionWeight = transition->evaluatedRandomWeight();
if (currentWeight + transitionWeight > randomWeight)
{
break;
return transition;
}
currentWeight += transitionWeight;
index++;
}
}
return nullptr;
}

StateTransition* findAllowedTransition(StateInstance* stateFromInstance, bool ignoreTriggers)
{
auto stateFrom = stateFromInstance->state();
// If it should randomize
if ((static_cast<LayerStateFlags>(stateFrom->flags()) & LayerStateFlags::Random) ==
LayerStateFlags::Random)
{
return findRandomTransition(stateFromInstance, ignoreTriggers);
}
// Else search the first valid transition
for (size_t i = 0, length = stateFrom->transitionCount(); i < length; i++)
{
auto transition = stateFrom->transition(i);
auto allowed =
transition->allowed(stateFromInstance, m_stateMachineInstance, ignoreTriggers);
if (allowed == AllowTransition::yes && canChangeState(transition->stateTo()))
{
transition->evaluatedRandomWeight(transition->randomWeight());
return transition;
}
else
{
transition->evaluatedRandomWeight(0);
if (allowed == AllowTransition::waitingForExit)
{
m_waitingForExit = true;
}
}
}
return nullptr;
}

bool tryChangeState(StateInstance* stateFromInstance, bool ignoreTriggers)
{
if (stateFromInstance == nullptr)
{
return false;
}
auto outState = m_currentState;
auto transition = findAllowedTransition(stateFromInstance, ignoreTriggers);
if (transition != nullptr)
{
changeState(transition->stateTo());
m_stateMachineChangedOnAdvance = true;
// state actually has changed
Expand Down

0 comments on commit 6fcc4fa

Please sign in to comment.