diff --git a/Source/ToonTanks/HealthComponent.cpp b/Source/ToonTanks/HealthComponent.cpp index 771b553..9a8905f 100644 --- a/Source/ToonTanks/HealthComponent.cpp +++ b/Source/ToonTanks/HealthComponent.cpp @@ -38,13 +38,9 @@ void UHealthComponent::TickComponent(float DeltaTime, ELevelTick TickType, FActo void UHealthComponent::DamageTaken(AActor* DamagedActor, float Damage, const UDamageType* DamageType, AController* Instigator, AActor* DamageCauser) { if (Health <= 0.0f) - { return; - } Health -= Damage; if (Health <= 0.0f && GameMode) - { GameMode->ActorDied(DamagedActor); - } UE_LOG(LogTemp, Warning, TEXT("Health: %f"), Health); } diff --git a/Source/ToonTanks/Projectile.cpp b/Source/ToonTanks/Projectile.cpp index 1184522..b0134ff 100644 --- a/Source/ToonTanks/Projectile.cpp +++ b/Source/ToonTanks/Projectile.cpp @@ -16,7 +16,6 @@ AProjectile::AProjectile() ProjectileMovement = CreateDefaultSubobject(TEXT("Projectile Movement")); ProjectileMovement->InitialSpeed = 400.0f; ProjectileMovement->MaxSpeed = 1000.0f; - } // Called when the game starts or when spawned @@ -24,14 +23,12 @@ void AProjectile::BeginPlay() { Super::BeginPlay(); ProjectileMesh->OnComponentHit.AddDynamic(this, &AProjectile::OnHit); - } // Called every frame void AProjectile::Tick(float DeltaTime) { Super::Tick(DeltaTime); - } void AProjectile::OnHit(UPrimitiveComponent* HitComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit) diff --git a/Source/ToonTanks/ToonTanksGameMode.cpp b/Source/ToonTanks/ToonTanksGameMode.cpp index 6a562db..febbba2 100644 --- a/Source/ToonTanks/ToonTanksGameMode.cpp +++ b/Source/ToonTanks/ToonTanksGameMode.cpp @@ -10,8 +10,7 @@ void AToonTanksGameMode::BeginPlay() { Super::BeginPlay(); - Tank = Cast(UGameplayStatics::GetPlayerPawn(this, 0)); - PlayerController = Cast(UGameplayStatics::GetPlayerController(this, 0)); + HandleGameStart(); } void AToonTanksGameMode::ActorDied(AActor* DeadActor) @@ -20,12 +19,30 @@ void AToonTanksGameMode::ActorDied(AActor* DeadActor) { Tank->HandleDestruction(); if (PlayerController) - { PlayerController->SetPlayerEnabledState(false); - } } else if (ATower* DestroyedTower = Cast(DeadActor)) - { DestroyedTower->HandleDestruction(); - } } + +void AToonTanksGameMode::HandleGameStart() +{ + Tank = Cast(UGameplayStatics::GetPlayerPawn(this, 0)); + PlayerController = Cast(UGameplayStatics::GetPlayerController(this, 0)); + if (PlayerController) + { + PlayerController->SetPlayerEnabledState(false); + FTimerHandle PlayerEnableTimerHandle; + FTimerDelegate TimerDelegate = FTimerDelegate::CreateUObject( + PlayerController, + &AToonTanksPlayerController::SetPlayerEnabledState, + true + ); + GetWorldTimerManager().SetTimer( + PlayerEnableTimerHandle, + TimerDelegate, + StartDelay, + false + ); + } +} \ No newline at end of file diff --git a/Source/ToonTanks/ToonTanksGameMode.h b/Source/ToonTanks/ToonTanksGameMode.h index c44340b..48c2ef4 100644 --- a/Source/ToonTanks/ToonTanksGameMode.h +++ b/Source/ToonTanks/ToonTanksGameMode.h @@ -29,5 +29,11 @@ private: // ToonTanksPlayerController Pointer of player pawn. UPROPERTY() class AToonTanksPlayerController* PlayerController; + + // Delay time for starting game + float StartDelay = 3.f; + + // Handles Game Start Action + void HandleGameStart(); }; diff --git a/Source/ToonTanks/ToonTanksPlayerController.cpp b/Source/ToonTanks/ToonTanksPlayerController.cpp index 7a7d240..cd7bae5 100644 --- a/Source/ToonTanks/ToonTanksPlayerController.cpp +++ b/Source/ToonTanks/ToonTanksPlayerController.cpp @@ -6,12 +6,8 @@ void AToonTanksPlayerController::SetPlayerEnabledState(bool bPlayerEnabled) { if (bPlayerEnabled) - { GetPawn()->EnableInput(this); - } else - { GetPawn()->DisableInput(this); - } bShowMouseCursor = bPlayerEnabled; } \ No newline at end of file diff --git a/Source/ToonTanks/Tower.cpp b/Source/ToonTanks/Tower.cpp index 6845d28..eedf9f1 100644 --- a/Source/ToonTanks/Tower.cpp +++ b/Source/ToonTanks/Tower.cpp @@ -9,9 +9,7 @@ void ATower::Tick(float DeltaTime) { Super::Tick(DeltaTime); if (InFireRange()) - { RotateTurret(Tank->GetActorLocation()); - } } void ATower::HandleDestruction() @@ -30,9 +28,7 @@ void ATower::BeginPlay() void ATower::TryFire() { if (InFireRange()) - { Fire(); - } } bool ATower::InFireRange() const @@ -41,9 +37,7 @@ bool ATower::InFireRange() const { float Distance = FVector::Dist(this->GetActorLocation(), Tank->GetActorLocation()); if (Distance <= FireRange) - { return true; - } } return false; }