diff --git a/Content/BPML_StateMacros.uasset b/Content/BPML_StateMacros.uasset index 092c1de..35fbad5 100644 Binary files a/Content/BPML_StateMacros.uasset and b/Content/BPML_StateMacros.uasset differ diff --git a/Source/UnrealFightingFramework/GameplayFramework/FFGameState.cpp b/Source/UnrealFightingFramework/GameplayFramework/FFGameState.cpp new file mode 100644 index 0000000..ebb0e9a --- /dev/null +++ b/Source/UnrealFightingFramework/GameplayFramework/FFGameState.cpp @@ -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++; + } +} diff --git a/Source/UnrealFightingFramework/GameplayFramework/FFGameState.h b/Source/UnrealFightingFramework/GameplayFramework/FFGameState.h new file mode 100644 index 0000000..7e89f24 --- /dev/null +++ b/Source/UnrealFightingFramework/GameplayFramework/FFGameState.h @@ -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; +}; diff --git a/Source/UnrealFightingFramework/State/FFStateMachineComponent.cpp b/Source/UnrealFightingFramework/State/FFStateMachineComponent.cpp index b942bbf..679111c 100644 --- a/Source/UnrealFightingFramework/State/FFStateMachineComponent.cpp +++ b/Source/UnrealFightingFramework/State/FFStateMachineComponent.cpp @@ -4,9 +4,11 @@ // FF includes #include "FFState.h" +#include "GameplayFramework/FFGameState.h" #include "Input/FFPlayerController.h" #if !UE_BUILD_SHIPPING +class ASNGGameState; static int32 StateMachineDebug = 0; FAutoConsoleVariableRef CVARStateMachineDebug(TEXT("ff.StateMachine.ShowDebug"), StateMachineDebug, @@ -116,6 +118,7 @@ void UFFStateMachineComponent::GoToState(UFFState* NewState) CurrentState->Exit(GetCurrentStateContext()); CurrentState = NewState; TicksInState = 0; + TickStateWasEntered = GetWorld()->GetGameState()->GetCurrentTick(); CurrentSubStateLabel = NAME_None; CurrentState->Enter(GetCurrentStateContext()); } @@ -129,12 +132,6 @@ void UFFStateMachineComponent::GoToEntryState() } -int64 UFFStateMachineComponent::GetTicksInState() const -{ - return TicksInState; -} - - FName UFFStateMachineComponent::GetCurrentStateName() const { check(CurrentState) @@ -143,12 +140,6 @@ FName UFFStateMachineComponent::GetCurrentStateName() const } -FName UFFStateMachineComponent::GetCurrentSubStateLabel() const -{ - return CurrentSubStateLabel; -} - - void UFFStateMachineComponent::SetSubStateLabel(FName InSubStateLabel) { CurrentSubStateLabel = InSubStateLabel; diff --git a/Source/UnrealFightingFramework/State/FFStateMachineComponent.h b/Source/UnrealFightingFramework/State/FFStateMachineComponent.h index 38c990e..52b9c54 100644 --- a/Source/UnrealFightingFramework/State/FFStateMachineComponent.h +++ b/Source/UnrealFightingFramework/State/FFStateMachineComponent.h @@ -82,7 +82,15 @@ public: void GoToEntryState(); 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 @@ -91,7 +99,7 @@ public: FORCEINLINE FName GetCurrentStateName() const; UFUNCTION(BlueprintPure) - FORCEINLINE FName GetCurrentSubStateLabel() const; + FORCEINLINE FName GetCurrentSubStateLabel() const { return CurrentSubStateLabel; } UFUNCTION(BlueprintCallable) void SetSubStateLabel(FName InSubStateLabel); @@ -129,6 +137,13 @@ protected: /** How many ticks have elapsed since the currently active state was entered */ 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 */ UPROPERTY() UFFState* CurrentState;