167 - Swap Weapons

This commit is contained in:
Kingsmedia 2022-05-25 15:24:29 +02:00
parent a38547ce5b
commit 69c77256c2
5 changed files with 137 additions and 84 deletions

View File

@ -436,9 +436,16 @@ void ABlasterCharacter::EquipButtonPressed()
void ABlasterCharacter::ServerEquipButtonPressed_Implementation() void ABlasterCharacter::ServerEquipButtonPressed_Implementation()
{ {
if (Combat) if (Combat)
{
if (OverlappingWeapon)
{ {
Combat->EquipWeapon(OverlappingWeapon); Combat->EquipWeapon(OverlappingWeapon);
} }
else if (Combat->ShouldSwapWeapons())
{
Combat->SwapWeapons();
}
}
} }
void ABlasterCharacter::CrouchButtonPressed() void ABlasterCharacter::CrouchButtonPressed()

View File

@ -173,6 +173,27 @@ void UCombatComponent::EquipWeapon(AWeapon* WeaponToEquip)
Character->bUseControllerRotationYaw = true; Character->bUseControllerRotationYaw = true;
} }
void UCombatComponent::SwapWeapons()
{
AWeapon* TempWeapon = PrimaryWeapon;
PrimaryWeapon = SecondaryWeapon;
SecondaryWeapon = TempWeapon;
PrimaryWeapon->SetWeaponState(EWeaponState::EWS_Equipped);
AttachActorToRightHand(PrimaryWeapon);
PrimaryWeapon->SetHUDAmmo();
UpdateCarriedAmmo();
PlayEquipWeaponSound(PrimaryWeapon);
SecondaryWeapon->SetWeaponState(EWeaponState::EWS_EquippedSecondary);
AttachActorToBackpack(SecondaryWeapon);
}
bool UCombatComponent::ShouldSwapWeapons()
{
return PrimaryWeapon != nullptr && SecondaryWeapon != nullptr;
}
void UCombatComponent::EquipPrimaryWeapon(AWeapon* WeaponToEquip) void UCombatComponent::EquipPrimaryWeapon(AWeapon* WeaponToEquip)
{ {
if (WeaponToEquip == nullptr) return; if (WeaponToEquip == nullptr) return;
@ -191,12 +212,34 @@ void UCombatComponent::EquipPrimaryWeapon(AWeapon* WeaponToEquip)
void UCombatComponent::EquipSecondaryWeapon(AWeapon* WeaponToEquip) void UCombatComponent::EquipSecondaryWeapon(AWeapon* WeaponToEquip)
{ {
if (WeaponToEquip == nullptr) return; if (WeaponToEquip == nullptr) return;
SecondaryWeapon = WeaponToEquip; SecondaryWeapon = WeaponToEquip;
SecondaryWeapon->SetWeaponState(EWeaponState::EWS_Equipped); SecondaryWeapon->SetWeaponState(EWeaponState::EWS_EquippedSecondary);
AttachActorToBackpack(WeaponToEquip); AttachActorToBackpack(WeaponToEquip);
PrimaryWeapon->SetOwner(Character);
PlayEquipWeaponSound(SecondaryWeapon); PlayEquipWeaponSound(SecondaryWeapon);
SecondaryWeapon->SetOwner(Character);
}
void UCombatComponent::OnRep_PrimaryWeapon()
{
if (PrimaryWeapon && Character)
{
PrimaryWeapon->SetWeaponState(EWeaponState::EWS_Equipped);
AttachActorToRightHand(PrimaryWeapon);
Character->GetCharacterMovement()->bOrientRotationToMovement = false;
Character->bUseControllerRotationYaw = true;
PlayEquipWeaponSound(PrimaryWeapon);
PrimaryWeapon->SetHUDAmmo();
}
}
void UCombatComponent::OnRep_SecondaryWeapon()
{
if (SecondaryWeapon && Character)
{
SecondaryWeapon->SetWeaponState(EWeaponState::EWS_EquippedSecondary);
AttachActorToBackpack(SecondaryWeapon);
PlayEquipWeaponSound(SecondaryWeapon);
}
} }
void UCombatComponent::DropPrimaryWeapon() void UCombatComponent::DropPrimaryWeapon()
@ -495,28 +538,6 @@ void UCombatComponent::ShowAttachedGrenade(bool bShowGrenade)
} }
} }
void UCombatComponent::OnRep_PrimaryWeapon()
{
if (PrimaryWeapon && Character)
{
PrimaryWeapon->SetWeaponState(EWeaponState::EWS_Equipped);
AttachActorToRightHand(PrimaryWeapon);
Character->GetCharacterMovement()->bOrientRotationToMovement = false;
Character->bUseControllerRotationYaw = true;
PlayEquipWeaponSound(PrimaryWeapon);
}
}
void UCombatComponent::OnRep_SecondaryWeapon()
{
if (SecondaryWeapon && Character)
{
SecondaryWeapon->SetWeaponState(EWeaponState::EWS_Equipped);
AttachActorToBackpack(SecondaryWeapon);
PlayEquipWeaponSound(SecondaryWeapon);
}
}
void UCombatComponent::TraceUnderCrosshairs(FHitResult& TraceHitResult) void UCombatComponent::TraceUnderCrosshairs(FHitResult& TraceHitResult)
{ {
FVector2D ViewportSize; FVector2D ViewportSize;

View File

@ -23,6 +23,7 @@ public:
virtual void GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const override; virtual void GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const override;
void EquipWeapon(class AWeapon* WeaponToEquip); void EquipWeapon(class AWeapon* WeaponToEquip);
void SwapWeapons();
void Reload(); void Reload();
UFUNCTION(BlueprintCallable) UFUNCTION(BlueprintCallable)
void FinishedReloading(); void FinishedReloading();
@ -43,8 +44,6 @@ public:
UFUNCTION(Server, Reliable) UFUNCTION(Server, Reliable)
void ServerLaunchGrenade(const FVector_NetQuantize& Target); void ServerLaunchGrenade(const FVector_NetQuantize& Target);
FORCEINLINE int32 GetGrenades() const { return Grenades; }
void PickupAmmo(EWeaponType WeaponType, int32 AmmoAmount); void PickupAmmo(EWeaponType WeaponType, int32 AmmoAmount);
protected: protected:
@ -209,4 +208,9 @@ private:
int32 MaxGrenades = 4; int32 MaxGrenades = 4;
void UpdateHUDGrenades(); void UpdateHUDGrenades();
public:
FORCEINLINE int32 GetGrenades() const { return Grenades; }
bool ShouldSwapWeapons();
}; };

View File

@ -148,9 +148,32 @@ void AWeapon::OnRep_Ammo()
void AWeapon::SetWeaponState(EWeaponState State) void AWeapon::SetWeaponState(EWeaponState State)
{ {
WeaponState = State; WeaponState = State;
OnWeaponStateSet();
}
void AWeapon::OnWeaponStateSet()
{
switch (WeaponState) switch (WeaponState)
{ {
case EWeaponState::EWS_Equipped: case EWeaponState::EWS_Equipped:
OnEquipped();
break;
case EWeaponState::EWS_EquippedSecondary:
OnEquippedSecondary();
break;
case EWeaponState::EWS_Dropped:
OnDropped();
break;
}
}
void AWeapon::OnRep_WeaponState()
{
OnWeaponStateSet();
}
void AWeapon::OnEquipped()
{
ShowPickupWidget(false); ShowPickupWidget(false);
GetAreaSphere()->SetCollisionEnabled(ECollisionEnabled::NoCollision); GetAreaSphere()->SetCollisionEnabled(ECollisionEnabled::NoCollision);
WeaponMesh->SetSimulatePhysics(false); WeaponMesh->SetSimulatePhysics(false);
@ -163,8 +186,31 @@ void AWeapon::SetWeaponState(EWeaponState State)
WeaponMesh->SetCollisionResponseToAllChannels(ECollisionResponse::ECR_Ignore); WeaponMesh->SetCollisionResponseToAllChannels(ECollisionResponse::ECR_Ignore);
} }
EnableCustomDepth(false); EnableCustomDepth(false);
break; }
case EWeaponState::EWS_Dropped:
void AWeapon::OnEquippedSecondary()
{
ShowPickupWidget(false);
GetAreaSphere()->SetCollisionEnabled(ECollisionEnabled::NoCollision);
WeaponMesh->SetSimulatePhysics(false);
WeaponMesh->SetEnableGravity(false);
WeaponMesh->SetCollisionEnabled(ECollisionEnabled::NoCollision);
if (IsSMG())
{
WeaponMesh->SetCollisionEnabled(ECollisionEnabled::QueryAndPhysics);
WeaponMesh->SetEnableGravity(true);
WeaponMesh->SetCollisionResponseToAllChannels(ECollisionResponse::ECR_Ignore);
}
EnableCustomDepth(true);
if (WeaponMesh)
{
WeaponMesh->SetCustomDepthStencilValue(CUSTOM_DEPTH_TAN);
WeaponMesh->MarkRenderStateDirty();
}
}
void AWeapon::OnDropped()
{
if (HasAuthority()) if (HasAuthority())
{ {
GetAreaSphere()->SetCollisionEnabled(ECollisionEnabled::QueryOnly); GetAreaSphere()->SetCollisionEnabled(ECollisionEnabled::QueryOnly);
@ -178,10 +224,10 @@ void AWeapon::SetWeaponState(EWeaponState State)
WeaponMesh->SetCustomDepthStencilValue(CUSTOM_DEPTH_BLUE); WeaponMesh->SetCustomDepthStencilValue(CUSTOM_DEPTH_BLUE);
WeaponMesh->MarkRenderStateDirty(); WeaponMesh->MarkRenderStateDirty();
EnableCustomDepth(true); EnableCustomDepth(true);
break;
}
} }
bool AWeapon::IsEmpty() bool AWeapon::IsEmpty()
{ {
return Ammo <= 0; return Ammo <= 0;
@ -192,37 +238,6 @@ bool AWeapon::IsFull()
return Ammo == MagCapacity; return Ammo == MagCapacity;
} }
void AWeapon::OnRep_WeaponState()
{
switch (WeaponState)
{
case EWeaponState::EWS_Equipped:
ShowPickupWidget(false);
WeaponMesh->SetSimulatePhysics(false);
WeaponMesh->SetEnableGravity(false);
WeaponMesh->SetCollisionEnabled(ECollisionEnabled::NoCollision);
if (IsSMG())
{
WeaponMesh->SetCollisionEnabled(ECollisionEnabled::QueryAndPhysics);
WeaponMesh->SetEnableGravity(true);
WeaponMesh->SetCollisionResponseToAllChannels(ECollisionResponse::ECR_Ignore);
}
EnableCustomDepth(false);
break;
case EWeaponState::EWS_Dropped:
WeaponMesh->SetSimulatePhysics(true);
WeaponMesh->SetEnableGravity(true);
WeaponMesh->SetCollisionEnabled(ECollisionEnabled::QueryAndPhysics);
WeaponMesh->SetCollisionResponseToAllChannels(ECollisionResponse::ECR_Block);
WeaponMesh->SetCollisionResponseToChannel(ECollisionChannel::ECC_Pawn, ECollisionResponse::ECR_Ignore);
WeaponMesh->SetCollisionResponseToChannel(ECollisionChannel::ECC_Camera, ECollisionResponse::ECR_Ignore);
WeaponMesh->SetCustomDepthStencilValue(CUSTOM_DEPTH_BLUE);
WeaponMesh->MarkRenderStateDirty();
EnableCustomDepth(true);
break;
}
}
void AWeapon::ShowPickupWidget(bool bShowWidget) void AWeapon::ShowPickupWidget(bool bShowWidget)
{ {
if (PickupWidget) if (PickupWidget)

View File

@ -12,6 +12,7 @@ enum class EWeaponState : uint8
{ {
EWS_Initial UMETA(DisplayName = "Initial State"), EWS_Initial UMETA(DisplayName = "Initial State"),
EWS_Equipped UMETA(DisplayName = "Equipped"), EWS_Equipped UMETA(DisplayName = "Equipped"),
EWS_EquippedSecondary UMETA(DisplayName = "Equipped Secondary"),
EWS_Dropped UMETA(DisplayName = "Dropped"), EWS_Dropped UMETA(DisplayName = "Dropped"),
EWS_MAX UMETA(DisplayName = "DefaultMAX") EWS_MAX UMETA(DisplayName = "DefaultMAX")
@ -74,6 +75,11 @@ public:
protected: protected:
virtual void BeginPlay() override; virtual void BeginPlay() override;
virtual void OnWeaponStateSet();
virtual void OnEquipped();
virtual void OnDropped();
virtual void OnEquippedSecondary();
UFUNCTION() UFUNCTION()
virtual void OnSphereOverlap( virtual void OnSphereOverlap(
UPrimitiveComponent* OverlappedComponent, UPrimitiveComponent* OverlappedComponent,