diff --git a/Config/DefaultEngine.ini b/Config/DefaultEngine.ini index a54f58e..ce961fa 100644 --- a/Config/DefaultEngine.ini +++ b/Config/DefaultEngine.ini @@ -72,3 +72,6 @@ ConnectionType=USBOnly bUseManualIPAddress=False ManualIPAddress= + +[CoreRedirects] ++PropertyRedirects=(OldName="/Script/ToonTanks.Tank.PlayerControllerRef",NewName="/Script/ToonTanks.Tank.PlayerController") \ No newline at end of file diff --git a/Source/ToonTanks/BasePawn.cpp b/Source/ToonTanks/BasePawn.cpp index c957537..d5ee42d 100644 --- a/Source/ToonTanks/BasePawn.cpp +++ b/Source/ToonTanks/BasePawn.cpp @@ -26,6 +26,12 @@ ABasePawn::ABasePawn() ProjectileSpawnPoint->SetupAttachment(TurretMesh); } +void ABasePawn::HandleDestruction() +{ + // TODO: Visual & Sound Effect Implementation + +} + void ABasePawn::RotateTurret(FVector LookAtTarget) { FVector ToTarget = LookAtTarget - TurretMesh->GetComponentLocation(); diff --git a/Source/ToonTanks/BasePawn.h b/Source/ToonTanks/BasePawn.h index 4198334..93ad6f7 100644 --- a/Source/ToonTanks/BasePawn.h +++ b/Source/ToonTanks/BasePawn.h @@ -15,6 +15,9 @@ public: // Sets default values for this pawn's properties ABasePawn(); + // Handles Base Pawn destruction by Damage + virtual void HandleDestruction(); + protected: // Rotate Turret to Target Location void RotateTurret(FVector LookAtTarget); diff --git a/Source/ToonTanks/HealthComponent.cpp b/Source/ToonTanks/HealthComponent.cpp index e5fa356..771b553 100644 --- a/Source/ToonTanks/HealthComponent.cpp +++ b/Source/ToonTanks/HealthComponent.cpp @@ -3,6 +3,9 @@ #include "HealthComponent.h" +#include "ToonTanksGameMode.h" +#include "Kismet/GameplayStatics.h" + // Sets default values for this component's properties UHealthComponent::UHealthComponent() { @@ -20,6 +23,7 @@ void UHealthComponent::BeginPlay() Super::BeginPlay(); Health = MaxHealth; GetOwner()->OnTakeAnyDamage.AddDynamic(this, &UHealthComponent::DamageTaken); + GameMode = Cast(UGameplayStatics::GetGameMode(this)); } @@ -33,8 +37,14 @@ void UHealthComponent::TickComponent(float DeltaTime, ELevelTick TickType, FActo void UHealthComponent::DamageTaken(AActor* DamagedActor, float Damage, const UDamageType* DamageType, AController* Instigator, AActor* DamageCauser) { - if (Damage <= 0.0f) + 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/HealthComponent.h b/Source/ToonTanks/HealthComponent.h index 7a2d980..60fa7b1 100644 --- a/Source/ToonTanks/HealthComponent.h +++ b/Source/ToonTanks/HealthComponent.h @@ -32,6 +32,10 @@ private: UFUNCTION() void DamageTaken(AActor* DamagedActor, float Damage, const UDamageType* DamageType, AController* Instigator, AActor* DamageCauser); + // AToonTanksGameMode Pointer for Handling Dead Actor + UPROPERTY() + class AToonTanksGameMode* GameMode; + public: // Called every frame virtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override; diff --git a/Source/ToonTanks/Tank.cpp b/Source/ToonTanks/Tank.cpp index 6fab97f..ee0b7f9 100644 --- a/Source/ToonTanks/Tank.cpp +++ b/Source/ToonTanks/Tank.cpp @@ -29,10 +29,10 @@ void ATank::Tick(float DeltaTime) Super::Tick(DeltaTime); // Rotate Tank to Mouse Pointer Location - if (PlayerControllerRef) + if (PlayerController) { FHitResult HitResult; - PlayerControllerRef->GetHitResultUnderCursor( + PlayerController->GetHitResultUnderCursor( ECollisionChannel::ECC_Visibility, false, HitResult @@ -48,11 +48,23 @@ void ATank::Tick(float DeltaTime) } } +void ATank::HandleDestruction() +{ + Super::HandleDestruction(); + SetActorHiddenInGame(true); + SetActorTickEnabled(false); +} + +APlayerController* ATank::GetPlayerController() const +{ + return this->PlayerController; +} + // Called when the game starts or when spawned void ATank::BeginPlay() { Super::BeginPlay(); - PlayerControllerRef = Cast(GetController()); + PlayerController = Cast(GetController()); } void ATank::Move(float Value) diff --git a/Source/ToonTanks/Tank.h b/Source/ToonTanks/Tank.h index 34342f4..6c548c7 100644 --- a/Source/ToonTanks/Tank.h +++ b/Source/ToonTanks/Tank.h @@ -22,6 +22,12 @@ public: // Called every frame virtual void Tick(float DeltaTime) override; + + // Handle's Tank Destruction by Damage + virtual void HandleDestruction() override; + + // Get Current Player Controller Pointer + APlayerController* GetPlayerController() const; protected: // Called when the game starts or when spawned @@ -46,7 +52,7 @@ private: // PlayerController Pointer for Mouse Location Detection UPROPERTY() - APlayerController* PlayerControllerRef; + APlayerController* PlayerController; // Move operation through W/S key void Move(float Value); diff --git a/Source/ToonTanks/ToonTanksGameMode.cpp b/Source/ToonTanks/ToonTanksGameMode.cpp index 9db2f13..b22f069 100644 --- a/Source/ToonTanks/ToonTanksGameMode.cpp +++ b/Source/ToonTanks/ToonTanksGameMode.cpp @@ -2,4 +2,29 @@ #include "ToonTanksGameMode.h" +#include "Tank.h" +#include "Tower.h" +#include "Kismet/GameplayStatics.h" +void AToonTanksGameMode::BeginPlay() +{ + Super::BeginPlay(); + Tank = Cast(UGameplayStatics::GetPlayerPawn(this, 0)); +} + +void AToonTanksGameMode::ActorDied(AActor* DeadActor) +{ + if (DeadActor == Tank) + { + Tank->HandleDestruction(); + if (Tank->GetPlayerController()) + { + Tank->DisableInput(Tank->GetPlayerController()); + Tank->GetPlayerController()->bShowMouseCursor = false; + } + } + else if (ATower* DestroyedTower = Cast(DeadActor)) + { + DestroyedTower->HandleDestruction(); + } +} diff --git a/Source/ToonTanks/ToonTanksGameMode.h b/Source/ToonTanks/ToonTanksGameMode.h index 9986ccb..5bf38e1 100644 --- a/Source/ToonTanks/ToonTanksGameMode.h +++ b/Source/ToonTanks/ToonTanksGameMode.h @@ -13,5 +13,17 @@ UCLASS() class TOONTANKS_API AToonTanksGameMode : public AGameModeBase { GENERATED_BODY() + +public: + // Handles input or something after Actor died. + void ActorDied(AActor* DeadActor); + +protected: + virtual void BeginPlay() override; + +private: + // Tank Pointer for DeadActor which is ATank. + UPROPERTY() + class ATank* Tank; }; diff --git a/Source/ToonTanks/Tower.cpp b/Source/ToonTanks/Tower.cpp index d5e7707..6845d28 100644 --- a/Source/ToonTanks/Tower.cpp +++ b/Source/ToonTanks/Tower.cpp @@ -14,6 +14,12 @@ void ATower::Tick(float DeltaTime) } } +void ATower::HandleDestruction() +{ + Super::HandleDestruction(); + Destroy(); +} + void ATower::BeginPlay() { Super::BeginPlay(); diff --git a/Source/ToonTanks/Tower.h b/Source/ToonTanks/Tower.h index 961ec0c..a3e356b 100644 --- a/Source/ToonTanks/Tower.h +++ b/Source/ToonTanks/Tower.h @@ -17,11 +17,15 @@ class TOONTANKS_API ATower : public ABasePawn public: virtual void Tick(float DeltaTime) override; + // Handles Tower Destruction by Damage + virtual void HandleDestruction() override; + protected: virtual void BeginPlay() override; private: // Tank Object Pointer which is target + UPROPERTY() class ATank* Tank; // Distance to Start Tracking the target