120 lines
4.0 KiB
C++
120 lines
4.0 KiB
C++
#include "SpaceshipPawn.h"
|
|
#include "GameFramework/SpringArmComponent.h"
|
|
#include "Camera/CameraComponent.h"
|
|
#include "Components/StaticMeshComponent.h"
|
|
#include "EnhancedInputComponent.h"
|
|
#include "EnhancedInputSubsystems.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();
|
|
|
|
// Add Input Mapping Context
|
|
if (APlayerController* PlayerController = Cast<APlayerController>(Controller))
|
|
{
|
|
if (UEnhancedInputLocalPlayerSubsystem* Subsystem = ULocalPlayer::GetSubsystem<UEnhancedInputLocalPlayerSubsystem>(PlayerController->GetLocalPlayer()))
|
|
{
|
|
Subsystem->AddMappingContext(DefaultMappingContext, 0);
|
|
}
|
|
}
|
|
}
|
|
|
|
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);
|
|
|
|
if (UEnhancedInputComponent* EnhancedInputComponent = CastChecked<UEnhancedInputComponent>(PlayerInputComponent))
|
|
{
|
|
// Binding the Movement action
|
|
EnhancedInputComponent->BindAction(MovementAction, ETriggerEvent::Triggered, this, &ASpaceshipPawn::Move);
|
|
|
|
// Binding the Look action
|
|
EnhancedInputComponent->BindAction(LookAction, ETriggerEvent::Triggered, this, &ASpaceshipPawn::Look);
|
|
|
|
// Binding the Fire action
|
|
EnhancedInputComponent->BindAction(FireAction, ETriggerEvent::Triggered, this, &ASpaceshipPawn::Fire);
|
|
}
|
|
}
|
|
|
|
void ASpaceshipPawn::Move(const FInputActionValue& Value)
|
|
{
|
|
const FVector2D MovementVector = Value.Get<FVector2D>();
|
|
|
|
CurrentVelocity = FVector(
|
|
MovementVector.X * MovementSpeed,
|
|
MovementVector.Y * MovementSpeed,
|
|
0.0f
|
|
);
|
|
}
|
|
|
|
void ASpaceshipPawn::Look(const FInputActionValue& Value)
|
|
{
|
|
const FVector2D LookAxisVector = Value.Get<FVector2D>();
|
|
|
|
// Yaw rotation (left/right)
|
|
AddActorLocalRotation(FRotator(0.0f, LookAxisVector.X * RotationSpeed * GetWorld()->GetDeltaSeconds(), 0.0f));
|
|
|
|
// Pitch rotation (up/down) for camera
|
|
float NewPitch = CameraSpringArm->GetRelativeRotation().Pitch +
|
|
LookAxisVector.Y * RotationSpeed * GetWorld()->GetDeltaSeconds();
|
|
NewPitch = FMath::Clamp(NewPitch, -80.0f, 80.0f);
|
|
CameraSpringArm->SetRelativeRotation(FRotator(NewPitch, 0.0f, 0.0f));
|
|
}
|
|
|
|
void ASpaceshipPawn::Fire(const FInputActionValue& Value)
|
|
{
|
|
// Implementation remains the same as before
|
|
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
|
|
);
|
|
}
|
|
} |