Add reworked Worker Enemy AI #3

Closed
baz wants to merge 24 commits from worker-ai into master
7 changed files with 80 additions and 6 deletions
Showing only changes of commit a1832e352e - Show all commits

View File

@ -160,7 +160,6 @@ void AEnemyAIController::OnPerceptionUpdated(const TArray<AActor*>& actors)
else if (stimulus.Type == HearingID) else if (stimulus.Type == HearingID)
{ {
SensedHearing(actor, stimulus); SensedHearing(actor, stimulus);
stimulus.StimulusLocation;
} }
else if (stimulus.Type == DamageID) else if (stimulus.Type == DamageID)
{ {

View File

@ -2,6 +2,7 @@
#include "EnemyCharacter.h" #include "EnemyCharacter.h"
#include "EnemyAIController.h" #include "EnemyAIController.h"
#include "EnemyHealthComponent.h"
#include "InteractableComponent.h" #include "InteractableComponent.h"
#define COLLISION_WEAPON ECC_GameTraceChannel1 #define COLLISION_WEAPON ECC_GameTraceChannel1
@ -10,6 +11,10 @@ AEnemyCharacter::AEnemyCharacter(const FObjectInitializer& ObjectInitializer) :
{ {
RandomWeaponParameters = CreateDefaultSubobject<URandomWeaponParameters>(TEXT("Random Weapon Parameters")); RandomWeaponParameters = CreateDefaultSubobject<URandomWeaponParameters>(TEXT("Random Weapon Parameters"));
auto healthComponent = CreateDefaultSubobject<UEnemyHealthComponent>(TEXT("Health Component"));
SetHealthComponent(healthComponent);
GetHealthComponent()->OnDamaged.BindUFunction(this, "OnDamaged");
GetHealthComponent()->OnDeath.BindUFunction(this, "OnDeath");
GetHealthComponent()->SetMaxHealth(100.0f); GetHealthComponent()->SetMaxHealth(100.0f);
this->Tags.Add(FName("Enemy")); this->Tags.Add(FName("Enemy"));

View File

@ -0,0 +1,36 @@
// Fill out your copyright notice in the Description page of Project Settings.
#include "EnemyHealthComponent.h"
#include "EnemyAIController.h"
#include "EnemyCharacter.h"
void UEnemyHealthComponent::BeginPlay()
{
Super::BeginPlay();
}
void UEnemyHealthComponent::TakeDamage(AActor* damagedActor, float damage, const UDamageType* damageType,
AController* instigatedBy, AActor* damageCauser)
{
if (damagedActor == nullptr || IsDead || !CanDamage)
{
return;
}
CurrentHealth -= damage;
AEnemyCharacter* enemyCharacter = Cast<AEnemyCharacter>(damagedActor);
UAISense_Damage::ReportDamageEvent(GetWorld(), damagedActor, damageCauser, 1,
damageCauser->GetTransform().GetLocation(),
damageCauser->GetTransform().GetLocation());
OnDamaged.ExecuteIfBound({damagedActor, damage, damageType, instigatedBy, damageCauser});
if (CurrentHealth <= 0.0f)
{
IsDead = true;
OnDeath.ExecuteIfBound({damagedActor, damage, damageType, instigatedBy, damageCauser});
}
}

View File

@ -0,0 +1,22 @@
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "HealthComponent.h"
#include "EnemyHealthComponent.generated.h"
/**
*
*/
UCLASS()
class NAKATOMI_API UEnemyHealthComponent : public UHealthComponent
{
GENERATED_BODY()
protected:
virtual void BeginPlay() override;
virtual void TakeDamage(AActor* damagedActor, float damage, const UDamageType* damageType, AController* instigatedBy, AActor* damageCauser) override;
};

View File

@ -39,7 +39,7 @@ public:
FOnDamageDelegate OnDamaged; FOnDamageDelegate OnDamaged;
FOnDeathDelegate OnDeath; FOnDeathDelegate OnDeath;
private: protected:
UPROPERTY(EditDefaultsOnly) UPROPERTY(EditDefaultsOnly)
float MaxHealth = 100.f; float MaxHealth = 100.f;
@ -55,7 +55,7 @@ public:
UHealthComponent(); UHealthComponent();
UFUNCTION() UFUNCTION()
void TakeDamage(AActor* damagedActor, float damage, const UDamageType* damageType, AController* instigatedBy, virtual void TakeDamage(AActor* damagedActor, float damage, const UDamageType* damageType, AController* instigatedBy,
AActor* damageCauser); AActor* damageCauser);
UFUNCTION() UFUNCTION()

View File

@ -14,9 +14,12 @@ ANakatomiCharacter::ANakatomiCharacter(const FObjectInitializer& ObjectInitializ
NakatomiCMC = Cast<UNakatomiCMC>(GetCharacterMovement()); NakatomiCMC = Cast<UNakatomiCMC>(GetCharacterMovement());
HealthComponent = CreateDefaultSubobject<UHealthComponent>(TEXT("Health Component")); // if (!HealthComponent)
HealthComponent->OnDamaged.BindUFunction(this, "OnDamaged"); // {
HealthComponent->OnDeath.BindUFunction(this, "OnDeath"); // HealthComponent = CreateDefaultSubobject<UHealthComponent>(TEXT("Health Component"));
// HealthComponent->OnDamaged.BindUFunction(this, "OnDamaged");
// HealthComponent->OnDeath.BindUFunction(this, "OnDeath");
// }
} }
// Called when the game starts or when spawned // Called when the game starts or when spawned

View File

@ -30,6 +30,10 @@ APlayerCharacter::APlayerCharacter(const FObjectInitializer& ObjectInitializer)
//bUseControllerRotationYaw = true; //bUseControllerRotationYaw = true;
//bUseControllerRotationRoll = false; //bUseControllerRotationRoll = false;
SetHealthComponent(CreateDefaultSubobject<UHealthComponent>(TEXT("Health Component")));
GetHealthComponent()->OnDamaged.BindUFunction(this, "OnDamaged");
GetHealthComponent()->OnDeath.BindUFunction(this, "OnDeath");
// Setup the camera boom // Setup the camera boom
CameraSpringArmComponent = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraSpringArmComponent")); CameraSpringArmComponent = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraSpringArmComponent"));
CameraSpringArmComponent->SetupAttachment(RootComponent); CameraSpringArmComponent->SetupAttachment(RootComponent);
@ -398,6 +402,7 @@ void APlayerCharacter::ProcessHits(TArray<FHitResult> hits)
{ {
healthComponent->TakeDamage(Hit.GetActor(), CurrentWeapon->GetWeaponProperties()->WeaponDamage, nullptr, healthComponent->TakeDamage(Hit.GetActor(), CurrentWeapon->GetWeaponProperties()->WeaponDamage, nullptr,
GetController(), this); GetController(), this);
if (!healthComponent->GetIsDead()) if (!healthComponent->GetIsDead())
{ {
OnEnemyHit.ExecuteIfBound(); OnEnemyHit.ExecuteIfBound();
@ -437,6 +442,8 @@ void APlayerCharacter::ProcessHits(TArray<FHitResult> hits)
true); true);
} }
} }
MakeNoise(1, this, Hit.Location);
} }
} }
@ -639,6 +646,8 @@ void APlayerCharacter::OnFire()
CurrentWeapon->PlayFireSoundAtLocation(this->GetTransform().GetLocation()); CurrentWeapon->PlayFireSoundAtLocation(this->GetTransform().GetLocation());
MakeNoise(1,this, this->GetTransform().GetLocation());
PlayOnFireAnimations(); PlayOnFireAnimations();
CurrentWeapon->SetCurrentWeaponStatus(Cooldown); CurrentWeapon->SetCurrentWeaponStatus(Cooldown);