|
|
|
|
| #include "LyraWeaponStateComponent.h"
|
|
|
| #include "Abilities/GameplayAbilityTargetTypes.h"
|
| #include "Equipment/LyraEquipmentManagerComponent.h"
|
| #include "GameFramework/Pawn.h"
|
| #include "GameplayEffectTypes.h"
|
| #include "Kismet/GameplayStatics.h"
|
| #include "NativeGameplayTags.h"
|
| #include "Physics/PhysicalMaterialWithTags.h"
|
| #include "Teams/LyraTeamSubsystem.h"
|
| #include "Weapons/LyraRangedWeaponInstance.h"
|
|
|
| #include UE_INLINE_GENERATED_CPP_BY_NAME(LyraWeaponStateComponent)
|
|
|
| UE_DEFINE_GAMEPLAY_TAG_STATIC(TAG_Gameplay_Zone, "Gameplay.Zone");
|
|
|
| ULyraWeaponStateComponent::ULyraWeaponStateComponent(const FObjectInitializer& ObjectInitializer)
|
| : Super(ObjectInitializer)
|
| {
|
| SetIsReplicatedByDefault(true);
|
|
|
| PrimaryComponentTick.bStartWithTickEnabled = true;
|
| PrimaryComponentTick.bCanEverTick = true;
|
| }
|
|
|
| void ULyraWeaponStateComponent::TickComponent(float DeltaTime, enum ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
|
| {
|
| Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
|
|
|
| if (APawn* Pawn = GetPawn<APawn>())
|
| {
|
| if (ULyraEquipmentManagerComponent* EquipmentManager = Pawn->FindComponentByClass<ULyraEquipmentManagerComponent>())
|
| {
|
| if (ULyraRangedWeaponInstance* CurrentWeapon = Cast<ULyraRangedWeaponInstance>(EquipmentManager->GetFirstInstanceOfType(ULyraRangedWeaponInstance::StaticClass())))
|
| {
|
| CurrentWeapon->Tick(DeltaTime);
|
| }
|
| }
|
| }
|
| }
|
|
|
| bool ULyraWeaponStateComponent::ShouldShowHitAsSuccess(const FHitResult& Hit) const
|
| {
|
| AActor* HitActor = Hit.GetActor();
|
|
|
|
|
| UWorld* World = GetWorld();
|
| if (ULyraTeamSubsystem* TeamSubsystem = UWorld::GetSubsystem<ULyraTeamSubsystem>(GetWorld()))
|
| {
|
| return TeamSubsystem->CanCauseDamage(GetController<APlayerController>(), Hit.GetActor());
|
| }
|
|
|
| return false;
|
| }
|
|
|
| bool ULyraWeaponStateComponent::ShouldUpdateDamageInstigatedTime(const FGameplayEffectContextHandle& EffectContext) const
|
| {
|
|
|
|
|
|
|
| return EffectContext.GetEffectCauser() != nullptr;
|
| }
|
|
|
| void ULyraWeaponStateComponent::ClientConfirmTargetData_Implementation(uint16 UniqueId, bool bSuccess, const TArray<uint8>& HitReplaces)
|
| {
|
| for (int i = 0; i < UnconfirmedServerSideHitMarkers.Num(); i++)
|
| {
|
| FLyraServerSideHitMarkerBatch& Batch = UnconfirmedServerSideHitMarkers[i];
|
| if (Batch.UniqueId == UniqueId)
|
| {
|
| if (bSuccess && (HitReplaces.Num() != Batch.Markers.Num()))
|
| {
|
| UWorld* World = GetWorld();
|
| bool bFoundShowAsSuccessHit = false;
|
|
|
| int32 HitLocationIndex = 0;
|
| for (const FLyraScreenSpaceHitLocation& Entry : Batch.Markers)
|
| {
|
| if (!HitReplaces.Contains(HitLocationIndex) && Entry.bShowAsSuccess)
|
| {
|
|
|
| if (!bFoundShowAsSuccessHit)
|
| {
|
| ActuallyUpdateDamageInstigatedTime();
|
| }
|
|
|
| bFoundShowAsSuccessHit = true;
|
|
|
| LastWeaponDamageScreenLocations.Add(Entry);
|
| }
|
| ++HitLocationIndex;
|
| }
|
| }
|
|
|
| UnconfirmedServerSideHitMarkers.RemoveAt(i);
|
| break;
|
| }
|
| }
|
| }
|
|
|
| void ULyraWeaponStateComponent::AddUnconfirmedServerSideHitMarkers(const FGameplayAbilityTargetDataHandle& InTargetData, const TArray<FHitResult>& FoundHits)
|
| {
|
| FLyraServerSideHitMarkerBatch& NewUnconfirmedHitMarker = UnconfirmedServerSideHitMarkers.Emplace_GetRef(InTargetData.UniqueId);
|
|
|
| if (APlayerController* OwnerPC = GetController<APlayerController>())
|
| {
|
| for (const FHitResult& Hit : FoundHits)
|
| {
|
| FVector2D HitScreenLocation;
|
| if (UGameplayStatics::ProjectWorldToScreen(OwnerPC, Hit.Location, HitScreenLocation, false))
|
| {
|
| FLyraScreenSpaceHitLocation& Entry = NewUnconfirmedHitMarker.Markers.AddDefaulted_GetRef();
|
| Entry.Location = HitScreenLocation;
|
| Entry.bShowAsSuccess = ShouldShowHitAsSuccess(Hit);
|
|
|
|
|
| FGameplayTag HitZone;
|
| if (const UPhysicalMaterialWithTags* PhysMatWithTags = Cast<const UPhysicalMaterialWithTags>(Hit.PhysMaterial.Get()))
|
| {
|
| for (const FGameplayTag MaterialTag : PhysMatWithTags->Tags)
|
| {
|
| if (MaterialTag.MatchesTag(TAG_Gameplay_Zone))
|
| {
|
| Entry.HitZone = MaterialTag;
|
| break;
|
| }
|
| }
|
| }
|
| }
|
| }
|
| }
|
| }
|
|
|
| void ULyraWeaponStateComponent::UpdateDamageInstigatedTime(const FGameplayEffectContextHandle& EffectContext)
|
| {
|
| if (ShouldUpdateDamageInstigatedTime(EffectContext))
|
| {
|
| ActuallyUpdateDamageInstigatedTime();
|
| }
|
| }
|
|
|
| void ULyraWeaponStateComponent::ActuallyUpdateDamageInstigatedTime()
|
| {
|
|
|
| UWorld* World = GetWorld();
|
| if (World->GetTimeSeconds() - LastWeaponDamageInstigatedTime > 0.1)
|
| {
|
| LastWeaponDamageScreenLocations.Reset();
|
| }
|
| LastWeaponDamageInstigatedTime = World->GetTimeSeconds();
|
| }
|
|
|
| double ULyraWeaponStateComponent::GetTimeSinceLastHitNotification() const
|
| {
|
| UWorld* World = GetWorld();
|
| return World->TimeSince(LastWeaponDamageInstigatedTime);
|
| }
|
|
|
|
|