109 - Weapon Ammo

This commit is contained in:
Kingsmedia 2022-05-09 18:39:41 +02:00
parent 1eaa5ffca5
commit 9373e4a1c2
9 changed files with 90 additions and 1 deletions

View File

@ -137,6 +137,10 @@ void ABlasterCharacter::Eliminated()
void ABlasterCharacter::MulticastEliminated_Implementation()
{
if (BlasterPlayerController)
{
BlasterPlayerController->SetHUDWeaponAmmo(0);
}
bEliminated = true;
PlayEliminatedMontage();

View File

@ -274,7 +274,10 @@ void UCombatComponent::TraceUnderCrosshairs(FHitResult& TraceHitResult)
void UCombatComponent::EquipWeapon(AWeapon* WeaponToEquip)
{
if (Character == nullptr || WeaponToEquip == nullptr) return;
if (EquippedWeapon)
{
EquippedWeapon->Dropped();
}
EquippedWeapon = WeaponToEquip;
EquippedWeapon->SetWeaponState(EWeaponState::EWS_Equipped);
const USkeletalMeshSocket* HandSocket = Character->GetMesh()->GetSocketByName(FName("RightHandSocket"));
@ -283,6 +286,7 @@ void UCombatComponent::EquipWeapon(AWeapon* WeaponToEquip)
HandSocket->AttachActor(EquippedWeapon, Character->GetMesh());
}
EquippedWeapon->SetOwner(Character);
EquippedWeapon->SetHUDAmmo();
Character->GetCharacterMovement()->bOrientRotationToMovement = false;
Character->bUseControllerRotationYaw = true;
}

View File

@ -28,4 +28,7 @@ public:
UPROPERTY(meta = (BindWidget))
UTextBlock* DefeatsValue;
UPROPERTY(meta = (BindWidget))
UTextBlock* WeaponAmmoValue;
};

View File

@ -74,3 +74,18 @@ void ABlasterPlayerController::SetHUDDefeats(int32 Defeats)
BlasterHUD->CharacterOverlay->DefeatsValue->SetText(FText::FromString(DefeatsAmount));
}
}
void ABlasterPlayerController::SetHUDWeaponAmmo(int32 Ammo)
{
BlasterHUD = BlasterHUD == nullptr ? Cast<ABlasterHUD>(GetHUD()) : BlasterHUD;
bool bHUDValid =
BlasterHUD &&
BlasterHUD->CharacterOverlay &&
BlasterHUD->CharacterOverlay->WeaponAmmoValue;
if (bHUDValid)
{
const FString WeaponAmmoAmount = FString::Printf(TEXT("%d"), Ammo);
BlasterHUD->CharacterOverlay->WeaponAmmoValue->SetText(FText::FromString(WeaponAmmoAmount));
}
}

View File

@ -19,6 +19,7 @@ public:
void SetHUDHealth(float Health, float MaxHealth);
void SetHUDScore(float Score);
void SetHUDDefeats(int32 Defeats);
void SetHUDWeaponAmmo(int32 Ammo);
virtual void OnPossess(APawn* InPawn) override;
protected:

View File

@ -5,6 +5,7 @@
#include "Casing.h"
#include "Blaster/Character/BlasterCharacter.h"
#include "Blaster/PlayerController/BlasterPlayerController.h"
#include "Components/SphereComponent.h"
#include "Components/WidgetComponent.h"
#include "Engine/SkeletalMeshSocket.h"
@ -37,6 +38,7 @@ void AWeapon::GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeP
Super::GetLifetimeReplicatedProps(OutLifetimeProps);
DOREPLIFETIME(AWeapon, WeaponState);
DOREPLIFETIME(AWeapon, Ammo);
}
void AWeapon::BeginPlay()
@ -81,6 +83,45 @@ void AWeapon::OnSphereEndOverlap(UPrimitiveComponent* OverlappedComponent, AActo
}
}
void AWeapon::OnRep_Owner()
{
Super::OnRep_Owner();
if (Owner == nullptr)
{
OwnerCharacter = nullptr;
OwnerController = nullptr;
} else
{
SetHUDAmmo();
}
}
void AWeapon::SetHUDAmmo()
{
OwnerCharacter = OwnerCharacter == nullptr ? Cast<ABlasterCharacter>(GetOwner()) : OwnerCharacter;
if (OwnerCharacter)
{
OwnerController = OwnerController == nullptr ? Cast<ABlasterPlayerController>(OwnerCharacter->Controller) : OwnerController;
if (OwnerController)
{
OwnerController->SetHUDWeaponAmmo(Ammo);
}
}
}
void AWeapon::SpendRound()
{
--Ammo;
SetHUDAmmo();
}
void AWeapon::OnRep_Ammo()
{
SetHUDAmmo();
}
void AWeapon::SetWeaponState(EWeaponState State)
{
WeaponState = State;
@ -155,6 +196,7 @@ void AWeapon::Fire(const FVector& HitTarget)
}
}
}
SpendRound();
}
void AWeapon::Dropped()
@ -163,4 +205,6 @@ void AWeapon::Dropped()
const FDetachmentTransformRules DetachRules(EDetachmentRule::KeepWorld, true);
WeaponMesh->DetachFromComponent(DetachRules);
SetOwner(nullptr);
OwnerCharacter = nullptr;
OwnerController = nullptr;
}

View File

@ -24,6 +24,8 @@ class BLASTER_API AWeapon : public AActor
public:
AWeapon();
virtual void GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const override;
virtual void OnRep_Owner() override;
void SetHUDAmmo();
void ShowPickupWidget(bool bShowWidget);
virtual void Fire(const FVector& HitTarget);
void Dropped();
@ -102,7 +104,23 @@ private:
UPROPERTY(EditAnywhere, Category= "Weapon Properties")
TSubclassOf<class ACasing> CasingClass;
UPROPERTY(EditAnywhere, ReplicatedUsing=OnRep_Ammo)
int32 Ammo;
UFUNCTION()
void OnRep_Ammo();
void SpendRound();
UPROPERTY(EditAnywhere)
int32 MagCapacity;
UPROPERTY()
class ABlasterCharacter* OwnerCharacter;
UPROPERTY()
class ABlasterPlayerController* OwnerController;
public:
void SetWeaponState(EWeaponState State);