diff --git a/EndlessVendetta/Source/EndlessVendetta/AI/AICharacter.cpp b/EndlessVendetta/Source/EndlessVendetta/AI/AICharacter.cpp index 8e976d0a..6a2debfc 100644 --- a/EndlessVendetta/Source/EndlessVendetta/AI/AICharacter.cpp +++ b/EndlessVendetta/Source/EndlessVendetta/AI/AICharacter.cpp @@ -25,7 +25,6 @@ AAICharacter::AAICharacter() void AAICharacter::BeginPlay() { Super::BeginPlay(); - } // Called every frame @@ -54,31 +53,7 @@ float AAICharacter::TakeDamage(const float DamageAmount, FDamageEvent const& Dam CurrentHealth = 0; UE_LOG(LogTemp, Display, TEXT("%s is dead"), *CharacterName.ToString()); - /*const AAI_EnemyController* AIController = Cast(GetController()); - AIController->GetBrainComponent()->StopLogic(" is dead");*/ - this->Tags.Add(FName("Dead")); - //Ragdoll - DetachFromControllerPendingDestroy(); - UCapsuleComponent* CapsuleComp = GetCapsuleComponent(); - CapsuleComp->SetCollisionEnabled(ECollisionEnabled::NoCollision); - CapsuleComp->SetCollisionResponseToAllChannels(ECR_Ignore); - - GetMesh()->SetCollisionProfileName(TEXT("Ragdoll")); - SetActorEnableCollision(true); - - GetMesh()->SetAllBodiesSimulatePhysics(true); - GetMesh()->SetSimulatePhysics(true); - GetMesh()->WakeAllRigidBodies(); - GetMesh()->bBlendPhysics = true; - - if (UCharacterMovementComponent* CharacterComp = Cast(GetMovementComponent())) - { - CharacterComp->StopMovementImmediately(); - CharacterComp->DisableMovement(); - CharacterComp->SetComponentTickEnabled(false); - } - - SetLifeSpan(30.0f); + OnDeath(); } return Super::TakeDamage(DamageAmount, DamageEvent, EventInstigator, DamageCauser); } @@ -98,3 +73,31 @@ void AAICharacter::SetupStimuliSourceComponent() } } +void AAICharacter::OnDeath() +{ + /*const AAI_EnemyController* AIController = Cast(GetController()); + AIController->GetBrainComponent()->StopLogic(" is dead");*/ + this->Tags.Add(FName("Dead")); + //Ragdoll + DetachFromControllerPendingDestroy(); + UCapsuleComponent* CapsuleComp = GetCapsuleComponent(); + CapsuleComp->SetCollisionEnabled(ECollisionEnabled::NoCollision); + CapsuleComp->SetCollisionResponseToAllChannels(ECR_Ignore); + + GetMesh()->SetCollisionProfileName(TEXT("Ragdoll")); + SetActorEnableCollision(true); + + GetMesh()->SetAllBodiesSimulatePhysics(true); + GetMesh()->SetSimulatePhysics(true); + GetMesh()->WakeAllRigidBodies(); + GetMesh()->bBlendPhysics = true; + + if (UCharacterMovementComponent* CharacterComp = Cast(GetMovementComponent())) + { + CharacterComp->StopMovementImmediately(); + CharacterComp->DisableMovement(); + CharacterComp->SetComponentTickEnabled(false); + } + + SetLifeSpan(30.0f); +} diff --git a/EndlessVendetta/Source/EndlessVendetta/AI/AICharacter.h b/EndlessVendetta/Source/EndlessVendetta/AI/AICharacter.h index 44bbd309..09d63741 100644 --- a/EndlessVendetta/Source/EndlessVendetta/AI/AICharacter.h +++ b/EndlessVendetta/Source/EndlessVendetta/AI/AICharacter.h @@ -35,6 +35,8 @@ protected: class UAIPerceptionStimuliSourceComponent* StimuliSourceComponent; void SetupStimuliSourceComponent(); + virtual void OnDeath(); + public: // Called every frame virtual void Tick(float DeltaTime) override; diff --git a/EndlessVendetta/Source/EndlessVendetta/AI/AIControlHub.cpp b/EndlessVendetta/Source/EndlessVendetta/AI/AIControlHub.cpp index 75f4a7f1..6c14c0cc 100644 --- a/EndlessVendetta/Source/EndlessVendetta/AI/AIControlHub.cpp +++ b/EndlessVendetta/Source/EndlessVendetta/AI/AIControlHub.cpp @@ -74,3 +74,8 @@ void AAIControlHub::SetPlayerLastKnownLocation(FVector Location) PlayerLastKnownLocation = Location; } } + +void AAIControlHub::RemoveEnemyActor(AEnemyCharacter* EnemyActor) +{ + EnemyActors.Remove(EnemyActor); +} diff --git a/EndlessVendetta/Source/EndlessVendetta/AI/AIControlHub.h b/EndlessVendetta/Source/EndlessVendetta/AI/AIControlHub.h index d75b7fd6..bbe2e21e 100644 --- a/EndlessVendetta/Source/EndlessVendetta/AI/AIControlHub.h +++ b/EndlessVendetta/Source/EndlessVendetta/AI/AIControlHub.h @@ -35,6 +35,7 @@ public: int GetAlertLevel() const; void OnAlertLevelChanged(); void SetPlayerLastKnownLocation(FVector Location = FVector(0, 0, 0)); + void RemoveEnemyActor(AEnemyCharacter* EnemyActor); private: int AlertLevel = 0; diff --git a/EndlessVendetta/Source/EndlessVendetta/AI/EnemyCharacter.cpp b/EndlessVendetta/Source/EndlessVendetta/AI/EnemyCharacter.cpp index aa84cb9b..e7e9360c 100644 --- a/EndlessVendetta/Source/EndlessVendetta/AI/EnemyCharacter.cpp +++ b/EndlessVendetta/Source/EndlessVendetta/AI/EnemyCharacter.cpp @@ -23,6 +23,16 @@ void AEnemyCharacter::BeginPlay() CharacterName = "Enemy"; } +void AEnemyCharacter::OnDeath() +{ + Super::OnDeath(); + DelegatedControlHub->AlertLevelEvent.Remove(AlertLevelDelegateHandle); + DelegatedControlHub->HuntPlayerEvent.Remove(HuntPlayerDelegateHandle); + AlertLevelDelegateHandle.Reset(); + HuntPlayerDelegateHandle.Reset(); + DelegatedControlHub->RemoveEnemyActor(this); +} + // Called every frame void AEnemyCharacter::Tick(float DeltaTime) { @@ -38,6 +48,8 @@ void AEnemyCharacter::SubscribeToGroupAIEvents(AAIControlHub* ControlHub) void AEnemyCharacter::SetAlertLevel(const int NewAlertLevel) const { + if (!IsValid(this)) return; + if (!IsValid(GetController())) return; Cast(GetController())->GetBlackboardComponent()->SetValueAsInt("AlertLevel", NewAlertLevel); } @@ -49,6 +61,8 @@ void AEnemyCharacter::SetLocalAlertLevel(int NewAlertLevel) const void AEnemyCharacter::HuntPlayer(FVector PlayerLastKnownLocation) { + if (!IsValid(this)) return; + if (!IsValid(GetController())) return; SetAlertLevel(2); Cast(GetController())->GetBlackboardComponent()->SetValueAsVector("LastKnownLocation", PlayerLastKnownLocation); } diff --git a/EndlessVendetta/Source/EndlessVendetta/AI/EnemyCharacter.h b/EndlessVendetta/Source/EndlessVendetta/AI/EnemyCharacter.h index 6d3deab7..7b6f23f4 100644 --- a/EndlessVendetta/Source/EndlessVendetta/AI/EnemyCharacter.h +++ b/EndlessVendetta/Source/EndlessVendetta/AI/EnemyCharacter.h @@ -21,6 +21,7 @@ protected: // Called when the game starts or when spawned virtual void BeginPlay() override; class AAIControlHub* DelegatedControlHub; + virtual void OnDeath() override; public: // Called every frame