Advertisement
Guest User

Laser.cpp

a guest
Dec 1st, 2020
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.09 KB | None | 0 0
  1. // Fill out your copyright notice in the Description page of Project Settings.
  2. #include "Laser.h"
  3. #include "Beam.h"
  4. #include "DrawDebugHelpers.h"
  5. #include "EngineUtils.h"
  6. #include "CollisionQueryParams.h"
  7. #include "Kismet/GameplayStatics.h"
  8. #include "Math/Vector.h"
  9.  
  10. // Sets default values
  11. ALaser::ALaser()
  12. {
  13.     // Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
  14.     PrimaryActorTick.bCanEverTick = true;
  15.  
  16.     Arrow1P = CreateDefaultSubobject<UArrowComponent>(TEXT("Arrow"));
  17.     Arrow1P->SetupAttachment(RootComponent);
  18.     Arrow1P->SetMobility(EComponentMobility::Movable);
  19.  
  20.     LaserMaterial = CreateDefaultSubobject<UMaterial>(TEXT("LaserMaterial"));
  21.  
  22. }
  23.  
  24. // Called when the game starts or when spawned
  25. void ALaser::BeginPlay()
  26. {
  27.     Super::BeginPlay();
  28.     GetWorld()->GetTimerManager().SetTimer(StartLaserTimer, this, &ALaser::StartCast, 0.01f, true);
  29. }
  30.  
  31. // Called every frame
  32. void ALaser::Tick(float DeltaTime)
  33. {
  34.     Super::Tick(DeltaTime);
  35.  
  36. }
  37.  
  38. void ALaser::StartCast()
  39. {
  40.  
  41.     CastBeam(Arrow1P->GetComponentLocation() , Arrow1P->GetForwardVector(), Range);
  42. }
  43.  
  44. void ALaser::CastBeam(FVector LOrigin, FVector LDirection, float Distance)
  45. {
  46.  
  47.     FVector BeamOrigin = LOrigin;
  48.     FVector BeamDirection = LDirection;
  49.     float BeamDistance = Distance;
  50.     UWorld* world = GetWorld();
  51.     FCollisionQueryParams TraceParams;
  52.  
  53.     for (int index = 0; index < Lasers.Num(); index++)
  54.     {
  55.         GEngine->AddOnScreenDebugMessage(index, 30.f, FColor::Red, FString(Lasers[index]->GetName()));
  56.         if (!Lasers[index]) return;
  57.         if (!Lasers[index]->IsValidLowLevel()) return;
  58.  
  59.         Lasers[index]->K2_DestroyActor();
  60.         Lasers[index] = NULL;
  61.  
  62.     }
  63.     Lasers.Empty();
  64.  
  65.     bool Continue = true;
  66.     while (Continue)
  67.     {
  68.         FHitResult Hit;
  69.         DrawDebugLine(GetWorld(), BeamOrigin, (BeamOrigin + (BeamDirection * BeamDistance)), FColor::Red, false, 1, 0 , 1);
  70.  
  71.         bool cast = GetWorld()->LineTraceSingleByChannel(Hit, BeamOrigin, (BeamOrigin + (BeamDirection * BeamDistance)), ECC_Visibility, TraceParams);
  72.         LaserStart = BeamOrigin;
  73.         if (cast)
  74.         {
  75.             DrawDebugBox(world, Hit.ImpactPoint, FVector(5, 5, 5), FColor::Emerald, false, 2.0f);
  76.             LaserEnd = Hit.ImpactPoint;
  77.             bool HitComponent = Hit.Component->ComponentHasTag(TEXT("Reflect"));
  78.             bool HitActor = Hit.Actor->ActorHasTag(TEXT("Reflect"));
  79.  
  80.  
  81.             if (HitComponent || HitActor)
  82.             {
  83.                 BeamOrigin = Hit.ImpactPoint;
  84.                 BeamDirection = BeamDirection.MirrorByVector(Hit.ImpactNormal);
  85.                 Continue = true;
  86.             }
  87.             else
  88.             {
  89.                 Continue = false;
  90.             }
  91.         }
  92.         else
  93.         {
  94.             LaserEnd = (BeamOrigin + (BeamDirection * BeamDistance));
  95.             Continue = false;
  96.         }
  97.  
  98.         FVector Location(0.0f, 0.0f, 0.0f);
  99.         FRotator Rotation(0.0f, 0.0f, 0.0f);
  100.         FActorSpawnParameters SpawnInfo;
  101.         Beamref = GetWorld()->SpawnActor<ABeam>(Location, Rotation, SpawnInfo);
  102.         if (Beamref != nullptr)
  103.         {
  104.             Cast<ABeam>(Beamref)->SetEnds(LaserStart, LaserEnd); UE_LOG(LogTemp, Warning, TEXT("Spawned"));
  105.             Lasers.Add(Beamref); UE_LOG(LogTemp, Warning, TEXT("Added"));
  106.         }
  107.         else
  108.         {
  109.             UE_LOG(LogClass, Log, TEXT("NULL"));
  110.         }
  111.        
  112.        
  113.  
  114.     }
  115.    
  116.  
  117.  
  118. }
  119.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement