UnrealFightingFramework/Source/UnrealFightingEngine/State/FEStateMachineComponent.h

119 lines
3.4 KiB
C++

// Unreal Fighting Engine by Kevin Poretti
#pragma once
// FE includes
#include "UnrealFightingEngine/IFESystemInterface.h"
// UE includes
#include "CoreMinimal.h"
#include "Components/ActorComponent.h"
#include "FEStateMachineComponent.generated.h"
/**
* A state machine is a component that evaluates and controls the transitions for state objects that
* are a part of this state machine.
*
* This component also calls the appropriate state logic when a state is changed or the component ticks.
*/
UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
class UNREALFIGHTINGENGINE_API UFEStateMachineComponent : public UActorComponent, public IIFESystemInterface
{
GENERATED_BODY()
public:
UFEStateMachineComponent();
/**
* Creates and adds default states and enters the first state
*/
void Initialize();
/**
* Initializes pointers to what owns this state machine and what avatar this state represents.
*
* These pointers will also be passed to the states owned by this state machine.
*
* @param InOwner Actor that owns this state machine.
* @param InAvatar Actor that this state machine represents.
*/
virtual void InitActorInfo(AActor* InOwner, AActor* InAvatar);
/**
* Creates an instance of the state class and adds newly created state to this state machine.
*
* @param StateClassToAdd State class type to be added to this state machine
*
* @return A pointer to the state that was added or nullptr if there was an issue adding or creating the state
*/
UFEState* AddState(TSubclassOf<UFEState> StateClassToAdd);
/**
* Creates an instance of the state classes and adds newly created states to this state machine.
*
* @param StateClassesToAdd Array of state class types to be added to this state machine
*/
void AddStates(const TArray<TSubclassOf<UFEState>>& StateClassesToAdd);
/**
* Destroys the state with corresponding name and removes it from this state machine.
*/
void RemoveState(FName StateToRemove);
/**
* Transitions from CurrentState to the new state passed to this function
*
* Triggers the Exit callback on the CurrentState and the Enter callback on the new state
*/
void SwitchStates(UFEState* NewState);
/**
* Returns the name of the current state
*/
UFUNCTION(BlueprintPure)
FName GetCurrentStateName() const;
// IIFESystemInterface interface
virtual void FixedTick(float OneFrame) override;
// End of IIFESystemInterface interface
protected:
/**
* Actor that owns this state machine.
* This will typically be a player controller that possesses the avatar.
*/
UPROPERTY(BlueprintReadOnly)
AActor* Owner;
/**
* The avatar is an actor that this state machine represents.
* This will typically be a pawn or character the state machine is attached to.
*/
UPROPERTY(BlueprintReadOnly)
AActor* Avatar;
/**
* States classes to create and add to this state machine when the game starts
*/
UPROPERTY(EditDefaultsOnly, Category="UFE|State Machine")
TArray<TSubclassOf<UFEState>> DefaultStates;
/** Current active state for this state machine */
UPROPERTY(BlueprintReadOnly)
UFEState* CurrentState;
// States that have been added
UPROPERTY(BlueprintReadOnly)
TArray<UFEState*> States;
/**
* Returns the state with corresponding name
*/
UFEState* FindStateWithName(FName StateName);
// UActorComponent interface
virtual void BeginPlay() override;
// End of UActorComponent interface
};