Projectile Spawn and Projectile Mesh

This commit is contained in:
2025-07-21 05:08:41 -04:00
parent 0c58992652
commit bb8d95d8c2
11 changed files with 121 additions and 15 deletions

Binary file not shown.

Binary file not shown.

View File

@@ -4,6 +4,7 @@
#include "BasePawn.h" #include "BasePawn.h"
#include "Components/CapsuleComponent.h" #include "Components/CapsuleComponent.h"
#include "Kismet/GameplayStatics.h" #include "Kismet/GameplayStatics.h"
#include "Projectile.h"
// Sets default values // Sets default values
ABasePawn::ABasePawn() ABasePawn::ABasePawn()
@@ -40,16 +41,19 @@ void ABasePawn::RotateTurret(FVector LookAtTarget)
void ABasePawn::Fire() void ABasePawn::Fire()
{ {
FVector ProjectileSpawnPointLocation = ProjectileSpawnPoint->GetComponentLocation(); FVector Location = ProjectileSpawnPoint->GetComponentLocation();
FRotator Rotation = ProjectileSpawnPoint->GetComponentRotation();
DrawDebugSphere( GetWorld()->SpawnActor<AProjectile>(ProjectileClass, Location, Rotation);
/*DrawDebugSphere(
GetWorld(), GetWorld(),
ProjectileSpawnPointLocation, ProjectileSpawnPointLocation,
25.f, 25.f,
12, 12,
FColor::Red, FColor::Red,
false, false,
3.f); 3.f); */
} }
// Called every frame // Called every frame

View File

@@ -36,6 +36,7 @@ protected:
void RotateTurret(FVector LookAtTarget); void RotateTurret(FVector LookAtTarget);
void Fire(); void Fire();
private: private:
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Components", meta = (AllowPrivateAccess = "true")) UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Components", meta = (AllowPrivateAccess = "true"))
@@ -57,4 +58,6 @@ private:
UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Super Duper Variables", meta = (AllowPrivateAccess = "true")) UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Super Duper Variables", meta = (AllowPrivateAccess = "true"))
int32 VisibleAnywhere = 12; int32 VisibleAnywhere = 12;
UPROPERTY(EditDefaultsOnly, Category = "Combat")
TSubclassOf<class AProjectile> ProjectileClass;
}; };

View File

@@ -0,0 +1,33 @@
// Fill out your copyright notice in the Description page of Project Settings.
#include "Projectile.h"
#include "Components/StaticMeshComponent.h"
#include "GameFramework/ProjectileMovementComponent.h"
// Sets default values
AProjectile::AProjectile()
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = false;
ProjectileMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Projectile Mesh"));
RootComponent = ProjectileMesh;
ProjectileMovementComponent = CreateDefaultSubobject<UProjectileMovementComponent>(TEXT("Projectile Movement Component"));
ProjectileMovementComponent->MaxSpeed = 1300.f;
ProjectileMovementComponent->InitialSpeed = 1300.f;
}
// Called when the game starts or when spawned
void AProjectile::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void AProjectile::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}

View File

@@ -0,0 +1,33 @@
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "Projectile.generated.h"
UCLASS()
class TOONTANKS_API AProjectile : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
AProjectile();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
private:
//Mesh in the bp
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Combat", meta = (AllowPrivateAccess = "true"))
UStaticMeshComponent* ProjectileMesh;
UPROPERTY(VisibleAnywhere, Category = "Movement")
class UProjectileMovementComponent* ProjectileMovementComponent;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
};

View File

@@ -4,29 +4,54 @@
#include "Tower.h" #include "Tower.h"
#include "Tank.h" #include "Tank.h"
#include "Kismet/GameplayStatics.h" #include "Kismet/GameplayStatics.h"
#include "TimerManager.h"
void ATower::Tick(float DeltaTime) void ATower::Tick(float DeltaTime)
{ {
Super::Tick(DeltaTime); Super::Tick(DeltaTime);
// Find the distance to the tank if (InFireRange())
if (Tank)
{
float Distance = FVector::Dist(GetActorLocation(), Tank->GetActorLocation());
// check to see if the tank is in range
if (Distance <= FireRange)
{ {
// if in range, rotate turret toward the tank // if in range, rotate turret toward the tank
RotateTurret(Tank->GetActorLocation()); RotateTurret(Tank->GetActorLocation());
} }
} }
}
void ATower::BeginPlay() void ATower::BeginPlay()
{ {
Super::BeginPlay(); Super::BeginPlay();
Tank = Cast<ATank>(UGameplayStatics::GetPlayerPawn(this, 0)); Tank = Cast<ATank>(UGameplayStatics::GetPlayerPawn(this, 0));
GetWorldTimerManager().SetTimer(
FireRateTimerHandle,
this,
&ATower::CheckFireCondition,
FireRate,
true);
}
void ATower::CheckFireCondition()
{
// Find the distance to the tank
if (InFireRange())
{
//UE_LOG(LogTemp, Warning, TEXT("Shot Fired!"));
Fire();
}
}
bool ATower::InFireRange()
{
// Find the distance to the tank
if (Tank)
{
float Distance = FVector::Dist(GetActorLocation(), Tank->GetActorLocation());
// check to see if the tank is in range
if (Distance <= FireRange)
{
return true;
}
}
return false;
} }

View File

@@ -29,4 +29,9 @@ private:
UPROPERTY(EditDefaultsOnly, Category="Combat") UPROPERTY(EditDefaultsOnly, Category="Combat")
float FireRange = 700.f; float FireRange = 700.f;
FTimerHandle FireRateTimerHandle;
float FireRate = 2.f;
void CheckFireCondition();
bool InFireRange();
}; };

View File

@@ -7,7 +7,10 @@
{ {
"Name": "ToonTanks", "Name": "ToonTanks",
"Type": "Runtime", "Type": "Runtime",
"LoadingPhase": "Default" "LoadingPhase": "Default",
"AdditionalDependencies": [
"Engine"
]
} }
], ],
"Plugins": [ "Plugins": [