Advertisement
Guest User

FPPCharacter.cpp

a guest
Oct 20th, 2017
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 11.54 KB | None | 0 0
  1. // Copyright 1998-2017 Epic Games, Inc. All Rights Reserved.
  2.  
  3. #include "FPPCharacter.h"
  4. #include "FPPProjectile.h"
  5. #include "Animation/AnimInstance.h"
  6. #include "Camera/CameraComponent.h"
  7. #include "Components/CapsuleComponent.h"
  8. #include "Components/InputComponent.h"
  9. #include "GameFramework/InputSettings.h"
  10. #include "HeadMountedDisplayFunctionLibrary.h"
  11. #include "Kismet/GameplayStatics.h"
  12. #include "MotionControllerComponent.h"
  13. #include "Engine.h"
  14.  
  15. DEFINE_LOG_CATEGORY_STATIC(LogFPChar, Warning, All);
  16.  
  17. #define print(text) if (GEngine) GEngine->AddOnScreenDebugMessage(-1, 1.5, FColor::White,text)
  18.  
  19. bool bIsSprinting;
  20. bool bIsCrouched;
  21.  
  22. //////////////////////////////////////////////////////////////////////////
  23. // AFPPCharacter
  24.  
  25. AFPPCharacter()
  26. {
  27.     // Set size for collision capsule
  28.     GetCapsuleComponent()->InitCapsuleSize(55.f, 96.0f);
  29.  
  30.     // set our turn rates for input
  31.     BaseTurnRate = 45.f;
  32.     BaseLookUpRate = 45.f;
  33.  
  34.     // Create a CameraComponent
  35.     FirstPersonCameraComponent = CreateDefaultSubobject<UCameraComponent>(TEXT("FirstPersonCamera"));
  36.     FirstPersonCameraComponent->SetupAttachment(GetCapsuleComponent());
  37.     FirstPersonCameraComponent->RelativeLocation = FVector(-39.56f, 1.75f, 64.f); // Position the camera
  38.     FirstPersonCameraComponent->bUsePawnControlRotation = true;
  39.  
  40.     // Create a mesh component that will be used when being viewed from a '1st person' view (when controlling this pawn)
  41.     Mesh1P = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("CharacterMesh1P"));
  42.     Mesh1P->SetOnlyOwnerSee(true);
  43.     Mesh1P->SetupAttachment(FirstPersonCameraComponent);
  44.     Mesh1P->bCastDynamicShadow = false;
  45.     Mesh1P->CastShadow = false;
  46.     Mesh1P->RelativeRotation = FRotator(1.9f, -19.19f, 5.2f);
  47.     Mesh1P->RelativeLocation = FVector(-0.5f, -4.4f, -155.7f);
  48.  
  49.     // Create a gun mesh component
  50.     FP_Gun = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("FP_Gun"));
  51.     FP_Gun->SetOnlyOwnerSee(true);          // only the owning player will see this mesh
  52.     FP_Gun->bCastDynamicShadow = false;
  53.     FP_Gun->CastShadow = false;
  54.     // FP_Gun->SetupAttachment(Mesh1P, TEXT("GripPoint"));
  55.     FP_Gun->SetupAttachment(RootComponent);
  56.  
  57.     FP_MuzzleLocation = CreateDefaultSubobject<USceneComponent>(TEXT("MuzzleLocation"));
  58.     FP_MuzzleLocation->SetupAttachment(FP_Gun);
  59.     FP_MuzzleLocation->SetRelativeLocation(FVector(0.2f, 48.4f, -10.6f));
  60.  
  61.     // Default offset from the character location for projectiles to spawn
  62.     GunOffset = FVector(100.0f, 0.0f, 10.0f);
  63.  
  64.     // Note: The ProjectileClass and the skeletal mesh/anim blueprints for Mesh1P, FP_Gun, and VR_Gun
  65.     // are set in the derived blueprint asset named MyCharacter to avoid direct content references in C++.
  66.  
  67.     // Create VR Controllers.
  68.     R_MotionController = CreateDefaultSubobject<UMotionControllerComponent>(TEXT("R_MotionController"));
  69.     R_MotionController->Hand = EControllerHand::Right;
  70.     R_MotionController->SetupAttachment(RootComponent);
  71.     L_MotionController = CreateDefaultSubobject<UMotionControllerComponent>(TEXT("L_MotionController"));
  72.     L_MotionController->SetupAttachment(RootComponent);
  73.  
  74.     // Create a gun and attach it to the right-hand VR controller.
  75.     // Create a gun mesh component
  76.     VR_Gun = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("VR_Gun"));
  77.     VR_Gun->SetOnlyOwnerSee(true);          // only the owning player will see this mesh
  78.     VR_Gun->bCastDynamicShadow = false;
  79.     VR_Gun->CastShadow = false;
  80.     VR_Gun->SetupAttachment(R_MotionController);
  81.     VR_Gun->SetRelativeRotation(FRotator(0.0f, -90.0f, 0.0f));
  82.  
  83.     VR_MuzzleLocation = CreateDefaultSubobject<USceneComponent>(TEXT("VR_MuzzleLocation"));
  84.     VR_MuzzleLocation->SetupAttachment(VR_Gun);
  85.     VR_MuzzleLocation->SetRelativeLocation(FVector(0.000004, 53.999992, 10.000000));
  86.     VR_MuzzleLocation->SetRelativeRotation(FRotator(0.0f, 90.0f, 0.0f));        // Counteract the rotation of the VR gun model.
  87.  
  88.     // Uncomment the following line to turn motion controllers on by default:
  89.     //bUsingMotionControllers = true;
  90. }
  91.  
  92. void AFPPCharacter::BeginPlay()
  93. {
  94.     // Call the base class  
  95.     Super::BeginPlay();
  96.  
  97.     //Attach gun mesh component to Skeleton, doing it here because the skeleton is not yet created in the constructor
  98.     FP_Gun->AttachToComponent(Mesh1P, FAttachmentTransformRules(EAttachmentRule::SnapToTarget, true), TEXT("GripPoint"));
  99.  
  100.     // Show or hide the two versions of the gun based on whether or not we're using motion controllers.
  101.     if (bUsingMotionControllers)
  102.     {
  103.         VR_Gun->SetHiddenInGame(false, true);
  104.         Mesh1P->SetHiddenInGame(true, true);
  105.     }
  106.     else
  107.     {
  108.         VR_Gun->SetHiddenInGame(true, true);
  109.         Mesh1P->SetHiddenInGame(false, true);
  110.     }
  111. }
  112.  
  113. //////////////////////////////////////////////////////////////////////////
  114. // Input
  115.  
  116. void AFPPCharacter::SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent)
  117. {
  118.     // set up gameplay key bindings
  119.     check(PlayerInputComponent);
  120.  
  121.     PlayerInputComponent->BindAction("Jump", IE_Pressed, this, &AFPPCharacter::Jump);
  122.     PlayerInputComponent->BindAction("Jump", IE_Released, this, &AFPPCharacter::StopJumping);
  123.  
  124.     //InputComponent->BindTouch(EInputEvent::IE_Pressed, this, &AFPPCharacter::TouchStarted);
  125.     if (EnableTouchscreenMovement(PlayerInputComponent) == false)
  126.     {
  127.         PlayerInputComponent->BindAction("Fire", IE_Pressed, this, &AFPPCharacter::OnFire);
  128.     }
  129.  
  130.     PlayerInputComponent->BindAction("ResetVR", IE_Pressed, this, &AFPPCharacter::OnResetVR);
  131.    
  132.     //Sprinting
  133.     PlayerInputComponent->BindAction("Sprint", IE_Pressed, this, &AFPPCharacter::BeginSprint);
  134.     PlayerInputComponent->BindAction("Sprint", IE_Released, this, &AFPPCharacter::EndSprint);
  135.    
  136.     //Crouching
  137.     PlayerInputComponent->BindAction("Crouch", IE_Pressed, this, &AFPPCharacter::BeginCrouch);
  138.     PlayerInputComponent->BindAction("Crouch", IE_Released, this, &AFPPCharacter::EndCrouch);
  139.  
  140.     PlayerInputComponent->BindAxis("MoveForward", this, &AFPPCharacter::MoveForward);
  141.     PlayerInputComponent->BindAxis("MoveRight", this, &AFPPCharacter::MoveRight);
  142.  
  143.     // We have 2 versions of the rotation bindings to handle different kinds of devices differently
  144.     // "turn" handles devices that provide an absolute delta, such as a mouse.
  145.     // "turnrate" is for devices that we choose to treat as a rate of change, such as an analog joystick
  146.     PlayerInputComponent->BindAxis("Turn", this, &APawn::AddControllerYawInput);
  147.     PlayerInputComponent->BindAxis("TurnRate", this, &AFPPCharacter::TurnAtRate);
  148.     PlayerInputComponent->BindAxis("LookUp", this, &APawn::AddControllerPitchInput);
  149.     PlayerInputComponent->BindAxis("LookUpRate", this, &AFPPCharacter::LookUpAtRate);
  150. }
  151.  
  152. void AFPPCharacter::OnFire()
  153. {
  154.     // try and fire a projectile
  155.     if (ProjectileClass != NULL)
  156.     {
  157.         UWorld* const World = GetWorld();
  158.         if (World != NULL)
  159.         {
  160.             if (bUsingMotionControllers)
  161.             {
  162.                 const FRotator SpawnRotation = VR_MuzzleLocation->GetComponentRotation();
  163.                 const FVector SpawnLocation = VR_MuzzleLocation->GetComponentLocation();
  164.                 World->SpawnActor<AFPPProjectile>(ProjectileClass, SpawnLocation, SpawnRotation);
  165.             }
  166.             else
  167.             {
  168.                 const FRotator SpawnRotation = GetControlRotation();
  169.                 // MuzzleOffset is in camera space, so transform it to world space before offsetting from the character location to find the final muzzle position
  170.                 const FVector SpawnLocation = ((FP_MuzzleLocation != nullptr) ? FP_MuzzleLocation->GetComponentLocation() : GetActorLocation()) + SpawnRotation.RotateVector(GunOffset);
  171.  
  172.                 //Set Spawn Collision Handling Override
  173.                 FActorSpawnParameters ActorSpawnParams;
  174.                 ActorSpawnParams.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AdjustIfPossibleButDontSpawnIfColliding;
  175.  
  176.                 // spawn the projectile at the muzzle
  177.                 World->SpawnActor<AFPPProjectile>(ProjectileClass, SpawnLocation, SpawnRotation, ActorSpawnParams);
  178.             }
  179.         }
  180.     }
  181.  
  182.     // try and play the sound if specified
  183.     if (FireSound != NULL)
  184.     {
  185.         UGameplayStatics::PlaySoundAtLocation(this, FireSound, GetActorLocation());
  186.     }
  187.  
  188.     // try and play a firing animation if specified
  189.     if (FireAnimation != NULL)
  190.     {
  191.         // Get the animation object for the arms mesh
  192.         UAnimInstance* AnimInstance = Mesh1P->GetAnimInstance();
  193.         if (AnimInstance != NULL)
  194.         {
  195.             AnimInstance->Montage_Play(FireAnimation, 1.f);
  196.         }
  197.     }
  198. }
  199.  
  200. void AFPPCharacter::OnResetVR()
  201. {
  202.     UHeadMountedDisplayFunctionLibrary::ResetOrientationAndPosition();
  203. }
  204.  
  205. void AFPPCharacter::BeginTouch(const ETouchIndex::Type FingerIndex, const FVector Location)
  206. {
  207.     if (TouchItem.bIsPressed == true)
  208.     {
  209.         return;
  210.     }
  211.     TouchItem.bIsPressed = true;
  212.     TouchItem.FingerIndex = FingerIndex;
  213.     TouchItem.Location = Location;
  214.     TouchItem.bMoved = false;
  215. }
  216.  
  217. void AFPPCharacter::EndTouch(const ETouchIndex::Type FingerIndex, const FVector Location)
  218. {
  219.     if (TouchItem.bIsPressed == false)
  220.     {
  221.         return;
  222.     }
  223.     if ((FingerIndex == TouchItem.FingerIndex) && (TouchItem.bMoved == false))
  224.     {
  225.         OnFire();
  226.     }
  227.     TouchItem.bIsPressed = false;
  228. }
  229.  
  230. //Commenting this section out to be consistent with FPS BP template.
  231. //This allows the user to turn without using the right virtual joystick
  232.  
  233. //void AFPPCharacter::TouchUpdate(const ETouchIndex::Type FingerIndex, const FVector Location)
  234. //{
  235. //  if ((TouchItem.bIsPressed == true) && (TouchItem.FingerIndex == FingerIndex))
  236. //  {
  237. //      if (TouchItem.bIsPressed)
  238. //      {
  239. //          if (GetWorld() != nullptr)
  240. //          {
  241. //              UGameViewportClient* ViewportClient = GetWorld()->GetGameViewport();
  242. //              if (ViewportClient != nullptr)
  243. //              {
  244. //                  FVector MoveDelta = Location - TouchItem.Location;
  245. //                  FVector2D ScreenSize;
  246. //                  ViewportClient->GetViewportSize(ScreenSize);
  247. //                  FVector2D ScaledDelta = FVector2D(MoveDelta.X, MoveDelta.Y) / ScreenSize;
  248. //                  if (FMath::Abs(ScaledDelta.X) >= 4.0 / ScreenSize.X)
  249. //                  {
  250. //                      TouchItem.bMoved = true;
  251. //                      float Value = ScaledDelta.X * BaseTurnRate;
  252. //                      AddControllerYawInput(Value);
  253. //                  }
  254. //                  if (FMath::Abs(ScaledDelta.Y) >= 4.0 / ScreenSize.Y)
  255. //                  {
  256. //                      TouchItem.bMoved = true;
  257. //                      float Value = ScaledDelta.Y * BaseTurnRate;
  258. //                      AddControllerPitchInput(Value);
  259. //                  }
  260. //                  TouchItem.Location = Location;
  261. //              }
  262. //              TouchItem.Location = Location;
  263. //          }
  264. //      }
  265. //  }
  266. //}
  267.  
  268. void AFPPCharacter::MoveForward(float Value)
  269. {
  270.     if (Value != 0.0f)
  271.     {
  272.         // add movement in that direction
  273.         AddMovementInput(GetActorForwardVector(), Value);
  274.     }
  275. }
  276.  
  277. void AFPPCharacter::MoveRight(float Value)
  278. {
  279.     if (Value != 0.0f)
  280.     {
  281.         // add movement in that direction
  282.         AddMovementInput(GetActorRightVector(), Value);
  283.     }
  284. }
  285.  
  286. void AFPPCharacter::TurnAtRate(float Rate)
  287. {
  288.     // calculate delta for this frame from the rate information
  289.     AddControllerYawInput(Rate * BaseTurnRate * GetWorld()->GetDeltaSeconds());
  290. }
  291.  
  292. void AFPPCharacter::LookUpAtRate(float Rate)
  293. {
  294.     // calculate delta for this frame from the rate information
  295.     AddControllerPitchInput(Rate * BaseLookUpRate * GetWorld()->GetDeltaSeconds());
  296. }
  297.  
  298. bool AFPPCharacter::EnableTouchscreenMovement(class UInputComponent* PlayerInputComponent)
  299. {
  300.     bool bResult = false;
  301.     if (FPlatformMisc::GetUseVirtualJoysticks() || GetDefault<UInputSettings>()->bUseMouseForTouch)
  302.     {
  303.         bResult = true;
  304.         PlayerInputComponent->BindTouch(EInputEvent::IE_Pressed, this, &AFPPCharacter::BeginTouch);
  305.         PlayerInputComponent->BindTouch(EInputEvent::IE_Released, this, &AFPPCharacter::EndTouch);
  306.  
  307.         //Commenting this out to be more consistent with FPS BP template.
  308.         //PlayerInputComponent->BindTouch(EInputEvent::IE_Repeat, this, &AFPPCharacter::TouchUpdate);
  309.     }
  310.     return bResult;
  311. }
  312.  
  313. void AFPPCharacter::BeginSprint()
  314. {
  315.     print("Sprint");
  316.     bool bIsSprinting = true;
  317. }
  318.  
  319. void AFPPCharacter::EndSprint()
  320. {
  321.     print("No Sprint");
  322.     bool bIsSprinting = false;
  323. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement