Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Copyright 1998-2018 Epic Games, Inc. All Rights Reserved.
- #include "RopeHabschCharacter.h"
- #include "Camera/CameraComponent.h"
- #include "Components/CapsuleComponent.h"
- #include "Components/InputComponent.h"
- #include "GameFramework/CharacterMovementComponent.h"
- #include <Runtime/Engine/Classes/Engine/Engine.h>
- #include "RopeHabschSwingComponent.h"
- #include "GameFramework/Controller.h"
- #include "Components/SkeletalMeshComponent.h"
- #include "DrawDebugHelpers.h"
- #include <Runtime/Engine/Classes/Engine/Engine.h>
- #include "GameFramework/SpringArmComponent.h"
- // ARopeHabschCharacter
- ARopeHabschCharacter::ARopeHabschCharacter()
- {
- // Set size for collision capsule
- GetCapsuleComponent()->InitCapsuleSize(42.f, 96.0f);
- // set our turn rates for input
- BaseTurnRate = 45.f;
- BaseLookUpRate = 45.f;
- // Don't rotate when the controller rotates. Let that just affect the camera.
- bUseControllerRotationPitch = false;
- bUseControllerRotationYaw = false;
- bUseControllerRotationRoll = false;
- // Configure character movement
- GetCharacterMovement()->bOrientRotationToMovement = true; // Character moves in the direction of input...
- GetCharacterMovement()->RotationRate = FRotator(0.0f, 540.0f, 0.0f); // ...at this rotation rate
- GetCharacterMovement()->JumpZVelocity = 600.f;
- GetCharacterMovement()->AirControl = 0.2f;
- // Create a camera boom (pulls in towards the player if there is a collision)
- CameraBoom = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraBoom"));
- CameraBoom->SetupAttachment(RootComponent);
- CameraBoom->TargetArmLength = 300.0f; // The camera follows at this distance behind the character
- CameraBoom->bUsePawnControlRotation = true; // Rotate the arm based on the controller
- // Create a follow camera
- FollowCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("FollowCamera"));
- FollowCamera->SetupAttachment(CameraBoom, USpringArmComponent::SocketName); // Attach the camera to the end of the boom and let the boom adjust to match the controller orientation
- FollowCamera->bUsePawnControlRotation = false; // Camera does not rotate relative to arm
- cableComp = CreateDefaultSubobject<UCPP_CableComponent>(TEXT("CableComp"));
- cableComp->SetupAttachment(GetMesh());
- // Note: The skeletal mesh and anim blueprint references on the Mesh component (inherited from Character)
- // are set in the derived blueprint asset named MyCharacter (to avoid direct content references in C++)
- }
- //---------------------------------------------------------
- void ARopeHabschCharacter::Tick(float DeltaTime) {
- Super::Tick(DeltaTime);
- switch (ePlayerState) {
- case Enum_PlayerState::IsNormal:
- TraceForInteractables();
- break;
- case Enum_PlayerState::IsSwinging:
- break;
- case Enum_PlayerState::IsReeling:
- break;
- case Enum_PlayerState::IsHookingAReel:
- break;
- case Enum_PlayerState::IsHookingASwing:
- break;
- default:
- break;
- }
- }
- //---------------------------------------------------------
- // Input
- void ARopeHabschCharacter::SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) {
- // Set up gameplay key bindings
- check(PlayerInputComponent);
- PlayerInputComponent->BindAction("Jump", IE_Pressed, this, &ACharacter::Jump);
- PlayerInputComponent->BindAction("Jump", IE_Released, this, &ACharacter::StopJumping);
- PlayerInputComponent->BindAxis("MoveForward", this, &ARopeHabschCharacter::MoveForward);
- PlayerInputComponent->BindAxis("MoveRight", this, &ARopeHabschCharacter::MoveRight);
- // We have 2 versions of the rotation bindings to handle different kinds of devices differently
- // "turn" handles devices that provide an absolute delta, such as a mouse.
- // "turnrate" is for devices that we choose to treat as a rate of change, such as an analog joystick
- PlayerInputComponent->BindAxis("Turn", this, &APawn::AddControllerYawInput);
- PlayerInputComponent->BindAxis("LookUp", this, &APawn::AddControllerPitchInput);
- }
- //---------------------------------------------------------
- // Character Landed
- void ARopeHabschCharacter::Landed(const FHitResult& Hit) {
- //GEngine->AddOnScreenDebugMessage(-1, 10.0f, FColor::Cyan, FString::Printf(TEXT("Char landed")));
- switch (ePlayerState) {
- case Enum_PlayerState::IsNormal:
- // attachPointHookingTo = nullptr; // Had to be reset here as well if player landed between HookingStart Anim and until it was finished
- break;
- case Enum_PlayerState::IsHookingASwing:
- CancelSwing(false);
- break;
- // If in IsSwinging when registering OnLandedd then Stops it and resets state
- case Enum_PlayerState::IsSwinging:
- CancelSwing(false);
- break;
- case Enum_PlayerState::IsReeling:
- // CancelReelIn();
- break;
- case Enum_PlayerState::IsHookingAReel:
- break;
- default:
- break;
- }
- // If Landed Having Swung then Stops it
- if (swingComponent->wasSwinging) {
- CancelSwing(false);
- }
- }
- //---------------------------------------------------------
- // Get Variables From BP and Set them
- void ARopeHabschCharacter::SetVariablesFromBP(URopeHabschSwingComponent* swingComp) {
- swingComponent = swingComp;
- }
- //---------------------------------------------------------
- // Execute Attach Point Interaction after the player has rotated and cable is connected (Called from BP)
- void ARopeHabschCharacter::ExecuteAttachPointInteraction() {
- // Depending on which type of Attach Point it Calls StartReeling or Start Swinging
- if (attachPointHookingTo->interactableType == Enum_InteractableTypes::ReelPoint) {
- // Starts Swinging with given attach point as ref
- swingComponent->StartReeling(attachPointHookingTo);
- ePlayerState = Enum_PlayerState::IsReeling;
- // attachPointHookingTo = nullptr; // Only meant to be saved during the Hook Start Animation so gets reset when actual swing starts
- }
- else if (attachPointHookingTo->interactableType == Enum_InteractableTypes::SwingPoint) {
- if (GetCharacterMovement()->IsFalling() && attachPointHookingTo) {
- ePlayerState = Enum_PlayerState::IsSwinging;
- swingComponent->StartSwinging(attachPointHookingTo); // SÅ INGET KMR SKE NU MED SWING COMP ÄN.
- }
- }
- }
- //---------------------------------------------------------
- // Trace for Interactables implementing Interact Interface
- void ARopeHabschCharacter::TraceForInteractables() {
- FHitResult interactTraceHitResult;
- // FVector traceStartLocation = GetFollowCamera()->GetComponentLocation();
- FVector traceStartLocation = traceForInteractablesStartLocation;
- FVector traceEndLocation = traceForInteractablesStartLocation + (traceForInteractablesDirection * reelPointInteractRange);
- GetWorld()->DebugDrawTraceTag = "";
- // Query Params
- FCollisionQueryParams TraceParams(
- "", // Trace Tag
- true, // Trace Complex
- GetOwner() // Actor att ignora.
- );
- // Line Trace
- GetWorld()->LineTraceSingleByChannel(
- OUT interactTraceHitResult, // Var den ska lagra HitResult
- traceStartLocation, // Start punkten
- traceEndLocation, // Slutpunkten
- interactCollisionChannel, // Kanalen som den ska traca
- TraceParams // Collision Query Params som ska användas
- );
- // If Got Hit
- if (interactTraceHitResult.bBlockingHit) {
- // Checks if already has a ref
- if (interactAbleActor) {
- // If Hit was the same that we're already looking at
- if (interactTraceHitResult.GetActor() == interactAbleActor) {
- ICPP_InteractInterface* interactInterface = Cast<ICPP_InteractInterface>(interactTraceHitResult.GetActor());
- // Then checks if still within acceptable distance, otherwise nulls it
- switch (interactInterface->GetInteractableType_Implementation()) {
- case Enum_InteractableTypes::ReelPoint:
- // If Distance to already highlighted interactable is to great or to small then nulls it
- if (GetRangeBetweenActors(this, interactAbleActor) > reelPointInteractRange || GetRangeBetweenActors(this, interactAbleActor) < reelPointMinDistanceToInteract) {
- interactInterface->PlayerLookingAtInteractable_Implementation(false);
- interactAbleActor = nullptr;
- }
- break;
- case Enum_InteractableTypes::SwingPoint:
- if (GetRangeBetweenActors(this, interactAbleActor) > swingPointInteractRange || GetRangeBetweenActors(this, interactAbleActor) < swingPointMinDistanceToInteract) {
- interactInterface->PlayerLookingAtInteractable_Implementation(false);
- interactAbleActor = nullptr;
- }
- break;
- case Enum_InteractableTypes::Button:
- break;
- default:
- break;
- }
- // return;
- }
- // 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
- else {
- ICPP_InteractInterface* interactInterface = Cast<ICPP_InteractInterface>(interactAbleActor);
- interactInterface->PlayerLookingAtInteractable_Implementation(false);
- interactAbleActor = nullptr;
- }
- }
- // If not the execution will continue and check if the hit actor implements interface, if so then updates ref and calls interface
- if (!interactAbleActor && interactTraceHitResult.GetActor()->GetClass()->ImplementsInterface(UCPP_InteractInterface::StaticClass())) {
- // Checks if it implements interactInterface
- ICPP_InteractInterface* interactInterface = Cast<ICPP_InteractInterface>(interactTraceHitResult.GetActor());
- if (interactInterface) {
- // Depending on the type of interactable, checks the interactable range
- switch (interactInterface->GetInteractableType_Implementation()) {
- case Enum_InteractableTypes::ReelPoint:
- // If within range that the character has to that type of interactable
- if (GetRangeBetweenActors(this, interactTraceHitResult.GetActor()) <= reelPointInteractRange && GetRangeBetweenActors(this, interactTraceHitResult.GetActor()) >= reelPointMinDistanceToInteract) {
- interactAbleActor = interactTraceHitResult.GetActor(); // If within Correct Range then sets the interactableRef
- interactInterface->PlayerLookingAtInteractable_Implementation(true); // Calls Looking At interface at the interactable
- }
- break;
- case Enum_InteractableTypes::SwingPoint:
- if (GetRangeBetweenActors(this, interactTraceHitResult.GetActor()) <= swingPointInteractRange && GetRangeBetweenActors(this, interactTraceHitResult.GetActor()) >= swingPointMinDistanceToInteract) {
- interactAbleActor = interactTraceHitResult.GetActor();
- interactInterface->PlayerLookingAtInteractable_Implementation(true);
- }
- break;
- case Enum_InteractableTypes::Button:
- /*
- if (GetRangeBetweenActors(this, interactTraceHitResult.GetActor()) <= INSERT BUTTON INTERACT RANGE) {
- interactInterface->PlayerLookingAtInteractable_Implementation(true);
- }
- */
- break;
- default:
- break;
- }
- }
- }
- }
- // If no Hit
- else if (!interactTraceHitResult.bBlockingHit) {
- // Resets the interactable if the player was previously looking at one and now is not looking at anything
- if (interactAbleActor) {
- ICPP_InteractInterface* interactInterface = Cast<ICPP_InteractInterface>(interactAbleActor);
- if (interactInterface) {
- interactInterface->PlayerLookingAtInteractable_Implementation(false);
- }
- interactAbleActor = nullptr;
- }
- // DrawDebugLine(GetWorld(), traceStartLocation, traceEndLocation, FColor::Red, true, 10, 0, 1);
- }
- }
- //---------------------------------------------------------
- // Player Interaction with actors implementing Interact Interface
- void ARopeHabschCharacter::PlayerInteract(bool pressed) {
- // GEngine->AddOnScreenDebugMessage(-1, 10.0f, FColor::Cyan, FString::Printf(TEXT("Player Interact")));
- // If Player Pressed the Interact Key
- if (pressed) {
- switch (ePlayerState) {
- case Enum_PlayerState::IsNormal:
- if (interactAbleActor) {
- ICPP_InteractInterface* interactInterface = Cast<ICPP_InteractInterface>(interactAbleActor);
- // If allowed to Interact with it, for example if not another player is swinging on it
- if (interactInterface->Interact_Implementation()) {
- // Depending on what it is the char can interact with it in different ways
- switch (interactInterface->GetInteractableType_Implementation()) {
- // If Interacting with Reel Point
- case Enum_InteractableTypes::ReelPoint:
- // Gets a AttachPoint Ref
- attachPointHookingTo = Cast<ARopeHabschAttachPoint>(interactAbleActor);
- if (attachPointHookingTo) {
- ePlayerState = Enum_PlayerState::IsHookingAReel;
- RotateAndHookToAttachPoint();
- }
- break;
- // If Interacting with Swing Point
- case Enum_InteractableTypes::SwingPoint:
- if (GetCharacterMovement()->IsFalling()) {
- // Gets a AttachPoint Ref
- attachPointHookingTo = Cast<ARopeHabschAttachPoint>(interactAbleActor);
- if (attachPointHookingTo) {
- ePlayerState = Enum_PlayerState::IsHookingASwing;
- RotateAndHookToAttachPoint();
- }
- }
- break;
- case Enum_InteractableTypes::Button:
- break;
- default:
- break;
- }
- }
- }
- break;
- case Enum_PlayerState::IsHookingASwing:
- break;
- case Enum_PlayerState::IsSwinging:
- break;
- case Enum_PlayerState::IsReeling:
- break;
- default:
- break;
- }
- }
- // If Player Released the Interact Key
- else {
- switch (ePlayerState) {
- case Enum_PlayerState::IsNormal:
- break;
- case Enum_PlayerState::IsHookingASwing:
- CancelSwing(false);
- break;
- case Enum_PlayerState::IsSwinging:
- CancelSwing(true);
- break;
- case Enum_PlayerState::IsHookingAReel:
- swingComponent->StopReeling();
- ePlayerState = Enum_PlayerState::IsNormal;
- attachPointHookingTo = nullptr;
- break;
- case Enum_PlayerState::IsReeling:
- CancelReelIn();
- break;
- default:
- break;
- }
- }
- }
- //---------------------------------------------------------
- void ARopeHabschCharacter::MoveForward(float Value) {
- switch (ePlayerState)
- {
- case Enum_PlayerState::IsNormal:
- if ((Controller != NULL) && (Value != 0.0f)) {
- // find out which way is forward
- const FRotator Rotation = Controller->GetControlRotation();
- const FRotator YawRotation(0, Rotation.Yaw, 0);
- // get forward vector
- const FVector Direction = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X);
- AddMovementInput(Direction, Value);
- }
- break;
- case Enum_PlayerState::IsHookingASwing:
- break;
- case Enum_PlayerState::IsSwinging:
- // TODO - Adda input för ge kraft till swingen
- if ((Controller != NULL) && (Value != 0.0f)) {
- // find out which way is forward
- const FRotator Rotation = Controller->GetControlRotation();
- const FRotator YawRotation(0, Rotation.Yaw, 0);
- // get forward vector
- const FVector Direction = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X);
- AddMovementInput(Direction, Value);
- }
- swingComponent->playerSwingInput = Value;
- break;
- case Enum_PlayerState::IsHookingAReel:
- break;
- case Enum_PlayerState::IsReeling:
- break;
- default:
- break;
- }
- }
- //---------------------------------------------------------
- void ARopeHabschCharacter::MoveRight(float Value) {
- switch (ePlayerState) {
- case Enum_PlayerState::IsNormal:
- if ((Controller != NULL) && (Value != 0.0f)) {
- // find out which way is right
- const FRotator Rotation = Controller->GetControlRotation();
- const FRotator YawRotation(0, Rotation.Yaw, 0);
- // get right vector
- const FVector Direction = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::Y);
- // add movement in that direction
- AddMovementInput(Direction, Value);
- }
- break;
- case Enum_PlayerState::IsSwinging:
- if ((Controller != NULL) && (Value != 0.0f)) {
- // find out which way is right
- const FRotator Rotation = Controller->GetControlRotation();
- const FRotator YawRotation(0, Rotation.Yaw, 0);
- // get right vector
- const FVector Direction = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::Y);
- // add movement in that direction
- AddMovementInput(Direction, Value);
- }
- // TODO - Add Slight Swing Steering
- break;
- case Enum_PlayerState::IsHookingAReel:
- break;
- case Enum_PlayerState::IsReeling:
- break;
- default:
- break;
- }
- }
- //---------------------------------------------------------
- float ARopeHabschCharacter::GetRangeBetweenActors(AActor* actorFrom, AActor* actorTo) {
- return ((actorFrom->GetActorLocation() - actorTo->GetActorLocation()).Size());
- }
- //---------------------------------------------------------
- FVector ARopeHabschCharacter::GetGrabHandSocketLocation() {
- if (GetMesh()->DoesSocketExist(grabHandSocketName)) {
- return GetMesh()->GetSocketLocation(grabHandSocketName);
- }
- else {
- return FVector(0, 0, 0);
- }
- }
- //---------------------------------------------------------
- void ARopeHabschCharacter::CancelReelIn() {
- CancelSwingOrReel();
- swingComponent->StopReeling();
- ePlayerState = Enum_PlayerState::IsNormal;
- attachPointHookingTo = nullptr;
- }
- //---------------------------------------------------------
- void ARopeHabschCharacter::CancelSwing(bool inheritSwingVelocity) {
- CancelSwingOrReel();
- swingComponent->StopSwinging(inheritSwingVelocity);
- ePlayerState = Enum_PlayerState::IsNormal;
- attachPointHookingTo = nullptr;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement