unleashedcode

RTS_PlayerController.cpp

Oct 24th, 2022
1,139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.45 KB | Source Code | 0 0
  1. // Fill out your copyright notice in the Description page of Project Settings.
  2.  
  3.  
  4. #include "RTSControls/RTSPlayerController.h"
  5. #include "RTSControls/RTSCameraPawn.h"
  6. #include "RTSControls/RTSMovementComponent.h"
  7. #include "Components/InputComponent.h"
  8. #include "GameFramework/PlayerController.h"
  9. #include "HUD/RTS_HUD.h"
  10.  
  11. /* Constructor */
  12. ARTSPlayerController::ARTSPlayerController()
  13. {
  14.     PrimaryActorTick.bCanEverTick = true;
  15.     PrimaryActorTick.bStartWithTickEnabled = true;
  16.     bShowMouseCursor = true;    
  17. }
  18.  
  19. void ARTSPlayerController::BeginPlay()
  20. {
  21.     Super::BeginPlay();
  22.  
  23.     //Cast Pawn to our CameraPawn
  24.     RTSCameraPawn  = Cast<ARTSCameraPawn>(GetPawn());
  25.     check(RTSCameraPawn);
  26.  
  27.     //Cast the HUD to our HUD
  28.     RTS_HUD = Cast<ARTS_HUD>(GetHUD());
  29.    
  30.  
  31.     //Lock the mouse to the screen bounds
  32.     FInputModeGameAndUI MouseLock;
  33.     MouseLock.SetLockMouseToViewportBehavior(EMouseLockMode::LockAlways);
  34.     MouseLock.SetHideCursorDuringCapture(false);
  35.  
  36.     //Setup the input mode
  37.     SetInputMode(MouseLock);
  38. }
  39.  
  40.  
  41. /* Input Bindings  */
  42. void ARTSPlayerController::SetupInputComponent()
  43. {
  44.  
  45.     Super::SetupInputComponent();
  46.     check(InputComponent);
  47.  
  48.      //Panning input
  49.     InputComponent->BindAction("RotatePanCameraRight", IE_Pressed, this, &ARTSPlayerController::CallPanCameraRight);
  50.     InputComponent->BindAction("RotatePanCameraLeft", IE_Pressed, this, &ARTSPlayerController::CallPanCameraLeft);
  51.     InputComponent->BindAction("ResetPanCamera", IE_Pressed, this, &ARTSPlayerController::CallPanCameraReset);
  52.     InputComponent->BindAction("MouseLookToggle", IE_Pressed, this, &ARTSPlayerController::CallMouseXToggle);
  53.     InputComponent->BindAction("MouseLookToogle", IE_Released, this, &ARTSPlayerController::CallMouseXToggle);
  54.     InputComponent->BindAxis("MouseAxisLR", this, &ARTSPlayerController::CallMouseAxisLR);
  55.  
  56.     //Zooming Input
  57.     InputComponent->BindAction("ZoomIn", IE_Pressed, this, &ARTSPlayerController::CallZoomIn);
  58.     InputComponent->BindAction("ZoomOut", IE_Pressed, this, &ARTSPlayerController::CallZoomOut);
  59.    
  60.     //Movement Forward - Back - Left - Right with keys
  61.     InputComponent->BindAxis("MoveForwardKeys", this, &ARTSPlayerController::CallCameraMoveForward);
  62.     InputComponent->BindAxis("MoveRightKeys", this, &ARTSPlayerController::CallCameraMoveRight);
  63.     InputComponent->BindAction("SpeedBoost", IE_Pressed, this, &ARTSPlayerController::CallSpeedBoost);
  64.     InputComponent->BindAction("SpeedBoost", IE_Released, this, &ARTSPlayerController::CallSpeedNormal);
  65.  
  66.     //Edge Scrolling Sides/Corners/Tops and Bottoms
  67.     InputComponent->BindAxis("EdgeScrollX", this, &ARTSPlayerController::CallEdgeScrollX);
  68.     InputComponent->BindAxis("EdgeScrollY", this, &ARTSPlayerController::CallEdgeScrollY);
  69.  
  70.     //Maquee and RTS HUD Controls
  71.     InputComponent->BindAction("MarqueeSelect", IE_Pressed, this, &ARTSPlayerController::OnMarqueeClicked);
  72.     InputComponent->BindAction("MarqueeSelect", IE_Released, this, &ARTSPlayerController::OnMarqueeReleased);
  73.     InputComponent->BindAction("MouseRightClick", IE_Pressed, this, &ARTSPlayerController::RightClickMenu);
  74.     InputComponent->BindAction("MouseRightClick", IE_Released, this, &ARTSPlayerController::RightClickRelease);
  75. }
  76.  
  77.  
  78. /* Methods to be called externally on our CameraMovementComponent for the above bindings  */
  79.  
  80. /* Camera rotation by keys (45 degrees at a time) and reset button */
  81. void ARTSPlayerController::CallPanCameraRight()
  82. {
  83.     RTSCameraPawn->RTSMovementComponent->PanCamera(45.0f);    
  84. }
  85.  
  86. void ARTSPlayerController::CallPanCameraLeft()
  87. {
  88.     RTSCameraPawn->RTSMovementComponent->PanCamera(-45.0f);
  89. }
  90.  
  91. void ARTSPlayerController::CallPanCameraReset()
  92. {
  93.     RTSCameraPawn->RTSMovementComponent->ResetCameraPan();
  94. }
  95.  
  96.  
  97. /* Zoom In and Out (Mouse Scroll Wheel)  */
  98. void ARTSPlayerController::CallZoomIn()
  99. {
  100.     RTSCameraPawn->RTSMovementComponent->ZoomIn();
  101. }
  102.  
  103. void ARTSPlayerController::CallZoomOut()
  104. {
  105.     RTSCameraPawn->RTSMovementComponent->ZoomOut();
  106. }
  107.  
  108. /* Mouse Axis Left and Rigt  */
  109. void ARTSPlayerController::CallMouseAxisLR(float AxisValue)
  110. {
  111.     RTSCameraPawn->RTSMovementComponent->PanCamera(AxisValue * PanSpeed);
  112. }
  113.  
  114. /* Mouse Free Look Toggle  */
  115. void ARTSPlayerController::CallMouseXToggle()
  116. {
  117.     bool bIsMouseViewLocked = RTSCameraPawn->RTSMovementComponent->GetMouseLookX();
  118.     //set the camera to free view on the x axis or not
  119.     if (!bIsMouseViewLocked)
  120.     {
  121.        RTSCameraPawn->RTSMovementComponent->SetMouseLookX(true);
  122.     }
  123.     else if (bIsMouseViewLocked)
  124.     {
  125.         RTSCameraPawn->RTSMovementComponent->SetMouseLookX(false);
  126.     }
  127. }
  128.  
  129.  
  130. /* Camera Forward/Back  */
  131. void ARTSPlayerController::CallCameraMoveForward(float AxisValue)
  132. {
  133.     RTSCameraPawn->RTSMovementComponent->BasicCameraMovement(0.0f, AxisValue);
  134. }
  135.  
  136. /* Camera Left-Right  */
  137. void ARTSPlayerController::CallCameraMoveRight(float AxisValue)
  138. {
  139.     RTSCameraPawn->RTSMovementComponent->BasicCameraMovement(AxisValue, 0.0f);
  140. }
  141.  
  142. /* Speed Boost  */
  143. void ARTSPlayerController::CallSpeedBoost()
  144. {
  145.     UE_LOG(LogTemp, Warning, TEXT("SpeedBoost_PlayerController"));
  146.     RTSCameraPawn->RTSMovementComponent->SetSpeedBoost(BoostSpeedMultiplier);
  147. }
  148.  
  149. /* Return speed to normal  */
  150. void ARTSPlayerController::CallSpeedNormal()
  151. {
  152.     RTSCameraPawn->RTSMovementComponent->SetSpeedBoost(SpeedDefault);
  153. }
  154.  
  155. /* Edge Scrolling X */
  156. void ARTSPlayerController::CallEdgeScrollX(float AxisValue)
  157. {
  158.     RTSCameraPawn->RTSMovementComponent->EdgeScrolling();
  159. }
  160.  
  161. /* Edge Scrolling Y  */
  162. void ARTSPlayerController::CallEdgeScrollY(float AxisValue)
  163. {
  164.     RTSCameraPawn->RTSMovementComponent->EdgeScrolling();
  165. }
  166.  
  167. /*  Marquee Select (Hightlight) */
  168. void ARTSPlayerController::OnMarqueeClicked()
  169. {
  170.     //Get the initial point of where we started to click (mouse position) and update the HUD variable
  171.     RTS_HUD->InitialPoint = RTS_HUD->GetMousePosition2D();
  172.     RTS_HUD->bStartMarqueeSelect = true;
  173.     UE_LOG(LogTemp,  Warning, TEXT("InitialPoint: %s"),  *RTS_HUD->GetMousePosition2D().ToString());
  174.  
  175.  
  176. }
  177.  
  178. /*  Marquee Release (Highlight) */
  179. void ARTSPlayerController::OnMarqueeReleased()
  180. {
  181.     RTS_HUD->bStartMarqueeSelect = false;
  182. }
  183.  
  184. /*  Right Click Menu */
  185. void ARTSPlayerController::RightClickMenu()
  186. {
  187.     UE_LOG(LogTemp, Warning, TEXT("RIghtClick_PlayerController"));
  188. }
  189.  
  190. /*  Right Click Release */
  191. void ARTSPlayerController::RightClickRelease()
  192. {
  193.     UE_LOG(LogTemp, Warning, TEXT("RightClickReleased_PlayerController"));
  194. }
  195.  
Advertisement
Add Comment
Please, Sign In to add comment