Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Fill out your copyright notice in the Description page of Project Settings.
- #include "Items\Weapons\Weapon.h"
- #include "Characters\SlashCharacter.h"
- #include "Kismet\GameplayStatics.h"
- #include "Components\SphereComponent.h"
- #include "Components\BoxComponent.h"
- #include "Kismet\KismetSystemLibrary.h"
- #include "Interfaces\HitInterface.h"
- #include "EnhancedInputSubsystems.h"//Debugger stuff
- AWeapon::AWeapon()
- {
- WeaponBox = CreateDefaultSubobject<UBoxComponent>(TEXT("Weapon Box"));
- WeaponBox->SetupAttachment(GetRootComponent());
- WeaponBox->SetCollisionEnabled(ECollisionEnabled::NoCollision);
- WeaponBox->SetCollisionResponseToAllChannels(ECollisionResponse::ECR_Overlap);
- WeaponBox->SetCollisionResponseToChannel(ECollisionChannel::ECC_Pawn, ECollisionResponse::ECR_Ignore);
- BoxTraceStart = CreateDefaultSubobject<USceneComponent>(TEXT("Box Trace Start"));
- BoxTraceStart->SetupAttachment(GetRootComponent());
- BoxTraceEnd = CreateDefaultSubobject<USceneComponent>(TEXT("Box Trace End"));
- BoxTraceEnd->SetupAttachment(GetRootComponent());
- }
- void AWeapon::BeginPlay()
- {
- Super::BeginPlay();
- WeaponBox->OnComponentBeginOverlap.AddDynamic(this, &AWeapon::OnBoxOverlap);
- }
- void AWeapon::Equip(USceneComponent* InParent, FName InSocketName)
- {
- AttachMeshToSocket(InParent, InSocketName);
- ItemState = EItemState::EIS_Equipped;
- if (EquipSound)
- {
- UGameplayStatics::PlaySoundAtLocation
- (
- this,
- EquipSound,
- GetActorLocation()
- );
- }
- if (Sphere)
- {
- Sphere->SetCollisionEnabled(ECollisionEnabled::NoCollision);
- }
- }
- void AWeapon::AttachMeshToSocket(USceneComponent* InParent, const FName& InSocketName)
- {
- FAttachmentTransformRules TransformRules(EAttachmentRule::SnapToTarget, true);
- ItemMesh->AttachToComponent(InParent, TransformRules, InSocketName);
- }
- void AWeapon::OnSphereOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
- {
- Super::OnSphereOverlap(OverlappedComponent, OtherActor, OtherComp, OtherBodyIndex, bFromSweep, SweepResult);
- /*
- ASlashCharacter* SlashCharacter = Cast<ASlashCharacter>(OtherActor);
- if (SlashCharacter)
- {
- FAttachmentTransformRules TransformRules(EAttachmentRule::SnapToTarget,true);
- ItemMesh->AttachToComponent(SlashCharacter->GetMesh(), TransformRules, FName("RightHandSocket"));
- }
- */
- }
- void AWeapon::OnSphereEndOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex)
- {
- Super::OnSphereEndOverlap(OverlappedComponent, OtherActor, OtherComp, OtherBodyIndex);
- }
- void AWeapon::OnBoxOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
- {
- const FVector Start = BoxTraceStart->GetComponentLocation();//GetComponentLocation() wolrd position GetRelativeLocation() is local
- const FVector End = BoxTraceEnd->GetComponentLocation();
- TArray<AActor*> ActorsToIgnore;
- ActorsToIgnore.Add(this);
- for (AActor* Actor : IgnoreActors)
- {
- ActorsToIgnore.AddUnique(Actor);
- UE_LOG(LogTemp, Warning, TEXT("Weapon.cpp: for loop - Actor Name: %s"), *Actor->GetName());
- }
- FHitResult BoxHit;
- //hidden debug element
- UKismetSystemLibrary::BoxTraceSingle
- (
- this,
- Start,
- End,
- FVector(5.f,5.f,5.f),
- BoxTraceStart->GetComponentRotation(),
- ETraceTypeQuery::TraceTypeQuery1,
- false,
- ActorsToIgnore,
- EDrawDebugTrace::None,//ForDuration
- BoxHit,
- true,
- FColor::Red,
- FColor::Green,
- 5.f
- );
- if (BoxHit.GetActor())
- {
- IHitInterface* HitInterface = Cast<IHitInterface>(BoxHit.GetActor());
- if (HitInterface && !bIsInOverlap)
- {
- //bIsInOverlap = true;
- //HitInterface->GetHit(BoxHit.ImpactPoint);
- UE_LOG(LogTemp, Warning, TEXT("Weapon.cpp: OtherActor Name: %s"), *BoxHit.GetActor()->GetName());
- UE_LOG(LogTemp, Warning, TEXT("Weapon.cpp: Beffor - Execute_GetHit"));
- HitInterface->Execute_GetHit(BoxHit.GetActor(), BoxHit.ImpactPoint);
- UE_LOG(LogTemp, Warning, TEXT("Weapon.cpp: After - Execute_GetHit"));
- }
- IgnoreActors.AddUnique(BoxHit.GetActor());
- CreateFields(BoxHit.ImpactPoint);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement