Guest User

Character CPP

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