Advertisement
JoaoCoutoCosta

UE4_EscapeGame_GrabBug

Mar 26th, 2017
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.17 KB | None | 0 0
  1. // Joao Costa 2017
  2.  
  3. #include "Unreal4_Escape.h"
  4. #include "Grabber.h"
  5.  
  6.  
  7. // Sets default values for this component's properties
  8. UGrabber::UGrabber()
  9. {
  10.     // Set this component to be initialized when the game starts, and to be ticked every frame.  You can turn these features
  11.     // off to improve performance if you don't need them.
  12.     PrimaryComponentTick.bCanEverTick = true;
  13.  
  14.     // ...
  15. }
  16.  
  17.  
  18. // Called when the game starts
  19. void UGrabber::BeginPlay()
  20. {
  21.     Super::BeginPlay();
  22.  
  23.     /// Get the owner
  24.     this->Owner = GetOwner();
  25.  
  26.     /// Find necessary handles
  27.     this->FindPhysicsHandleComponent();
  28.     this->FindInputComponent();
  29. }
  30.  
  31. // Called every frame
  32. void UGrabber::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
  33. {
  34.     Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
  35.  
  36.     /// Keep the player position updated
  37.     this->UpdatePlayerViewpoint();
  38.  
  39.     /// Draw and aux debug line
  40.     DrawDebugLine(
  41.         GetWorld(),
  42.         this->GetRayCastLineStart(),
  43.         this->GetRayCastLineEnd(),
  44.         FColor(255, 0, 0),
  45.         false,
  46.         0.0f,
  47.         0,
  48.         2.0f
  49.     );
  50.  
  51.     /// Move grabbed object if there is one
  52.     if (this->PhysicsHandle->GrabbedComponent)
  53.     {
  54.         /// Mode the handle
  55.         this->PhysicsHandle->SetTargetLocation(this->GetRayCastLineEnd());
  56.     }
  57. }
  58.  
  59. // Find physics handle
  60. void UGrabber::FindPhysicsHandleComponent() {
  61.     /// Attach Physics handle
  62.     this->PhysicsHandle = this->Owner->FindComponentByClass<UPhysicsHandleComponent>();
  63.  
  64.     /// Verify correct attachment
  65.     if (!this->PhysicsHandle) {
  66.         UE_LOG(LogTemp, Error, TEXT("%s mising physics handle component"), *this->Owner->GetName());
  67.     }
  68. }
  69.  
  70. // Find input component
  71. void UGrabber::FindInputComponent() {
  72.     /// Attach Input handler
  73.     this->InputComponent = this->Owner->FindComponentByClass<UInputComponent>();
  74.  
  75.     /// Verify correct attachment
  76.     if (!this->InputComponent) {
  77.         UE_LOG(LogTemp, Error, TEXT("%s mising input handle"), *this->Owner->GetName());
  78.     }
  79.     else {
  80.         this->InputComponent->BindAction("Grab", IE_Pressed, this, &UGrabber::Grab);
  81.         this->InputComponent->BindAction("Grab", IE_Released, this, &UGrabber::Release);
  82.     }
  83. }
  84.  
  85. void UGrabber::Grab() {
  86.     UE_LOG(LogTemp, Warning, TEXT("Grab key pressed"));
  87.  
  88.     /// Ray-cast and check objects within reach
  89.     FHitResult HitResult = this->GetFirstPhysicsBodyInReach();
  90.     auto ComponentToGrab = HitResult.GetComponent();
  91.     auto ActorHit = HitResult.GetActor();
  92.  
  93.     /// Attach physics handle if there's and object within reach
  94.     if (ActorHit) {
  95.         /// DEBUG AUX
  96.         UE_LOG(LogTemp, Warning, TEXT("Player hit actor %s"), *(ActorHit->GetName()));
  97.  
  98.         /// Update the viewpoint
  99.         this->UpdatePlayerViewpoint();
  100.         /// Attach the physic handle
  101.         this->PhysicsHandle->GrabComponentAtLocationWithRotation(
  102.             ComponentToGrab,
  103.             NAME_None,
  104.             this->PlayerViewPoint.Location,
  105.             FRotator(0.0f, 0.0f, 0.0f)
  106.         );
  107.     }
  108. }
  109.  
  110. void UGrabber::Release() {
  111.     UE_LOG(LogTemp, Warning, TEXT("Grab key released"));
  112.  
  113.     /// Release the handle
  114.     this->PhysicsHandle->ReleaseComponent();
  115. }
  116.  
  117. // Store Player Viewpoint
  118. void UGrabber::UpdatePlayerViewpoint()
  119. {
  120.     /// Get current player viewpoint and update attributes
  121.     GetWorld()->GetFirstPlayerController()->GetPlayerViewPoint(
  122.         this->PlayerViewPoint.Location,
  123.         this->PlayerViewPoint.Rotation
  124.     );
  125. }
  126.  
  127. // Get reach line start
  128. const FVector UGrabber::GetRayCastLineStart()
  129. {
  130.     /// Update the viewpoint
  131.     this->UpdatePlayerViewpoint();
  132.     return this->PlayerViewPoint.Location;
  133. }
  134.  
  135. // Get reach line end
  136. const FVector UGrabber::GetRayCastLineEnd()
  137. {
  138.     /// Update the viewpoint
  139.     this->UpdatePlayerViewpoint();
  140.     return this->PlayerViewPoint.Location + this->PlayerViewPoint.Rotation.Vector() * this->GrabReach;
  141. }
  142.  
  143. // Setup trace parameters
  144. const FCollisionQueryParams UGrabber::GetRayCastParameters()
  145. {
  146.     return FCollisionQueryParams(FName(TEXT("")), false, this->Owner);
  147. }
  148.  
  149. // Get physics body in witthin reach
  150. const FHitResult UGrabber::GetFirstPhysicsBodyInReach()
  151. {
  152.     FHitResult HitResult;
  153.     GetWorld()->LineTraceSingleByObjectType(
  154.         HitResult,
  155.         this->GetRayCastLineStart(),
  156.         this->GetRayCastLineEnd(),
  157.         FCollisionObjectQueryParams(ECollisionChannel::ECC_PhysicsBody),
  158.         this->GetRayCastParameters()
  159.     );
  160.  
  161.     return HitResult;
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement