presidents-brigade/Source/PresidentsBrigade/BasePawn.cpp
2022-11-03 22:12:47 -07:00

70 lines
2.2 KiB
C++
Executable File

// Fill out your copyright notice in the Description page of Project Settings.
#include "BasePawn.h"
#include "Camera/CameraComponent.h"
#include "Components/StaticMeshComponent.h"
#include "GameFramework/FloatingPawnMovement.h"
// Sets default values
ABasePawn::ABasePawn(const FObjectInitializer &object_initializer):
APawn(object_initializer)
{
// Create default components.
root_component = object_initializer.CreateDefaultSubobject<USceneComponent>(this, FName("Root"));
main_camera = object_initializer.CreateDefaultSubobject<UCameraComponent>(this, FName("MainCamera"));
player_mesh = object_initializer.CreateDefaultSubobject<UStaticMeshComponent>(this, FName("PlayerMesh"));
movement_component = object_initializer.CreateDefaultSubobject<UFloatingPawnMovement>(this, FName("MovementComponent"));
// Setup component hierarchy.
RootComponent = root_component;
main_camera->SetupAttachment(root_component);
player_mesh->SetupAttachment(root_component);
movement_component->UpdatedComponent = player_mesh;
PrimaryActorTick.bCanEverTick = true;
}
// Called when the game starts or when spawned
void ABasePawn::BeginPlay()
{
Super::BeginPlay();
}
UPawnMovementComponent* ABasePawn::GetMovementComponent() const
{
return movement_component;
}
// Called every frame
void ABasePawn::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
// Called to bind functionality to input
void ABasePawn::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
PlayerInputComponent->BindAxis("MoveForward", this, &ABasePawn::handle_move_forward);
PlayerInputComponent->BindAxis("MoveRight", this, &ABasePawn::handle_move_right);
}
void ABasePawn::handle_move_forward(float axis)
{
FRotator rotation = Controller->GetControlRotation();
FRotator yaw(0.0f, rotation.Yaw, 0.0f);
FVector forward = FRotationMatrix(yaw).GetUnitAxis(EAxis::X);
AddMovementInput(forward, axis);
}
void ABasePawn::handle_move_right(float axis)
{
FRotator rotation = Controller->GetControlRotation();
FRotator yaw(0.0f, rotation.Yaw, 0.0f);
FVector right = FRotationMatrix(yaw).GetUnitAxis(EAxis::Y);
AddMovementInput(right, axis);
}