2023-06-03 22:38:35 +00:00
|
|
|
// Unreal Fighting Engine by Kevin Poretti
|
2023-06-03 18:37:14 +00:00
|
|
|
|
2023-06-04 19:56:36 +00:00
|
|
|
// FE includes
|
|
|
|
#include "FEState.h"
|
|
|
|
|
2023-06-03 18:37:14 +00:00
|
|
|
#include "FEStateMachineComponent.h"
|
|
|
|
|
|
|
|
UFEStateMachineComponent::UFEStateMachineComponent()
|
|
|
|
{
|
2023-06-04 19:56:36 +00:00
|
|
|
// Don't use Unreal's tick instead use a fixed tick
|
2023-06-03 18:37:14 +00:00
|
|
|
PrimaryComponentTick.bCanEverTick = false;
|
2023-06-04 19:56:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void UFEStateMachineComponent::Initialize()
|
|
|
|
{
|
|
|
|
for(const TSubclassOf<UFEState>& CurrState : DefaultStates)
|
|
|
|
{
|
|
|
|
UFEState* TempState = AddState(CurrState);
|
|
|
|
if(!CurrentState) // first state to be created is the entry into this state machine
|
|
|
|
{
|
|
|
|
CurrentState = TempState;
|
|
|
|
CurrentState->Enter();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-06-03 18:37:14 +00:00
|
|
|
|
2023-06-04 19:56:36 +00:00
|
|
|
|
|
|
|
void UFEStateMachineComponent::InitActorInfo(AActor* InOwner, AActor* InAvatar)
|
|
|
|
{
|
|
|
|
Owner = InOwner;
|
|
|
|
Avatar = InAvatar;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
UFEState* UFEStateMachineComponent::AddState(TSubclassOf<UFEState> StateClassToAdd)
|
|
|
|
{
|
|
|
|
UFEState* TempState = NewObject<UFEState>(this, StateClassToAdd);
|
|
|
|
if(TempState)
|
|
|
|
{
|
|
|
|
States.Add(TempState);
|
|
|
|
TempState->InitActorInfo(Owner, Avatar);
|
|
|
|
return TempState;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void UFEStateMachineComponent::AddStates(const TArray<TSubclassOf<UFEState>>& StateClassesToAdd)
|
|
|
|
{
|
|
|
|
for(const TSubclassOf<UFEState>& CurrState : StateClassesToAdd)
|
|
|
|
{
|
|
|
|
AddState(CurrState);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void UFEStateMachineComponent::RemoveState(FName StateToRemove)
|
|
|
|
{
|
|
|
|
UE_LOG(LogTemp, Error, TEXT("UFEStateMachineComponent::RemoveState is not yet implemented"));
|
|
|
|
}
|
|
|
|
|
|
|
|
void UFEStateMachineComponent::SwitchStates(UFEState* NewState)
|
|
|
|
{
|
|
|
|
check(NewState);
|
|
|
|
|
|
|
|
CurrentState->Exit();
|
|
|
|
CurrentState = NewState;
|
|
|
|
CurrentState->Enter();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
FName UFEStateMachineComponent::GetCurrentStateName() const
|
|
|
|
{
|
|
|
|
return CurrentState ? CurrentState->Name : NAME_None;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
UFEState* UFEStateMachineComponent::FindStateWithName(FName StateName)
|
|
|
|
{
|
|
|
|
for (UFEState* CurrState : States)
|
|
|
|
{
|
|
|
|
if(CurrState ->Name == StateName)
|
|
|
|
{
|
|
|
|
return CurrState;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
UE_LOG(LogTemp, Warning,
|
|
|
|
TEXT("Could not find state in state machine with name %s on %s"), *StateName.ToString(), *Owner->GetName());
|
|
|
|
|
|
|
|
return nullptr;
|
2023-06-03 18:37:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void UFEStateMachineComponent::BeginPlay()
|
|
|
|
{
|
|
|
|
Super::BeginPlay();
|
|
|
|
|
2023-06-04 19:56:36 +00:00
|
|
|
Initialize();
|
2023-06-03 18:37:14 +00:00
|
|
|
}
|
|
|
|
|
2023-06-04 19:56:36 +00:00
|
|
|
|
2023-06-03 18:37:14 +00:00
|
|
|
void UFEStateMachineComponent::FixedTick(float OneFrame)
|
|
|
|
{
|
2023-06-04 19:56:36 +00:00
|
|
|
// Should we switch states?
|
|
|
|
|
|
|
|
for(UFEState* CurrState : States)
|
|
|
|
{
|
|
|
|
// Check if the state is enabled
|
|
|
|
|
|
|
|
// Check state entry conditions if there are any
|
|
|
|
|
|
|
|
// Check input conditions if there are any
|
|
|
|
|
|
|
|
// If all state entry conditions are good and at least one input condition is good then we can transition
|
|
|
|
|
|
|
|
// Lastly just check if the state we're about to transition into isn't the current state.
|
|
|
|
// It is OK to transition if state's "CanTransitionToSelf" is true
|
|
|
|
|
|
|
|
// SwitchStates(NewState);
|
|
|
|
// return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// CurrentState should never be null
|
|
|
|
// TODO: Should probably assert or whatever UE's equivalent is
|
|
|
|
check(CurrentState);
|
|
|
|
|
|
|
|
// Tick current state
|
|
|
|
CurrentState->FixedTick(OneFrame);
|
|
|
|
|
|
|
|
// Debug
|
2023-06-03 18:37:14 +00:00
|
|
|
}
|
|
|
|
|