diff --git a/Content/Blueprints/Actors/BP_Projectile.uasset b/Content/Blueprints/Actors/BP_Projectile.uasset new file mode 100644 index 0000000..fa79088 Binary files /dev/null and b/Content/Blueprints/Actors/BP_Projectile.uasset differ diff --git a/Content/Blueprints/Pawns/BP_PawnTurret.uasset b/Content/Blueprints/Pawns/BP_PawnTurret.uasset index 2f97f90..ebb49e9 100644 Binary files a/Content/Blueprints/Pawns/BP_PawnTurret.uasset and b/Content/Blueprints/Pawns/BP_PawnTurret.uasset differ diff --git a/Content/Blueprints/Pawns/BP_Tank.uasset b/Content/Blueprints/Pawns/BP_Tank.uasset index 42ae946..9be8fda 100644 Binary files a/Content/Blueprints/Pawns/BP_Tank.uasset and b/Content/Blueprints/Pawns/BP_Tank.uasset differ diff --git a/Content/Maps/Main.umap b/Content/Maps/Main.umap index b983246..3b31189 100644 Binary files a/Content/Maps/Main.umap and b/Content/Maps/Main.umap differ diff --git a/Source/ToonTanks/BasePawn.cpp b/Source/ToonTanks/BasePawn.cpp index 42f09cb..12d26c5 100644 --- a/Source/ToonTanks/BasePawn.cpp +++ b/Source/ToonTanks/BasePawn.cpp @@ -4,6 +4,7 @@ #include "BasePawn.h" #include "Components/CapsuleComponent.h" #include "Kismet/GameplayStatics.h" +#include "Projectile.h" // Sets default values ABasePawn::ABasePawn() @@ -40,16 +41,19 @@ void ABasePawn::RotateTurret(FVector LookAtTarget) void ABasePawn::Fire() { - FVector ProjectileSpawnPointLocation = ProjectileSpawnPoint->GetComponentLocation(); + FVector Location = ProjectileSpawnPoint->GetComponentLocation(); + FRotator Rotation = ProjectileSpawnPoint->GetComponentRotation(); - DrawDebugSphere( + GetWorld()->SpawnActor(ProjectileClass, Location, Rotation); + + /*DrawDebugSphere( GetWorld(), ProjectileSpawnPointLocation, 25.f, 12, FColor::Red, false, - 3.f); + 3.f); */ } // Called every frame diff --git a/Source/ToonTanks/BasePawn.h b/Source/ToonTanks/BasePawn.h index 7afe486..5fbfa96 100644 --- a/Source/ToonTanks/BasePawn.h +++ b/Source/ToonTanks/BasePawn.h @@ -36,6 +36,7 @@ protected: void RotateTurret(FVector LookAtTarget); void Fire(); + private: UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Components", meta = (AllowPrivateAccess = "true")) @@ -57,4 +58,6 @@ private: UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Super Duper Variables", meta = (AllowPrivateAccess = "true")) int32 VisibleAnywhere = 12; + UPROPERTY(EditDefaultsOnly, Category = "Combat") + TSubclassOf ProjectileClass; }; diff --git a/Source/ToonTanks/Projectile.cpp b/Source/ToonTanks/Projectile.cpp new file mode 100644 index 0000000..92cfe43 --- /dev/null +++ b/Source/ToonTanks/Projectile.cpp @@ -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(TEXT("Projectile Mesh")); + RootComponent = ProjectileMesh; + + ProjectileMovementComponent = CreateDefaultSubobject(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); +} + diff --git a/Source/ToonTanks/Projectile.h b/Source/ToonTanks/Projectile.h new file mode 100644 index 0000000..c997084 --- /dev/null +++ b/Source/ToonTanks/Projectile.h @@ -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; +}; diff --git a/Source/ToonTanks/Tower.cpp b/Source/ToonTanks/Tower.cpp index 513164c..e6f03f6 100644 --- a/Source/ToonTanks/Tower.cpp +++ b/Source/ToonTanks/Tower.cpp @@ -4,24 +4,17 @@ #include "Tower.h" #include "Tank.h" #include "Kismet/GameplayStatics.h" +#include "TimerManager.h" void ATower::Tick(float DeltaTime) { Super::Tick(DeltaTime); - // Find the distance to the tank - if (Tank) + if (InFireRange()) { - 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 - RotateTurret(Tank->GetActorLocation()); - } + // if in range, rotate turret toward the tank + RotateTurret(Tank->GetActorLocation()); } - } void ATower::BeginPlay() @@ -29,4 +22,36 @@ void ATower::BeginPlay() Super::BeginPlay(); Tank = Cast(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; +} \ No newline at end of file diff --git a/Source/ToonTanks/Tower.h b/Source/ToonTanks/Tower.h index d1115cc..57dfff9 100644 --- a/Source/ToonTanks/Tower.h +++ b/Source/ToonTanks/Tower.h @@ -29,4 +29,9 @@ private: UPROPERTY(EditDefaultsOnly, Category="Combat") float FireRange = 700.f; + FTimerHandle FireRateTimerHandle; + float FireRate = 2.f; + void CheckFireCondition(); + + bool InFireRange(); }; diff --git a/ToonTanks.uproject b/ToonTanks.uproject index c7673e5..b5b9dd7 100644 --- a/ToonTanks.uproject +++ b/ToonTanks.uproject @@ -7,7 +7,10 @@ { "Name": "ToonTanks", "Type": "Runtime", - "LoadingPhase": "Default" + "LoadingPhase": "Default", + "AdditionalDependencies": [ + "Engine" + ] } ], "Plugins": [