From 36891ab3d10bc1ce7c1f92195453646dda98ed47 Mon Sep 17 00:00:00 2001 From: 10000Je Date: Fri, 25 Apr 2025 22:01:13 +0900 Subject: [PATCH] Fire --- Source/ToonTanks/BasePawn.cpp | 20 +++++++++++++++++--- Source/ToonTanks/BasePawn.h | 3 ++- Source/ToonTanks/Tank.cpp | 8 +++++--- Source/ToonTanks/Tower.cpp | 15 +++++++-------- Source/ToonTanks/Tower.h | 3 --- 5 files changed, 31 insertions(+), 18 deletions(-) diff --git a/Source/ToonTanks/BasePawn.cpp b/Source/ToonTanks/BasePawn.cpp index e3fea92..8c42f79 100644 --- a/Source/ToonTanks/BasePawn.cpp +++ b/Source/ToonTanks/BasePawn.cpp @@ -33,7 +33,21 @@ void ABasePawn::RotateTurret(FVector LookAtTarget) FMath::RInterpTo( TurretMesh->GetComponentRotation(), LookAtRotation, - UGameplayStatics::GetWorldDeltaSeconds(this), - 5) - ); + UGameplayStatics::GetWorldDeltaSeconds(this), + 5 + ) + ); +} + +void ABasePawn::Fire() +{ + DrawDebugSphere( + GetWorld(), + ProjectileSpawnPoint->GetComponentLocation(), + 25, + 12, + FColor::Red, + false, + 3.0 + ); } diff --git a/Source/ToonTanks/BasePawn.h b/Source/ToonTanks/BasePawn.h index d7d83e4..d22fdbd 100644 --- a/Source/ToonTanks/BasePawn.h +++ b/Source/ToonTanks/BasePawn.h @@ -16,9 +16,10 @@ public: ABasePawn(); protected: - void RotateTurret(FVector LookAtTarget); + void Fire(); + private: UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Component", meta=(AllowPrivateAccess = "true")) class UCapsuleComponent* CapsuleComp; diff --git a/Source/ToonTanks/Tank.cpp b/Source/ToonTanks/Tank.cpp index ef4b980..8d4eddf 100644 --- a/Source/ToonTanks/Tank.cpp +++ b/Source/ToonTanks/Tank.cpp @@ -21,6 +21,7 @@ void ATank::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent) Super::SetupPlayerInputComponent(PlayerInputComponent); PlayerInputComponent->BindAxis(TEXT("MoveForward"), this, &ATank::Move); PlayerInputComponent->BindAxis(TEXT("Turn"), this, &ATank::Turn); + PlayerInputComponent->BindAction(TEXT("Fire"), EInputEvent::IE_Pressed, this, &ATank::Fire); } // Called every frame @@ -34,13 +35,15 @@ void ATank::Tick(float DeltaTime) PlayerControllerRef->GetHitResultUnderCursor( ECollisionChannel::ECC_Visibility, false, - HitResult); + HitResult + ); DrawDebugSphere( GetWorld(), HitResult.ImpactPoint, 25, 12, - FColor::Green); + FColor::Green + ); RotateTurret(HitResult.ImpactPoint); } } @@ -49,7 +52,6 @@ void ATank::Tick(float DeltaTime) void ATank::BeginPlay() { Super::BeginPlay(); - PlayerControllerRef = Cast(GetController()); } diff --git a/Source/ToonTanks/Tower.cpp b/Source/ToonTanks/Tower.cpp index b7f31c1..35cca05 100644 --- a/Source/ToonTanks/Tower.cpp +++ b/Source/ToonTanks/Tower.cpp @@ -8,20 +8,19 @@ void ATower::Tick(float DeltaTime) { Super::Tick(DeltaTime); - - if (Tank) + if (Tank == nullptr) { - float Distance = FVector::Dist(this->GetActorLocation(), Tank->GetActorLocation()); - if (Distance <= FireRange) - { - RotateTurret(Tank->GetActorLocation()); - } + return; + } + float Distance = FVector::Dist(this->GetActorLocation(), Tank->GetActorLocation()); + if (Distance <= FireRange) + { + RotateTurret(Tank->GetActorLocation()); } } void ATower::BeginPlay() { Super::BeginPlay(); - Tank = Cast(UGameplayStatics::GetPlayerPawn(this, 0)); } \ No newline at end of file diff --git a/Source/ToonTanks/Tower.h b/Source/ToonTanks/Tower.h index fcdf1f0..ea5d80c 100644 --- a/Source/ToonTanks/Tower.h +++ b/Source/ToonTanks/Tower.h @@ -15,15 +15,12 @@ class TOONTANKS_API ATower : public ABasePawn GENERATED_BODY() public: - virtual void Tick(float DeltaTime) override; protected: - virtual void BeginPlay() override; private: - class ATank* Tank; UPROPERTY(EditAnywhere, Category = "Rotate")