#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(TEXT("ShipMesh")); RootComponent = ShipMesh; // Create and setup the camera spring arm CameraSpringArm = CreateDefaultSubobject(TEXT("CameraSpringArm")); CameraSpringArm->SetupAttachment(RootComponent); CameraSpringArm->TargetArmLength = 400.0f; CameraSpringArm->bEnableCameraLag = true; CameraSpringArm->CameraLagSpeed = 3.0f; // Create and setup the camera Camera = CreateDefaultSubobject(TEXT("Camera")); Camera->SetupAttachment(CameraSpringArm, USpringArmComponent::SocketName); } void ASpaceshipPawn::BeginPlay() { Super::BeginPlay(); // Add Input Mapping Context if (APlayerController* PlayerController = Cast(Controller)) { if (UEnhancedInputLocalPlayerSubsystem* Subsystem = ULocalPlayer::GetSubsystem(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(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(); CurrentVelocity = FVector( MovementVector.X * MovementSpeed, MovementVector.Y * MovementSpeed, 0.0f ); } void ASpaceshipPawn::Look(const FInputActionValue& Value) { const FVector2D LookAxisVector = Value.Get(); // 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::StaticClass(), SpawnLocation, SpawnRotation, SpawnParams ); } }