Update Character for Johns Hunger Mechanic

This commit is contained in:
Philip W 2024-01-27 10:00:22 +00:00
parent 4952fff6c1
commit 9e2fe3c0f5
2 changed files with 48 additions and 3 deletions

View File

@ -53,6 +53,19 @@ ASeagullGameCharacter::ASeagullGameCharacter()
void ASeagullGameCharacter::Tick(float DeltaSeconds) void ASeagullGameCharacter::Tick(float DeltaSeconds)
{ {
Super::Tick(DeltaSeconds); Super::Tick(DeltaSeconds);
if (JohnsCurrentHunger > 0)
{
JohnsCurrentHunger -= DeltaSeconds * JohnsHungerDeclineAmount;
}
else if (!bHasGameEnded)
{
JohnsCurrentHunger = 0;
OnGameEnd.Broadcast("You Lose!");
bHasGameEnded = true;
}
if (!IsValid(PickupBox)) return; if (!IsValid(PickupBox)) return;
TArray<AActor*> OverlappingActors; TArray<AActor*> OverlappingActors;
@ -96,10 +109,13 @@ void ASeagullGameCharacter::BeginPlay()
void ASeagullGameCharacter::DamagePlayer(float DamageAmount) void ASeagullGameCharacter::DamagePlayer(float DamageAmount)
{ {
CurrentHealth -= DamageAmount; CurrentHealth -= DamageAmount;
OnPlayerDamage.Broadcast();
if (CurrentHealth <= 0) if (CurrentHealth <= 0)
{ {
CurrentHealth = 0; CurrentHealth = 0;
OnPlayerDeath.Broadcast(); OnPlayerDeath.Broadcast();
OnGameEnd.Broadcast("You Lose!");
bHasGameEnded = true;
} }
} }
@ -114,6 +130,7 @@ void ASeagullGameCharacter::HealPlayer(float HealAmount)
void ASeagullGameCharacter::StartGame() void ASeagullGameCharacter::StartGame()
{ {
JohnsCurrentHunger = JohnsDefaultHunger;
if (GetWorld()->GetTimerManager().IsTimerActive(GameTimerHandle)) if (GetWorld()->GetTimerManager().IsTimerActive(GameTimerHandle))
{ {
GetWorld()->GetTimerManager().ClearTimer(GameTimerHandle); GetWorld()->GetTimerManager().ClearTimer(GameTimerHandle);
@ -123,7 +140,7 @@ void ASeagullGameCharacter::StartGame()
void ASeagullGameCharacter::EndGame() void ASeagullGameCharacter::EndGame()
{ {
OnGameEnd.Broadcast(); OnGameEnd.Broadcast("You Win!");
} }
void ASeagullGameCharacter::PickupItem() void ASeagullGameCharacter::PickupItem()
@ -171,6 +188,15 @@ void ASeagullGameCharacter::OnPickupBoxEndOverlap(UPrimitiveComponent* Overlappe
} }
} }
void ASeagullGameCharacter::IncreaseJohnsHunger(float HungerAmount)
{
JohnsCurrentHunger += HungerAmount;
if (JohnsCurrentHunger > JohnsMaxHunger)
{
JohnsCurrentHunger = JohnsMaxHunger;
}
}
void ASeagullGameCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent) void ASeagullGameCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{ {
// Set up action bindings // Set up action bindings

View File

@ -17,8 +17,12 @@ class ASeagullGameCharacter : public ACharacter
public: public:
DECLARE_DYNAMIC_MULTICAST_DELEGATE(FOnPlayerDeath); DECLARE_DYNAMIC_MULTICAST_DELEGATE(FOnPlayerDeath);
DECLARE_DYNAMIC_MULTICAST_DELEGATE(FOnGameEnd); DECLARE_DYNAMIC_MULTICAST_DELEGATE(FOnPlayerDamage);
DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FOnGameEnd, FString, Message);
UPROPERTY(BlueprintAssignable, Category = "Events")
FOnPlayerDeath OnPlayerDamage;
UPROPERTY(BlueprintAssignable, Category = "Events") UPROPERTY(BlueprintAssignable, Category = "Events")
FOnPlayerDeath OnPlayerDeath; FOnPlayerDeath OnPlayerDeath;
UPROPERTY(BlueprintAssignable, Category = "Events") UPROPERTY(BlueprintAssignable, Category = "Events")
@ -58,7 +62,7 @@ public:
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Health") UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Health")
float MaxHealth = 100; float MaxHealth = 100;
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Health") UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Health")
float CurrentHealth; float CurrentHealth = 100;
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Health") UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Health")
float DefaultHealth = 100; float DefaultHealth = 100;
@ -75,6 +79,8 @@ public:
FTimerHandle GameTimerHandle; FTimerHandle GameTimerHandle;
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "General") UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "General")
float GameTime = 120; float GameTime = 120;
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "General")
float Score = 0;
UFUNCTION(BlueprintCallable, Category = "Item") UFUNCTION(BlueprintCallable, Category = "Item")
void PickupItem(); void PickupItem();
@ -93,6 +99,17 @@ public:
UPROPERTY(BlueprintReadOnly, Category = "Item") UPROPERTY(BlueprintReadOnly, Category = "Item")
UBoxComponent* PickupBox; UBoxComponent* PickupBox;
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "John")
float JohnsCurrentHunger = 0;
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "John")
float JohnsDefaultHunger = 100;
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "John")
float JohnsMaxHunger = 200;
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "John")
float JohnsHungerDeclineAmount = 10.f;
UFUNCTION(BlueprintCallable, Category = "John")
void IncreaseJohnsHunger(float HungerAmount);
protected: protected:
// APawn interface // APawn interface
virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override; virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
@ -110,4 +127,6 @@ private:
/** Camera boom positioning the camera above the character */ /** Camera boom positioning the camera above the character */
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera, meta = (AllowPrivateAccess = "true")) UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera, meta = (AllowPrivateAccess = "true"))
class USpringArmComponent* CameraBoom; class USpringArmComponent* CameraBoom;
bool bHasGameEnded = false;
}; };