Advertisement
Guest User

Swing Mech Character

a guest
Jan 25th, 2020
534
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 17.41 KB | None | 0 0
  1. // Copyright 1998-2018 Epic Games, Inc. All Rights Reserved.
  2.  
  3. #include "RopeHabschCharacter.h"
  4. #include "Camera/CameraComponent.h"
  5. #include "Components/CapsuleComponent.h"
  6. #include "Components/InputComponent.h"
  7. #include "GameFramework/CharacterMovementComponent.h"
  8. #include <Runtime/Engine/Classes/Engine/Engine.h>
  9. #include "RopeHabschSwingComponent.h"
  10. #include "GameFramework/Controller.h"
  11. #include "Components/SkeletalMeshComponent.h"
  12. #include "DrawDebugHelpers.h"
  13. #include <Runtime/Engine/Classes/Engine/Engine.h>
  14. #include "GameFramework/SpringArmComponent.h"
  15.  
  16.  
  17.  
  18. // ARopeHabschCharacter
  19. ARopeHabschCharacter::ARopeHabschCharacter()
  20. {
  21.     // Set size for collision capsule
  22.     GetCapsuleComponent()->InitCapsuleSize(42.f, 96.0f);
  23.  
  24.     // set our turn rates for input
  25.     BaseTurnRate = 45.f;
  26.     BaseLookUpRate = 45.f;
  27.  
  28.     // Don't rotate when the controller rotates. Let that just affect the camera.
  29.     bUseControllerRotationPitch = false;
  30.     bUseControllerRotationYaw = false;
  31.     bUseControllerRotationRoll = false;
  32.  
  33.     // Configure character movement
  34.     GetCharacterMovement()->bOrientRotationToMovement = true; // Character moves in the direction of input...  
  35.     GetCharacterMovement()->RotationRate = FRotator(0.0f, 540.0f, 0.0f); // ...at this rotation rate
  36.     GetCharacterMovement()->JumpZVelocity = 600.f;
  37.     GetCharacterMovement()->AirControl = 0.2f;
  38.  
  39.     // Create a camera boom (pulls in towards the player if there is a collision)
  40.     CameraBoom = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraBoom"));
  41.     CameraBoom->SetupAttachment(RootComponent);
  42.     CameraBoom->TargetArmLength = 300.0f; // The camera follows at this distance behind the character  
  43.     CameraBoom->bUsePawnControlRotation = true; // Rotate the arm based on the controller
  44.  
  45.     // Create a follow camera
  46.     FollowCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("FollowCamera"));
  47.     FollowCamera->SetupAttachment(CameraBoom, USpringArmComponent::SocketName); // Attach the camera to the end of the boom and let the boom adjust to match the controller orientation
  48.     FollowCamera->bUsePawnControlRotation = false; // Camera does not rotate relative to arm
  49.  
  50.     cableComp = CreateDefaultSubobject<UCPP_CableComponent>(TEXT("CableComp"));
  51.     cableComp->SetupAttachment(GetMesh());
  52.  
  53.     // Note: The skeletal mesh and anim blueprint references on the Mesh component (inherited from Character)
  54.     // are set in the derived blueprint asset named MyCharacter (to avoid direct content references in C++)
  55. }
  56.  
  57. //---------------------------------------------------------
  58.  
  59. void ARopeHabschCharacter::Tick(float DeltaTime) {
  60.  
  61.     Super::Tick(DeltaTime);
  62.  
  63.  
  64.     switch (ePlayerState) {
  65.  
  66.     case Enum_PlayerState::IsNormal:
  67.  
  68.         TraceForInteractables();
  69.  
  70.         break;
  71.  
  72.     case Enum_PlayerState::IsSwinging:
  73.  
  74.         break;
  75.  
  76.     case Enum_PlayerState::IsReeling:
  77.  
  78.         break;
  79.  
  80.     case Enum_PlayerState::IsHookingAReel:
  81.  
  82.         break;
  83.  
  84.     case Enum_PlayerState::IsHookingASwing:
  85.  
  86.         break;
  87.  
  88.     default:
  89.         break;
  90.     }
  91. }
  92.  
  93. //---------------------------------------------------------
  94.  
  95. // Input
  96. void ARopeHabschCharacter::SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) {
  97.  
  98.     // Set up gameplay key bindings
  99.     check(PlayerInputComponent);
  100.  
  101.     PlayerInputComponent->BindAction("Jump", IE_Pressed, this, &ACharacter::Jump);
  102.     PlayerInputComponent->BindAction("Jump", IE_Released, this, &ACharacter::StopJumping);
  103.  
  104.     PlayerInputComponent->BindAxis("MoveForward", this, &ARopeHabschCharacter::MoveForward);
  105.     PlayerInputComponent->BindAxis("MoveRight", this, &ARopeHabschCharacter::MoveRight);
  106.  
  107.     // We have 2 versions of the rotation bindings to handle different kinds of devices differently
  108.     // "turn" handles devices that provide an absolute delta, such as a mouse.
  109.     // "turnrate" is for devices that we choose to treat as a rate of change, such as an analog joystick
  110.     PlayerInputComponent->BindAxis("Turn", this, &APawn::AddControllerYawInput);
  111.     PlayerInputComponent->BindAxis("LookUp", this, &APawn::AddControllerPitchInput);
  112. }
  113.  
  114. //---------------------------------------------------------
  115.  
  116. // Character Landed
  117. void ARopeHabschCharacter::Landed(const FHitResult& Hit) {
  118.  
  119.     //GEngine->AddOnScreenDebugMessage(-1, 10.0f, FColor::Cyan, FString::Printf(TEXT("Char landed")));
  120.  
  121.     switch (ePlayerState) {
  122.  
  123.     case Enum_PlayerState::IsNormal:
  124.  
  125.         // attachPointHookingTo = nullptr; // Had to be reset here as well if player landed between HookingStart Anim and until it was finished
  126.  
  127.         break;
  128.  
  129.     case Enum_PlayerState::IsHookingASwing:
  130.  
  131.         CancelSwing(false);
  132.  
  133.         break;
  134.    
  135.         // If in IsSwinging when registering OnLandedd then Stops it and resets state
  136.     case Enum_PlayerState::IsSwinging:
  137.  
  138.         CancelSwing(false);
  139.  
  140.         break;
  141.  
  142.     case Enum_PlayerState::IsReeling:
  143.  
  144.         // CancelReelIn();
  145.  
  146.         break;
  147.  
  148.     case Enum_PlayerState::IsHookingAReel:
  149.  
  150.         break;
  151.    
  152.     default:
  153.         break;
  154.     }
  155.  
  156.     // If Landed Having Swung then Stops it
  157.     if (swingComponent->wasSwinging) {
  158.  
  159.         CancelSwing(false);
  160.     }
  161. }
  162.  
  163. //---------------------------------------------------------
  164.  
  165. // Get Variables From BP and Set them
  166. void ARopeHabschCharacter::SetVariablesFromBP(URopeHabschSwingComponent* swingComp) {
  167.  
  168.     swingComponent = swingComp;
  169. }
  170.  
  171. //---------------------------------------------------------
  172.  
  173. // Execute Attach Point Interaction after the player has rotated and cable is connected (Called from BP)
  174. void ARopeHabschCharacter::ExecuteAttachPointInteraction() {
  175.  
  176.     // Depending on which type of Attach Point it Calls StartReeling or Start Swinging
  177.     if (attachPointHookingTo->interactableType == Enum_InteractableTypes::ReelPoint) {
  178.  
  179.         // Starts Swinging with given attach point as ref
  180.         swingComponent->StartReeling(attachPointHookingTo);
  181.  
  182.         ePlayerState = Enum_PlayerState::IsReeling;
  183.  
  184.         // attachPointHookingTo = nullptr; // Only meant to be saved during the Hook Start Animation so gets reset when actual swing starts
  185.     }
  186.  
  187.     else if (attachPointHookingTo->interactableType == Enum_InteractableTypes::SwingPoint) {
  188.  
  189.         if (GetCharacterMovement()->IsFalling() && attachPointHookingTo) {
  190.  
  191.             ePlayerState = Enum_PlayerState::IsSwinging;
  192.             swingComponent->StartSwinging(attachPointHookingTo); // SÅ INGET KMR SKE NU MED SWING COMP ÄN.
  193.         }
  194.     }
  195. }
  196.  
  197. //---------------------------------------------------------
  198.  
  199. // Trace for Interactables implementing Interact Interface
  200. void ARopeHabschCharacter::TraceForInteractables() {
  201.  
  202.     FHitResult interactTraceHitResult;
  203.     // FVector traceStartLocation = GetFollowCamera()->GetComponentLocation();
  204.     FVector traceStartLocation = traceForInteractablesStartLocation;
  205.  
  206.     FVector traceEndLocation = traceForInteractablesStartLocation + (traceForInteractablesDirection * reelPointInteractRange);
  207.  
  208.  
  209.     GetWorld()->DebugDrawTraceTag = "";
  210.  
  211.     // Query Params
  212.     FCollisionQueryParams TraceParams(
  213.         "", // Trace Tag
  214.         true, // Trace Complex
  215.         GetOwner() // Actor att ignora.
  216.     );
  217.  
  218.     // Line Trace
  219.     GetWorld()->LineTraceSingleByChannel(
  220.         OUT interactTraceHitResult, // Var den ska lagra HitResult
  221.         traceStartLocation, // Start punkten
  222.         traceEndLocation, // Slutpunkten
  223.         interactCollisionChannel, // Kanalen som den ska traca
  224.         TraceParams // Collision Query Params som ska användas
  225.     );
  226.    
  227.     // If Got Hit
  228.     if (interactTraceHitResult.bBlockingHit) {
  229.  
  230.         // Checks if already has a ref
  231.         if (interactAbleActor) {
  232.  
  233.             // If Hit was the same that we're already looking at
  234.             if (interactTraceHitResult.GetActor() == interactAbleActor) {
  235.  
  236.                
  237.                 ICPP_InteractInterface* interactInterface = Cast<ICPP_InteractInterface>(interactTraceHitResult.GetActor());
  238.  
  239.                 // Then checks if still within acceptable distance, otherwise nulls it
  240.                 switch (interactInterface->GetInteractableType_Implementation()) {
  241.  
  242.                 case Enum_InteractableTypes::ReelPoint:
  243.  
  244.                     // If Distance to already highlighted interactable is to great or to small then nulls it
  245.                     if (GetRangeBetweenActors(this, interactAbleActor) > reelPointInteractRange || GetRangeBetweenActors(this, interactAbleActor) < reelPointMinDistanceToInteract) {
  246.  
  247.                         interactInterface->PlayerLookingAtInteractable_Implementation(false);
  248.                         interactAbleActor = nullptr;
  249.                     }
  250.  
  251.                     break;
  252.  
  253.                 case Enum_InteractableTypes::SwingPoint:
  254.  
  255.                     if (GetRangeBetweenActors(this, interactAbleActor) > swingPointInteractRange || GetRangeBetweenActors(this, interactAbleActor) < swingPointMinDistanceToInteract) {
  256.  
  257.  
  258.                         interactInterface->PlayerLookingAtInteractable_Implementation(false);
  259.                         interactAbleActor = nullptr;
  260.                     }
  261.  
  262.                     break;
  263.  
  264.                 case Enum_InteractableTypes::Button:
  265.  
  266.                     break;
  267.  
  268.                 default:
  269.                     break;
  270.                 }
  271.  
  272.                 // return;
  273.             }
  274.  
  275.             // If has a interactAbleActor Ref but the new hit is not the same actor then nulls it before it gets exchanged for the new one below if the new one imlements the interface
  276.             else {
  277.  
  278.                 ICPP_InteractInterface* interactInterface = Cast<ICPP_InteractInterface>(interactAbleActor);
  279.  
  280.                 interactInterface->PlayerLookingAtInteractable_Implementation(false);
  281.                 interactAbleActor = nullptr;
  282.             }
  283.         }
  284.  
  285.         // If not the execution will continue and check if the hit actor implements interface, if so then updates ref and calls interface
  286.         if (!interactAbleActor && interactTraceHitResult.GetActor()->GetClass()->ImplementsInterface(UCPP_InteractInterface::StaticClass())) {
  287.  
  288.             // Checks if it implements interactInterface
  289.             ICPP_InteractInterface* interactInterface = Cast<ICPP_InteractInterface>(interactTraceHitResult.GetActor());
  290.  
  291.             if (interactInterface) {
  292.  
  293.                 // Depending on the type of interactable, checks the interactable range
  294.                 switch (interactInterface->GetInteractableType_Implementation()) {
  295.                    
  296.                 case Enum_InteractableTypes::ReelPoint:
  297.  
  298.                     // If within range that the character has to that type of interactable
  299.                     if (GetRangeBetweenActors(this, interactTraceHitResult.GetActor()) <= reelPointInteractRange && GetRangeBetweenActors(this, interactTraceHitResult.GetActor()) >= reelPointMinDistanceToInteract) {
  300.  
  301.                         interactAbleActor = interactTraceHitResult.GetActor(); // If within Correct Range then sets the interactableRef
  302.                         interactInterface->PlayerLookingAtInteractable_Implementation(true); // Calls Looking At interface at the interactable
  303.                     }
  304.                    
  305.                     break;
  306.                    
  307.                 case Enum_InteractableTypes::SwingPoint:
  308.  
  309.                     if (GetRangeBetweenActors(this, interactTraceHitResult.GetActor()) <= swingPointInteractRange && GetRangeBetweenActors(this, interactTraceHitResult.GetActor()) >= swingPointMinDistanceToInteract) {
  310.  
  311.                         interactAbleActor = interactTraceHitResult.GetActor();
  312.                         interactInterface->PlayerLookingAtInteractable_Implementation(true);
  313.                     }
  314.  
  315.                     break;
  316.  
  317.                    
  318.                 case Enum_InteractableTypes::Button:
  319.                    
  320.                     /*
  321.                     if (GetRangeBetweenActors(this, interactTraceHitResult.GetActor()) <= INSERT BUTTON INTERACT RANGE) {
  322.  
  323.                         interactInterface->PlayerLookingAtInteractable_Implementation(true);
  324.                     }
  325.                     */
  326.                    
  327.                     break;
  328.                    
  329.                 default:
  330.                     break;
  331.                 }
  332.             }
  333.         }
  334.     }
  335.  
  336.     // If no Hit
  337.     else if (!interactTraceHitResult.bBlockingHit) {
  338.  
  339.         // Resets the interactable if the player was previously looking at one and now is not looking at anything
  340.         if (interactAbleActor) {
  341.            
  342.             ICPP_InteractInterface* interactInterface = Cast<ICPP_InteractInterface>(interactAbleActor);
  343.  
  344.             if (interactInterface) {
  345.                 interactInterface->PlayerLookingAtInteractable_Implementation(false);
  346.             }
  347.            
  348.             interactAbleActor = nullptr;
  349.         }
  350.  
  351.         // DrawDebugLine(GetWorld(), traceStartLocation, traceEndLocation, FColor::Red, true, 10, 0, 1);
  352.     }
  353. }
  354.  
  355. //---------------------------------------------------------
  356.  
  357. // Player Interaction with actors implementing Interact Interface
  358. void ARopeHabschCharacter::PlayerInteract(bool pressed) {
  359.  
  360.     // GEngine->AddOnScreenDebugMessage(-1, 10.0f, FColor::Cyan, FString::Printf(TEXT("Player Interact")));
  361.  
  362.     // If Player Pressed the Interact Key
  363.     if (pressed) {
  364.  
  365.         switch (ePlayerState) {
  366.  
  367.         case Enum_PlayerState::IsNormal:
  368.  
  369.             if (interactAbleActor) {
  370.  
  371.                 ICPP_InteractInterface* interactInterface = Cast<ICPP_InteractInterface>(interactAbleActor);
  372.  
  373.                 // If allowed to Interact with it, for example if not another player is swinging on it
  374.                 if (interactInterface->Interact_Implementation()) {
  375.  
  376.                     // Depending on what it is the char can interact with it in different ways
  377.                     switch (interactInterface->GetInteractableType_Implementation()) {
  378.  
  379.                         // If Interacting with Reel Point
  380.                     case Enum_InteractableTypes::ReelPoint:
  381.  
  382.                         // Gets a AttachPoint Ref
  383.                         attachPointHookingTo = Cast<ARopeHabschAttachPoint>(interactAbleActor);
  384.  
  385.                         if (attachPointHookingTo) {
  386.  
  387.                             ePlayerState = Enum_PlayerState::IsHookingAReel;
  388.  
  389.                             RotateAndHookToAttachPoint();
  390.                         }
  391.  
  392.                         break;
  393.  
  394.                         // If Interacting with Swing Point
  395.                     case Enum_InteractableTypes::SwingPoint:
  396.  
  397.                         if (GetCharacterMovement()->IsFalling()) {
  398.  
  399.                             // Gets a AttachPoint Ref
  400.                             attachPointHookingTo = Cast<ARopeHabschAttachPoint>(interactAbleActor);
  401.  
  402.                             if (attachPointHookingTo) {
  403.  
  404.                                 ePlayerState = Enum_PlayerState::IsHookingASwing;
  405.  
  406.                                 RotateAndHookToAttachPoint();
  407.                             }
  408.                         }
  409.  
  410.                         break;
  411.  
  412.                     case Enum_InteractableTypes::Button:
  413.  
  414.                         break;
  415.  
  416.                     default:
  417.                         break;
  418.                     }
  419.                 }
  420.             }
  421.            
  422.             break;
  423.  
  424.         case Enum_PlayerState::IsHookingASwing:
  425.  
  426.             break;
  427.  
  428.         case Enum_PlayerState::IsSwinging:
  429.             break;
  430.  
  431.         case Enum_PlayerState::IsReeling:
  432.             break;
  433.  
  434.  
  435.         default:
  436.             break;
  437.         }
  438.     }
  439.  
  440.     // If Player Released the Interact Key
  441.     else {
  442.  
  443.         switch (ePlayerState) {
  444.  
  445.         case Enum_PlayerState::IsNormal:
  446.  
  447.             break;
  448.  
  449.         case Enum_PlayerState::IsHookingASwing:
  450.  
  451.             CancelSwing(false);
  452.  
  453.             break;
  454.  
  455.         case Enum_PlayerState::IsSwinging:
  456.  
  457.             CancelSwing(true);
  458.  
  459.             break;
  460.  
  461.         case Enum_PlayerState::IsHookingAReel:
  462.  
  463.             swingComponent->StopReeling();
  464.             ePlayerState = Enum_PlayerState::IsNormal;
  465.             attachPointHookingTo = nullptr;
  466.  
  467.             break;
  468.  
  469.         case Enum_PlayerState::IsReeling:
  470.  
  471.             CancelReelIn();
  472.  
  473.  
  474.             break;
  475.  
  476.         default:
  477.             break;
  478.         }
  479.     }
  480. }
  481.  
  482. //---------------------------------------------------------
  483.  
  484. void ARopeHabschCharacter::MoveForward(float Value) {
  485.  
  486.     switch (ePlayerState)
  487.     {
  488.     case Enum_PlayerState::IsNormal:
  489.  
  490.         if ((Controller != NULL) && (Value != 0.0f)) {
  491.             // find out which way is forward
  492.             const FRotator Rotation = Controller->GetControlRotation();
  493.             const FRotator YawRotation(0, Rotation.Yaw, 0);
  494.  
  495.             // get forward vector
  496.             const FVector Direction = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X);
  497.             AddMovementInput(Direction, Value);
  498.         }
  499.  
  500.         break;
  501.  
  502.     case Enum_PlayerState::IsHookingASwing:
  503.  
  504.         break;
  505.  
  506.     case Enum_PlayerState::IsSwinging:
  507.  
  508.         // TODO - Adda input för ge kraft till swingen
  509.  
  510.         if ((Controller != NULL) && (Value != 0.0f)) {
  511.  
  512.             // find out which way is forward
  513.             const FRotator Rotation = Controller->GetControlRotation();
  514.             const FRotator YawRotation(0, Rotation.Yaw, 0);
  515.  
  516.             // get forward vector
  517.             const FVector Direction = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X);
  518.             AddMovementInput(Direction, Value);
  519.            
  520.         }
  521.  
  522.         swingComponent->playerSwingInput = Value;
  523.  
  524.         break;
  525.  
  526.     case Enum_PlayerState::IsHookingAReel:
  527.  
  528.         break;
  529.  
  530.     case Enum_PlayerState::IsReeling:
  531.  
  532.  
  533.         break;
  534.  
  535.     default:
  536.         break;
  537.     }
  538. }
  539.  
  540. //---------------------------------------------------------
  541.  
  542. void ARopeHabschCharacter::MoveRight(float Value) {
  543.  
  544.  
  545.     switch (ePlayerState) {
  546.  
  547.     case Enum_PlayerState::IsNormal:
  548.  
  549.         if ((Controller != NULL) && (Value != 0.0f)) {
  550.             // find out which way is right
  551.             const FRotator Rotation = Controller->GetControlRotation();
  552.             const FRotator YawRotation(0, Rotation.Yaw, 0);
  553.  
  554.             // get right vector
  555.             const FVector Direction = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::Y);
  556.             // add movement in that direction
  557.             AddMovementInput(Direction, Value);
  558.         }
  559.  
  560.         break;
  561.  
  562.     case Enum_PlayerState::IsSwinging:
  563.  
  564.         if ((Controller != NULL) && (Value != 0.0f)) {
  565.  
  566.             // find out which way is right
  567.             const FRotator Rotation = Controller->GetControlRotation();
  568.             const FRotator YawRotation(0, Rotation.Yaw, 0);
  569.  
  570.             // get right vector
  571.             const FVector Direction = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::Y);
  572.             // add movement in that direction
  573.             AddMovementInput(Direction, Value);
  574.            
  575.         }
  576.        
  577.         // TODO - Add Slight Swing Steering
  578.        
  579.         break;
  580.  
  581.     case Enum_PlayerState::IsHookingAReel:
  582.  
  583.         break;
  584.  
  585.     case Enum_PlayerState::IsReeling:
  586.  
  587.         break;
  588.  
  589.     default:
  590.         break;
  591.     }
  592. }
  593.  
  594. //---------------------------------------------------------
  595.  
  596. float ARopeHabschCharacter::GetRangeBetweenActors(AActor* actorFrom, AActor* actorTo) {
  597.  
  598.     return ((actorFrom->GetActorLocation() - actorTo->GetActorLocation()).Size());
  599. }
  600.  
  601. //---------------------------------------------------------
  602.  
  603. FVector ARopeHabschCharacter::GetGrabHandSocketLocation() {
  604.  
  605.     if (GetMesh()->DoesSocketExist(grabHandSocketName)) {
  606.  
  607.         return GetMesh()->GetSocketLocation(grabHandSocketName);
  608.     }
  609.  
  610.     else {
  611.  
  612.         return FVector(0, 0, 0);
  613.     }
  614. }
  615.  
  616. //---------------------------------------------------------
  617.  
  618. void ARopeHabschCharacter::CancelReelIn() {
  619.  
  620.     CancelSwingOrReel();
  621.     swingComponent->StopReeling();
  622.     ePlayerState = Enum_PlayerState::IsNormal;
  623.     attachPointHookingTo = nullptr;
  624. }
  625.  
  626. //---------------------------------------------------------
  627.  
  628. void ARopeHabschCharacter::CancelSwing(bool inheritSwingVelocity) {
  629.  
  630.     CancelSwingOrReel();
  631.     swingComponent->StopSwinging(inheritSwingVelocity);
  632.     ePlayerState = Enum_PlayerState::IsNormal;
  633.     attachPointHookingTo = nullptr;
  634. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement