Advertisement
Guest User

BaseHand.cpp

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