// Fill out your copyright notice in the Description page of Project Settings. #include "BlasterCharacter.h" #include "NiagaraComponent.h" #include "NiagaraFunctionLibrary.h" #include "Blaster/Blaster.h" #include "Blaster/Components/BuffComponent.h" #include "Blaster/Components/CombatComponent.h" #include "Blaster/Components/LagCompensationComponent.h" #include "Blaster/GameMode/BlasterGameMode.h" #include "Blaster/GameState/BlasterGameState.h" #include "Blaster/PlayerController/BlasterPlayerController.h" #include "Blaster/PlayerState/BlasterPlayerState.h" #include "Blaster/Weapon/Weapon.h" #include "Camera/CameraComponent.h" #include "Components/BoxComponent.h" #include "Components/CapsuleComponent.h" #include "Components/WidgetComponent.h" #include "GameFramework/CharacterMovementComponent.h" #include "GameFramework/SpringArmComponent.h" #include "Kismet/GameplayStatics.h" #include "Kismet/KismetMathLibrary.h" #include "Net/UnrealNetwork.h" #include "Particles/ParticleSystemComponent.h" ABlasterCharacter::ABlasterCharacter() { PrimaryActorTick.bCanEverTick = true; SpawnCollisionHandlingMethod = ESpawnActorCollisionHandlingMethod::AdjustIfPossibleButAlwaysSpawn; CameraBoom = CreateDefaultSubobject(TEXT("CameraBoom")); CameraBoom->SetupAttachment(GetMesh()); CameraBoom->TargetArmLength = 600.f; CameraBoom->bUsePawnControlRotation = true; FollowCamera = CreateDefaultSubobject(TEXT("FollowCamera")); FollowCamera->SetupAttachment(CameraBoom, USpringArmComponent::SocketName); FollowCamera->bUsePawnControlRotation = false; bUseControllerRotationYaw = false; GetCharacterMovement()->bOrientRotationToMovement = true; OverheadWidget = CreateDefaultSubobject(TEXT("OverheadWidget")); OverheadWidget->SetupAttachment(RootComponent); Combat = CreateDefaultSubobject(TEXT("CombatComponent")); Combat->SetIsReplicated(true); Buff = CreateDefaultSubobject(TEXT("BuffComponent")); Buff->SetIsReplicated(true); LagCompensation = CreateDefaultSubobject(TEXT("LagCompensation")); GetCharacterMovement()->NavAgentProps.bCanCrouch = true; GetCapsuleComponent()->SetCollisionResponseToChannel(ECollisionChannel::ECC_Camera, ECollisionResponse::ECR_Ignore); GetMesh()->SetCollisionObjectType(ECC_SkeletalMesh); GetMesh()->SetCollisionResponseToChannel(ECollisionChannel::ECC_Camera, ECollisionResponse::ECR_Ignore); GetMesh()->SetCollisionResponseToChannel(ECollisionChannel::ECC_Visibility, ECollisionResponse::ECR_Block); GetCharacterMovement()->RotationRate = FRotator(0.f, 850.f, 0.f); TurningInPlace = ETurningInPlace::ETIP_NotTurning; NetUpdateFrequency = 66.f; MinNetUpdateFrequency = 33.f; DissolveTimeline = CreateDefaultSubobject(TEXT("DissolveTimelineComponent")); AttachedGrenade = CreateDefaultSubobject(TEXT("Attached Grenade")); AttachedGrenade->SetupAttachment(GetMesh(), FName("RightHandThrowableSocket")); AttachedGrenade->SetCollisionEnabled(ECollisionEnabled::NoCollision); // Hit boxes for server-side rewind head = CreateDefaultSubobject(TEXT("head")); head->SetupAttachment(GetMesh(), FName("head")); HitCollisionBoxes.Add(FName("head"), head); pelvis = CreateDefaultSubobject(TEXT("pelvis")); pelvis->SetupAttachment(GetMesh(), FName("pelvis")); HitCollisionBoxes.Add(FName("pelvis"), pelvis); spine_02 = CreateDefaultSubobject(TEXT("spine_02")); spine_02->SetupAttachment(GetMesh(), FName("spine_02")); HitCollisionBoxes.Add(FName("spine_02"), spine_02); spine_03 = CreateDefaultSubobject(TEXT("spine_03")); spine_03->SetupAttachment(GetMesh(), FName("spine_03")); HitCollisionBoxes.Add(FName("spine_03"), spine_03); upperarm_l = CreateDefaultSubobject(TEXT("upperarm_l")); upperarm_l->SetupAttachment(GetMesh(), FName("upperarm_l")); HitCollisionBoxes.Add(FName("upperarm_l"), upperarm_l); upperarm_r = CreateDefaultSubobject(TEXT("upperarm_r")); upperarm_r->SetupAttachment(GetMesh(), FName("upperarm_r")); HitCollisionBoxes.Add(FName("upperarm_r"), upperarm_r); lowerarm_l = CreateDefaultSubobject(TEXT("lowerarm_l")); lowerarm_l->SetupAttachment(GetMesh(), FName("lowerarm_l")); HitCollisionBoxes.Add(FName("lowerarm_l"), lowerarm_l); lowerarm_r = CreateDefaultSubobject(TEXT("lowerarm_r")); lowerarm_r->SetupAttachment(GetMesh(), FName("lowerarm_r")); HitCollisionBoxes.Add(FName("lowerarm_r"), lowerarm_r); hand_l = CreateDefaultSubobject(TEXT("hand_l")); hand_l->SetupAttachment(GetMesh(), FName("hand_l")); HitCollisionBoxes.Add(FName("hand_l"), hand_l); hand_r = CreateDefaultSubobject(TEXT("hand_r")); hand_r->SetupAttachment(GetMesh(), FName("hand_r")); HitCollisionBoxes.Add(FName("hand_r"), hand_r); blanket = CreateDefaultSubobject(TEXT("blanket")); blanket->SetupAttachment(GetMesh(), FName("backpack")); HitCollisionBoxes.Add(FName("blanket"), blanket); backpack = CreateDefaultSubobject(TEXT("backpack")); backpack->SetupAttachment(GetMesh(), FName("backpack")); HitCollisionBoxes.Add(FName("backpack"), backpack); thigh_l = CreateDefaultSubobject(TEXT("thigh_l")); thigh_l->SetupAttachment(GetMesh(), FName("thigh_l")); HitCollisionBoxes.Add(FName("thigh_l"), thigh_l); thigh_r = CreateDefaultSubobject(TEXT("thigh_r")); thigh_r->SetupAttachment(GetMesh(), FName("thigh_r")); HitCollisionBoxes.Add(FName("thigh_r"), thigh_r); calf_l = CreateDefaultSubobject(TEXT("calf_l")); calf_l->SetupAttachment(GetMesh(), FName("calf_l")); HitCollisionBoxes.Add(FName("calf_l"), calf_l); calf_r = CreateDefaultSubobject(TEXT("calf_r")); calf_r->SetupAttachment(GetMesh(), FName("calf_r")); HitCollisionBoxes.Add(FName("calf_r"), calf_r); foot_l = CreateDefaultSubobject(TEXT("foot_l")); foot_l->SetupAttachment(GetMesh(), FName("foot_l")); HitCollisionBoxes.Add(FName("foot_l"), foot_l); foot_r = CreateDefaultSubobject(TEXT("foot_r")); foot_r->SetupAttachment(GetMesh(), FName("foot_r")); HitCollisionBoxes.Add(FName("foot_r"), foot_r); for (auto Box : HitCollisionBoxes) { if (Box.Value) { Box.Value->SetCollisionObjectType(ECC_HitBox); Box.Value->SetCollisionResponseToAllChannels(ECR_Ignore); Box.Value->SetCollisionResponseToChannel(ECC_HitBox, ECR_Block); Box.Value->SetCollisionEnabled(ECollisionEnabled::NoCollision); } } } void ABlasterCharacter::GetLifetimeReplicatedProps(TArray& OutLifetimeProps) const { Super::GetLifetimeReplicatedProps(OutLifetimeProps); DOREPLIFETIME_CONDITION(ABlasterCharacter, OverlappingWeapon, COND_OwnerOnly); DOREPLIFETIME(ABlasterCharacter, Health); DOREPLIFETIME(ABlasterCharacter, Shield); DOREPLIFETIME(ABlasterCharacter, bDisableGameplay); } void ABlasterCharacter::OnRep_ReplicatedMovement() { Super::OnRep_ReplicatedMovement(); SimProxiesTurn(); TimeSinceLastMovementReplication = 0.f; } void ABlasterCharacter::Eliminated(bool bPlayerLeftGame) { if (Combat) Combat->DropWeapons(); MulticastEliminated(bPlayerLeftGame); } void ABlasterCharacter::MulticastEliminated_Implementation(bool bPlayerLeftGame) { bLeftGame = bPlayerLeftGame; if (BlasterPlayerController) { BlasterPlayerController->SetHUDWeaponAmmo(0); } bEliminated = true; PlayEliminatedMontage(); // Start dissolve effect if (DissolveMaterialInstance) { DynamicDissolveMaterialInstance = UMaterialInstanceDynamic::Create(DissolveMaterialInstance, this); GetMesh()->SetMaterial(0, DynamicDissolveMaterialInstance); DynamicDissolveMaterialInstance->SetScalarParameterValue(TEXT("Dissolve"), 0.55f); DynamicDissolveMaterialInstance->SetScalarParameterValue(TEXT("Glow"), 200.f); } StartDissolve(); // Disable character movement bDisableGameplay = true; GetCharacterMovement()->DisableMovement(); GetCharacterMovement()->StopMovementImmediately(); if (Combat) { Combat->FireButtonPressed(false); } // Disable collision GetCapsuleComponent()->SetCollisionEnabled(ECollisionEnabled::NoCollision); GetMesh()->SetCollisionEnabled(ECollisionEnabled::NoCollision); AttachedGrenade->SetCollisionEnabled(ECollisionEnabled::NoCollision); // Elimination bot if (EliminationBotEffect) { const FVector EliminationBotSpawnPoint(GetActorLocation().X, GetActorLocation().Y, GetActorLocation().Z + 200.f); EliminationBotComponent = UGameplayStatics::SpawnEmitterAtLocation( GetWorld(), EliminationBotEffect, EliminationBotSpawnPoint, GetActorRotation() ); } if (EliminationBotSound) { UGameplayStatics::SpawnSoundAtLocation( this, EliminationBotSound, GetActorLocation() ); } if (IsLocallyControlled() && GetEquippedWeapon() && GetEquippedWeapon()->IsSniper() && IsAiming()) { ShowSniperScopeWidget(false); } if (CrownComponent) { CrownComponent->DestroyComponent(); } GetWorldTimerManager().SetTimer( EliminationTimer, this, &ABlasterCharacter::EliminationTimerFinished, EliminationDelay ); } void ABlasterCharacter::EliminationTimerFinished() { ABlasterGameMode* BlasterGameMode = GetWorld()->GetAuthGameMode(); if (BlasterGameMode && !bLeftGame) { BlasterGameMode->RequestRespawn(this, Controller); } if (bLeftGame && IsLocallyControlled()) { OnLeftGame.Broadcast(); } } void ABlasterCharacter::ServerLeaveGame_Implementation() { ABlasterGameMode* BlasterGameMode = GetWorld()->GetAuthGameMode(); BlasterPlayerState = BlasterPlayerState == nullptr ? GetPlayerState() : BlasterPlayerState; if (BlasterGameMode && BlasterPlayerState) { BlasterGameMode->PlayerLeftGame(BlasterPlayerState); } } void ABlasterCharacter::Destroyed() { Super::Destroyed(); if (EliminationBotComponent) { EliminationBotComponent->DestroyComponent(); } ABlasterGameMode* BlasterGameMode = Cast(UGameplayStatics::GetGameMode(this)); bool bMatchNotInProgress = BlasterGameMode && BlasterGameMode->GetMatchState() != MatchState::InProgress; if (Combat && Combat->EquippedWeapon && bMatchNotInProgress) { Combat->EquippedWeapon->Destroy(); } } void ABlasterCharacter::MulticastGainedTheLead_Implementation() { if (CrownSystem == nullptr) return; if (CrownComponent == nullptr) { CrownComponent = UNiagaraFunctionLibrary::SpawnSystemAttached( CrownSystem, GetMesh(), FName(), GetActorLocation() + FVector(0.f, 0.f, 110.f), GetActorRotation(), EAttachLocation::KeepWorldPosition, false ); } if (CrownComponent) { CrownComponent->Activate(); } } void ABlasterCharacter::MulticastLostTheLead_Implementation() { if (CrownComponent) { CrownComponent->DestroyComponent(); } } void ABlasterCharacter::BeginPlay() { Super::BeginPlay(); SpawnDefaultWeapon(); UpdateHUDAmmo(); UpdateHUDHealth(); UpdateHUDShield(); if (HasAuthority()) { OnTakeAnyDamage.AddDynamic(this, &ABlasterCharacter::ReceiveDamage); } if (AttachedGrenade) { AttachedGrenade->SetVisibility(false); } } void ABlasterCharacter::Tick(float DeltaTime) { Super::Tick(DeltaTime); RotateInPlace(DeltaTime); HideCameraIfCharacterClose(); PollInit(); } void ABlasterCharacter::RotateInPlace(float DeltaTime) { if (bDisableGameplay) { bUseControllerRotationYaw = false; TurningInPlace = ETurningInPlace::ETIP_NotTurning; return; } if (GetLocalRole() > ENetRole::ROLE_SimulatedProxy && IsLocallyControlled()) { AimOffset(DeltaTime); } else { TimeSinceLastMovementReplication += DeltaTime; if (TimeSinceLastMovementReplication > 0.25f) { OnRep_ReplicatedMovement(); } CalculateAO_Pitch(); } } void ABlasterCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent) { Super::SetupPlayerInputComponent(PlayerInputComponent); PlayerInputComponent->BindAction("Jump", IE_Pressed, this, &ABlasterCharacter::Jump); PlayerInputComponent->BindAxis("MoveForward", this, &ABlasterCharacter::MoveForward); PlayerInputComponent->BindAxis("MoveRight", this, &ABlasterCharacter::MoveRight); PlayerInputComponent->BindAxis("Turn", this, &ABlasterCharacter::Turn); PlayerInputComponent->BindAxis("LookUp", this, &ABlasterCharacter::LookUp); PlayerInputComponent->BindAction("Equip", IE_Pressed, this, &ABlasterCharacter::EquipButtonPressed); PlayerInputComponent->BindAction("Crouch", IE_Pressed, this, &ABlasterCharacter::CrouchButtonPressed); PlayerInputComponent->BindAction("Aim", IE_Pressed, this, &ABlasterCharacter::AimButtonPressed); PlayerInputComponent->BindAction("Aim", IE_Released, this, &ABlasterCharacter::AimButtonReleased); PlayerInputComponent->BindAction("Fire", IE_Pressed, this, &ABlasterCharacter::FireButtonPressed); PlayerInputComponent->BindAction("Fire", IE_Released, this, &ABlasterCharacter::FireButtonReleased); PlayerInputComponent->BindAction("Reload", IE_Pressed, this, &ABlasterCharacter::ReloadButtonPressed); PlayerInputComponent->BindAction("ThrowGrenade", IE_Pressed, this, &ABlasterCharacter::GrenadeButtonPressed); } void ABlasterCharacter::PostInitializeComponents() { Super::PostInitializeComponents(); if (Combat) { Combat->Character = this; } if (Buff) { Buff->Character = this; Buff->SetInitialSpeeds(GetCharacterMovement()->MaxWalkSpeed, GetCharacterMovement()->MaxWalkSpeedCrouched); Buff->SetInitialJumpVelocity(GetCharacterMovement()->JumpZVelocity); } if (LagCompensation) { LagCompensation->Character = this; if (Controller) { LagCompensation->Controller = Cast(Controller); } } } void ABlasterCharacter::PlayFireMontage(bool bAiming) { if (Combat == nullptr || Combat->EquippedWeapon == nullptr) return; UAnimInstance* AnimInstance = GetMesh()->GetAnimInstance(); if (AnimInstance && FireWeaponMontage) { AnimInstance->Montage_Play(FireWeaponMontage); FName SectionName; SectionName = bAiming ? FName("RifleADS") : FName("RifleHip"); AnimInstance->Montage_JumpToSection(SectionName); } } void ABlasterCharacter::PlayReloadMontage() { if (Combat == nullptr || Combat->EquippedWeapon == nullptr) return; UAnimInstance* AnimInstance = GetMesh()->GetAnimInstance(); if (AnimInstance && ReloadMontage) { AnimInstance->Montage_Play(ReloadMontage); FName SectionName; switch (Combat->EquippedWeapon->GetWeaponType()) { case EWeaponType::EWT_AssaultRifle: SectionName = FName("Rifle"); break; case EWeaponType::EWT_RocketLauncher: SectionName = FName("RocketLauncher"); break; case EWeaponType::EWT_Pistol: SectionName = FName("Pistol"); break; case EWeaponType::EWT_SubmachineGun: SectionName = FName("Pistol"); break; case EWeaponType::EWT_Shotgun: SectionName = FName("Shotgun"); break; case EWeaponType::EWT_SniperRifle: SectionName = FName("SniperRifle"); break; case EWeaponType::EWT_GrenadeLauncher: SectionName = FName("GrenadeLauncher"); break; default: SectionName = FName("Rifle"); break; } AnimInstance->Montage_JumpToSection(SectionName); } } void ABlasterCharacter::PlayEliminatedMontage() { UAnimInstance* AnimInstance = GetMesh()->GetAnimInstance(); if (AnimInstance && EliminatedMontage) { AnimInstance->Montage_Play(EliminatedMontage); } } void ABlasterCharacter::PlayThrowGrenadeMontage() { UAnimInstance* AnimInstance = GetMesh()->GetAnimInstance(); if (AnimInstance && ThrowGrenadeMontage) { AnimInstance->Montage_Play(ThrowGrenadeMontage); } } void ABlasterCharacter::PlaySwapMontage() { UAnimInstance* AnimInstance = GetMesh()->GetAnimInstance(); if (AnimInstance && SwapMontage) { AnimInstance->Montage_Play(SwapMontage); } } void ABlasterCharacter::PlayHitReactMontage() { if (Combat == nullptr || Combat->EquippedWeapon == nullptr) return; UAnimInstance* AnimInstance = GetMesh()->GetAnimInstance(); if (AnimInstance && HitReactMontage) { AnimInstance->Montage_Play(HitReactMontage); FName SectionName("FromFront"); AnimInstance->Montage_JumpToSection(SectionName); } } void ABlasterCharacter::GrenadeButtonPressed() { if (Combat) { Combat->ThrowGrenade(); } } void ABlasterCharacter::ReceiveDamage(AActor* DamagedActor, float Damage, const UDamageType* DamageType, AController* InstigatorController, AActor* DamageCauser) { if (bEliminated) return; float DamageToHealth = Damage; if (Shield > 0.f) { if (Shield >= Damage) { Shield = FMath::Clamp(Shield - Damage, 0.f, MaxShield); DamageToHealth = 0.f; } else { DamageToHealth = FMath::Clamp(DamageToHealth - Shield, 0.f, Damage); Shield = 0.f; } } Health = FMath::Clamp(Health - DamageToHealth, 0.f, MaxHealth); UpdateHUDHealth(); UpdateHUDShield(); if (Health > 0.f) { PlayHitReactMontage(); } if (Health == 0.f) { if (DamageType->bCausedByWorld) { Eliminated(false); return; } ABlasterGameMode* BlasterGameMode = GetWorld()->GetAuthGameMode(); if (BlasterGameMode) { BlasterPlayerController = BlasterPlayerController == nullptr ? Cast(Controller) : BlasterPlayerController; ABlasterPlayerController* AttackerController = Cast(InstigatorController); BlasterGameMode->PlayerEliminated(this, BlasterPlayerController, AttackerController); } } } void ABlasterCharacter::MoveForward(float Value) { if (bDisableGameplay) return; if (Controller != nullptr && Value != 0.f) { const FRotator YawRotation(0.f, Controller->GetControlRotation().Yaw, 0.f); const FVector Direction(FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X)); AddMovementInput(Direction, Value); } } void ABlasterCharacter::MoveRight(float Value) { if (bDisableGameplay) return; if (Controller != nullptr && Value != 0.f) { const FRotator YawRotation(0.f, Controller->GetControlRotation().Yaw, 0.f); const FVector Direction(FRotationMatrix(YawRotation).GetUnitAxis(EAxis::Y)); AddMovementInput(Direction, Value); } } void ABlasterCharacter::Turn(float Value) { AddControllerYawInput(Value); } void ABlasterCharacter::LookUp(float Value) { AddControllerPitchInput(Value); } void ABlasterCharacter::EquipButtonPressed() { if (bDisableGameplay) return; if (Combat) { if (Combat->CombatState == ECombatState::ECS_Unoccupied) ServerEquipButtonPressed(); bool bSwap = Combat->ShouldSwapWeapons() && !HasAuthority() && Combat->CombatState == ECombatState::ECS_Unoccupied && OverlappingWeapon == nullptr; if (bSwap) { PlaySwapMontage(); Combat->CombatState = ECombatState::ECS_SwappingWeapons; bFinishedSwapping = false; } } } void ABlasterCharacter::ServerEquipButtonPressed_Implementation() { if (Combat) { if (OverlappingWeapon) { Combat->EquipWeapon(OverlappingWeapon); } else if (Combat->ShouldSwapWeapons()) { Combat->SwapWeapons(); } } } void ABlasterCharacter::CrouchButtonPressed() { if (bDisableGameplay) return; if (bIsCrouched) { UnCrouch(); } else { Crouch(); } } void ABlasterCharacter::ReloadButtonPressed() { if (bDisableGameplay) return; if (Combat) { Combat->Reload(); } } void ABlasterCharacter::AimButtonPressed() { if (bDisableGameplay) return; if (Combat) { Combat->SetAiming(true); } } void ABlasterCharacter::AimButtonReleased() { if (bDisableGameplay) return; if (Combat) { Combat->SetAiming(false); } } float ABlasterCharacter::CalculateSpeed() { FVector Velocity = GetVelocity(); Velocity.Z = 0.f; return Velocity.Size(); } void ABlasterCharacter::AimOffset(float DeltaTime) { if (Combat && Combat->EquippedWeapon == nullptr) return; float Speed = CalculateSpeed(); bool bIsInAir = GetCharacterMovement()->IsFalling(); if (Speed == 0.f && !bIsInAir) // Standing still, not jumping { bRotateRootBone = true; FRotator CurrentAimRotation = FRotator(0.f, GetBaseAimRotation().Yaw, 0.f); FRotator DeltaAimRotation = UKismetMathLibrary::NormalizedDeltaRotator(CurrentAimRotation, StartingAimRotation); AO_Yaw = DeltaAimRotation.Yaw; if (TurningInPlace == ETurningInPlace::ETIP_NotTurning) { InterpAO_Yaw = AO_Yaw; } bUseControllerRotationYaw = true; TurnInPlace(DeltaTime); } if (Speed > 0.f || bIsInAir) // Running or jumping { bRotateRootBone = false; StartingAimRotation = FRotator(0.f, GetBaseAimRotation().Yaw, 0.f); AO_Yaw = 0.f; bUseControllerRotationYaw = true; TurningInPlace = ETurningInPlace::ETIP_NotTurning; } CalculateAO_Pitch(); } void ABlasterCharacter::CalculateAO_Pitch() { AO_Pitch = GetBaseAimRotation().Pitch; if (AO_Pitch > 90.f && !IsLocallyControlled()) { // map pitch from [270, 360) to [-90, 0) FVector2D InRange(270.f, 360.f); FVector2D OutRange(-90.f, 0.f); AO_Pitch = FMath::GetMappedRangeValueClamped(InRange, OutRange, AO_Pitch); } } void ABlasterCharacter::SimProxiesTurn() { if (Combat == nullptr || Combat->EquippedWeapon == nullptr) return; bRotateRootBone = false; float Speed = CalculateSpeed(); if (Speed > 0.f) { TurningInPlace = ETurningInPlace::ETIP_NotTurning; return; } ProxyRotationLastFrame = ProxyRotation; ProxyRotation = GetActorRotation(); ProxyYaw = UKismetMathLibrary::NormalizedDeltaRotator(ProxyRotation, ProxyRotationLastFrame).Yaw; if (FMath::Abs(ProxyYaw) > TurnThreshold) { if (ProxyYaw > TurnThreshold) { TurningInPlace = ETurningInPlace::ETIP_Right; } else if (ProxyYaw < -TurnThreshold) { TurningInPlace = ETurningInPlace::ETIP_Left; } else { TurningInPlace = ETurningInPlace::ETIP_NotTurning; } return; } TurningInPlace = ETurningInPlace::ETIP_NotTurning; } void ABlasterCharacter::Jump() { if (bDisableGameplay) return; if (bIsCrouched) { UnCrouch(); } else { Super::Jump(); } } void ABlasterCharacter::FireButtonPressed() { if (bDisableGameplay) return; if (Combat) { Combat->FireButtonPressed(true); } } void ABlasterCharacter::FireButtonReleased() { if (bDisableGameplay) return; if (Combat) { Combat->FireButtonPressed(false); } } void ABlasterCharacter::TurnInPlace(float DeltaTime) { if (AO_Yaw > 90.f) { TurningInPlace = ETurningInPlace::ETIP_Right; } else if (AO_Yaw < -90.f) { TurningInPlace = ETurningInPlace::ETIP_Left; } if (TurningInPlace != ETurningInPlace::ETIP_NotTurning) { InterpAO_Yaw = FMath::FInterpTo(InterpAO_Yaw, 0.f, DeltaTime, 4.f); AO_Yaw = InterpAO_Yaw; if (FMath::Abs(AO_Yaw) < 15.f) { TurningInPlace = ETurningInPlace::ETIP_NotTurning; StartingAimRotation = FRotator(0.f, GetBaseAimRotation().Yaw, 0.f); } } } void ABlasterCharacter::HideCameraIfCharacterClose() { if (!IsLocallyControlled()) return; if ((FollowCamera->GetComponentLocation() - GetActorLocation()).Size() < CameraThreshold) { GetMesh()->SetVisibility(false); if (Combat && Combat->EquippedWeapon && Combat->EquippedWeapon->GetWeaponMesh()) { Combat->EquippedWeapon->GetWeaponMesh()->bOwnerNoSee = true; } if (Combat && Combat->SecondaryWeapon && Combat->SecondaryWeapon->GetWeaponMesh()) { Combat->SecondaryWeapon->GetWeaponMesh()->bOwnerNoSee = true; } } else { GetMesh()->SetVisibility(true); if (Combat && Combat->EquippedWeapon && Combat->EquippedWeapon->GetWeaponMesh()) { Combat->EquippedWeapon->GetWeaponMesh()->bOwnerNoSee = false; } if (Combat && Combat->SecondaryWeapon && Combat->SecondaryWeapon->GetWeaponMesh()) { Combat->SecondaryWeapon->GetWeaponMesh()->bOwnerNoSee = false; } } } void ABlasterCharacter::OnRep_Health(float LastHealth) { UpdateHUDHealth(); if (Health < LastHealth) { PlayHitReactMontage(); } } void ABlasterCharacter::OnRep_Shield(float LastShield) { UpdateHUDShield(); if (Shield < LastShield) { PlayHitReactMontage(); } } void ABlasterCharacter::UpdateHUDHealth() { BlasterPlayerController = BlasterPlayerController == nullptr ? Cast(Controller) : BlasterPlayerController; if (BlasterPlayerController) { BlasterPlayerController->SetHUDHealth(Health, MaxHealth); } } void ABlasterCharacter::UpdateHUDShield() { BlasterPlayerController = BlasterPlayerController == nullptr ? Cast(Controller) : BlasterPlayerController; if (BlasterPlayerController) { BlasterPlayerController->SetHUDShield(Shield, MaxShield); } } 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) { BlasterPlayerState = GetPlayerState(); if (BlasterPlayerState) { // Initialize Score now we have the PlayerState BlasterPlayerState->IncreaseScore(0.f); BlasterPlayerState->IncreaseDefeats(0); const ABlasterGameState* BlasterGameState = Cast(UGameplayStatics::GetGameState(this)); if (BlasterGameState && BlasterGameState->TopScoringPlayers.Contains(BlasterPlayerState)) { MulticastGainedTheLead(); } } } } void ABlasterCharacter::UpdateDissolveMaterial(float DissolveValue) { if (DynamicDissolveMaterialInstance) { DynamicDissolveMaterialInstance->SetScalarParameterValue(TEXT("Dissolve"), DissolveValue); } } void ABlasterCharacter::StartDissolve() { DissolveTrack.BindDynamic(this, &ABlasterCharacter::UpdateDissolveMaterial); if (DissolveCurve && DissolveTimeline) { DissolveTimeline->AddInterpFloat(DissolveCurve, DissolveTrack); DissolveTimeline->Play(); } } void ABlasterCharacter::SetOverlappingWeapon(AWeapon* Weapon) { if (OverlappingWeapon) { OverlappingWeapon->ShowPickupWidget(false); } OverlappingWeapon = Weapon; if (IsLocallyControlled()) { if (OverlappingWeapon) { OverlappingWeapon->ShowPickupWidget(true); } } } void ABlasterCharacter::OnRep_OverlappingWeapon(AWeapon* LastWeapon) { if (OverlappingWeapon) { OverlappingWeapon->ShowPickupWidget(true); } if (LastWeapon) { LastWeapon->ShowPickupWidget(false); } } bool ABlasterCharacter::IsWeaponEquipped() { return Combat && Combat->EquippedWeapon; } bool ABlasterCharacter::IsAiming() { return Combat && Combat->bAiming; } AWeapon* ABlasterCharacter::GetEquippedWeapon() { if (Combat == nullptr) return nullptr; return Combat->EquippedWeapon; } FVector ABlasterCharacter::GetHitTarget() const { if (Combat == nullptr) return FVector(); return Combat->HitTarget; } ECombatState ABlasterCharacter::GetCombatState() const { if (Combat == nullptr) return ECombatState::ECS_MAX; return Combat->CombatState; } bool ABlasterCharacter::IsLocallyReloading() { if (Combat == nullptr) return false; return Combat->bLocallyReloading; }