Skip to content

Commit dadc0a8

Browse files
committed
Added battery play state
1 parent de4998b commit dadc0a8

File tree

2 files changed

+47
-2
lines changed

2 files changed

+47
-2
lines changed

Source/BatteryCollector/BatteryCollectorGameMode.cpp

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ ABatteryCollectorGameMode::ABatteryCollectorGameMode()
2222
void ABatteryCollectorGameMode::BeginPlay()
2323
{
2424
Super::BeginPlay();
25+
SetCurrentState(EBatteryPlayState::EPlaying);
26+
2527
ABatteryCollectorCharacter* MyCharacter = Cast<ABatteryCollectorCharacter>(UGameplayStatics::GetPlayerPawn(this, 0));
2628
if (MyCharacter)
2729
{
@@ -48,17 +50,38 @@ void ABatteryCollectorGameMode::Tick(float DeltaTime)
4850
ABatteryCollectorCharacter* MyCharacter = Cast<ABatteryCollectorCharacter>(UGameplayStatics::GetPlayerPawn(this, 0));
4951
if (MyCharacter)
5052
{
53+
// If our power is greater than needed to win, set the game's state to Won
54+
if (MyCharacter->GetCurrentPower() > PowerToWin)
55+
{
56+
SetCurrentState(EBatteryPlayState::EWon);
57+
}
5158
// if the character's power is positive
52-
if (MyCharacter->GetCurrentPower() > 0)
59+
else if (MyCharacter->GetCurrentPower() > 0)
5360
{
5461
// decrease the character's power using the decay rate
5562
MyCharacter->UpdatePower(-DeltaTime*DecayRate*(MyCharacter->GetInitialPower()));
5663
}
64+
else
65+
{
66+
SetCurrentState(EBatteryPlayState::EGameOver);
67+
}
68+
5769
}
5870

5971
}
6072

6173
float ABatteryCollectorGameMode::GetPowerToWin() const
6274
{
6375
return PowerToWin;
64-
}
76+
}
77+
78+
EBatteryPlayState ABatteryCollectorGameMode::GetCurrentState() const
79+
{
80+
return CurrentState;
81+
}
82+
83+
void ABatteryCollectorGameMode::SetCurrentState(EBatteryPlayState NewState)
84+
{
85+
//set current state
86+
CurrentState = NewState;
87+
}

Source/BatteryCollector/BatteryCollectorGameMode.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,17 @@
33
#include "GameFramework/GameMode.h"
44
#include "BatteryCollectorGameMode.generated.h"
55

6+
7+
//enum to store the current state of gameplay
8+
UENUM(BlueprintType)
9+
enum class EBatteryPlayState
10+
{
11+
EPlaying,
12+
EGameOver,
13+
EWon,
14+
EUnknown
15+
};
16+
617
UCLASS(minimalapi)
718
class ABatteryCollectorGameMode : public AGameMode
819
{
@@ -19,6 +30,13 @@ class ABatteryCollectorGameMode : public AGameMode
1930

2031
virtual void BeginPlay() override;
2132

33+
/** Returns the current playing state */
34+
UFUNCTION(BlueprintPure, Category = "Power")
35+
EBatteryPlayState GetCurrentState() const;
36+
37+
/** Sets a new playing state */
38+
void SetCurrentState(EBatteryPlayState NewState);
39+
2240
protected:
2341
/**The rate at which the character loses power */
2442
UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = "Power", Meta = (BlueprintProtected = "true"))
@@ -35,6 +53,10 @@ class ABatteryCollectorGameMode : public AGameMode
3553
/** The instance of the HUD */
3654
UPROPERTY()
3755
class UUserWidget* CurrentWidget;
56+
57+
private:
58+
/**Keeps track of the current playing state */
59+
EBatteryPlayState CurrentState;
3860
};
3961

4062

0 commit comments

Comments
 (0)