From d6dfe7366cdb1a6404a9844b51a0add904ee7cc1 Mon Sep 17 00:00:00 2001 From: Tyrel Souza Date: Tue, 20 Feb 2018 23:57:01 -0500 Subject: [PATCH] refactor --- RoomEscape/Source/RoomEscape/Grabber.cpp | 88 ++++++++++++++---------- RoomEscape/Source/RoomEscape/Grabber.h | 13 +++- 2 files changed, 62 insertions(+), 39 deletions(-) diff --git a/RoomEscape/Source/RoomEscape/Grabber.cpp b/RoomEscape/Source/RoomEscape/Grabber.cpp index 4aa0ea4..a77bd38 100644 --- a/RoomEscape/Source/RoomEscape/Grabber.cpp +++ b/RoomEscape/Source/RoomEscape/Grabber.cpp @@ -10,8 +10,7 @@ #define OUT // Sets default values for this component's properties -UGrabber::UGrabber() -{ +UGrabber::UGrabber() { // Set this component to be initialized when the game starts, and to be ticked every frame. You can turn these features // off to improve performance if you don't need them. PrimaryComponentTick.bCanEverTick = true; @@ -20,19 +19,58 @@ UGrabber::UGrabber() // Called when the game starts -void UGrabber::BeginPlay() -{ +void UGrabber::BeginPlay() { Super::BeginPlay(); - UE_LOG(LogTemp, Warning, TEXT("Grabber beginPlay")); - ///Look for attached physics handle + FindPhysicsHandleComponent(); + SetUpInputComponent(); + + +} +// Called every fraem +void UGrabber::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) +{ + Super::TickComponent(DeltaTime, TickType, ThisTickFunction); + + // if physics handle is attached + // move object holding + // do nothing + + + + + +} + + +// Raycast and grab what is in reach +void UGrabber::Grab() { + UE_LOG(LogTemp, Warning, TEXT("GRABBING!")); + // LINE TRACE and Try and reach any actors with physics body collision channel set + GetFirstPhysicsBodyInReach(); + // if we hit something attach physics handle + // TODO: attach physics handle + +} + +// Release +void UGrabber::Release() { + UE_LOG(LogTemp, Warning, TEXT("Released!")); + //TODO: Release physics handle +} + +// Look for attached physics handle +void UGrabber::FindPhysicsHandleComponent() { PhysicsHandle = GetOwner()->FindComponentByClass(); if (PhysicsHandle) { } else { UE_LOG(LogTemp, Error, TEXT("%s has no PhysicsHandle"), *(GetOwner()->GetName())); } +} +// Look for attached input component +void UGrabber::SetUpInputComponent() { InputComponent = GetOwner()->FindComponentByClass(); if (InputComponent) { UE_LOG(LogTemp, Warning, TEXT("%s has a InputHandle"), *(GetOwner()->GetName())); @@ -43,23 +81,10 @@ void UGrabber::BeginPlay() } else { UE_LOG(LogTemp, Error, TEXT("%s has no InputHandle"), *(GetOwner()->GetName())); } - } -// Raycast and grab what is in reach -void UGrabber::Grab() { - UE_LOG(LogTemp, Warning, TEXT("GRABBING!")); -} -void UGrabber::Release() { - UE_LOG(LogTemp, Warning, TEXT("Released!")); -} - - -// Called every frame -void UGrabber::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) +FHitResult UGrabber::GetFirstPhysicsBodyInReach() const { - Super::TickComponent(DeltaTime, TickType, ThisTickFunction); - /// Get player View point in this tick FVector PlayerViewpointLocation; FRotator PlayerViewpointRotation; @@ -71,17 +96,6 @@ void UGrabber::TickComponent(float DeltaTime, ELevelTick TickType, FActorCompone FVector LineTraceEnd = PlayerViewpointLocation + PlayerViewpointRotation.Vector() * Reach; - DrawDebugLine( - GetWorld(), - PlayerViewpointLocation, - LineTraceEnd, - FColor(255, 0, 0), - false, - 0.f, - 0, - 5.0f - ); - ///Setup Query Parameters FCollisionQueryParams TraceParameters(FName(TEXT("")), false, GetOwner()); @@ -96,15 +110,15 @@ void UGrabber::TickComponent(float DeltaTime, ELevelTick TickType, FActorCompone TraceParameters ); + /// See what we hit AActor* ActorHit = Hit.GetActor(); if (ActorHit) { UE_LOG(LogTemp, Warning, TEXT("Line Tract Hit: %s"), *(ActorHit->GetName())); } - - /// See what we hit - - - - + return Hit; } + +//DrawDebugLine(GetWorld(), PlayerViewpointLocation, LineTraceEnd, FColor(255, 0, 0), false, +// 0.f, 0, 5.0f +//); \ No newline at end of file diff --git a/RoomEscape/Source/RoomEscape/Grabber.h b/RoomEscape/Source/RoomEscape/Grabber.h index d588c28..6dc6a15 100644 --- a/RoomEscape/Source/RoomEscape/Grabber.h +++ b/RoomEscape/Source/RoomEscape/Grabber.h @@ -9,12 +9,12 @@ #include "Grabber.generated.h" -UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) ) +UCLASS(ClassGroup = (Custom), meta = (BlueprintSpawnableComponent)) class ROOMESCAPE_API UGrabber : public UActorComponent { GENERATED_BODY() -public: +public: UGrabber(); virtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override; @@ -28,4 +28,13 @@ private: UInputComponent* InputComponent = nullptr; void Grab(); // Raycast and grab what is in reach void Release(); // Raycast and grab what is in reach + + // Find (assumed) attached physics handle + void FindPhysicsHandleComponent(); + + // Setup assumed or attached input component + void SetUpInputComponent(); + + // Return hit for first physics body in reach + FHitResult GetFirstPhysicsBodyInReach() const; };