Advertisement
Guest User

TWGameCharacter.cpp

a guest
Dec 2nd, 2015
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.52 KB | None | 0 0
  1. // Copyright 1998-2015 Epic Games, Inc. All Rights Reserved.
  2.  
  3. #include "TWGame.h"
  4. #include "TWGameCharacter.h"
  5.  
  6. //////////////////////////////////////////////////////////////////////////
  7. // ATWGameCharacter
  8.  
  9. ATWGameCharacter::ATWGameCharacter()
  10. {
  11.     // Set size for collision capsule
  12.     GetCapsuleComponent()->InitCapsuleSize(42.f, 96.0f);
  13.  
  14.     // set our turn rates for input
  15.     BaseTurnRate = 45.f;
  16.     BaseLookUpRate = 45.f;
  17.  
  18.     // Don't rotate when the controller rotates. Let that just affect the camera.
  19.     bUseControllerRotationPitch = false;
  20.     bUseControllerRotationYaw = true;
  21.     bUseControllerRotationRoll = true;
  22.  
  23.     // Configure character movement
  24.     GetCharacterMovement()->bOrientRotationToMovement = true; // Character moves in the direction of input...  
  25.     GetCharacterMovement()->RotationRate = FRotator(0.0f, 540.0f, 0.0f); // ...at this rotation rate
  26.     GetCharacterMovement()->JumpZVelocity = 600.f;
  27.     GetCharacterMovement()->AirControl = 0.2f;
  28.  
  29.     // Create a camera boom (pulls in towards the player if there is a collision)
  30.     CameraBoom = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraBoom"));
  31.     CameraBoom->AttachTo(Mesh, "sHead");
  32.     CameraBoom->TargetArmLength = 0.0f; // The camera follows at this distance behind the character
  33.     CameraBoom->bUsePawnControlRotation = true; // Rotate the arm based on the controller
  34.  
  35.     // Create a follow camera
  36.     FollowCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("FollowCamera"));
  37.     FollowCamera->AttachTo(CameraBoom, USpringArmComponent::SocketName); // Attach the camera to the end of the boom and let the boom adjust to match the controller orientation
  38.     FollowCamera->bUsePawnControlRotation = false; // Camera does not rotate relative to arm
  39.  
  40.     // Note: The skeletal mesh and anim blueprint references on the Mesh component (inherited from Character)
  41.     // are set in the derived blueprint asset named MyCharacter (to avoid direct content references in C++)
  42. }
  43.  
  44.  
  45. // Free view
  46. void TWGameCharacter::BeginPlay()
  47. {
  48.     bIsInFreeView = false;
  49. }
  50.  
  51. void TWGameCharacter::Tick(float DeltaTime)
  52. {
  53.     if (bIsInFreeView)
  54.     {
  55.         bUseControllerRotationPitch = false;
  56.         bUseControllerRotationYaw = false;
  57.         bUseControllerRotationRoll = false;
  58.     }
  59.     else
  60.     {
  61.         bUseControllerRotationPitch = false;
  62.         bUseControllerRotationYaw = true;
  63.         bUseControllerRotationRoll = true;
  64.     }
  65.  
  66. }
  67.  
  68. //////////////////////////////////////////////////////////////////////////
  69. // Input
  70.  
  71. void ATWGameCharacter::SetupPlayerInputComponent(class UInputComponent* InputComponent)
  72. {
  73.     // Set up gameplay key bindings
  74.     check(InputComponent);
  75.     InputComponent->BindAction("Jump", IE_Pressed, this, &ACharacter::Jump);
  76.     InputComponent->BindAction("Jump", IE_Released, this, &ACharacter::StopJumping);
  77.  
  78.     InputComponent->BindAxis("MoveForward", this, &ATWGameCharacter::MoveForward);
  79.     InputComponent->BindAxis("MoveRight", this, &ATWGameCharacter::MoveRight);
  80.  
  81.     // We have 2 versions of the rotation bindings to handle different kinds of devices differently
  82.     // "turn" handles devices that provide an absolute delta, such as a mouse.
  83.     // "turnrate" is for devices that we choose to treat as a rate of change, such as an analog joystick
  84.     InputComponent->BindAxis("Turn", this, &APawn::AddControllerYawInput);
  85.     InputComponent->BindAxis("TurnRate", this, &ATWGameCharacter::TurnAtRate);
  86.     InputComponent->BindAxis("LookUp", this, &APawn::AddControllerPitchInput);
  87.     InputComponent->BindAxis("LookUpRate", this, &ATWGameCharacter::LookUpAtRate);
  88.     BIND_ACTION(InputComponent, "FreeView", IE_Pressed, &ATWGameCharacter::FreeViewStarted);
  89.     BIND_ACTION(InputComponent, "FreeView", IE_Released, &ATWGameCharacter::FreeViewStopped);
  90.  
  91.     // handle touch devices
  92.     InputComponent->BindTouch(IE_Pressed, this, &ATWGameCharacter::TouchStarted);
  93.     InputComponent->BindTouch(IE_Released, this, &ATWGameCharacter::TouchStopped);
  94. }
  95.  
  96. // Free View
  97. void TWGameCharacter::FreeViewStarted()
  98. {
  99.     bIsInFreeView = true;
  100. }
  101.  
  102. void TWGameCharacter::FreeViewStopped()
  103. {
  104.     bIsInFreeView = false;
  105. }
  106.  
  107.  
  108. void ATWGameCharacter::TouchStarted(ETouchIndex::Type FingerIndex, FVector Location)
  109. {
  110.     // jump, but only on the first touch
  111.     if (FingerIndex == ETouchIndex::Touch1)
  112.     {
  113.         Jump();
  114.     }
  115. }
  116.  
  117. void ATWGameCharacter::TouchStopped(ETouchIndex::Type FingerIndex, FVector Location)
  118. {
  119.     if (FingerIndex == ETouchIndex::Touch1)
  120.     {
  121.         StopJumping();
  122.     }
  123. }
  124.  
  125. void ATWGameCharacter::TurnAtRate(float Rate)
  126. {
  127.     // calculate delta for this frame from the rate information
  128.     AddControllerYawInput(Rate * BaseTurnRate * GetWorld()->GetDeltaSeconds());
  129. }
  130.  
  131. void ATWGameCharacter::LookUpAtRate(float Rate)
  132. {
  133.     // calculate delta for this frame from the rate information
  134.     AddControllerPitchInput(Rate * BaseLookUpRate * GetWorld()->GetDeltaSeconds());
  135. }
  136.  
  137. void ATWGameCharacter::MoveForward(float Value)
  138. {
  139.     if ((Controller != NULL) && (Value != 0.0f))
  140.     {
  141.         // find out which way is forward
  142.         const FRotator Rotation = Controller->GetControlRotation();
  143.         const FRotator YawRotation(0, Rotation.Yaw, 0);
  144.  
  145.         // get forward vector
  146.         const FVector Direction = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X);
  147.         AddMovementInput(Direction, Value);
  148.     }
  149. }
  150.  
  151. void ATWGameCharacter::MoveRight(float Value)
  152. {
  153.     if ( (Controller != NULL) && (Value != 0.0f) )
  154.     {
  155.         // find out which way is right
  156.         const FRotator Rotation = Controller->GetControlRotation();
  157.         const FRotator YawRotation(0, Rotation.Yaw, 0);
  158.    
  159.         // get right vector
  160.         const FVector Direction = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::Y);
  161.         // add movement in that direction
  162.         AddMovementInput(Direction, Value);
  163.     }
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement