Advertisement
Guest User

BaseHand.cpp

a guest
May 20th, 2020
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 24.71 KB | None | 0 0
  1. #include "TheDesertsRose.h"
  2. #include "Ace.h"
  3. #include "BaseHand.h"
  4. #include "BaseCharacter.h"
  5. #include "Items/BaseItem.h"
  6. #include "Items/BaseWeapon.h"
  7. #include "Animation/AnimBlueprint.h"
  8. #include "GameFramework/InputSettings.h"
  9. #include "Kismet/HeadMountedDisplayFunctionLibrary.h"
  10. #include "Runtime/UMG/Public/Components/WidgetInteractionComponent.h"
  11. #include "MotionControllerComponent.h"
  12.  
  13. ABaseHand::ABaseHand() : Super()
  14. {
  15.     myIsUsingMotionControllers = true;
  16.     myIsCurrentlyGrabbing = false;
  17.     myIsCurrentlyCrushing = false;
  18.     myIsTriggerPressed = false;
  19.     myReachDistance = 50.0f;
  20.     myDistanceFromController = 10.0f;
  21.     myMinDistanceFromController = 10.0f;
  22.     myMaxDistanceFromController = 250.0f;
  23.     myControllerLocation = FVector::ZeroVector;
  24.     myControllerRotation = FRotator::ZeroRotator;
  25.     myIsItemEquipped = false;
  26.  
  27.     myGrabbedItem = NULL;
  28.     myGrabbedPhysicsHandle = NULL;
  29.     myWidgetInteractionComponent = NULL;
  30.  
  31.     PrimaryActorTick.bCanEverTick = true;
  32.  
  33.     myMotionController = CreateDefaultSubobject<UMotionControllerComponent>(TEXT("MotionController"));
  34.     RootComponent = myMotionController;
  35.  
  36.     //myMotionController->Hand = myControllerHand; DEPRICATED, they changed how this is handled in 4.19
  37.     if (myControllerHand == EControllerHand::Left)
  38.     {
  39.         myMotionController->MotionSource = "LEFT";
  40.     }
  41.     else
  42.     {
  43.         myMotionController->MotionSource = "RIGHT";
  44.     }
  45.  
  46.     myVRHandMesh = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("VRHandMesh"));
  47.     myVRHandMesh->SetOnlyOwnerSee(false);          
  48.     myVRHandMesh->bCastDynamicShadow = false;
  49.     myVRHandMesh->CastShadow = false;
  50.     myVRHandMesh->SetupAttachment(RootComponent);
  51.  
  52.     myCollisionChannels[0] = ECollisionChannel::ECC_Camera;
  53.     myCollisionChannels[1] = ECollisionChannel::ECC_Destructible;
  54.     myCollisionChannels[2] = ECollisionChannel::ECC_MAX;
  55.     myCollisionChannels[3] = ECollisionChannel::ECC_Pawn;
  56.     myCollisionChannels[4] = ECollisionChannel::ECC_PhysicsBody;
  57.     myCollisionChannels[5] = ECollisionChannel::ECC_Vehicle;
  58.     myCollisionChannels[6] = ECollisionChannel::ECC_Visibility;
  59.     myCollisionChannels[7] = ECollisionChannel::ECC_WorldDynamic;
  60.     myCollisionChannels[8] = ECollisionChannel::ECC_WorldStatic;
  61.     myCollisionChannels[9] = ECollisionChannel::ECC_EngineTraceChannel1;
  62.     myCollisionChannels[10] = ECollisionChannel::ECC_EngineTraceChannel2;
  63.     myCollisionChannels[11] = ECollisionChannel::ECC_EngineTraceChannel3;
  64.     myCollisionChannels[12] = ECollisionChannel::ECC_EngineTraceChannel4;
  65.     myCollisionChannels[13] = ECollisionChannel::ECC_EngineTraceChannel5;
  66.     myCollisionChannels[14] = ECollisionChannel::ECC_EngineTraceChannel6;
  67.     myCollisionChannels[15] = ECollisionChannel::ECC_GameTraceChannel1;
  68.     myCollisionChannels[16] = ECollisionChannel::ECC_GameTraceChannel2;
  69.     myCollisionChannels[17] = ECollisionChannel::ECC_GameTraceChannel3;
  70.     myCollisionChannels[18] = ECollisionChannel::ECC_GameTraceChannel4;
  71.     myCollisionChannels[19] = ECollisionChannel::ECC_GameTraceChannel5;
  72.     myCollisionChannels[20] = ECollisionChannel::ECC_GameTraceChannel6;
  73.     myCollisionChannels[21] = ECollisionChannel::ECC_GameTraceChannel7;
  74.     myCollisionChannels[22] = ECollisionChannel::ECC_GameTraceChannel8;
  75.     myCollisionChannels[23] = ECollisionChannel::ECC_GameTraceChannel9;
  76.     myCollisionChannels[24] = ECollisionChannel::ECC_GameTraceChannel10;
  77.     myCollisionChannels[25] = ECollisionChannel::ECC_GameTraceChannel11;
  78.     myCollisionChannels[26] = ECollisionChannel::ECC_GameTraceChannel12;
  79.     myCollisionChannels[27] = ECollisionChannel::ECC_GameTraceChannel13;
  80.     myCollisionChannels[28] = ECollisionChannel::ECC_GameTraceChannel14;
  81.     myCollisionChannels[29] = ECollisionChannel::ECC_GameTraceChannel15;
  82.     myCollisionChannels[30] = ECollisionChannel::ECC_GameTraceChannel16;
  83.     myCollisionChannels[31] = ECollisionChannel::ECC_GameTraceChannel17;
  84.     myCollisionChannels[32] = ECollisionChannel::ECC_GameTraceChannel18;
  85. }
  86.  
  87. /**
  88. * Ticks.
  89. *
  90. * @param deltaTime The delta time.
  91. */
  92. void ABaseHand::Tick(float deltaTime)
  93. {
  94.     Super::Tick(deltaTime);
  95.  
  96.     //Get the VR controller location and rotation if we are using them.
  97.     if (myVRHandMesh != NULL)
  98.     {
  99.         myControllerLocation = myMotionController->GetComponentLocation();
  100.         myControllerRotation = myMotionController->GetComponentRotation();
  101.         myControllerTransform = myMotionController->GetComponentTransform();
  102.     }
  103.  
  104.     //Neither work
  105.     //UE_LOG(LogTemp, Error, TEXT("myControllerLocation:%s"), *myControllerLocation.ToString());
  106.     //UE_LOG(LogTemp, Error, TEXT("myControllerRotation:%s"), *myControllerRotation.ToString());
  107.     //UE_LOG(LogTemp, Error, TEXT("myControllerTransform:%s"), *myControllerTransform.ToString());
  108.     //UE_LOG(LogTemp, Error, TEXT("myMotionController->GetComponentLocation():%s"), *myMotionController->GetComponentLocation().ToString());
  109.     //UE_LOG(LogTemp, Error, TEXT("myMotionController->GetComponentRotation():%s"), *myMotionController->GetComponentRotation().ToString());
  110.     //UE_LOG(LogTemp, Error, TEXT("myMotionController->GetComponentTransform():%s"), *myMotionController->GetComponentTransform().ToString());
  111.  
  112.     //Update grabbed object location & rotation (if any)
  113.     if (myGrabbedPhysicsHandle != NULL)
  114.     {
  115.         myNewGrabbedLocation = myControllerLocation + (myControllerRotation.Vector());
  116.  
  117.         myGrabbedPhysicsHandle->SetTargetLocation(myNewGrabbedLocation);
  118.     }
  119. }
  120.  
  121. /** Functions. */
  122. void ABaseHand::BeginPlay()
  123. {
  124.     TArray<UActorComponent*> components;
  125.  
  126.     //Call the base class
  127.     Super::BeginPlay();
  128.  
  129.     myVRHandMesh->SetHiddenInGame(false, true);
  130.  
  131.     //Show or hide the two versions of the gun based on whether or not we're using motion controllers.
  132.     //if (myFirstPersonHandMesh != NULL && myVRHandMesh != NULL)
  133.     //{
  134.     //  if (myIsUsingMotionControllers == true)
  135.     //  {
  136.     //      myFirstPersonHandMesh->SetHiddenInGame(true, true);
  137.     //      myVRHandMesh->SetHiddenInGame(false, true);
  138.     //  }
  139.     //  else
  140.     //  {
  141.     //      myVRHandMesh->SetHiddenInGame(true, true);
  142.     //      myFirstPersonHandMesh->SetHiddenInGame(false, true);
  143.     //  }
  144.     //}
  145.  
  146.     //Find the Widget Interaction Component so we can detach it from the hand at runtime.
  147.     components = this->GetComponentsByClass(UActorComponent::StaticClass());
  148.     for (int currComponentIndex = 0; currComponentIndex < components.Num(); currComponentIndex++)
  149.     {
  150.         UWidgetInteractionComponent* currComponent = Cast<UWidgetInteractionComponent>(components[currComponentIndex]);
  151.         if (currComponent != NULL)
  152.         {
  153.             myWidgetInteractionComponent = currComponent;
  154.         }
  155.     }
  156.  
  157.     //Only remove the interaction component if the level is NOT a menu level.
  158.     if (GetWorld()->GetCurrentLevel()->OwningWorld->GetName().Contains(FString("Menu")) == false)
  159.     {
  160.         //Remove the interaction component since it has annoying arrows always visible.
  161.         if (myWidgetInteractionComponent != NULL)
  162.         {
  163.             myWidgetInteractionComponent->DetachFromParent();
  164.         }
  165.     }
  166. }
  167.  
  168. bool ABaseHand::Grab(AActor* actorThatIsGrabbing, bool showDebugLine)
  169. {
  170.     double grabRadius = 7.0;
  171.  
  172.     FVector lineTraceEnd;
  173.     FVector lineTraceStart;
  174.     AActor* actorHit = NULL;
  175.     FHitResult traceHitResult;
  176.  
  177.     //Only allow grab if the hand isn't already holding something and it is grabbable.
  178.     if (actorThatIsGrabbing != NULL && myIsItemEquipped == false && myIsCurrentlyGrabbing == false)
  179.     {
  180.         //We are now grabbing.
  181.         myIsCurrentlyGrabbing = true;
  182.  
  183.         //Update the current location and rotation variables.
  184.         myControllerLocation = myMotionController->GetComponentLocation();
  185.         myControllerRotation = myMotionController->GetComponentRotation();
  186.         myControllerTransform = myMotionController->GetComponentTransform();
  187.  
  188.         //Set Line Trace (Ray-Cast) endpoints
  189.         lineTraceStart = myControllerLocation;
  190.         lineTraceEnd = lineTraceStart + (myControllerRotation.Vector() * myReachDistance);
  191.  
  192.         myLastHitDistance = lineTraceEnd - lineTraceStart;
  193.  
  194.         //Ray trace
  195.         actorHit = ABaseHand::CastCircleOfRays(grabRadius, lineTraceStart, lineTraceEnd, traceHitResult, showDebugLine);
  196.  
  197.         //Check if there's a valid object to grab
  198.         if (actorHit != NULL)
  199.         {
  200.             //Retain a reference to the owning character and the grabbed item.
  201.             myOwningCharacter = Cast<ABaseCharacter>(actorThatIsGrabbing);
  202.             myGrabbedItem = Cast<ABaseItem>(actorHit);
  203.  
  204.             //Ensure cast was successful.
  205.             if (myOwningCharacter != NULL && myGrabbedItem != NULL)
  206.             {
  207.                 if (myGrabbedItem->GetIsGrabbable() == true)
  208.                 {
  209.                     if (myControllerHand == EControllerHand::Left)
  210.                     {
  211.                         myGrabbedItem->SetLeftOwningHand(this);
  212.                     }
  213.                     else
  214.                     {
  215.                         myGrabbedItem->SetRightOwningHand(this);
  216.                     }
  217.  
  218.                     //Do the actual attachment of the item to the hand.
  219.                     if (Cast<ABaseWeapon>(myGrabbedItem) != NULL)
  220.                     {
  221.                         this->GrabWeapon(myGrabbedItem);
  222.                     }
  223.                     else
  224.                     {
  225.                         this->GrabItem(myGrabbedItem);
  226.                     }
  227.                 }
  228.                 else if (myGrabbedItem->GetIsTouchable() == true)
  229.                 {
  230.                     this->TouchItem(myGrabbedItem);
  231.                 }
  232.             }
  233.         }
  234.     }
  235.  
  236.     return false;
  237. }
  238.  
  239. bool ABaseHand::GrabItem(ABaseItem* grabbedItem)
  240. {
  241.     //Get the orientation of the item and hand to use when it is grabbed.
  242.     //Prevent grabbing broken items since this picks up all chunks.
  243.     if (grabbedItem != NULL && grabbedItem->GetIsGrabbable() == true && grabbedItem->GetIsFractured() == false)
  244.     {
  245.         //Attempt to Grab Object
  246.         myGrabbedItem = grabbedItem;
  247.  
  248.         //Only reset the component state if it is being picked up with no hands currently touching it.
  249.         myGrabbedPrimitiveComponent = Cast<UPrimitiveComponent>(myGrabbedItem->GetGrabbableRootComponent());
  250.         if (myGrabbedPrimitiveComponent != NULL)
  251.         {
  252.             if (myGrabbedPrimitiveComponent->IsPhysicsStateCreated() == true)
  253.             {
  254.                 myGrabbedPrimitiveComponent->RecreatePhysicsState();
  255.             }
  256.            
  257.             myGrabbedPrimitiveComponent->AttachToComponent(myVRHandMesh, FAttachmentTransformRules(EAttachmentRule::KeepWorld, false));
  258.  
  259.             myGrabbedPrimitiveComponent->SetSimulatePhysics(false);
  260.             myGrabbedPrimitiveComponent->SetEnableGravity(false);
  261.             myGrabbedPrimitiveComponent->RecreatePhysicsState();
  262.         }
  263.  
  264.         //Let the item know it has been grabbed.
  265.         myGrabbedItem->OnPlayerReceive(this);
  266.  
  267.         return true;
  268.     }
  269.  
  270.     return false;
  271. }
  272.  
  273. bool ABaseHand::TouchItem(ABaseItem* grabbedItem)
  274. {
  275.     if (grabbedItem != NULL && grabbedItem->GetIsTouchable() == true)
  276.     {
  277.         //Let the item know it has been touched.
  278.         grabbedItem->OnPlayerTouched(this);
  279.  
  280.         return true;
  281.     }
  282.  
  283.     return false;
  284. }
  285.  
  286. bool ABaseHand::GrabWeapon(ABaseItem* grabbedItem)
  287. {
  288.     UPrimitiveComponent* grabbedComponent = NULL;
  289.     FTransform itemOnGrabTransform = FTransform();
  290.     UPhysicsHandleComponent* physicsHandle = NULL;
  291.  
  292.     //Get the orientation of the item and hand to use when it is grabbed.
  293.     //Prevent grabbing broken items since this picks up all chunks.
  294.     if (grabbedItem != NULL && grabbedItem->GetIsGrabbable() == true && grabbedItem->GetIsFractured() == false)
  295.     {
  296.         //Only process the grab is it is not equipped already.
  297.         if (grabbedItem->GetIsEquipped() == false)
  298.         {
  299.             myGrabbedItem = grabbedItem;
  300.             itemOnGrabTransform = grabbedItem->GetOnGrabItemTransform();
  301.  
  302.             //Attempt to Grab Object
  303.             grabbedComponent = Cast<UPrimitiveComponent>(grabbedItem->GetGrabbableRootComponent());
  304.             if (grabbedComponent != NULL)
  305.             {
  306.                 physicsHandle = myGrabbedItem->FindComponentByClass<UPhysicsHandleComponent>();
  307.                 if (physicsHandle != NULL)
  308.                 {
  309.                     physicsHandle->GrabComponentAtLocationWithRotation(grabbedComponent,
  310.                         FName("PhysicsGrabHandle"),
  311.                         myGrabbedItem->GetActorLocation() + itemOnGrabTransform.GetLocation(),
  312.                         itemOnGrabTransform.GetRotation().Rotator());
  313.                 }
  314.  
  315.                 //If object is successfully grabbed, move object with Controller
  316.                 if (myGrabbedItem->GetPrimaryHand() != NULL)
  317.                 {
  318.                     myGrabbedPrimitiveComponent = grabbedComponent;
  319.                     myGrabbedPhysicsHandle = physicsHandle;
  320.  
  321.                     if (this == myGrabbedItem->GetPrimaryHand())
  322.                     {
  323.                         //Let the item know it has been grabbed.
  324.                         myGrabbedItem->OnPlayerReceive(this);
  325.  
  326.                         if (myGrabbedItem->GetIsAutomaticallyEquipped() == true)
  327.                         {
  328.                             ABaseHand::EquipItem();
  329.                         }
  330.                     }
  331.                     else // this is secondary hand.
  332.                     {
  333.                         //Item has already been received.
  334.                     }
  335.  
  336.                     //Item was successfully received.
  337.                     return true;
  338.                 }
  339.             }
  340.         }
  341.        
  342.     }
  343.  
  344.     return false;
  345. }
  346.  
  347. bool ABaseHand::Crush(int playerStrength)
  348. {
  349.     //TODO: FIX CRUSH ANIMATION.
  350.     if (myGrabbedItem != NULL)
  351.     {  
  352.         //Only crush the item if you have enough strength and is breakable!
  353.         if (myGrabbedItem->GetIsBreakable() == true && playerStrength >= myGrabbedItem->GetDestructibleStrength())
  354.         {
  355.             this->SetIsCurrentlyCrushing(true);
  356.             myGrabbedItem->OnPlayerCrush(this, playerStrength);
  357.  
  358.             return true;
  359.         }
  360.     }
  361.    
  362.     return false;
  363. }
  364.  
  365. void ABaseHand::UseItem()
  366. {
  367.     if (myGrabbedItem != NULL)
  368.     {
  369.         myGrabbedItem->OnPlayerUse(this);
  370.     }
  371. }
  372.  
  373. void ABaseHand::Drop()
  374. {
  375.     //Do not allow going directly from equip to drop.
  376.     if (myIsItemEquipped == false)
  377.     {
  378.         myIsCurrentlyGrabbing = false;
  379.         myIsCurrentlyCrushing = false;
  380.  
  381.         if (myGrabbedItem != NULL && this->GetControllerHand() == EControllerHand::Left && myGrabbedItem->GetRightHand() != NULL)
  382.         {
  383.             myGrabbedItem->SetLeftOwningHand(NULL);
  384.             myGrabbedItem = NULL;
  385.         }
  386.         else if (myGrabbedItem != NULL && this->GetControllerHand() == EControllerHand::Right && myGrabbedItem->GetLeftHand() != NULL)
  387.         {
  388.             myGrabbedItem->SetRightOwningHand(NULL);
  389.             myGrabbedItem = NULL;
  390.         }
  391.         else
  392.         {
  393.             if (myGrabbedItem != NULL)
  394.             {
  395.                 myGrabbedItem->OnPlayerDrop(this);
  396.                 myGrabbedItem = NULL;
  397.             }
  398.  
  399.             if (myGrabbedPrimitiveComponent != NULL)
  400.             {
  401.                 myGrabbedPrimitiveComponent->DetachFromComponent(FDetachmentTransformRules::KeepWorldTransform);
  402.                 myGrabbedPrimitiveComponent->SetSimulatePhysics(true);
  403.                 myGrabbedPrimitiveComponent = NULL;
  404.             }
  405.  
  406.             if (myGrabbedPhysicsHandle != NULL)
  407.             {
  408.                 //Player has latched on to something, release it
  409.                 myGrabbedPhysicsHandle->ReleaseComponent();
  410.                 myGrabbedPhysicsHandle = NULL;
  411.             }
  412.         }
  413.     }
  414. }
  415.  
  416. bool ABaseHand::EquipItem()
  417. {
  418.     if (myGrabbedItem != NULL && myIsItemEquipped == false)
  419.     {
  420.         //Ensure an item is an Equipable.
  421.         if (myGrabbedItem->GetIsEquipable() == true)
  422.         {
  423.             //Ensure the item is NOT already equipped.
  424.             if (myGrabbedItem->GetIsEquipped() == false)
  425.             {
  426.                 myGrabbedItem->OnPlayerEquip();
  427.                 myIsItemEquipped = true;
  428.                 return true;
  429.             }
  430.         }
  431.     }
  432.  
  433.     return false;
  434. }
  435.  
  436. bool ABaseHand::UnEquipItem()
  437. {
  438.     //Ensure an item is equipped in the current hand.
  439.     if (myGrabbedItem != NULL && myIsItemEquipped == true)
  440.     {
  441.         myGrabbedItem->OnPlayerUnEquip();
  442.         myIsItemEquipped = false;
  443.  
  444.         return true;
  445.     }
  446.  
  447.     return false;
  448. }
  449.  
  450. AActor* ABaseHand::CastCircleOfRays(double radius, FVector& lineTraceStart, FVector& lineTraceEnd, bool showDebugLines)
  451. {
  452.     AActor* actorHit = NULL;
  453.     double iInRad;
  454.     double xOffset;
  455.     double yOffset;
  456.  
  457.     //Cast the first ray since its the center point.
  458.     actorHit = ABaseHand::CastRay(lineTraceStart, lineTraceEnd, showDebugLines);
  459.  
  460.     //Check if the first cast actually got the actor.
  461.     //Dynamically create a circle of rays to cast out from the hand.
  462.     for (int i = 1; i <= 360 && actorHit == NULL; i++)
  463.     {
  464.         iInRad = Ace::DegreesToRadians(i);
  465.         xOffset = radius * Ace::Cos(iInRad);
  466.         yOffset = radius * Ace::Sin(iInRad);
  467.  
  468.         lineTraceStart = FVector(myControllerLocation.X + xOffset, myControllerLocation.Y + yOffset, myControllerLocation.Z);
  469.         lineTraceEnd = lineTraceStart + (myControllerRotation.Vector() * myReachDistance);
  470.  
  471.         //Line trace
  472.         actorHit = ABaseHand::CastRay(lineTraceStart, lineTraceEnd, showDebugLines);
  473.     }
  474.  
  475.     return actorHit;
  476. }
  477.  
  478. //Also returns the hit result from the line trace.
  479. AActor* ABaseHand::CastCircleOfRays(double radius, FVector& lineTraceStart, FVector& lineTraceEnd, FHitResult& outHitResult, bool showDebugLines)
  480. {
  481.     AActor* actorHit = NULL;
  482.     double iInRad;
  483.     double xOffset;
  484.     double yOffset;
  485.  
  486.     //Cast the first ray since its the center point.
  487.     actorHit = ABaseHand::CastRay(lineTraceStart, lineTraceEnd, showDebugLines);
  488.  
  489.     //Check if the first cast actually got the actor.
  490.     //Dynamically create a circle of rays to cast out from the hand.
  491.     for (int i = 1; i <= 360 && actorHit == NULL; i++)
  492.     {
  493.         iInRad = Ace::DegreesToRadians(i);
  494.         xOffset = radius * Ace::Cos(iInRad);
  495.         yOffset = radius * Ace::Sin(iInRad);
  496.  
  497.         lineTraceStart = FVector(myControllerLocation.X + xOffset, myControllerLocation.Y + yOffset, myControllerLocation.Z);
  498.         lineTraceEnd = lineTraceStart + (myControllerRotation.Vector() * myReachDistance);
  499.  
  500.         //Line trace
  501.         actorHit = ABaseHand::CastRay(lineTraceStart, lineTraceEnd, outHitResult, showDebugLines);
  502.     }
  503.  
  504.     return actorHit;
  505. }
  506.  
  507. /**
  508. * Ray-cast and get any object hit by the line trace.
  509. *
  510. * @param [in,out] lineTraceStart The line trace start.
  511. * @param [in,out] lineTraceEnd   The line trace end.
  512. * @param [in,out] showDebugLine  If true, shows a graphical representation of the ray cast.
  513. *
  514. * @return null if it fails, else a pointer to an AActor.
  515. */
  516. AActor* ABaseHand::CastRay(FVector& lineTraceStart, FVector& lineTraceEnd, bool showDebugLine)
  517. {
  518.     FHitResult hitResult;
  519.     AActor* actorHit = NULL;
  520.     FCollisionQueryParams traceParameters(FName(TEXT("BaseHandRayCast")), true, GetOwner());
  521.  
  522.     //Show Debug line (helpful for a visual indicator during testing)
  523.     if (showDebugLine == true)
  524.     {
  525.         DrawDebugLine(GetWorld(), lineTraceStart, lineTraceEnd, FColor(255, 0, 0), false, -1, 0, 1.0f);
  526.     }
  527.  
  528.     //Do line trace / ray-cast
  529.     return ABaseHand::LineTrace(lineTraceStart, lineTraceEnd, hitResult);
  530. }
  531.  
  532. /**
  533. * Ray-cast and get any object hit by the line trace.
  534. *
  535. * @param [in,out] lineTraceStart The line trace start.
  536. * @param [in,out] lineTraceEnd   The line trace end.
  537. * @param [out]    outHitResult   If not null, the hit result from the line trace.
  538. * @param [in,out] showDebugLine  If true, shows a graphical representation of the ray cast.
  539. *
  540. * @return null if it fails, else a pointer to an AActor.
  541. */
  542. AActor* ABaseHand::CastRay(FVector& lineTraceStart, FVector& lineTraceEnd, FHitResult& outHitResult, bool showDebugLine)
  543. {
  544.     AActor* actorHit = NULL;
  545.     FCollisionQueryParams traceParameters(FName(TEXT("BaseHandRayCast")), false, GetOwner());
  546.  
  547.     //Show Debug line (helpful for a visual indicator during testing)
  548.     if (showDebugLine == true)
  549.     {
  550.         DrawDebugLine(GetWorld(), lineTraceStart, lineTraceEnd, FColor(255, 0, 0), false, -1, 0, 1.0f);
  551.     }
  552.  
  553.     //Do line trace / ray-cast
  554.     return ABaseHand::LineTrace(lineTraceStart, lineTraceEnd, outHitResult);
  555. }
  556.  
  557. AActor* ABaseHand::LineTrace(FVector& lineTraceStart, FVector& lineTraceEnd, FHitResult& outHitResult)
  558. {
  559.     FHitResult hit;
  560.     AActor* actorHit = NULL;
  561.     int numCollisionChannels = 33;
  562.     FCollisionQueryParams traceParameters(FName(TEXT("BaseHandRayCast")), false, GetOwner());
  563.  
  564.     //Check all possible channels for collision.
  565.     for (int i = 0; i < numCollisionChannels; i++)
  566.     {
  567.         ECollisionChannel currChannel = myCollisionChannels[i];
  568.  
  569.         if (&outHitResult == NULL)
  570.         {
  571.             ABaseHand::GetWorld()->LineTraceSingleByObjectType(hit, lineTraceStart, lineTraceEnd, currChannel, traceParameters);
  572.             actorHit = hit.GetActor();
  573.         }
  574.         else
  575.         {
  576.             ABaseHand::GetWorld()->LineTraceSingleByObjectType(outHitResult, lineTraceStart, lineTraceEnd, currChannel, traceParameters);
  577.             actorHit = outHitResult.GetActor();
  578.         }
  579.  
  580.         if (actorHit != NULL)
  581.         {
  582.             return actorHit;
  583.         }
  584.     }
  585.  
  586.     return NULL;   
  587. }
  588.  
  589. void ABaseHand::VibrateController(EVibrationType vibrationType)
  590. {
  591.     APlayerController* playerController = NULL;
  592.  
  593.     if (myOwningCharacter != NULL)
  594.     {
  595.         //Handle Vibration when picking up something.
  596.         playerController = Cast<APlayerController>(myOwningCharacter->Controller);
  597.         if (playerController != NULL)
  598.         {
  599.             UForceFeedbackEffect** foundVibration = myVibrations.Find(vibrationType);
  600.             if (foundVibration != NULL && *foundVibration != NULL)
  601.             {
  602.                 playerController->ClientPlayForceFeedback(*foundVibration, false, false, (*foundVibration)->GetFName());
  603.             }
  604.         }
  605.     }
  606. }
  607.  
  608. void ABaseHand::PlayDynamicForceFeedback(float intensity, float duration)
  609. {
  610.     APlayerController* handController = Cast<APlayerController>(this->GetOwningCharacter()->Controller);
  611.     FLatentActionInfo latentInfo;
  612.    
  613.     latentInfo.CallbackTarget = handController;
  614.  
  615.     if (myControllerHand == EControllerHand::Left)
  616.     {
  617.         //Play vibration on the left hand.
  618.         handController->PlayDynamicForceFeedback(intensity, duration, true, true, false, false);
  619.     }
  620.     else
  621.     {
  622.         //Play vibration on the right hand.
  623.         handController->PlayDynamicForceFeedback(intensity, duration, false, false, true, true);
  624.     }
  625. }
  626.  
  627. ///Getters
  628. USkeletalMeshComponent* ABaseHand::GetFirstPersonHandMesh()
  629. {
  630.     return myVRHandMesh;//myFirstPersonHandMesh;
  631. }
  632.  
  633. USkeletalMeshComponent* ABaseHand::GetVRHandMesh()
  634. {
  635.     return myVRHandMesh;
  636. }
  637.  
  638. UMotionControllerComponent* ABaseHand::GetMotionController()
  639. {
  640.     return myMotionController;
  641. }
  642.  
  643. EControllerHand ABaseHand::GetControllerHand()
  644. {
  645.     return myControllerHand;
  646. }
  647.  
  648. bool ABaseHand::GetIsItemEquipped()
  649. {
  650.     return myIsItemEquipped;
  651. }
  652.  
  653. bool ABaseHand::GetIsCurrentlyGrabbing()
  654. {
  655.     return myIsCurrentlyGrabbing;
  656. }
  657.  
  658. bool ABaseHand::GetIsCurrentlyCrushing()
  659. {
  660.     return myIsCurrentlyCrushing;
  661. }
  662.  
  663. bool ABaseHand::GetIsTriggerPressed()
  664. {
  665.     return myIsTriggerPressed;
  666. }
  667.  
  668. float ABaseHand::GetDistanceFromController()
  669. {
  670.     return myDistanceFromController;
  671. }
  672.  
  673. float ABaseHand::GetReachDistance()
  674. {
  675.     return myReachDistance;
  676. }
  677.  
  678. float ABaseHand::GetMinDistanceFromController()
  679. {
  680.     return myMinDistanceFromController;
  681. }
  682.  
  683. float ABaseHand::GetMaxDistanceFromController()
  684. {
  685.     return myMaxDistanceFromController;
  686. }
  687.  
  688. bool ABaseHand::GetIsUsingMotionControllers()
  689. {
  690.     return myIsUsingMotionControllers;
  691. }
  692.  
  693. FVector ABaseHand::GetControllerLocation()
  694. {
  695.     return myControllerLocation;
  696. }
  697.  
  698. FRotator ABaseHand::GetControllerRotation()
  699. {
  700.     return myControllerRotation;
  701. }
  702.  
  703. UPrimitiveComponent* ABaseHand::GetGrabbedPrimitiveComponent()
  704. {
  705.     return myGrabbedPrimitiveComponent;
  706. }
  707.  
  708. FTransform ABaseHand::GetGrabbedPhysicsHandleTransform()
  709. {
  710.     return myGrabbedPhysicsHandleTransform;
  711. }
  712.  
  713. FTransform ABaseHand::GetControllerTransform()
  714. {
  715.     return myControllerTransform;
  716. }
  717.  
  718. FVector ABaseHand::GetControllerRelativeLocation()
  719. {
  720.     return myMotionController->RelativeLocation;
  721. }
  722.  
  723. FRotator ABaseHand::GetControllerRelativeRotation()
  724. {
  725.     return myMotionController->RelativeRotation;
  726. }
  727.  
  728. FVector ABaseHand::GetNewGrabbedLocation()
  729. {
  730.     return myNewGrabbedLocation;
  731. }
  732.  
  733. FVector ABaseHand::GetLastHitDiffrence()
  734. {
  735.     return myLastHitDistance;
  736. }
  737.  
  738. FVector ABaseHand::GetDiffGrabbedLocation()
  739. {
  740.     return myDiffGrabbedLocation;
  741. }
  742.  
  743. FQuat ABaseHand::GetDiffGrabbedRotation()
  744. {
  745.     return myDiffGrabbedRotation;
  746. }
  747.  
  748. ABaseItem* ABaseHand::GetGrabbedItem()
  749. {
  750.     return myGrabbedItem;
  751. }
  752.  
  753. UPhysicsHandleComponent* ABaseHand::GetGrabbedPhysicsHandle()
  754. {
  755.     return myGrabbedPhysicsHandle;
  756. }
  757.  
  758. ABaseCharacter* ABaseHand::GetOwningCharacter()
  759. {
  760.     return myOwningCharacter;
  761. }
  762.  
  763. float ABaseHand::GetOwnerStrength()
  764. {
  765.     if (myOwningCharacter != NULL)
  766.     {
  767.         return myOwningCharacter->GetStrength();
  768.     }
  769.  
  770.     return -1.0f;
  771. }
  772.  
  773. ///Setters
  774. void ABaseHand::SetMinDistanceFromController(float minDistance)
  775. {
  776.     myMinDistanceFromController = minDistance;
  777. }
  778.  
  779. void ABaseHand::SetMaxDistanceFromController(float maxDistance)
  780. {
  781.     myMaxDistanceFromController = maxDistance;
  782. }
  783.  
  784. void ABaseHand::SetIsUsingMotionControllers(bool isUsingMotionControllers)
  785. {
  786.     myIsUsingMotionControllers = isUsingMotionControllers;
  787. }
  788.  
  789. void ABaseHand::SetControllerLocation(FVector controllerLocation)
  790. {
  791.     myControllerLocation = controllerLocation;
  792. }
  793.  
  794. void ABaseHand::SetControllerRotation(FRotator controllerRotation)
  795. {
  796.     myControllerRotation = controllerRotation;
  797. }
  798.  
  799. void ABaseHand::SetDistanceFromController(float newDistance)
  800. {
  801.     if (newDistance >= myMinDistanceFromController && newDistance <= myMaxDistanceFromController)
  802.     {
  803.         myDistanceFromController = newDistance;
  804.     }
  805. }
  806.  
  807. void ABaseHand::SetGrabbedItem(ABaseItem* grabbedItem)
  808. {
  809.     myGrabbedItem = grabbedItem;
  810. }
  811.  
  812. void ABaseHand::SetGrabbedPrimitiveComponent(UPrimitiveComponent* grabbedPrimitiveComponent)
  813. {
  814.     myGrabbedPrimitiveComponent = grabbedPrimitiveComponent;
  815. }
  816.  
  817. void ABaseHand::SetIsItemEquipped(bool isItemEquipped)
  818. {
  819.     myIsItemEquipped = isItemEquipped;
  820. }
  821.  
  822. void ABaseHand::SetIsCurrentlyGrabbing(bool isCurrentlyGrabbing)
  823. {
  824.     myIsCurrentlyGrabbing = isCurrentlyGrabbing;
  825. }
  826.  
  827. void ABaseHand::SetIsTriggerPressed(bool isTriggerPressed)
  828. {
  829.     myIsTriggerPressed = isTriggerPressed;
  830. }
  831.  
  832. void ABaseHand::SetGrabbedObjectTransform(FTransform grabbedObjectTransform)
  833. {
  834.     myGrabbedObjectTransform = grabbedObjectTransform;
  835. }
  836.  
  837. void ABaseHand::SetControllerTransform(FTransform controllerTransform)
  838. {
  839.     myControllerTransform = controllerTransform;
  840. }
  841.  
  842. void ABaseHand::SetIsCurrentlyCrushing(bool isCurrentlyCrushing)
  843. {
  844.     myIsCurrentlyCrushing = isCurrentlyCrushing;
  845. }
  846.  
  847. void ABaseHand::SetOwningCharacter(ABaseCharacter* owningCharacter)
  848. {
  849.     myOwningCharacter = owningCharacter;
  850. }
  851.  
  852. void ABaseHand::SetVRHandMesh(USkeletalMeshComponent* handMesh)
  853. {
  854.     myVRHandMesh = handMesh;
  855. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement