RaanCharacter.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. // Copyright 1998-2019 Epic Games, Inc. All Rights Reserved.
  2. #include "RaanCharacter.h"
  3. #include "HeadMountedDisplayFunctionLibrary.h"
  4. #include "Camera/CameraComponent.h"
  5. #include "Components/CapsuleComponent.h"
  6. #include "Components/InputComponent.h"
  7. #include "GameFramework/CharacterMovementComponent.h"
  8. #include "GameFramework/Controller.h"
  9. #include "GameFramework/SpringArmComponent.h"
  10. #include "Interactible.h"
  11. //////////////////////////////////////////////////////////////////////////
  12. // ARaanCharacter
  13. ARaanCharacter::ARaanCharacter()
  14. {
  15. // Set size for collision capsule
  16. GetCapsuleComponent()->InitCapsuleSize(42.f, 96.0f);
  17. // set our turn rates for input
  18. BaseTurnRate = 45.f;
  19. BaseLookUpRate = 45.f;
  20. // Don't rotate when the controller rotates. Let that just affect the camera.
  21. bUseControllerRotationPitch = false;
  22. bUseControllerRotationYaw = false;
  23. bUseControllerRotationRoll = false;
  24. // Configure character movement
  25. GetCharacterMovement()->bOrientRotationToMovement = true; // Character moves in the direction of input...
  26. GetCharacterMovement()->RotationRate = FRotator(0.0f, 540.0f, 0.0f); // ...at this rotation rate
  27. GetCharacterMovement()->JumpZVelocity = 600.f;
  28. GetCharacterMovement()->AirControl = 0.2f;
  29. // Create a camera boom (pulls in towards the player if there is a collision)
  30. CameraBoom = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraBoom"));
  31. CameraBoom->SetupAttachment(RootComponent);
  32. CameraBoom->TargetArmLength = 300.0f; // The camera follows at this distance behind the character
  33. CameraBoom->bUsePawnControlRotation = true; // Rotate the arm based on the controller
  34. // Create a follow camera
  35. FollowCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("FollowCamera"));
  36. FollowCamera->SetupAttachment(CameraBoom, USpringArmComponent::SocketName); // Attach the camera to the end of the boom and let the boom adjust to match the controller orientation
  37. FollowCamera->bUsePawnControlRotation = false; // Camera does not rotate relative to arm
  38. TargetVolume = CreateDefaultSubobject<UCapsuleComponent>(TEXT("TargetVolume"));
  39. TargetVolume->InitCapsuleSize(180, 170);
  40. TargetVolume->SetCollisionProfileName(TEXT("Trigger"));
  41. TargetVolume->SetupAttachment(RootComponent);
  42. // Note: The skeletal mesh and anim blueprint references on the Mesh component (inherited from Character)
  43. // are set in the derived blueprint asset named MyCharacter (to avoid direct content references in C++)
  44. }
  45. //////////////////////////////////////////////////////////////////////////
  46. // Input
  47. void ARaanCharacter::SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent)
  48. {
  49. // Set up gameplay key bindings
  50. check(PlayerInputComponent);
  51. PlayerInputComponent->BindAction("Jump", IE_Pressed, this, &ACharacter::Jump);
  52. PlayerInputComponent->BindAction("Jump", IE_Released, this, &ACharacter::StopJumping);
  53. PlayerInputComponent->BindAction("Target", IE_Pressed, this, &ARaanCharacter::ToggleTarget);
  54. PlayerInputComponent->BindAction("Interact", IE_Pressed, this, &ARaanCharacter::Interact);
  55. PlayerInputComponent->BindAxis("MoveForward", this, &ARaanCharacter::MoveForward);
  56. PlayerInputComponent->BindAxis("MoveRight", this, &ARaanCharacter::MoveRight);
  57. // We have 2 versions of the rotation bindings to handle different kinds of devices differently
  58. // "turn" handles devices that provide an absolute delta, such as a mouse.
  59. // "turnrate" is for devices that we choose to treat as a rate of change, such as an analog joystick
  60. PlayerInputComponent->BindAxis("Turn", this, &APawn::AddControllerYawInput);
  61. PlayerInputComponent->BindAxis("TurnRate", this, &ARaanCharacter::TurnAtRate);
  62. PlayerInputComponent->BindAxis("LookUp", this, &APawn::AddControllerPitchInput);
  63. PlayerInputComponent->BindAxis("LookUpRate", this, &ARaanCharacter::LookUpAtRate);
  64. // handle touch devices
  65. PlayerInputComponent->BindTouch(IE_Pressed, this, &ARaanCharacter::TouchStarted);
  66. PlayerInputComponent->BindTouch(IE_Released, this, &ARaanCharacter::TouchStopped);
  67. // VR headset functionality
  68. PlayerInputComponent->BindAction("ResetVR", IE_Pressed, this, &ARaanCharacter::OnResetVR);
  69. }
  70. void ARaanCharacter::OnResetVR()
  71. {
  72. UHeadMountedDisplayFunctionLibrary::ResetOrientationAndPosition();
  73. }
  74. void ARaanCharacter::TouchStarted(ETouchIndex::Type FingerIndex, FVector Location)
  75. {
  76. Jump();
  77. }
  78. void ARaanCharacter::TouchStopped(ETouchIndex::Type FingerIndex, FVector Location)
  79. {
  80. StopJumping();
  81. }
  82. void ARaanCharacter::TurnAtRate(float Rate)
  83. {
  84. // calculate delta for this frame from the rate information
  85. AddControllerYawInput(Rate * BaseTurnRate * GetWorld()->GetDeltaSeconds());
  86. }
  87. void ARaanCharacter::LookUpAtRate(float Rate)
  88. {
  89. // calculate delta for this frame from the rate information
  90. AddControllerPitchInput(Rate * BaseLookUpRate * GetWorld()->GetDeltaSeconds());
  91. }
  92. void ARaanCharacter::MoveForward(float Value)
  93. {
  94. if ((Controller != NULL) && (Value != 0.0f))
  95. {
  96. // find out which way is forward
  97. const FRotator Rotation = Controller->GetControlRotation();
  98. const FRotator YawRotation(0, Rotation.Yaw, 0);
  99. // get forward vector
  100. const FVector Direction = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X);
  101. AddMovementInput(Direction, Value);
  102. }
  103. }
  104. void ARaanCharacter::MoveRight(float Value)
  105. {
  106. if ( (Controller != NULL) && (Value != 0.0f) )
  107. {
  108. // find out which way is right
  109. const FRotator Rotation = Controller->GetControlRotation();
  110. const FRotator YawRotation(0, Rotation.Yaw, 0);
  111. // get right vector
  112. const FVector Direction = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::Y);
  113. // add movement in that direction
  114. AddMovementInput(Direction, Value);
  115. }
  116. }
  117. void ARaanCharacter::ToggleTarget()
  118. {
  119. }
  120. void ARaanCharacter::Interact() {
  121. TArray<AActor*> candidates;
  122. TargetVolume->GetOverlappingActors(candidates);
  123. for (auto candidate : candidates) {
  124. UInteractible* interactible = Cast<UInteractible>(candidate->FindComponentByClass(UInteractible::StaticClass()));
  125. if (interactible && GEngine) {
  126. GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Yellow, "dummy interaction invoked");
  127. }
  128. }
  129. }