Implement basic combat system

This commit is contained in:
2025-02-17 23:24:04 +05:30
parent 4b588e8667
commit 939e851c37
14 changed files with 293 additions and 112 deletions

View File

@@ -19,6 +19,10 @@ ASpaceshipPawn::ASpaceshipPawn()
ShipMesh->SetSimulatePhysics(false);
ShipMesh->SetEnableGravity(false);
// Create projectile spawn point
ProjectileSpawnPoint = CreateDefaultSubobject<USceneComponent>(TEXT("ProjectileSpawnPoint"));
ProjectileSpawnPoint->SetupAttachment(ShipMesh);
// Create camera spring arm
CameraSpringArm = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraSpringArm"));
CameraSpringArm->SetupAttachment(RootComponent);
@@ -46,12 +50,40 @@ ASpaceshipPawn::ASpaceshipPawn()
CurrentYaw = 0.0f;
TargetRotation = FRotator::ZeroRotator;
LastMouseDelta = FVector2D::ZeroVector;
if (ShipMesh)
{
ShipMesh->SetCollisionEnabled(ECollisionEnabled::QueryAndPhysics);
ShipMesh->SetCollisionObjectType(ECC_GameTraceChannel1); // This is the "Player" channel
ShipMesh->SetCollisionResponseToAllChannels(ECollisionResponse::ECR_Block);
ShipMesh->SetCollisionResponseToChannel(ECC_GameTraceChannel1, ECollisionResponse::ECR_Ignore);
ShipMesh->SetCollisionResponseToChannel(ECC_Pawn, ECollisionResponse::ECR_Ignore);
}
}
void ASpaceshipPawn::BeginPlay()
{
Super::BeginPlay();
// Debug messages for setup verification
if (GEngine)
{
if (ProjectileClass)
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Green, TEXT("ProjectileClass is set"));
else
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, TEXT("ProjectileClass is NOT set"));
if (ShootAction)
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Green, TEXT("ShootAction is set"));
else
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, TEXT("ShootAction is NOT set"));
if (ProjectileSpawnPoint)
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Green, TEXT("ProjectileSpawnPoint is set"));
else
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, TEXT("ProjectileSpawnPoint is NOT set"));
}
// Store player controller reference
PlayerControllerRef = Cast<APlayerController>(Controller);
@@ -217,8 +249,8 @@ void ASpaceshipPawn::SetupPlayerInputComponent(UInputComponent* PlayerInputCompo
// Bind mouse control
EnhancedInputComponent->BindAction(MouseLookAction, ETriggerEvent::Triggered, this, &ASpaceshipPawn::HandleMouseLook);
// Bind fire action
EnhancedInputComponent->BindAction(FireAction, ETriggerEvent::Triggered, this, &ASpaceshipPawn::HandleFire);
// Add shooting binding
EnhancedInputComponent->BindAction(ShootAction, ETriggerEvent::Triggered, this, &ASpaceshipPawn::HandleShoot);
}
}
@@ -243,27 +275,64 @@ void ASpaceshipPawn::HandleMouseLook(const FInputActionValue& Value)
LastMouseDelta = MouseDelta;
}
void ASpaceshipPawn::HandleFire(const FInputActionValue& Value)
void ASpaceshipPawn::HandleShoot(const FInputActionValue& Value)
{
// Debug message
if (GEngine)
GEngine->AddOnScreenDebugMessage(-1, 2.f, FColor::Yellow, TEXT("HandleShoot Called"));
if (bCanFire)
{
Fire();
}
}
void ASpaceshipPawn::Fire()
{
// Debug messages
if (!ProjectileClass)
{
if (GEngine)
GEngine->AddOnScreenDebugMessage(-1, 2.f, FColor::Red, TEXT("ProjectileClass not set!"));
return;
}
UWorld* World = GetWorld();
if (World)
{
FVector SpawnLocation = ShipMesh->GetSocketLocation(TEXT("ProjectileSpawn"));
if (SpawnLocation == FVector::ZeroVector)
{
SpawnLocation = GetActorLocation() + (GetActorForwardVector() * 100.0f);
}
FVector SpawnLocation = ProjectileSpawnPoint->GetComponentLocation();
FRotator SpawnRotation = GetActorRotation();
FActorSpawnParameters SpawnParams;
SpawnParams.Owner = this;
SpawnParams.Instigator = GetInstigator();
World->SpawnActor<ASpaceshipProjectile>(
ASpaceshipProjectile::StaticClass(),
// Spawn the projectile
ASpaceshipProjectile* Projectile = World->SpawnActor<ASpaceshipProjectile>(
ProjectileClass,
SpawnLocation,
SpawnRotation,
SpawnParams
);
if (Projectile)
{
if (GEngine)
GEngine->AddOnScreenDebugMessage(-1, 2.f, FColor::Green, TEXT("Projectile Spawned"));
}
else
{
if (GEngine)
GEngine->AddOnScreenDebugMessage(-1, 2.f, FColor::Red, TEXT("Failed to spawn projectile"));
}
// Start fire rate timer
bCanFire = false;
GetWorldTimerManager().SetTimer(FireTimerHandle, this, &ASpaceshipPawn::ResetFire, FireRate, false);
}
}
void ASpaceshipPawn::ResetFire()
{
bCanFire = true;
}