Advertisement
Guest User

Untitled

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