diff --git a/Content/Blueprints/Weapon/BP_AssaultRifle.uasset b/Content/Blueprints/Weapon/BP_AssaultRifle.uasset index dd3fde4..fad8d43 100644 Binary files a/Content/Blueprints/Weapon/BP_AssaultRifle.uasset and b/Content/Blueprints/Weapon/BP_AssaultRifle.uasset differ diff --git a/Content/Blueprints/Weapon/Projectiles/BP_ProjectileBullet.uasset b/Content/Blueprints/Weapon/Projectiles/BP_ProjectileBullet.uasset new file mode 100644 index 0000000..367e585 Binary files /dev/null and b/Content/Blueprints/Weapon/Projectiles/BP_ProjectileBullet.uasset differ diff --git a/Source/Blaster/Character/BlasterCharacter.cpp b/Source/Blaster/Character/BlasterCharacter.cpp index 982b74d..3e5b036 100644 --- a/Source/Blaster/Character/BlasterCharacter.cpp +++ b/Source/Blaster/Character/BlasterCharacter.cpp @@ -53,10 +53,10 @@ void ABlasterCharacter::BeginPlay() { Super::BeginPlay(); - BlasterPlayerController = Cast(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(Controller) : BlasterPlayerController; + if (BlasterPlayerController) + { + BlasterPlayerController->SetHUDHealth(Health, MaxHealth); + } } void ABlasterCharacter::SetOverlappingWeapon(AWeapon* Weapon) diff --git a/Source/Blaster/Character/BlasterCharacter.h b/Source/Blaster/Character/BlasterCharacter.h index 0e552a8..8230de3 100644 --- a/Source/Blaster/Character/BlasterCharacter.h +++ b/Source/Blaster/Character/BlasterCharacter.h @@ -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; diff --git a/Source/Blaster/Weapon/Projectile.cpp b/Source/Blaster/Weapon/Projectile.cpp index 7d2ba97..ed3ecfe 100644 --- a/Source/Blaster/Weapon/Projectile.cpp +++ b/Source/Blaster/Weapon/Projectile.cpp @@ -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(OtherActor)) - { - BlasterCharacter->MulticastHit(); - } Destroy(); } diff --git a/Source/Blaster/Weapon/Projectile.h b/Source/Blaster/Weapon/Projectile.h index 53aebe2..4e7c5ed 100644 --- a/Source/Blaster/Weapon/Projectile.h +++ b/Source/Blaster/Weapon/Projectile.h @@ -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: diff --git a/Source/Blaster/Weapon/ProjectileBullet.cpp b/Source/Blaster/Weapon/ProjectileBullet.cpp new file mode 100644 index 0000000..0cb814d --- /dev/null +++ b/Source/Blaster/Weapon/ProjectileBullet.cpp @@ -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(GetOwner()); + if (OwnerCharacter) + { + AController* OwnerController = OwnerCharacter->Controller; + if (OwnerController) + { + UGameplayStatics::ApplyDamage(OtherActor, Damage, OwnerController, this, UDamageType::StaticClass()); + } + } + + Super::OnHit(HitComp, OtherActor, OtherComp, NormalImpulse, Hit); +} diff --git a/Source/Blaster/Weapon/ProjectileBullet.h b/Source/Blaster/Weapon/ProjectileBullet.h new file mode 100644 index 0000000..8e3e8d7 --- /dev/null +++ b/Source/Blaster/Weapon/ProjectileBullet.h @@ -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; + +};