daily pastebin goal
40%
SHARE
TWEET

Untitled

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