Advertisement
Guest User

character

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