99 lines
3.2 KiB
C++
99 lines
3.2 KiB
C++
#include "SpaceshipPawn.h"
|
|
#include "GameFramework/SpringArmComponent.h"
|
|
#include "Camera/CameraComponent.h"
|
|
#include "Components/StaticMeshComponent.h"
|
|
#include "SpaceshipProjectile.h"
|
|
|
|
ASpaceshipPawn::ASpaceshipPawn()
|
|
{
|
|
PrimaryActorTick.bCanEverTick = true;
|
|
|
|
// Create and setup the ship's mesh
|
|
ShipMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("ShipMesh"));
|
|
RootComponent = ShipMesh;
|
|
|
|
// Create and setup the camera spring arm
|
|
CameraSpringArm = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraSpringArm"));
|
|
CameraSpringArm->SetupAttachment(RootComponent);
|
|
CameraSpringArm->TargetArmLength = 400.0f;
|
|
CameraSpringArm->bEnableCameraLag = true;
|
|
CameraSpringArm->CameraLagSpeed = 3.0f;
|
|
|
|
// Create and setup the camera
|
|
Camera = CreateDefaultSubobject<UCameraComponent>(TEXT("Camera"));
|
|
Camera->SetupAttachment(CameraSpringArm, USpringArmComponent::SocketName);
|
|
}
|
|
|
|
void ASpaceshipPawn::BeginPlay()
|
|
{
|
|
Super::BeginPlay();
|
|
}
|
|
|
|
void ASpaceshipPawn::Tick(float DeltaTime)
|
|
{
|
|
Super::Tick(DeltaTime);
|
|
|
|
if (!CurrentVelocity.IsZero())
|
|
{
|
|
FVector NewLocation = GetActorLocation() + (CurrentVelocity * DeltaTime);
|
|
SetActorLocation(NewLocation);
|
|
}
|
|
}
|
|
|
|
void ASpaceshipPawn::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
|
|
{
|
|
Super::SetupPlayerInputComponent(PlayerInputComponent);
|
|
|
|
PlayerInputComponent->BindAxis("MoveForward", this, &ASpaceshipPawn::MoveForward);
|
|
PlayerInputComponent->BindAxis("MoveRight", this, &ASpaceshipPawn::MoveRight);
|
|
PlayerInputComponent->BindAxis("Turn", this, &ASpaceshipPawn::Turn);
|
|
PlayerInputComponent->BindAxis("LookUp", this, &ASpaceshipPawn::LookUp);
|
|
PlayerInputComponent->BindAction("Fire", IE_Pressed, this, &ASpaceshipPawn::Fire);
|
|
}
|
|
|
|
void ASpaceshipPawn::MoveForward(float Value)
|
|
{
|
|
CurrentVelocity.X = FMath::Clamp(Value, -1.0f, 1.0f) * MovementSpeed;
|
|
}
|
|
|
|
void ASpaceshipPawn::MoveRight(float Value)
|
|
{
|
|
CurrentVelocity.Y = FMath::Clamp(Value, -1.0f, 1.0f) * MovementSpeed;
|
|
}
|
|
|
|
void ASpaceshipPawn::Turn(float Value)
|
|
{
|
|
AddActorLocalRotation(FRotator(0.0f, Value * RotationSpeed * GetWorld()->GetDeltaSeconds(), 0.0f));
|
|
}
|
|
|
|
void ASpaceshipPawn::LookUp(float Value)
|
|
{
|
|
float NewPitch = CameraSpringArm->GetRelativeRotation().Pitch + Value * RotationSpeed * GetWorld()->GetDeltaSeconds();
|
|
NewPitch = FMath::Clamp(NewPitch, -80.0f, 80.0f);
|
|
CameraSpringArm->SetRelativeRotation(FRotator(NewPitch, 0.0f, 0.0f));
|
|
}
|
|
|
|
void ASpaceshipPawn::Fire()
|
|
{
|
|
UWorld* World = GetWorld();
|
|
if (World)
|
|
{
|
|
FVector SpawnLocation = ShipMesh->GetSocketLocation(TEXT("ProjectileSpawn"));
|
|
if (SpawnLocation == FVector::ZeroVector)
|
|
{
|
|
SpawnLocation = GetActorLocation() + (GetActorForwardVector() * 100.0f);
|
|
}
|
|
|
|
FRotator SpawnRotation = GetActorRotation();
|
|
FActorSpawnParameters SpawnParams;
|
|
SpawnParams.Owner = this;
|
|
SpawnParams.Instigator = GetInstigator();
|
|
|
|
ASpaceshipProjectile* Projectile = World->SpawnActor<ASpaceshipProjectile>(
|
|
ASpaceshipProjectile::StaticClass(),
|
|
SpawnLocation,
|
|
SpawnRotation,
|
|
SpawnParams
|
|
);
|
|
}
|
|
} |