Skip to content

Commit

Permalink
[Add] Health System Functions and listeners
Browse files Browse the repository at this point in the history
  • Loading branch information
iJotape4 committed Oct 20, 2024
1 parent 3c08c30 commit 4baa7c8
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 5 deletions.
Binary file added BP_Pics/UI/HealthBar/WB_HealtbarDesign.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
24 changes: 24 additions & 0 deletions GameJamPlus2024/Source/GameJamPlus2024/Private/HealthBar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,28 @@


#include "HealthBar.h"
#include "EventsManager.h"

void UHealthBar::OnDamageTaken(E_EventType Arg, int Damage)
{
SetHealthBarPercent(-Damage);
UE_LOG(LogActor, Warning, TEXT("Damage Taken: %d"), Damage);
}

void UHealthBar::NativeConstruct()
{
EventsManager::AddListener(E_EventType::DamageTaken, this, &UHealthBar::OnDamageTaken);
EventsManager::AddListener(E_EventType::MaxHealthUpgrade, this, &UHealthBar::SetMaxHealth);
}

void UHealthBar::SetHealthBarPercent(float Percent)
{
float NewPercent = HealthBar->GetPercent() + Percent / MaxHealth;
HealthBar->SetPercent(NewPercent);
}

void UHealthBar::SetMaxHealth(E_EventType Arg, int NewMaxHealth)
{
MaxHealth = NewMaxHealth;
HealthBar->SetPercent(1.0f);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include "EnhancedInputComponent.h"
#include "EnhancedInputSubsystems.h"
#include "EventsManager.h"
#include "PaperFlipbookComponent.h"
#include "GameJamPlus2024/GameJamPlus2024Character.h"

Expand Down Expand Up @@ -111,10 +112,15 @@ void APaperCharacterBase::RotateCamera(const FInputActionValue& Value)
{
TargetRotation.Yaw += CameraRotationAngle; // Rotate right
}

/// DEBUG ONLY: TakeDamage(10);
GetController()->SetControlRotation(TargetRotation);
}

void APaperCharacterBase::TakeDamage(UINT Damage)
{
EventsManager::SendEvent(E_EventType::DamageTaken, Damage);
}


void APaperCharacterBase::BeginPlay()
{
Expand Down
2 changes: 2 additions & 0 deletions GameJamPlus2024/Source/GameJamPlus2024/Public/EventsManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
UENUM(BlueprintType)
enum class E_EventType : uint8
{
DamageTaken,
EnemyDeath,
MaxHealthUpgrade,
};

DECLARE_DELEGATE_TwoParams(FOnCallback, E_EventType, const int32);
Expand Down
23 changes: 19 additions & 4 deletions GameJamPlus2024/Source/GameJamPlus2024/Public/HealthBar.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,33 @@
#pragma once

#include "CoreMinimal.h"
#include "EventsManager.h"
#include "Blueprint/UserWidget.h"
#include "Components/ProgressBar.h"
#include "HealthBar.generated.h"

/**
*
*/
UCLASS()
class GAMEJAMPLUS2024_API UHealthBar : public UUserWidget
{
GENERATED_BODY()

void OnDamageTaken(E_EventType Arg, int Damage);

public:
UPROPERTY(VisibleAnywhere, BlueprintReadWrite, meta = (BindWidget))
UProgressBar* HealthBar;

virtual void BeginPlay() override;
protected:
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Health", meta = (AllowPrivateAccess = "true"))
int MaxHealth = 100;

protected:
// This function is called when the widget is constructed
virtual void NativeConstruct() override;

UFUNCTION(BlueprintCallable)
void SetHealthBarPercent(float Percent);

UFUNCTION(BlueprintCallable)
void SetMaxHealth(E_EventType Arg, int NewMaxHealth);
};
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class GAMEJAMPLUS2024_API APaperCharacterBase : public APaperCharacter
void Move(const FInputActionValue& Value);
void SwitchAnimation(const FVector2d& Value);
void RotateCamera(const FInputActionValue& Value);
void TakeDamage(UINT Damage);

// APawn interface
virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
Expand Down

0 comments on commit 4baa7c8

Please sign in to comment.