Section 13: The Enemy Class - Lecture 277

Move To Task
This commit is contained in:
charnet3d 2024-03-06 13:43:20 +01:00
parent 5352846678
commit 860e2d43cd
12 changed files with 118 additions and 9 deletions

View File

@ -86,11 +86,11 @@ DoubleClickTime=0.200000
+ActionMappings=(ActionName="Aiming",bShift=False,bCtrl=False,bAlt=False,bCmd=False,Key=RightMouseButton) +ActionMappings=(ActionName="Aiming",bShift=False,bCtrl=False,bAlt=False,bCmd=False,Key=RightMouseButton)
+ActionMappings=(ActionName="Aiming",bShift=False,bCtrl=False,bAlt=False,bCmd=False,Key=Gamepad_LeftTrigger) +ActionMappings=(ActionName="Aiming",bShift=False,bCtrl=False,bAlt=False,bCmd=False,Key=Gamepad_LeftTrigger)
+ActionMappings=(ActionName="Select",bShift=False,bCtrl=False,bAlt=False,bCmd=False,Key=E) +ActionMappings=(ActionName="Select",bShift=False,bCtrl=False,bAlt=False,bCmd=False,Key=E)
+ActionMappings=(ActionName="Select",bShift=False,bCtrl=False,bAlt=False,bCmd=False,Key=Gamepad_FaceButton_Right) +ActionMappings=(ActionName="Select",bShift=False,bCtrl=False,bAlt=False,bCmd=False,Key=Gamepad_FaceButton_Top)
+ActionMappings=(ActionName="Reload",bShift=False,bCtrl=False,bAlt=False,bCmd=False,Key=R) +ActionMappings=(ActionName="Reload",bShift=False,bCtrl=False,bAlt=False,bCmd=False,Key=R)
+ActionMappings=(ActionName="Reload",bShift=False,bCtrl=False,bAlt=False,bCmd=False,Key=Gamepad_FaceButton_Left) +ActionMappings=(ActionName="Reload",bShift=False,bCtrl=False,bAlt=False,bCmd=False,Key=Gamepad_FaceButton_Left)
+ActionMappings=(ActionName="Crouch",bShift=False,bCtrl=False,bAlt=False,bCmd=False,Key=C) +ActionMappings=(ActionName="Crouch",bShift=False,bCtrl=False,bAlt=False,bCmd=False,Key=C)
+ActionMappings=(ActionName="Crouch",bShift=False,bCtrl=False,bAlt=False,bCmd=False,Key=Gamepad_RightThumbstick) +ActionMappings=(ActionName="Crouch",bShift=False,bCtrl=False,bAlt=False,bCmd=False,Key=Gamepad_FaceButton_Right)
+ActionMappings=(ActionName="FKey",bShift=False,bCtrl=False,bAlt=False,bCmd=False,Key=F) +ActionMappings=(ActionName="FKey",bShift=False,bCtrl=False,bAlt=False,bCmd=False,Key=F)
+ActionMappings=(ActionName="1Key",bShift=False,bCtrl=False,bAlt=False,bCmd=False,Key=One) +ActionMappings=(ActionName="1Key",bShift=False,bCtrl=False,bAlt=False,bCmd=False,Key=One)
+ActionMappings=(ActionName="2Key",bShift=False,bCtrl=False,bAlt=False,bCmd=False,Key=Two) +ActionMappings=(ActionName="2Key",bShift=False,bCtrl=False,bAlt=False,bCmd=False,Key=Two)
@ -114,5 +114,6 @@ DoubleClickTime=0.200000
DefaultPlayerInputClass=/Script/EnhancedInput.EnhancedPlayerInput DefaultPlayerInputClass=/Script/EnhancedInput.EnhancedPlayerInput
DefaultInputComponentClass=/Script/EnhancedInput.EnhancedInputComponent DefaultInputComponentClass=/Script/EnhancedInput.EnhancedInputComponent
DefaultTouchInterface=/Engine/MobileResources/HUD/DefaultVirtualJoysticks.DefaultVirtualJoysticks DefaultTouchInterface=/Engine/MobileResources/HUD/DefaultVirtualJoysticks.DefaultVirtualJoysticks
+ConsoleKeys=Tilde -ConsoleKeys=Tilde
+ConsoleKeys=`

BIN
Content/_Game/Enemies/EnemyBP.uasset (Stored with Git LFS)

Binary file not shown.

BIN
Content/_Game/EnemyController/EnemyBehaviorTree.uasset (Stored with Git LFS) Normal file

Binary file not shown.

BIN
Content/_Game/EnemyController/EnemyBlackboard.uasset (Stored with Git LFS) Normal file

Binary file not shown.

BIN
Content/_Game/EnemyController/EnemyControllerBP.uasset (Stored with Git LFS) Normal file

Binary file not shown.

BIN
Content/_Game/Maps/DefaultMap.umap (Stored with Git LFS)

Binary file not shown.

View File

@ -9,7 +9,8 @@
"Type": "Runtime", "Type": "Runtime",
"LoadingPhase": "Default", "LoadingPhase": "Default",
"AdditionalDependencies": [ "AdditionalDependencies": [
"Engine" "Engine",
"AIModule"
] ]
} }
], ],

View File

@ -7,6 +7,10 @@
#include "Kismet/GameplayStatics.h" #include "Kismet/GameplayStatics.h"
#include "Sound/SoundCue.h" #include "Sound/SoundCue.h"
#include "Particles/ParticleSystemComponent.h" #include "Particles/ParticleSystemComponent.h"
#include "Kismet/KismetMathLibrary.h"
#include "DrawDebugHelpers.h"
#include "EnemyController.h"
#include "BehaviorTree/BlackboardComponent.h"
// Sets default values // Sets default values
AEnemy::AEnemy() : AEnemy::AEnemy() :
@ -28,6 +32,19 @@ void AEnemy::BeginPlay()
Super::BeginPlay(); Super::BeginPlay();
GetMesh()->SetCollisionResponseToChannel(ECollisionChannel::ECC_Visibility, ECollisionResponse::ECR_Block); GetMesh()->SetCollisionResponseToChannel(ECollisionChannel::ECC_Visibility, ECollisionResponse::ECR_Block);
// Get the AI Controller
EnemyController = Cast<AEnemyController>(GetController());
const FVector WorldPatrolPoint = UKismetMathLibrary::TransformLocation(GetActorTransform(), PatrolPoint);
DrawDebugSphere(GetWorld(), WorldPatrolPoint, 25.f, 12, FColor::Red, true);
if (EnemyController)
{
EnemyController->GetBlackboardComponent()->SetValueAsVector("PatrolPoint", WorldPatrolPoint);
EnemyController->RunBehaviorTree(BehaviorTree);
}
} }
void AEnemy::ShowHealthBar_Implementation() void AEnemy::ShowHealthBar_Implementation()

View File

@ -10,6 +10,8 @@
class UParticleSystem; class UParticleSystem;
class USoundCue; class USoundCue;
class UAnimMontage; class UAnimMontage;
class UBehaviorTree;
class AEnemyController;
UCLASS() UCLASS()
class SHOOTER_API AEnemy : public ACharacter, public IBulletHitInterface class SHOOTER_API AEnemy : public ACharacter, public IBulletHitInterface
@ -91,6 +93,16 @@ private:
/** Time before a hit number is removed from the screen */ /** Time before a hit number is removed from the screen */
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Combat, meta = (AllowPrivateAccess = true)) UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Combat, meta = (AllowPrivateAccess = true))
float HitNumberDestroyTime; float HitNumberDestroyTime;
/** Behavior tree for the AI Character */
UPROPERTY(EditAnywhere, Category = "Behavior Tree", meta = (AllowPrivateAccess = true))
UBehaviorTree* BehaviorTree;
/** Point for the enemy to move to */
UPROPERTY(EditAnywhere, Category = "Behavior Tree", meta = (AllowPrivateAccess = true, MakeEditWidget = true))
FVector PatrolPoint;
AEnemyController* EnemyController;
public: public:
// Called every frame // Called every frame
virtual void Tick(float DeltaTime) override; virtual void Tick(float DeltaTime) override;
@ -106,4 +118,6 @@ public:
UFUNCTION(BlueprintImplementableEvent) UFUNCTION(BlueprintImplementableEvent)
void ShowHitNumber(int32 Damage, FVector HitLocation, bool bHeadshot); void ShowHitNumber(int32 Damage, FVector HitLocation, bool bHeadshot);
FORCEINLINE UBehaviorTree* GetBehaviorTree() const { return BehaviorTree; }
}; };

View File

@ -0,0 +1,33 @@
// Fill out your copyright notice in the Description page of Project Settings.
#include "EnemyController.h"
#include "Enemy.h"
#include "BehaviorTree/BehaviorTree.h"
#include "BehaviorTree/BehaviorTreeComponent.h"
#include "BehaviorTree/BlackboardComponent.h"
AEnemyController::AEnemyController()
{
BlackboardComponent = CreateDefaultSubobject<UBlackboardComponent>(TEXT("BlackboardComponent"));
check(BlackboardComponent);
BehaviorTreeComponent = CreateDefaultSubobject<UBehaviorTreeComponent>(TEXT("BehaviorTreeComponent"));
check(BehaviorTreeComponent);
}
void AEnemyController::OnPossess(APawn* InPawn)
{
Super::OnPossess(InPawn);
if (!InPawn) return;
if (AEnemy* Enemy = Cast<AEnemy>(InPawn))
{
if (Enemy->GetBehaviorTree())
{
BlackboardComponent->InitializeBlackboard(*(Enemy->GetBehaviorTree()->BlackboardAsset));
}
}
}

View File

@ -0,0 +1,34 @@
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "AIController.h"
#include "EnemyController.generated.h"
class UBlackboardComponent;
class UBehaviorTreeComponent;
/**
*
*/
UCLASS()
class SHOOTER_API AEnemyController : public AAIController
{
GENERATED_BODY()
public:
AEnemyController();
virtual void OnPossess(APawn* InPawn) override;
private:
/** Blackboard component for this enemy */
UPROPERTY(BlueprintReadWrite, Category = "AI Behavior", meta = (AllowPrivateAccess = true))
UBlackboardComponent* BlackboardComponent;
/** BehaviorTree component for this enemy */
UPROPERTY(BlueprintReadWrite, Category = "AI Behavior", meta = (AllowPrivateAccess = true))
UBehaviorTreeComponent* BehaviorTreeComponent;
public:
FORCEINLINE UBlackboardComponent* GetBlackboardComponent() const { return BlackboardComponent; }
};

View File

@ -8,7 +8,7 @@ public class Shooter : ModuleRules
{ {
PCHUsage = PCHUsageMode.UseExplicitOrSharedPCHs; PCHUsage = PCHUsageMode.UseExplicitOrSharedPCHs;
PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore", "UMG", "PhysicsCore" }); PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore", "UMG", "PhysicsCore", "NavigationSystem", "AIModule" });
PrivateDependencyModuleNames.AddRange(new string[] { }); PrivateDependencyModuleNames.AddRange(new string[] { });