From 75c067e0d1ea3aa51178fff8c49d861d2c8ad0f4 Mon Sep 17 00:00:00 2001 From: PHILIP WHITE Date: Fri, 26 Jan 2024 21:13:03 +0000 Subject: [PATCH] Update Character for Damage Control --- .../SeagullGame/SeagullGameCharacter.cpp | 28 +++++++++++++++---- .../Source/SeagullGame/SeagullGameCharacter.h | 18 +++++++++++- .../SeagullGame/SeagullGameGameMode.cpp | 14 ---------- 3 files changed, 39 insertions(+), 21 deletions(-) diff --git a/SeagullGame/Source/SeagullGame/SeagullGameCharacter.cpp b/SeagullGame/Source/SeagullGame/SeagullGameCharacter.cpp index 7741f58..5faf330 100644 --- a/SeagullGame/Source/SeagullGame/SeagullGameCharacter.cpp +++ b/SeagullGame/Source/SeagullGame/SeagullGameCharacter.cpp @@ -67,11 +67,30 @@ void ASeagullGameCharacter::BeginPlay() } } +void ASeagullGameCharacter::DamagePlayer(float DamageAmount) +{ + CurrentHealth -= DamageAmount; + if (CurrentHealth <= 0) + { + CurrentHealth = 0; + OnPlayerDeath.Broadcast(); + } +} + +void ASeagullGameCharacter::HealPlayer(float HealAmount) +{ + CurrentHealth += HealAmount; + if (CurrentHealth > MaxHealth) + { + CurrentHealth = MaxHealth; + } +} + void ASeagullGameCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent) { // Set up action bindings - if (UEnhancedInputComponent* EnhancedInputComponent = CastChecked(PlayerInputComponent)) { - + if (UEnhancedInputComponent* EnhancedInputComponent = CastChecked(PlayerInputComponent)) + { //Jumping EnhancedInputComponent->BindAction(JumpAction, ETriggerEvent::Triggered, this, &ACharacter::Jump); EnhancedInputComponent->BindAction(JumpAction, ETriggerEvent::Completed, this, &ACharacter::StopJumping); @@ -81,7 +100,6 @@ void ASeagullGameCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInp //Looking // EnhancedInputComponent->BindAction(LookAction, ETriggerEvent::Triggered, this, &ATP_ThirdPersonCharacter::Look); - } } @@ -90,8 +108,6 @@ void ASeagullGameCharacter::Move(const FInputActionValue& Value) // input is a Vector2D FVector2D MovementVector = Value.Get(); - GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, FString::Printf(TEXT("Move: %f, %f"), MovementVector.X, MovementVector.Y)); - if (Controller != nullptr) { // find out which way is forward @@ -100,7 +116,7 @@ void ASeagullGameCharacter::Move(const FInputActionValue& Value) // get forward vector const FVector ForwardDirection = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X); - + // get right vector const FVector RightDirection = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::Y); diff --git a/SeagullGame/Source/SeagullGame/SeagullGameCharacter.h b/SeagullGame/Source/SeagullGame/SeagullGameCharacter.h index 4804fbd..f4c1c92 100644 --- a/SeagullGame/Source/SeagullGame/SeagullGameCharacter.h +++ b/SeagullGame/Source/SeagullGame/SeagullGameCharacter.h @@ -13,6 +13,11 @@ class ASeagullGameCharacter : public ACharacter GENERATED_BODY() public: + DECLARE_DYNAMIC_MULTICAST_DELEGATE(FOnPlayerDeath); + + UPROPERTY(BlueprintAssignable, Category = "Events") + FOnPlayerDeath OnPlayerDeath; + ASeagullGameCharacter(); // Called every frame. @@ -40,6 +45,18 @@ public: virtual void BeginPlay() override; + UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Health") + float MaxHealth = 100; + UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Health") + float CurrentHealth; + UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Health") + float DefaultHealth = 100; + + UFUNCTION(BlueprintCallable, Category = "Health") + void DamagePlayer(float DamageAmount); + UFUNCTION(BlueprintCallable, Category = "Health") + void HealPlayer(float HealAmount); + protected: // APawn interface virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override; @@ -57,5 +74,4 @@ private: /** Camera boom positioning the camera above the character */ UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera, meta = (AllowPrivateAccess = "true")) class USpringArmComponent* CameraBoom; - }; diff --git a/SeagullGame/Source/SeagullGame/SeagullGameGameMode.cpp b/SeagullGame/Source/SeagullGame/SeagullGameGameMode.cpp index 8f0e72d..5c6d4a7 100644 --- a/SeagullGame/Source/SeagullGame/SeagullGameGameMode.cpp +++ b/SeagullGame/Source/SeagullGame/SeagullGameGameMode.cpp @@ -9,18 +9,4 @@ ASeagullGameGameMode::ASeagullGameGameMode() { // use our custom PlayerController class PlayerControllerClass = ASeagullGamePlayerController::StaticClass(); - - // set default pawn class to our Blueprinted character - static ConstructorHelpers::FClassFinder PlayerPawnBPClass(TEXT("/Game/TopDown/Blueprints/BP_TopDownCharacter")); - if (PlayerPawnBPClass.Class != nullptr) - { - DefaultPawnClass = PlayerPawnBPClass.Class; - } - - // set default controller to our Blueprinted controller - static ConstructorHelpers::FClassFinder PlayerControllerBPClass(TEXT("/Game/TopDown/Blueprints/BP_TopDownPlayerController")); - if(PlayerControllerBPClass.Class != NULL) - { - PlayerControllerClass = PlayerControllerBPClass.Class; - } } \ No newline at end of file