Advertisement
Dieton

Weapon.cpp

Dec 12th, 2023 (edited)
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.25 KB | Gaming | 0 0
  1. // Fill out your copyright notice in the Description page of Project Settings.
  2.  
  3.  
  4. #include "Items\Weapons\Weapon.h"
  5. #include "Characters\SlashCharacter.h"
  6. #include "Kismet\GameplayStatics.h"
  7. #include "Components\SphereComponent.h"
  8. #include "Components\BoxComponent.h"
  9. #include "Kismet\KismetSystemLibrary.h"
  10. #include "Interfaces\HitInterface.h"
  11. #include "EnhancedInputSubsystems.h"//Debugger stuff
  12.  
  13. AWeapon::AWeapon()
  14. {
  15.     WeaponBox = CreateDefaultSubobject<UBoxComponent>(TEXT("Weapon Box"));
  16.     WeaponBox->SetupAttachment(GetRootComponent());
  17.     WeaponBox->SetCollisionEnabled(ECollisionEnabled::NoCollision);
  18.     WeaponBox->SetCollisionResponseToAllChannels(ECollisionResponse::ECR_Overlap);
  19.     WeaponBox->SetCollisionResponseToChannel(ECollisionChannel::ECC_Pawn, ECollisionResponse::ECR_Ignore);
  20.  
  21.     BoxTraceStart = CreateDefaultSubobject<USceneComponent>(TEXT("Box Trace Start"));
  22.     BoxTraceStart->SetupAttachment(GetRootComponent());
  23.  
  24.     BoxTraceEnd = CreateDefaultSubobject<USceneComponent>(TEXT("Box Trace End"));
  25.     BoxTraceEnd->SetupAttachment(GetRootComponent());
  26. }
  27.  
  28. void AWeapon::BeginPlay()
  29. {
  30.     Super::BeginPlay();
  31.     WeaponBox->OnComponentBeginOverlap.AddDynamic(this, &AWeapon::OnBoxOverlap);
  32. }
  33.  
  34. void AWeapon::Equip(USceneComponent* InParent, FName InSocketName)
  35. {
  36.     AttachMeshToSocket(InParent, InSocketName);
  37.     ItemState = EItemState::EIS_Equipped;
  38.     if (EquipSound)
  39.     {
  40.         UGameplayStatics::PlaySoundAtLocation
  41.         (
  42.             this,
  43.             EquipSound,
  44.             GetActorLocation()
  45.         );
  46.     }
  47.  
  48.     if (Sphere)
  49.     {
  50.         Sphere->SetCollisionEnabled(ECollisionEnabled::NoCollision);
  51.     }
  52. }
  53.  
  54. void AWeapon::AttachMeshToSocket(USceneComponent* InParent, const FName& InSocketName)
  55. {
  56.     FAttachmentTransformRules TransformRules(EAttachmentRule::SnapToTarget, true);
  57.     ItemMesh->AttachToComponent(InParent, TransformRules, InSocketName);
  58. }
  59.  
  60. void AWeapon::OnSphereOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
  61. {
  62.     Super::OnSphereOverlap(OverlappedComponent, OtherActor, OtherComp, OtherBodyIndex, bFromSweep, SweepResult);
  63.    
  64.    
  65.     /*
  66.     ASlashCharacter* SlashCharacter = Cast<ASlashCharacter>(OtherActor);
  67.     if (SlashCharacter)
  68.     {
  69.         FAttachmentTransformRules TransformRules(EAttachmentRule::SnapToTarget,true);
  70.         ItemMesh->AttachToComponent(SlashCharacter->GetMesh(), TransformRules, FName("RightHandSocket"));
  71.     }
  72.     */
  73. }
  74.  
  75. void AWeapon::OnSphereEndOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex)
  76. {
  77.     Super::OnSphereEndOverlap(OverlappedComponent, OtherActor, OtherComp, OtherBodyIndex);
  78.  
  79. }
  80.  
  81. void AWeapon::OnBoxOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
  82. {
  83.     const FVector Start = BoxTraceStart->GetComponentLocation();//GetComponentLocation() wolrd position GetRelativeLocation() is local
  84.     const FVector End = BoxTraceEnd->GetComponentLocation();
  85.  
  86.     TArray<AActor*> ActorsToIgnore;
  87.     ActorsToIgnore.Add(this);
  88.  
  89.     for (AActor* Actor : IgnoreActors)
  90.     {
  91.         ActorsToIgnore.AddUnique(Actor);
  92.         UE_LOG(LogTemp, Warning, TEXT("Weapon.cpp: for loop - Actor Name: %s"), *Actor->GetName());
  93.     }
  94.  
  95.     FHitResult BoxHit;
  96.  
  97.     //hidden debug element
  98.     UKismetSystemLibrary::BoxTraceSingle
  99.     (
  100.         this,
  101.         Start,
  102.         End,
  103.         FVector(5.f,5.f,5.f),
  104.         BoxTraceStart->GetComponentRotation(),
  105.         ETraceTypeQuery::TraceTypeQuery1,
  106.         false,
  107.         ActorsToIgnore,
  108.         EDrawDebugTrace::None,//ForDuration
  109.         BoxHit,
  110.         true,
  111.         FColor::Red,
  112.         FColor::Green,
  113.         5.f
  114.     );
  115.  
  116.     if (BoxHit.GetActor())
  117.     {
  118.         IHitInterface* HitInterface = Cast<IHitInterface>(BoxHit.GetActor());
  119.         if (HitInterface && !bIsInOverlap)
  120.         {
  121.             //bIsInOverlap = true;
  122.             //HitInterface->GetHit(BoxHit.ImpactPoint);        
  123.             UE_LOG(LogTemp, Warning, TEXT("Weapon.cpp: OtherActor Name: %s"), *BoxHit.GetActor()->GetName());
  124.             UE_LOG(LogTemp, Warning, TEXT("Weapon.cpp: Beffor - Execute_GetHit"));
  125.             HitInterface->Execute_GetHit(BoxHit.GetActor(), BoxHit.ImpactPoint);
  126.             UE_LOG(LogTemp, Warning, TEXT("Weapon.cpp: After - Execute_GetHit"));
  127.            
  128.         }
  129.         IgnoreActors.AddUnique(BoxHit.GetActor());
  130.  
  131.         CreateFields(BoxHit.ImpactPoint);
  132.     }
  133. }
  134.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement