Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "EnterAurora.h"
- #include "AuroraBaseCharacter.h"
- // Sets default values
- AAuroraBaseCharacter::AAuroraBaseCharacter(const FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer)
- {
- // Set this character to call Tick() every frame. You can turn this off to improve performance if you don't need it.
- PrimaryActorTick.bCanEverTick = true;
- // Set size for player capsule
- GetCapsuleComponent()->InitCapsuleSize(42.f, 96.0f);
- // Don't rotate character to camera direction
- bUseControllerRotationPitch = false;
- bUseControllerRotationYaw = false;
- bUseControllerRotationRoll = false;
- // Configure character movement
- GetCharacterMovement()->bOrientRotationToMovement = true; // Rotate character to moving direction
- GetCharacterMovement()->RotationRate = FRotator(0.f, 640.f, 0.f);
- GetCharacterMovement()->bConstrainToPlane = true;
- GetCharacterMovement()->bSnapToPlaneAtStart = true;
- // Create a camera boom...
- cameraBoom = ObjectInitializer.CreateDefaultSubobject<USpringArmComponent>(this, TEXT("Camera Boom"));
- cameraBoom->AttachTo(RootComponent);
- cameraBoom->bAbsoluteRotation = true; // Don't want arm to rotate when character does
- cameraBoom->TargetArmLength = 1000.0f;
- cameraBoom->RelativeRotation = FRotator(-90.f, 0.f, 0.f);
- cameraBoom->bDoCollisionTest = false; // Don't want to pull camera in when it collides with level
- // Create a camera...
- mainCamera = ObjectInitializer.CreateDefaultSubobject<UCameraComponent>(this, TEXT("Main Camera"));
- mainCamera->AttachTo(cameraBoom, USpringArmComponent::SocketName);
- mainCamera->bUsePawnControlRotation = false; // Camera does not rotate relative to arm
- }
- // Called when the game starts or when spawned
- void AAuroraBaseCharacter::BeginPlay()
- {
- Super::BeginPlay();
- // Set Health
- currentHealth = maxHealth;
- // Set Speed
- GetCharacterMovement()->MaxWalkSpeed = moveSpeed;
- // Spawn Weapon
- OnSpawnWeapon(primaryClass, primaryAttachSocket);
- }
- // Called every frame
- void AAuroraBaseCharacter::Tick(float DeltaTime)
- {
- Super::Tick(DeltaTime);
- // Set Rotation
- OnCharacterRotation();
- }
- // Called to bind functionality to input
- void AAuroraBaseCharacter::SetupPlayerInputComponent(class UInputComponent* InputComponent)
- {
- Super::SetupPlayerInputComponent(InputComponent);
- // Axis Bindings
- InputComponent->BindAxis("MoveForward", this, &AAuroraBaseCharacter::OnMoveForward);
- InputComponent->BindAxis("MoveSideways", this, &AAuroraBaseCharacter::OnMoveSideways);
- InputComponent->BindAxis("RotateX", this, &AAuroraBaseCharacter::GetJoystickXValue);
- InputComponent->BindAxis("RotateY", this, &AAuroraBaseCharacter::GetJoystickYValue);
- }
- void AAuroraBaseCharacter::OnMoveForward(float Rate)
- {
- if ((Controller != NULL) && (Rate != 0.0f))
- {
- // find out which way is forward
- FRotator Rotation = Controller->GetControlRotation();
- // Limit pitch when walking or falling
- if (GetCharacterMovement()->IsMovingOnGround() || GetCharacterMovement()->IsFalling())
- {
- Rotation.Pitch = 0.0f;
- }
- // add movement in that direction
- const FVector Direction = FRotationMatrix(Rotation).GetScaledAxis(EAxis::X);
- AddMovementInput(Direction, Rate);
- }
- }
- void AAuroraBaseCharacter::OnMoveSideways(float Rate)
- {
- if ((Controller != NULL) && (Rate != 0.0f))
- {
- // find out which way is right
- const FRotator Rotation = Controller->GetControlRotation();
- const FVector Direction = FRotationMatrix(Rotation).GetScaledAxis(EAxis::Y);
- // add movement in that direction
- AddMovementInput(Direction, Rate);
- }
- }
- void AAuroraBaseCharacter::GetJoystickXValue(float Val)
- {
- // Push Feed to variable
- joystickXValue = Val;
- GEngine->AddOnScreenDebugMessage(-1, 2.0f, FColor::Yellow, TEXT("Val: " + FString::SanitizeFloat(Val)));
- if (Val > 0.02f || Val < -0.02f)
- {
- currentWeapon->OnWeaponFireStart();
- }
- }
- void AAuroraBaseCharacter::GetJoystickYValue(float Val)
- {
- // Push Feed to variable
- joystickYValue = Val;
- GEngine->AddOnScreenDebugMessage(-1, 2.0f, FColor::Yellow, TEXT("Val: " + FString::SanitizeFloat(Val)));
- if (Val > 0.02f || Val < -0.02f)
- {
- currentWeapon->OnWeaponFireStart();
- }
- }
- void AAuroraBaseCharacter::OnCharacterRotation()
- {
- // We must flip the input to opposite
- joystickXValue = joystickXValue * -1;
- joystickYValue = joystickYValue * -1;
- // Create rotation Vector
- FVector joystickVector = FVector(joystickXValue, joystickYValue, 0.0f);
- // Get Normalized version of the Vector
- joystickNormalized = joystickVector.GetSafeNormal();
- // Create Target Rotation
- FRotator targetRotation = joystickNormalized.Rotation();
- // Create final rotation
- FRotator finalRotation = FRotator(targetRotation.Pitch, targetRotation.Yaw + -90.0f, targetRotation.Roll);
- // Set Rotation if Joystick is above dead zone
- if (joystickNormalized.Size() > 0.2f)
- {
- this->SetActorRotation(finalRotation);
- }
- }
- void AAuroraBaseCharacter::OnSpawnWeapon(TSubclassOf<AAuroraBaseWeapon> weapon, FName attachSocket)
- {
- // Create Spawn Params
- FActorSpawnParameters spawnParams;
- spawnParams.Owner = this;
- spawnParams.Instigator = Instigator;
- // Spawn Weapon
- AAuroraBaseWeapon* spawnWeapon = GetWorld()->SpawnActor<AAuroraBaseWeapon>(weapon, FVector(0, 0, 0), FRotator(0, 0, 0), spawnParams);
- // Set it as current
- currentWeapon = spawnWeapon;
- // Attach to character
- spawnWeapon->AttachRootComponentTo(this->GetMesh(), attachSocket, EAttachLocation::SnapToTarget, false);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement