unrealgame4/Source/ThirdPerson/Private/ThirdPersonCharacter.cpp

152 lines
5.1 KiB
C++

// All content (c) Shaun Reed 2021, all rights reserved
// Custom includes
#include "ThirdPersonCharacter.h"
#include "ActorSpawner.h" // Fireball spawner object
#include "BallActor.h" // Fireball object
// Engine includes
#include "Kismet/GameplayStatics.h" // For spawning fireball static mesh
#include "Camera/CameraComponent.h"
#include "GameFramework/SpringArmComponent.h"
#include "GameFramework/CharacterMovementComponent.h"
// Sets default values
AThirdPersonCharacter::AThirdPersonCharacter()
{
// 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;
// Instantiate required components for the player character
SpringArmComp = CreateDefaultSubobject<USpringArmComponent>(TEXT("SpringArmComp"));
CameraComp = CreateDefaultSubobject<UCameraComponent>(TEXT("CameraComp"));
// Set location and rotation of character mesh
GetMesh()->SetRelativeLocationAndRotation(FVector(0.0f, 0.0f, 0.0f),
FQuat(FRotator(0.0f, -90.0f, 0.0f)));
// Set up character component hierarchy
SpringArmComp->SetupAttachment(this->GetRootComponent()); // Attach spring arm to mesh
CameraComp->SetupAttachment(SpringArmComp); // Attach camera to spring arm
//CameraComp->SetRelativeLocation(0.0f, 0.0f, )
// Initialize variables
SpringArmComp->bUsePawnControlRotation = true;
// Setup ACharacter class movement variables for desired movement
GetCharacterMovement()->bOrientRotationToMovement = true;
GetCharacterMovement()->bUseControllerDesiredRotation = true;
GetCharacterMovement()->bIgnoreBaseRotation = true;
}
// Called when the game starts or when spawned
void AThirdPersonCharacter::BeginPlay()
{
Super::BeginPlay();
}
void AThirdPersonCharacter::SpawnActors()
{
AActor* ActorSpawnerToFind = UGameplayStatics::GetActorOfClass(GetWorld(), AActorSpawner::StaticClass());
AActorSpawner* ActorSpawnerReference = Cast<AActorSpawner>(ActorSpawnerToFind);
if (ActorSpawnerReference)
{
ActorSpawnerReference->SpawnActor();
}
}
void AThirdPersonCharacter::DestroyActors()
{
TArray<AActor*> FoundActors;
UGameplayStatics::GetAllActorsOfClass(GetWorld(), ABallActor::StaticClass(), FoundActors);
for (AActor* ActorFound : FoundActors)
{
ActorFound->Destroy();
}
}
void AThirdPersonCharacter::MoveForward(float InputAxis)
{
if ((Controller != nullptr) && (InputAxis != 0.0f))
{
// Find which direction is forward relative to the player's current rotation
const FRotator Rotation = Controller->GetControlRotation();
const FRotator YawRotation(0.0f, Rotation.Yaw, 0.0f);
// Get forward vector from YawRotation and apply movement from InputAxis
const FVector Direction = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X);
AddMovementInput(Direction, InputAxis);
}
}
void AThirdPersonCharacter::MoveRight(float InputAxis)
{
if ((Controller != nullptr) && (InputAxis != 0.0f))
{
const FRotator Rotation = Controller->GetControlRotation();
const FRotator YawRotation(0.0f, Rotation.Yaw, 0.0f);
const FVector Direction = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::Y);
AddMovementInput(Direction, InputAxis);
}
}
void AThirdPersonCharacter::BeginSprint()
{
GetCharacterMovement()->MaxWalkSpeed = 1000.0f;
}
void AThirdPersonCharacter::EndSprint()
{
GetCharacterMovement()->MaxWalkSpeed = 600.0f;
}
void AThirdPersonCharacter::BeginCrouch()
{
Crouch();
}
void AThirdPersonCharacter::EndCrouch()
{
UnCrouch();
}
// Called every frame
void AThirdPersonCharacter::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
// Called to bind functionality to input
void AThirdPersonCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
// Set up callbacks for project input configuration (Edit->Project Settings-Input)
// Movement and camera control
PlayerInputComponent->BindAxis("MoveForward", this, &AThirdPersonCharacter::MoveForward);
PlayerInputComponent->BindAxis("MoveRight", this, &AThirdPersonCharacter::MoveRight);
PlayerInputComponent->BindAxis("Turn", this, &AThirdPersonCharacter::AddControllerYawInput);
PlayerInputComponent->BindAxis("TurnRate", this, &AThirdPersonCharacter::AddControllerYawInput);
PlayerInputComponent->BindAxis("LookUp", this, &AThirdPersonCharacter::AddControllerPitchInput);
PlayerInputComponent->BindAxis("LookUpRate", this, &AThirdPersonCharacter::AddControllerPitchInput);
PlayerInputComponent->BindAction("Jump", IE_Pressed, this, &ACharacter::Jump);
PlayerInputComponent->BindAction("Jump", IE_Released, this, &ACharacter::StopJumping);
PlayerInputComponent->BindAction("Crouch", IE_Pressed, this, &AThirdPersonCharacter::BeginCrouch);
PlayerInputComponent->BindAction("Crouch", IE_Released, this, &AThirdPersonCharacter::EndCrouch);
PlayerInputComponent->BindAction("Sprint", IE_Pressed, this, &AThirdPersonCharacter::BeginSprint);
PlayerInputComponent->BindAction("Sprint", IE_Released, this, &AThirdPersonCharacter::EndSprint);
// Player actions
PlayerInputComponent->BindAction("SpawnActors", IE_Pressed, this, &AThirdPersonCharacter::SpawnActors);
PlayerInputComponent->BindAction("DestroyActors", IE_Pressed, this, &AThirdPersonCharacter::DestroyActors);
}