97 - Damage

This commit is contained in:
Kingsmedia 2022-05-07 13:03:19 +02:00
parent 26e44a4b4e
commit 835069d362
8 changed files with 73 additions and 16 deletions

View File

@ -53,10 +53,10 @@ void ABlasterCharacter::BeginPlay()
{
Super::BeginPlay();
BlasterPlayerController = Cast<ABlasterPlayerController>(Controller);
if (BlasterPlayerController)
UpdateHUDHealth();
if (HasAuthority())
{
BlasterPlayerController->SetHUDHealth(Health, MaxHealth);
OnTakeAnyDamage.AddDynamic(this, &ABlasterCharacter::ReceiveDamage);
}
}
@ -112,6 +112,15 @@ void ABlasterCharacter::PlayHitReactMontage()
}
}
void ABlasterCharacter::ReceiveDamage(AActor* DamagedActor, float Damage, const UDamageType* DamageType, AController* InstigatorController,
AActor* DamageCauser)
{
Health = FMath::Clamp(Health - Damage, 0.f, MaxHealth);
UpdateHUDHealth();
PlayHitReactMontage();
}
void ABlasterCharacter::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
@ -376,11 +385,6 @@ void ABlasterCharacter::TurnInPlace(float DeltaTime)
}
}
void ABlasterCharacter::MulticastHit_Implementation()
{
PlayHitReactMontage();
}
void ABlasterCharacter::HideCameraIfCharacterClose()
{
if (!IsLocallyControlled()) return;
@ -411,7 +415,17 @@ float ABlasterCharacter::CalculateSpeed()
void ABlasterCharacter::OnRep_Health()
{
UpdateHUDHealth();
PlayHitReactMontage();
}
void ABlasterCharacter::UpdateHUDHealth()
{
BlasterPlayerController = BlasterPlayerController == nullptr ?Cast<ABlasterPlayerController>(Controller) : BlasterPlayerController;
if (BlasterPlayerController)
{
BlasterPlayerController->SetHUDHealth(Health, MaxHealth);
}
}
void ABlasterCharacter::SetOverlappingWeapon(AWeapon* Weapon)

View File

@ -21,9 +21,6 @@ public:
virtual void PostInitializeComponents() override;
virtual void OnRep_ReplicatedMovement() override;
void PlayFireMontage(bool bAiming);
UFUNCTION(NetMulticast, Unreliable)
void MulticastHit();
protected:
virtual void BeginPlay() override;
@ -44,6 +41,10 @@ protected:
void FireButtonReleased();
void PlayHitReactMontage();
UFUNCTION()
void ReceiveDamage(AActor* DamagedActor, float Damage, const UDamageType* DamageType, class AController* InstigatorController, AActor* DamageCauser);
void UpdateHUDHealth();
private:
UPROPERTY(VisibleAnywhere, Category="Camera")
class USpringArmComponent* CameraBoom;

View File

@ -52,10 +52,6 @@ void AProjectile::BeginPlay()
void AProjectile::OnHit(UPrimitiveComponent* HitComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit)
{
if (ABlasterCharacter* BlasterCharacter = Cast<ABlasterCharacter>(OtherActor))
{
BlasterCharacter->MulticastHit();
}
Destroy();
}

View File

@ -22,6 +22,9 @@ protected:
UFUNCTION()
virtual void OnHit(UPrimitiveComponent* HitComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit);
virtual void Destroyed() override;
UPROPERTY(EditAnywhere)
float Damage = 20.f;
private:

View File

@ -0,0 +1,22 @@
// Fill out your copyright notice in the Description page of Project Settings.
#include "ProjectileBullet.h"
#include "GameFramework/Character.h"
#include "Kismet/GameplayStatics.h"
void AProjectileBullet::OnHit(UPrimitiveComponent* HitComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit)
{
ACharacter* OwnerCharacter = Cast<ACharacter>(GetOwner());
if (OwnerCharacter)
{
AController* OwnerController = OwnerCharacter->Controller;
if (OwnerController)
{
UGameplayStatics::ApplyDamage(OtherActor, Damage, OwnerController, this, UDamageType::StaticClass());
}
}
Super::OnHit(HitComp, OtherActor, OtherComp, NormalImpulse, Hit);
}

View File

@ -0,0 +1,21 @@
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "Projectile.h"
#include "ProjectileBullet.generated.h"
/**
*
*/
UCLASS()
class BLASTER_API AProjectileBullet : public AProjectile
{
GENERATED_BODY()
protected:
virtual void OnHit(UPrimitiveComponent* HitComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit) override;
};