Advertisement
Guest User

Character CPP

a guest
Feb 21st, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.44 KB | None | 0 0
  1. #include "EnterAurora.h"
  2. #include "AuroraBaseCharacter.h"
  3.  
  4.  
  5. // Sets default values
  6. AAuroraBaseCharacter::AAuroraBaseCharacter(const FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer)
  7. {
  8.     // Set this character to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
  9.     PrimaryActorTick.bCanEverTick = true;
  10.  
  11.     // Set size for player capsule
  12.     GetCapsuleComponent()->InitCapsuleSize(42.f, 96.0f);
  13.  
  14.     // Don't rotate character to camera direction
  15.     bUseControllerRotationPitch = false;
  16.     bUseControllerRotationYaw = false;
  17.     bUseControllerRotationRoll = false;
  18.  
  19.     // Configure character movement
  20.     GetCharacterMovement()->bOrientRotationToMovement = true; // Rotate character to moving direction
  21.     GetCharacterMovement()->RotationRate = FRotator(0.f, 640.f, 0.f);
  22.     GetCharacterMovement()->bConstrainToPlane = true;
  23.     GetCharacterMovement()->bSnapToPlaneAtStart = true;
  24.  
  25.     // Create a camera boom...
  26.     cameraBoom = ObjectInitializer.CreateDefaultSubobject<USpringArmComponent>(this, TEXT("Camera Boom"));
  27.     cameraBoom->AttachTo(RootComponent);
  28.     cameraBoom->bAbsoluteRotation = true; // Don't want arm to rotate when character does
  29.     cameraBoom->TargetArmLength = 1000.0f;
  30.     cameraBoom->RelativeRotation = FRotator(-90.f, 0.f, 0.f);
  31.     cameraBoom->bDoCollisionTest = false; // Don't want to pull camera in when it collides with level
  32.  
  33.     // Create a camera...
  34.     mainCamera = ObjectInitializer.CreateDefaultSubobject<UCameraComponent>(this, TEXT("Main Camera"));
  35.     mainCamera->AttachTo(cameraBoom, USpringArmComponent::SocketName);
  36.     mainCamera->bUsePawnControlRotation = false; // Camera does not rotate relative to arm
  37. }
  38.  
  39. // Called when the game starts or when spawned
  40. void AAuroraBaseCharacter::BeginPlay()
  41. {
  42.     Super::BeginPlay();
  43.  
  44.     // Set Health
  45.     currentHealth = maxHealth;
  46.  
  47.     // Set Speed
  48.     GetCharacterMovement()->MaxWalkSpeed = moveSpeed;
  49.  
  50.     // Spawn Weapon
  51.     OnSpawnWeapon(primaryClass, primaryAttachSocket);
  52. }
  53.  
  54. // Called every frame
  55. void AAuroraBaseCharacter::Tick(float DeltaTime)
  56. {
  57.     Super::Tick(DeltaTime);
  58.  
  59.     // Set Rotation
  60.     OnCharacterRotation();
  61. }
  62.  
  63. // Called to bind functionality to input
  64. void AAuroraBaseCharacter::SetupPlayerInputComponent(class UInputComponent* InputComponent)
  65. {
  66.     Super::SetupPlayerInputComponent(InputComponent);
  67.  
  68.     // Axis Bindings
  69.     InputComponent->BindAxis("MoveForward", this, &AAuroraBaseCharacter::OnMoveForward);
  70.     InputComponent->BindAxis("MoveSideways", this, &AAuroraBaseCharacter::OnMoveSideways);
  71.     InputComponent->BindAxis("RotateX", this, &AAuroraBaseCharacter::GetJoystickXValue);
  72.     InputComponent->BindAxis("RotateY", this, &AAuroraBaseCharacter::GetJoystickYValue);
  73. }
  74.  
  75. void AAuroraBaseCharacter::OnMoveForward(float Rate)
  76. {
  77.     if ((Controller != NULL) && (Rate != 0.0f))
  78.     {
  79.         // find out which way is forward
  80.         FRotator Rotation = Controller->GetControlRotation();
  81.  
  82.         // Limit pitch when walking or falling
  83.         if (GetCharacterMovement()->IsMovingOnGround() || GetCharacterMovement()->IsFalling())
  84.         {
  85.             Rotation.Pitch = 0.0f;
  86.         }
  87.  
  88.         // add movement in that direction
  89.         const FVector Direction = FRotationMatrix(Rotation).GetScaledAxis(EAxis::X);
  90.         AddMovementInput(Direction, Rate);
  91.     }
  92. }
  93.  
  94. void AAuroraBaseCharacter::OnMoveSideways(float Rate)
  95. {
  96.     if ((Controller != NULL) && (Rate != 0.0f))
  97.     {
  98.         // find out which way is right
  99.         const FRotator Rotation = Controller->GetControlRotation();
  100.         const FVector Direction = FRotationMatrix(Rotation).GetScaledAxis(EAxis::Y);
  101.  
  102.         // add movement in that direction
  103.         AddMovementInput(Direction, Rate);
  104.     }
  105. }
  106.  
  107. void AAuroraBaseCharacter::GetJoystickXValue(float Val)
  108. {
  109.     // Push Feed to variable
  110.     joystickXValue = Val;
  111.  
  112.     GEngine->AddOnScreenDebugMessage(-1, 2.0f, FColor::Yellow, TEXT("Val: " + FString::SanitizeFloat(Val)));
  113.  
  114.     if (Val > 0.02f || Val < -0.02f)
  115.     {
  116.         currentWeapon->OnWeaponFireStart();
  117.     }
  118. }
  119.  
  120. void AAuroraBaseCharacter::GetJoystickYValue(float Val)
  121. {
  122.     // Push Feed to variable
  123.     joystickYValue = Val;
  124.  
  125.     GEngine->AddOnScreenDebugMessage(-1, 2.0f, FColor::Yellow, TEXT("Val: " + FString::SanitizeFloat(Val)));
  126.  
  127.     if (Val > 0.02f || Val < -0.02f)
  128.     {
  129.         currentWeapon->OnWeaponFireStart();
  130.     }
  131. }
  132.  
  133. void AAuroraBaseCharacter::OnCharacterRotation()
  134. {
  135.     // We must flip the input to opposite
  136.     joystickXValue = joystickXValue * -1;
  137.     joystickYValue = joystickYValue * -1;
  138.  
  139.     // Create rotation Vector
  140.     FVector joystickVector = FVector(joystickXValue, joystickYValue, 0.0f);
  141.  
  142.     // Get Normalized version of the Vector
  143.     joystickNormalized = joystickVector.GetSafeNormal();
  144.  
  145.     // Create Target Rotation
  146.     FRotator targetRotation = joystickNormalized.Rotation();
  147.  
  148.     // Create final rotation
  149.     FRotator finalRotation = FRotator(targetRotation.Pitch, targetRotation.Yaw + -90.0f, targetRotation.Roll);
  150.  
  151.     // Set Rotation if Joystick is above dead zone
  152.     if (joystickNormalized.Size() > 0.2f)
  153.     {
  154.         this->SetActorRotation(finalRotation);
  155.     }
  156. }
  157.  
  158. void AAuroraBaseCharacter::OnSpawnWeapon(TSubclassOf<AAuroraBaseWeapon> weapon, FName attachSocket)
  159. {
  160.     // Create Spawn Params
  161.     FActorSpawnParameters spawnParams;
  162.     spawnParams.Owner = this;
  163.     spawnParams.Instigator = Instigator;
  164.  
  165.     // Spawn Weapon
  166.     AAuroraBaseWeapon* spawnWeapon = GetWorld()->SpawnActor<AAuroraBaseWeapon>(weapon, FVector(0, 0, 0), FRotator(0, 0, 0), spawnParams);
  167.  
  168.     // Set it as current
  169.     currentWeapon = spawnWeapon;
  170.  
  171.     // Attach to character
  172.     spawnWeapon->AttachRootComponentTo(this->GetMesh(), attachSocket, EAttachLocation::SnapToTarget, false);
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement