Guest User

Untitled

a guest
Mar 22nd, 2016
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.77 KB | None | 0 0
  1. // Copyright 1998-2015 Epic Games, Inc. All Rights Reserved.
  2.  
  3. #include "Engine.h"
  4. #include "Test.h"
  5. #include "TestCharacter.h"
  6.  
  7. //////////////////////////////////////////////////////////////////////////
  8. // ATestCharacter
  9.  
  10. ATestCharacter::ATestCharacter()
  11. {
  12.     // Set size for collision capsule
  13.     GetCapsuleComponent()->InitCapsuleSize(42.f, 96.0f);
  14.  
  15.     // set our turn rates for input
  16.     BaseTurnRate = 45.f;
  17.     BaseLookUpRate = 45.f;
  18.  
  19.     /**/
  20.     // Don't rotate when the controller rotates. Let that just affect the camera.
  21.     bUseControllerRotationPitch = false;
  22.     bUseControllerRotationYaw = false;
  23.     bUseControllerRotationRoll = false;
  24.    
  25.     // Configure character movement
  26.     GetCharacterMovement()->bOrientRotationToMovement = false; // Character moves in the direction of input... 
  27.     GetCharacterMovement()->RotationRate = FRotator(0.0f, 540.0f, 0.0f); // ...at this rotation rate
  28.     GetCharacterMovement()->JumpZVelocity = 600.f;
  29.     GetCharacterMovement()->AirControl = 0.2f;
  30.  
  31.     // Create a camera boom (pulls in towards the player if there is a collision)
  32.     CameraBoom = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraBoom"));
  33.     CameraBoom->AttachTo(RootComponent);
  34.     CameraBoom->TargetArmLength = 300.0f; // The camera follows at this distance behind the character  
  35.     CameraBoom->bUsePawnControlRotation = false; // Rotate the arm based on the controller
  36.     CameraBoom->bInheritYaw = false;
  37.     CameraBoom->bInheritRoll = false;
  38.     CameraBoom->bInheritPitch = false;
  39.  
  40.     // Create a follow camera
  41.     FollowCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("FollowCamera"));
  42.     FollowCamera->AttachTo(CameraBoom, USpringArmComponent::SocketName); // Attach the camera to the end of the boom and let the boom adjust to match the controller orientation
  43.     FollowCamera->bUsePawnControlRotation = false; // Camera does not rotate relative to arm
  44.  
  45.     // Note: The skeletal mesh and anim blueprint references on the Mesh component (inherited from Character)
  46.     // are set in the derived blueprint asset named MyCharacter (to avoid direct content references in C++)
  47.  
  48.     bRightMouseDown = false;
  49.  
  50.     APlayerController *PlayerController = Cast<APlayerController>(GetController());
  51.  
  52.     if (PlayerController)
  53.     {
  54.         PlayerController->bEnableClickEvents = true;
  55.         PlayerController->bEnableTouchEvents = true;
  56.         PlayerController->bEnableMouseOverEvents = true;
  57.     }
  58. }
  59.  
  60. //////////////////////////////////////////////////////////////////////////
  61. // Input
  62.  
  63. void ATestCharacter::SetupPlayerInputComponent(class UInputComponent* InputComponent)
  64. {
  65.     // Set up gameplay key bindings
  66.     check(InputComponent);
  67.  
  68.     // add mouse right click event
  69.     InputComponent->BindAction("RightMouse", IE_Pressed, this, &ATestCharacter::RightMousePressed);
  70.     InputComponent->BindAction("RightMouse", IE_Released, this, &ATestCharacter::RightMouseReleased);
  71.  
  72.     InputComponent->BindAction("Jump", IE_Pressed, this, &ACharacter::Jump);
  73.     InputComponent->BindAction("Jump", IE_Released, this, &ACharacter::StopJumping);
  74.  
  75.     InputComponent->BindAxis("MoveForward", this, &ATestCharacter::MoveForward);
  76.     InputComponent->BindAxis("Rotate", this, &ATestCharacter::Rotate);
  77.      
  78.     InputComponent->BindAxis("LookHorizontal", this, &ATestCharacter::TurnAtRate);
  79.     InputComponent->BindAxis("LookVertical", this, &ATestCharacter::LookUpAtRate);
  80.  
  81.     InputComponent->BindAction("MouseWheelDown", IE_Pressed, this, &ATestCharacter::MouseWheelDown);
  82.     InputComponent->BindAction("MouseWheelUp", IE_Released, this, &ATestCharacter::MouseWheelUp);
  83.  
  84.     InputComponent->BindAxis("StrafeRight", this, &ATestCharacter::StrafeRight);
  85.  
  86.     MaxBoomLength = 1000.0f;
  87.     MinBoomLength = 50.0f;
  88.     ZoomAmount = 20.0;
  89. }
  90.  
  91. void ATestCharacter::Rotate(float Rate)
  92. {
  93.     auto NewRotation = FRotator(0.0f, Rate, 0.0f);
  94.     CameraBoom->AddWorldRotation(NewRotation);
  95.  
  96.     GetController()->SetControlRotation(FRotator(0.0f, CameraBoom->RelativeRotation.Yaw, 0.0f));
  97. }
  98.  
  99. void ATestCharacter::MouseWheelUp()
  100. {
  101.     float NewArmLength = CameraBoom->TargetArmLength;
  102.  
  103.  
  104.     float ZoomSensitivity = 1.0f;// NewArmLength / 100.0f;
  105.  
  106.     NewArmLength -= (ZoomAmount * ZoomSensitivity);
  107.  
  108.     if (NewArmLength < MinBoomLength)
  109.         NewArmLength = MinBoomLength;
  110.  
  111.     CameraBoom->TargetArmLength = NewArmLength; // The camera follows at this distance behind the character
  112. }
  113.  
  114. void ATestCharacter::MouseWheelDown()
  115. {
  116.     float NewArmLength = CameraBoom->TargetArmLength;
  117.  
  118.     float ZoomSensitivity = 1.0f;// NewArmLength / 100.0f;
  119.    
  120.     NewArmLength += (ZoomAmount * ZoomSensitivity);
  121.  
  122.     if (NewArmLength > MaxBoomLength)
  123.         NewArmLength = MaxBoomLength;
  124.  
  125.     CameraBoom->TargetArmLength = NewArmLength; // The camera follows at this distance behind the character
  126. }
  127.  
  128. void ATestCharacter::TurnAtRate(float Rate)
  129. {
  130.     // calculate delta for this frame from the rate information
  131.     //AddControllerYawInput(Rate * BaseTurnRate * GetWorld()->GetDeltaSeconds());
  132.     if (!bRightMouseDown)
  133.         return;
  134.  
  135.     CameraBoom->AddRelativeRotation(FRotator(0.0f, Rate, 0.0f));
  136. //  CameraBoom->AddLocalRotation(FRotator(0.0f, Rate, 0.0f));
  137.     AddControllerYawInput(Rate);
  138. }
  139.  
  140. void ATestCharacter::LookUpAtRate(float Rate)
  141. {
  142.     if (!bRightMouseDown)
  143.         return;
  144.     CameraBoom->AddRelativeRotation(FRotator(Rate, 0.0f, 0.0f));
  145.     //CameraBoom->AddLocalRotation(FRotator(Rate, 0.0f, 0.0f));
  146.     AddControllerPitchInput(Rate);
  147.     // calculate delta for this frame from the rate information
  148.     //AddControllerPitchInput(Rate * BaseLookUpRate * GetWorld()->GetDeltaSeconds());
  149. }
  150.  
  151. void ATestCharacter::MoveForward(float Value)
  152. {
  153.     if ((Controller != NULL) && (Value != 0.0f))
  154.     {
  155.         // find out which way is forward
  156.         const FRotator Rotation = Controller->GetControlRotation();
  157.         const FRotator YawRotation(0, Rotation.Yaw, 0);
  158.         // get forward vector
  159.         FVector Direction = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X);    
  160.         if (Value < 0)
  161.         {
  162.             Direction = GetActorForwardVector();
  163.             Value *= 0.5f;
  164.         }
  165.         AddMovementInput(Direction, Value);
  166.     }
  167. }
  168.  
  169. void ATestCharacter::StrafeRight(float Value)
  170. {
  171.     if ( (Controller != NULL) && (Value != 0.0f) )
  172.     {
  173.         // find out which way is right
  174.         const FRotator Rotation = Controller->GetControlRotation();
  175.         const FRotator YawRotation(0, Rotation.Yaw, 0);
  176.    
  177.         // get right vector
  178.         const FVector Direction = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::Y);
  179.         // add movement in that direction
  180.         AddMovementInput(Direction, Value);
  181.     }
  182. }
  183.  
  184. void ATestCharacter::RightMousePressed()
  185. {
  186.     bRightMouseDown = true;
  187.  
  188.     APlayerController* PlayerController = Cast<APlayerController>(GetController());
  189.  
  190.     if (PlayerController)
  191.     {
  192.         FInputModeGameOnly InputMode;
  193.         PlayerController->SetInputMode(InputMode);
  194.         PlayerController->bEnableClickEvents = false;
  195.         PlayerController->bEnableTouchEvents = false;
  196.         PlayerController->bEnableMouseOverEvents = false;
  197.         PlayerController->bShowMouseCursor = false;
  198.     }
  199. }
  200.  
  201. void ATestCharacter::RightMouseReleased()
  202. {
  203.     bRightMouseDown = false;
  204.  
  205.     APlayerController* PlayerController = Cast<APlayerController>(GetController());
  206.  
  207.     if (PlayerController)
  208.     {
  209.         FInputModeGameAndUI InputMode;
  210.         InputMode.SetLockMouseToViewport(true);
  211.         InputMode.SetHideCursorDuringCapture(false);
  212.         PlayerController->SetInputMode(InputMode);
  213.         PlayerController->bEnableClickEvents = true;
  214.         PlayerController->bEnableTouchEvents = true;
  215.         PlayerController->bEnableMouseOverEvents = true;
  216.         PlayerController->bShowMouseCursor = true;
  217.  
  218.         const ULocalPlayer* LocalPlayer = PlayerController->GetLocalPlayer();
  219.         if (LocalPlayer && LocalPlayer->ViewportClient)
  220.         {
  221.             FViewport* Viewport = LocalPlayer->ViewportClient->Viewport;
  222.             if (Viewport)
  223.             {
  224.                 FVector2D ViewportSize;
  225.                 LocalPlayer->ViewportClient->GetViewportSize(ViewportSize);
  226.                 const int32 X = static_cast<int32>(ViewportSize.X * 0.5f);
  227.                 const int32 Y = static_cast<int32>(ViewportSize.Y * 0.5f);
  228.  
  229.                 Viewport->SetMouse(X, Y);
  230.             }
  231.         }
  232.     }
  233.  
  234. }
Advertisement
Add Comment
Please, Sign In to add comment