From 9e2fe3c0f5e33c127784604461be6349aad248a1 Mon Sep 17 00:00:00 2001 From: PHILIP WHITE Date: Sat, 27 Jan 2024 10:00:22 +0000 Subject: [PATCH] Update Character for Johns Hunger Mechanic --- .../SeagullGame/SeagullGameCharacter.cpp | 28 ++++++++++++++++++- .../Source/SeagullGame/SeagullGameCharacter.h | 23 +++++++++++++-- 2 files changed, 48 insertions(+), 3 deletions(-) diff --git a/SeagullGame/Source/SeagullGame/SeagullGameCharacter.cpp b/SeagullGame/Source/SeagullGame/SeagullGameCharacter.cpp index f7d8f7c..31529d4 100644 --- a/SeagullGame/Source/SeagullGame/SeagullGameCharacter.cpp +++ b/SeagullGame/Source/SeagullGame/SeagullGameCharacter.cpp @@ -53,6 +53,19 @@ ASeagullGameCharacter::ASeagullGameCharacter() void ASeagullGameCharacter::Tick(float 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; TArray OverlappingActors; @@ -96,10 +109,13 @@ void ASeagullGameCharacter::BeginPlay() void ASeagullGameCharacter::DamagePlayer(float DamageAmount) { CurrentHealth -= DamageAmount; + OnPlayerDamage.Broadcast(); if (CurrentHealth <= 0) { CurrentHealth = 0; OnPlayerDeath.Broadcast(); + OnGameEnd.Broadcast("You Lose!"); + bHasGameEnded = true; } } @@ -114,6 +130,7 @@ void ASeagullGameCharacter::HealPlayer(float HealAmount) void ASeagullGameCharacter::StartGame() { + JohnsCurrentHunger = JohnsDefaultHunger; if (GetWorld()->GetTimerManager().IsTimerActive(GameTimerHandle)) { GetWorld()->GetTimerManager().ClearTimer(GameTimerHandle); @@ -123,7 +140,7 @@ void ASeagullGameCharacter::StartGame() void ASeagullGameCharacter::EndGame() { - OnGameEnd.Broadcast(); + OnGameEnd.Broadcast("You Win!"); } 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) { // Set up action bindings diff --git a/SeagullGame/Source/SeagullGame/SeagullGameCharacter.h b/SeagullGame/Source/SeagullGame/SeagullGameCharacter.h index 46061b6..e2d8847 100644 --- a/SeagullGame/Source/SeagullGame/SeagullGameCharacter.h +++ b/SeagullGame/Source/SeagullGame/SeagullGameCharacter.h @@ -17,8 +17,12 @@ class ASeagullGameCharacter : public ACharacter public: 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") FOnPlayerDeath OnPlayerDeath; UPROPERTY(BlueprintAssignable, Category = "Events") @@ -58,7 +62,7 @@ public: UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Health") float MaxHealth = 100; UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Health") - float CurrentHealth; + float CurrentHealth = 100; UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Health") float DefaultHealth = 100; @@ -75,6 +79,8 @@ public: FTimerHandle GameTimerHandle; UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "General") float GameTime = 120; + UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "General") + float Score = 0; UFUNCTION(BlueprintCallable, Category = "Item") void PickupItem(); @@ -93,6 +99,17 @@ public: UPROPERTY(BlueprintReadOnly, Category = "Item") 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: // APawn interface virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override; @@ -110,4 +127,6 @@ private: /** Camera boom positioning the camera above the character */ UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera, meta = (AllowPrivateAccess = "true")) class USpringArmComponent* CameraBoom; + + bool bHasGameEnded = false; };