Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Joao Costa 2017
- #include "Unreal4_Escape.h"
- #include "Grabber.h"
- // Sets default values for this component's properties
- 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;
- // ...
- }
- // Called when the game starts
- void UGrabber::BeginPlay()
- {
- Super::BeginPlay();
- /// Get the owner
- this->Owner = GetOwner();
- /// Find necessary handles
- this->FindPhysicsHandleComponent();
- this->FindInputComponent();
- }
- // Called every frame
- void UGrabber::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
- {
- Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
- /// Keep the player position updated
- this->UpdatePlayerViewpoint();
- /// Draw and aux debug line
- DrawDebugLine(
- GetWorld(),
- this->GetRayCastLineStart(),
- this->GetRayCastLineEnd(),
- FColor(255, 0, 0),
- false,
- 0.0f,
- 0,
- 2.0f
- );
- /// Move grabbed object if there is one
- if (this->PhysicsHandle->GrabbedComponent)
- {
- /// Mode the handle
- this->PhysicsHandle->SetTargetLocation(this->GetRayCastLineEnd());
- }
- }
- // Find physics handle
- void UGrabber::FindPhysicsHandleComponent() {
- /// Attach Physics handle
- this->PhysicsHandle = this->Owner->FindComponentByClass<UPhysicsHandleComponent>();
- /// Verify correct attachment
- if (!this->PhysicsHandle) {
- UE_LOG(LogTemp, Error, TEXT("%s mising physics handle component"), *this->Owner->GetName());
- }
- }
- // Find input component
- void UGrabber::FindInputComponent() {
- /// Attach Input handler
- this->InputComponent = this->Owner->FindComponentByClass<UInputComponent>();
- /// Verify correct attachment
- if (!this->InputComponent) {
- UE_LOG(LogTemp, Error, TEXT("%s mising input handle"), *this->Owner->GetName());
- }
- else {
- this->InputComponent->BindAction("Grab", IE_Pressed, this, &UGrabber::Grab);
- this->InputComponent->BindAction("Grab", IE_Released, this, &UGrabber::Release);
- }
- }
- void UGrabber::Grab() {
- UE_LOG(LogTemp, Warning, TEXT("Grab key pressed"));
- /// Ray-cast and check objects within reach
- FHitResult HitResult = this->GetFirstPhysicsBodyInReach();
- auto ComponentToGrab = HitResult.GetComponent();
- auto ActorHit = HitResult.GetActor();
- /// Attach physics handle if there's and object within reach
- if (ActorHit) {
- /// DEBUG AUX
- UE_LOG(LogTemp, Warning, TEXT("Player hit actor %s"), *(ActorHit->GetName()));
- /// Update the viewpoint
- this->UpdatePlayerViewpoint();
- /// Attach the physic handle
- this->PhysicsHandle->GrabComponentAtLocationWithRotation(
- ComponentToGrab,
- NAME_None,
- this->PlayerViewPoint.Location,
- FRotator(0.0f, 0.0f, 0.0f)
- );
- }
- }
- void UGrabber::Release() {
- UE_LOG(LogTemp, Warning, TEXT("Grab key released"));
- /// Release the handle
- this->PhysicsHandle->ReleaseComponent();
- }
- // Store Player Viewpoint
- void UGrabber::UpdatePlayerViewpoint()
- {
- /// Get current player viewpoint and update attributes
- GetWorld()->GetFirstPlayerController()->GetPlayerViewPoint(
- this->PlayerViewPoint.Location,
- this->PlayerViewPoint.Rotation
- );
- }
- // Get reach line start
- const FVector UGrabber::GetRayCastLineStart()
- {
- /// Update the viewpoint
- this->UpdatePlayerViewpoint();
- return this->PlayerViewPoint.Location;
- }
- // Get reach line end
- const FVector UGrabber::GetRayCastLineEnd()
- {
- /// Update the viewpoint
- this->UpdatePlayerViewpoint();
- return this->PlayerViewPoint.Location + this->PlayerViewPoint.Rotation.Vector() * this->GrabReach;
- }
- // Setup trace parameters
- const FCollisionQueryParams UGrabber::GetRayCastParameters()
- {
- return FCollisionQueryParams(FName(TEXT("")), false, this->Owner);
- }
- // Get physics body in witthin reach
- const FHitResult UGrabber::GetFirstPhysicsBodyInReach()
- {
- FHitResult HitResult;
- GetWorld()->LineTraceSingleByObjectType(
- HitResult,
- this->GetRayCastLineStart(),
- this->GetRayCastLineEnd(),
- FCollisionObjectQueryParams(ECollisionChannel::ECC_PhysicsBody),
- this->GetRayCastParameters()
- );
- return HitResult;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement