Move fixed framerate logic from SNG to FF

This commit is contained in:
Kevin Poretti 2023-10-21 18:51:04 -04:00
parent c43aca536a
commit 755ee34343
5 changed files with 86 additions and 14 deletions

Binary file not shown.

View File

@ -0,0 +1,25 @@
// Project Sword & Gun Copyright Kevin Poretti
#include "GameplayFramework/FFGameState.h"
void AFFGameState::OnFixedTick(float OneTick)
{
}
void AFFGameState::Tick(float DeltaSeconds)
{
Super::Tick(DeltaSeconds);
// Run anything game logic related at a fixed tick rate
// TODO: Interpolate between prev and current game state if there is time left in accumulator
AccumulatedTime += DeltaSeconds;
while(AccumulatedTime > ONE_TICK)
{
OnFixedTick(ONE_TICK);
AccumulatedTime -= ONE_TICK;
CurrentTick++;
}
}

View File

@ -0,0 +1,41 @@
// Project Sword & Gun Copyright Kevin Poretti
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/GameState.h"
#include "FFGameState.generated.h"
constexpr float ONE_TICK = 0.0083333333;
constexpr int64 TICKS_PER_SECOND = 120;
/**
*
*/
UCLASS()
class UNREALFIGHTINGFRAMEWORK_API AFFGameState : public AGameState
{
GENERATED_BODY()
public:
UFUNCTION(BlueprintPure)
int64 GetCurrentTick() const { return CurrentTick; }
virtual void OnFixedTick(float OneTick);
// Begin AActor interface
virtual void Tick(float DeltaSeconds) override;
// End AActor interface
private:
/**
* Amount of time accumulated from ticks. When Tick is called the delta time since the
* last Tick will be added to this variable.
*
* Once enough time has accumulated to simulate at least one frame (defined by the value ONE_TICK) then the
* game logic will update/tick. ONE_TICK's worth of time will be subtracted from the this variable
*/
float AccumulatedTime = 0;
int64 CurrentTick = 0;
};

View File

@ -4,9 +4,11 @@
// FF includes // FF includes
#include "FFState.h" #include "FFState.h"
#include "GameplayFramework/FFGameState.h"
#include "Input/FFPlayerController.h" #include "Input/FFPlayerController.h"
#if !UE_BUILD_SHIPPING #if !UE_BUILD_SHIPPING
class ASNGGameState;
static int32 StateMachineDebug = 0; static int32 StateMachineDebug = 0;
FAutoConsoleVariableRef CVARStateMachineDebug(TEXT("ff.StateMachine.ShowDebug"), FAutoConsoleVariableRef CVARStateMachineDebug(TEXT("ff.StateMachine.ShowDebug"),
StateMachineDebug, StateMachineDebug,
@ -116,6 +118,7 @@ void UFFStateMachineComponent::GoToState(UFFState* NewState)
CurrentState->Exit(GetCurrentStateContext()); CurrentState->Exit(GetCurrentStateContext());
CurrentState = NewState; CurrentState = NewState;
TicksInState = 0; TicksInState = 0;
TickStateWasEntered = GetWorld()->GetGameState<AFFGameState>()->GetCurrentTick();
CurrentSubStateLabel = NAME_None; CurrentSubStateLabel = NAME_None;
CurrentState->Enter(GetCurrentStateContext()); CurrentState->Enter(GetCurrentStateContext());
} }
@ -129,12 +132,6 @@ void UFFStateMachineComponent::GoToEntryState()
} }
int64 UFFStateMachineComponent::GetTicksInState() const
{
return TicksInState;
}
FName UFFStateMachineComponent::GetCurrentStateName() const FName UFFStateMachineComponent::GetCurrentStateName() const
{ {
check(CurrentState) check(CurrentState)
@ -143,12 +140,6 @@ FName UFFStateMachineComponent::GetCurrentStateName() const
} }
FName UFFStateMachineComponent::GetCurrentSubStateLabel() const
{
return CurrentSubStateLabel;
}
void UFFStateMachineComponent::SetSubStateLabel(FName InSubStateLabel) void UFFStateMachineComponent::SetSubStateLabel(FName InSubStateLabel)
{ {
CurrentSubStateLabel = InSubStateLabel; CurrentSubStateLabel = InSubStateLabel;

View File

@ -82,7 +82,15 @@ public:
void GoToEntryState(); void GoToEntryState();
UFUNCTION(BlueprintPure) UFUNCTION(BlueprintPure)
FORCEINLINE int64 GetTicksInState() const; FORCEINLINE int64 GetTicksInState() const { return TicksInState; }
/**
* Returns the tick number which corresponds to when this state was entered into.
*
* The tick number represents the ticks that have elapsed since the game began.
*/
UFUNCTION(BlueprintPure)
FORCEINLINE int64 GetTickStateWasEntered() const { return TickStateWasEntered; }
/** /**
* Returns the name of the current state * Returns the name of the current state
@ -91,7 +99,7 @@ public:
FORCEINLINE FName GetCurrentStateName() const; FORCEINLINE FName GetCurrentStateName() const;
UFUNCTION(BlueprintPure) UFUNCTION(BlueprintPure)
FORCEINLINE FName GetCurrentSubStateLabel() const; FORCEINLINE FName GetCurrentSubStateLabel() const { return CurrentSubStateLabel; }
UFUNCTION(BlueprintCallable) UFUNCTION(BlueprintCallable)
void SetSubStateLabel(FName InSubStateLabel); void SetSubStateLabel(FName InSubStateLabel);
@ -129,6 +137,13 @@ protected:
/** How many ticks have elapsed since the currently active state was entered */ /** How many ticks have elapsed since the currently active state was entered */
int64 TicksInState; int64 TicksInState;
/**
* The tick number which corresponds to when this state was entered into.
*
* The tick number represents the ticks that have elapsed since the game began.
*/
int64 TickStateWasEntered;
/** Current active state for this state machine */ /** Current active state for this state machine */
UPROPERTY() UPROPERTY()
UFFState* CurrentState; UFFState* CurrentState;