Section 13: The Enemy Class - Lecture 258

Hide Health Bar
This commit is contained in:
charnet3d 2024-03-03 12:13:02 +01:00
parent 1de94528c8
commit 09827265de
9 changed files with 120 additions and 23 deletions

Binary file not shown.

BIN
Content/_Game/Enemies/EnemyBP.uasset (Stored with Git LFS)

Binary file not shown.

BIN
Content/_Game/HUD/EnemyHealthBarBP.uasset (Stored with Git LFS) Normal file

Binary file not shown.

BIN
Content/_Game/Maps/DefaultMap.umap (Stored with Git LFS)

Binary file not shown.

View File

@ -7,7 +7,10 @@
#include "Particles/ParticleSystemComponent.h"
// Sets default values
AEnemy::AEnemy()
AEnemy::AEnemy() :
Health(100.f),
MaxHealth(100.f),
HealthBarDisplayTime(4.f)
{
// Set this character to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
@ -22,6 +25,12 @@ void AEnemy::BeginPlay()
GetMesh()->SetCollisionResponseToChannel(ECollisionChannel::ECC_Visibility, ECollisionResponse::ECR_Block);
}
void AEnemy::ShowHealthBar_Implementation()
{
GetWorldTimerManager().ClearTimer(HealthBarTimer);
GetWorldTimerManager().SetTimer(HealthBarTimer, this, &AEnemy::HideHealthBar, HealthBarDisplayTime);
}
// Called every frame
void AEnemy::Tick(float DeltaTime)
{
@ -47,5 +56,23 @@ void AEnemy::BulletHit_Implementation(FHitResult HitResult)
{
UGameplayStatics::SpawnEmitterAtLocation(GetWorld(), ImpactParticles, HitResult.Location, FRotator(0.f), true);
}
ShowHealthBar();
}
float AEnemy::TakeDamage(float DamageAmount, FDamageEvent const& DamageEvent, AController* EventInstigator,
AActor* DamageCauser)
{
float DamageInflicted = DamageAmount;
if (Health - DamageAmount <= 0.f)
{
DamageInflicted = Health;
Health = 0.f;
}
else
{
Health -= DamageAmount;
}
return DamageInflicted;
}

View File

@ -23,6 +23,14 @@ protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
UFUNCTION(BlueprintNativeEvent)
void ShowHealthBar();
void ShowHealthBar_Implementation();
UFUNCTION(BlueprintImplementableEvent)
void HideHealthBar();
private:
/** Particles to spawn when hit by bullets */
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Combat, meta = (AllowPrivateAccess = true))
UParticleSystem* ImpactParticles;
@ -30,6 +38,23 @@ protected:
/** Sound to play when hit by bullets */
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Combat, meta = (AllowPrivateAccess = true))
USoundCue* ImpactSound;
/** Current health */
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Combat, meta = (AllowPrivateAccess = true))
float Health;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Combat, meta = (AllowPrivateAccess = true))
float MaxHealth;
/** Name of the head bone */
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Combat, meta = (AllowPrivateAccess = true))
FString HeadBone;
/** Time to display health bar once shot */
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Combat, meta = (AllowPrivateAccess = true))
float HealthBarDisplayTime;
FTimerHandle HealthBarTimer;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
@ -38,4 +63,8 @@ public:
virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
virtual void BulletHit_Implementation(FHitResult HitResult) override;
virtual float TakeDamage(float DamageAmount, FDamageEvent const& DamageEvent, AController* EventInstigator, AActor* DamageCauser) override;
FORCEINLINE FString GetHeadBone() const { return HeadBone; }
};

View File

@ -6,8 +6,6 @@
#include "Item.h"
#include "Camera/CameraComponent.h"
#include "Components/SphereComponent.h"
#include "Components/BoxComponent.h"
#include "Components/CapsuleComponent.h"
#include "Components/WidgetComponent.h"
#include "Engine/SkeletalMeshSocket.h"
@ -18,8 +16,8 @@
#include "Sound/SoundCue.h"
#include "Ammo.h"
#include "BulletHitInterface.h"
#include "Enemy.h"
#include "PhysicalMaterials/PhysicalMaterial.h"
#include "Shooter.h"
// Sets default values
AShooterCharacter::AShooterCharacter() :
@ -662,24 +660,48 @@ void AShooterCharacter::SendBullet()
// Spawn impact particles after updating BeamEndPoint
// Does hit actor implement BulletHitInterface?
IBulletHitInterface* BulletHitInterface = nullptr;
if (IsValid(BeamHitResult.GetActor()) &&
(BulletHitInterface = Cast<IBulletHitInterface>(BeamHitResult.GetActor())) != nullptr)
bool spawnGenericImpactParticles = false;
if (IsValid(BeamHitResult.GetActor()))
{
BulletHitInterface->BulletHit_Implementation(BeamHitResult);
UE_LOG(LogTemp, Warning, TEXT("actor particle"));
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, TEXT("actor particle"));
if (IBulletHitInterface* BulletHitInterface = Cast<IBulletHitInterface>(BeamHitResult.GetActor()))
{
BulletHitInterface->BulletHit_Implementation(BeamHitResult);
}
else
{
spawnGenericImpactParticles = true;
}
if (AEnemy* HitEnemy = Cast<AEnemy>(BeamHitResult.GetActor()))
{
float damageToApply;
if (BeamHitResult.BoneName.ToString() == HitEnemy->GetHeadBone())
{
damageToApply = EquippedWeapon->GetHeadshotDamage();
}
else
{
damageToApply = EquippedWeapon->GetDamage();
}
UGameplayStatics::ApplyDamage(HitEnemy,
damageToApply,
GetController(), this,
UDamageType::StaticClass());
UE_LOG(LogTemp, Warning, TEXT("Hit Component: %s"), *BeamHitResult.BoneName.ToString());
}
}
else
{
if (ImpactParticles)
{
UGameplayStatics::SpawnEmitterAtLocation(GetWorld(), ImpactParticles,
BeamHitResult.Location);
UE_LOG(LogTemp, Warning, TEXT("normal particle"));
}
spawnGenericImpactParticles = true;
}
if (ImpactParticles && spawnGenericImpactParticles)
{
UGameplayStatics::SpawnEmitterAtLocation(GetWorld(), ImpactParticles,
BeamHitResult.Location);
}
// Spawn smoke trail particles
if (BeamParticles)
{

View File

@ -156,6 +156,8 @@ void AWeapon::OnConstruction(const FTransform& Transform)
}
bAutomatic = WeaponDataRow->bAutomatic;
Damage = WeaponDataRow->Damage;
HeadshotDamage = WeaponDataRow->HeadshotDamage;
}
if (GetMaterialInstance())

View File

@ -90,6 +90,12 @@ struct FWeaponDataTable : public FTableRowBase
UPROPERTY(EditAnywhere, BlueprintReadWrite)
bool bAutomatic;
UPROPERTY(EditAnywhere, BlueprintReadWrite)
float Damage;
UPROPERTY(EditAnywhere, BlueprintReadWrite)
float HeadshotDamage;
};
/**
@ -221,6 +227,12 @@ private:
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Weapon Properties", meta = (AllowPrivateAccess = "true"))
bool bAutomatic;
/** Amount of damage caused by bullet */
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Weapon Properties", meta = (AllowPrivateAccess = "true"))
float Damage;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Weapon Properties", meta = (AllowPrivateAccess = "true"))
float HeadshotDamage;
public:
/** Adds an impulse to the weapon */
void ThrowWeapon();
@ -239,6 +251,8 @@ public:
FORCEINLINE UParticleSystem* GetMuzzleFlash() const { return MuzzleFlash; }
FORCEINLINE USoundCue* GetFireSound() const { return FireSound; }
FORCEINLINE bool GetAutomatic() const { return bAutomatic; }
FORCEINLINE float GetDamage() const { return Damage; }
FORCEINLINE float GetHeadshotDamage() const { return HeadshotDamage; }
void StartSlideTimer();