diff --git a/Content/Blueprints/Character/BP_BlasterCharacter.uasset b/Content/Blueprints/Character/BP_BlasterCharacter.uasset index 5176dcc..ea0efec 100644 Binary files a/Content/Blueprints/Character/BP_BlasterCharacter.uasset and b/Content/Blueprints/Character/BP_BlasterCharacter.uasset differ diff --git a/Source/Blaster/Character/BlasterCharacter.cpp b/Source/Blaster/Character/BlasterCharacter.cpp index b865810..2503653 100644 --- a/Source/Blaster/Character/BlasterCharacter.cpp +++ b/Source/Blaster/Character/BlasterCharacter.cpp @@ -183,6 +183,8 @@ void ABlasterCharacter::BeginPlay() { Super::BeginPlay(); + SpawnDefaultWeapon(); + UpdateHUDAmmo(); UpdateHUDHealth(); UpdateHUDShield(); if (HasAuthority()) @@ -686,6 +688,31 @@ void ABlasterCharacter::UpdateHUDShield() } } +void ABlasterCharacter::UpdateHUDAmmo() +{ + BlasterPlayerController = BlasterPlayerController == nullptr ? Cast(Controller) : BlasterPlayerController; + if (BlasterPlayerController && Combat && Combat->EquippedWeapon) + { + BlasterPlayerController->SetHUDCarriedAmmo(Combat->CarriedAmmo); + BlasterPlayerController->SetHUDWeaponAmmo(Combat->EquippedWeapon->GetAmmo()); + } +} + +void ABlasterCharacter::SpawnDefaultWeapon() +{ + const ABlasterGameMode* BlasterGameMode = Cast(UGameplayStatics::GetGameMode(this)); + UWorld* World = GetWorld(); + if (BlasterGameMode && World && !bEliminated && DefaultWeaponClass) + { + AWeapon* StartingWeapon = World->SpawnActor(DefaultWeaponClass); + StartingWeapon->bDestroyWeapon = true; + if (Combat) + { + Combat->EquipWeapon(StartingWeapon); + } + } +} + void ABlasterCharacter::PollInit() { if (BlasterPlayerState == nullptr) diff --git a/Source/Blaster/Character/BlasterCharacter.h b/Source/Blaster/Character/BlasterCharacter.h index 7dbcddf..5e257fb 100644 --- a/Source/Blaster/Character/BlasterCharacter.h +++ b/Source/Blaster/Character/BlasterCharacter.h @@ -43,6 +43,9 @@ public: void UpdateHUDHealth(); void UpdateHUDShield(); + void UpdateHUDAmmo(); + + void SpawnDefaultWeapon(); protected: virtual void BeginPlay() override; @@ -201,6 +204,10 @@ private: UPROPERTY(EditAnywhere) USoundCue* EliminationBotSound; + // Default weapon + UPROPERTY(EditAnywhere) + TSubclassOf DefaultWeaponClass; + // Grenade UPROPERTY(VisibleAnywhere) diff --git a/Source/Blaster/PlayerController/BlasterPlayerController.cpp b/Source/Blaster/PlayerController/BlasterPlayerController.cpp index f7e3409..2bd2eb7 100644 --- a/Source/Blaster/PlayerController/BlasterPlayerController.cpp +++ b/Source/Blaster/PlayerController/BlasterPlayerController.cpp @@ -234,6 +234,11 @@ void ABlasterPlayerController::SetHUDWeaponAmmo(int32 Ammo) FString AmmoText = FString::Printf(TEXT("%d"), Ammo); BlasterHUD->CharacterOverlay->WeaponAmmoValue->SetText(FText::FromString(AmmoText)); } + else + { + bInitializeHUDWeaponAmmo = true; + HUDWeaponAmmo = Ammo; + } } void ABlasterPlayerController::SetHUDCarriedAmmo(int32 Ammo) @@ -247,6 +252,11 @@ void ABlasterPlayerController::SetHUDCarriedAmmo(int32 Ammo) FString AmmoText = FString::Printf(TEXT("%d"), Ammo); BlasterHUD->CharacterOverlay->CarriedAmmoValue->SetText(FText::FromString(AmmoText)); } + else + { + bInitializeCarriedAmmo = true; + HUDCarriedAmmo = Ammo; + } } void ABlasterPlayerController::SetHUDGrenades(int32 Grenades) @@ -367,6 +377,8 @@ void ABlasterPlayerController::PollInit() if (bInitializeShield) SetHUDShield(HUDShield, HUDMaxShield); if (bInitializeScore) SetHUDScore(HUDScore); if (bInitializeDefeats) SetHUDDefeats(HUDDefeats); + if (bInitializeCarriedAmmo) SetHUDCarriedAmmo(HUDCarriedAmmo); + if (bInitializeHUDWeaponAmmo) SetHUDWeaponAmmo(HUDWeaponAmmo); ABlasterCharacter* BlasterCharacter = Cast(GetPawn()); if (BlasterCharacter && BlasterCharacter->GetCombat()) { diff --git a/Source/Blaster/PlayerController/BlasterPlayerController.h b/Source/Blaster/PlayerController/BlasterPlayerController.h index f491432..2f6dd33 100644 --- a/Source/Blaster/PlayerController/BlasterPlayerController.h +++ b/Source/Blaster/PlayerController/BlasterPlayerController.h @@ -119,4 +119,10 @@ private: bool bInitializeGrenades = false; int32 HUDGrenades; + float bInitializeCarriedAmmo = false; + float HUDCarriedAmmo; + + float bInitializeHUDWeaponAmmo = false; + float HUDWeaponAmmo; + }; diff --git a/Source/Blaster/Weapon/Weapon.cpp b/Source/Blaster/Weapon/Weapon.cpp index f9a476f..674a3b9 100644 --- a/Source/Blaster/Weapon/Weapon.cpp +++ b/Source/Blaster/Weapon/Weapon.cpp @@ -255,6 +255,10 @@ void AWeapon::Fire(const FVector& HitTarget) void AWeapon::Dropped() { + if (bDestroyWeapon) + { + SetLifeSpan(30.f); + } SetWeaponState(EWeaponState::EWS_Dropped); const FDetachmentTransformRules DetachRules(EDetachmentRule::KeepWorld, true); WeaponMesh->DetachFromComponent(DetachRules); diff --git a/Source/Blaster/Weapon/Weapon.h b/Source/Blaster/Weapon/Weapon.h index 222e32e..c4c0a13 100644 --- a/Source/Blaster/Weapon/Weapon.h +++ b/Source/Blaster/Weapon/Weapon.h @@ -69,7 +69,8 @@ public: // Enable or disable custom depth void EnableCustomDepth(bool bEnabled); - + + bool bDestroyWeapon = false; protected: virtual void BeginPlay() override;