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 "Laser.h"
- #include "Beam.h"
- #include "DrawDebugHelpers.h"
- #include "EngineUtils.h"
- #include "CollisionQueryParams.h"
- #include "Kismet/GameplayStatics.h"
- #include "Math/Vector.h"
- // Sets default values
- ALaser::ALaser()
- {
- // Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
- PrimaryActorTick.bCanEverTick = true;
- Arrow1P = CreateDefaultSubobject<UArrowComponent>(TEXT("Arrow"));
- Arrow1P->SetupAttachment(RootComponent);
- Arrow1P->SetMobility(EComponentMobility::Movable);
- LaserMaterial = CreateDefaultSubobject<UMaterial>(TEXT("LaserMaterial"));
- }
- // Called when the game starts or when spawned
- void ALaser::BeginPlay()
- {
- Super::BeginPlay();
- GetWorld()->GetTimerManager().SetTimer(StartLaserTimer, this, &ALaser::StartCast, 0.01f, true);
- }
- // Called every frame
- void ALaser::Tick(float DeltaTime)
- {
- Super::Tick(DeltaTime);
- }
- void ALaser::StartCast()
- {
- CastBeam(Arrow1P->GetComponentLocation() , Arrow1P->GetForwardVector(), Range);
- }
- void ALaser::CastBeam(FVector LOrigin, FVector LDirection, float Distance)
- {
- FVector BeamOrigin = LOrigin;
- FVector BeamDirection = LDirection;
- float BeamDistance = Distance;
- UWorld* world = GetWorld();
- FCollisionQueryParams TraceParams;
- for (int index = 0; index < Lasers.Num(); index++)
- {
- GEngine->AddOnScreenDebugMessage(index, 30.f, FColor::Red, FString(Lasers[index]->GetName()));
- if (!Lasers[index]) return;
- if (!Lasers[index]->IsValidLowLevel()) return;
- Lasers[index]->K2_DestroyActor();
- Lasers[index] = NULL;
- }
- Lasers.Empty();
- bool Continue = true;
- while (Continue)
- {
- FHitResult Hit;
- DrawDebugLine(GetWorld(), BeamOrigin, (BeamOrigin + (BeamDirection * BeamDistance)), FColor::Red, false, 1, 0 , 1);
- bool cast = GetWorld()->LineTraceSingleByChannel(Hit, BeamOrigin, (BeamOrigin + (BeamDirection * BeamDistance)), ECC_Visibility, TraceParams);
- LaserStart = BeamOrigin;
- if (cast)
- {
- DrawDebugBox(world, Hit.ImpactPoint, FVector(5, 5, 5), FColor::Emerald, false, 2.0f);
- LaserEnd = Hit.ImpactPoint;
- bool HitComponent = Hit.Component->ComponentHasTag(TEXT("Reflect"));
- bool HitActor = Hit.Actor->ActorHasTag(TEXT("Reflect"));
- if (HitComponent || HitActor)
- {
- BeamOrigin = Hit.ImpactPoint;
- BeamDirection = BeamDirection.MirrorByVector(Hit.ImpactNormal);
- Continue = true;
- }
- else
- {
- Continue = false;
- }
- }
- else
- {
- LaserEnd = (BeamOrigin + (BeamDirection * BeamDistance));
- Continue = false;
- }
- FVector Location(0.0f, 0.0f, 0.0f);
- FRotator Rotation(0.0f, 0.0f, 0.0f);
- FActorSpawnParameters SpawnInfo;
- Beamref = GetWorld()->SpawnActor<ABeam>(Location, Rotation, SpawnInfo);
- if (Beamref != nullptr)
- {
- Cast<ABeam>(Beamref)->SetEnds(LaserStart, LaserEnd); UE_LOG(LogTemp, Warning, TEXT("Spawned"));
- Lasers.Add(Beamref); UE_LOG(LogTemp, Warning, TEXT("Added"));
- }
- else
- {
- UE_LOG(LogClass, Log, TEXT("NULL"));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement