#include "Tooth.h"
#include "Guide.h"
#include "GuideController.h"
#include "BehaviorTree/BehaviorTree.h"
#include "BehaviorTree/BehaviorTreeComponent.h"
#include "BehaviorTree/BlackboardComponent.h"
#include "BehaviorTree/Blackboard/BlackboardKeyAllTypes.h"
AGuideController::AGuideController(const class FObjectInitializer& ObjectInitializer)
: Super(ObjectInitializer), PreDestinationActor(nullptr), PreTurnToActor(nullptr)
{
BehaviorTreeComponent = ObjectInitializer.CreateDefaultSubobject<UBehaviorTreeComponent>(this, TEXT("BehaviorTree"));
BlackboardComponent = ObjectInitializer.CreateDefaultSubobject<UBlackboardComponent>(this, TEXT("Blackboard"));
}
void AGuideController::Possess(APawn* InPawn)
{
Super::Possess(InPawn);
AGuide* Guide = Cast<AGuide>(InPawn);
if (Guide)
{
if (Guide->BehaviorTree)
{
if (Guide->BehaviorTree->BlackboardAsset)
{
BlackboardComponent->InitializeBlackboard(*Guide->BehaviorTree->BlackboardAsset);
BehaviorTreeComponent->StartTree(*Guide->BehaviorTree);
}
else
{
UE_LOG(GuideLog, Error, TEXT(LOG_HEADER"No blackboard is assigned to the guide's behavior tree."));
}
}
else
{
UE_LOG(GuideLog, Error, TEXT(LOG_HEADER"No behavior tree is assigned to guide."));
}
}
else
{
UE_LOG(GuideLog, Error, TEXT(LOG_HEADER"The pawn possessed is not an instance of AGuide."));
}
}
void AGuideController::UnPossess()
{
Super::UnPossess();
BehaviorTreeComponent->StopTree();
}
void AGuideController::GuideMoveToActor(AActor* DestinationActor, AActor* TurnToActor, float RelativeDistance)
{
if (BlackboardComponent)
{
BlackboardComponent->SetValueAsFloat(RelativeDistanceKeyName, RelativeDistance);
if (!BlackboardComponent->GetValueAsBool(MoveToActorKeyName))
{
if (!PreDestinationActor || !DestinationActor->GetActorLabel().Equals(PreDestinationActor->GetActorLabel()))
{
PreDestinationActor = DestinationActor;
BlackboardComponent->SetValueAsObject(DestinationActorKeyName, DestinationActor);
BlackboardComponent->SetValueAsObject(TurnToActorKeyName, TurnToActor);
BlackboardComponent->SetValueAsBool(MoveToActorKeyName, true);
}
}
}
}
void AGuideController::GuideMoveToLocation(FVector DestinationLocation, AActor* TurnToActor, float RelativeDistance)
{
if (BlackboardComponent)
{
BlackboardComponent->SetValueAsFloat(RelativeDistanceKeyName, RelativeDistance);
if (!BlackboardComponent->GetValueAsBool(MoveToLocationKeyName))
{
BlackboardComponent->SetValueAsVector(DestinationLocationKeyName, DestinationLocation);
BlackboardComponent->SetValueAsObject(TurnToActorKeyName, TurnToActor);
BlackboardComponent->SetValueAsBool(MoveToLocationKeyName, true);
}
}
}
void AGuideController::GuideTurnTo(AActor* TurnToActor, float RelativeAngle)
{
if (BlackboardComponent)
{
BlackboardComponent->SetValueAsFloat(RelativeAngleKeyName, RelativeAngle);
if (!BlackboardComponent->GetValueAsBool(TurnKeyName))
{
if (!PreTurnToActor || !TurnToActor->GetActorLabel().Equals(PreTurnToActor->GetActorLabel()))
{
PreTurnToActor = TurnToActor;
BlackboardComponent->SetValueAsObject(TurnToActorKeyName, TurnToActor);
BlackboardComponent->SetValueAsBool(TurnKeyName, true);
}
}
}
}
void AGuideController::GuidePlayAudio(FString AudioName)
{
if (BlackboardComponent)
{
BlackboardComponent->SetValueAsString(AudioNameKeyName, AudioName);
if (!BlackboardComponent->GetValueAsBool(PlayAudioKeyName))
{
BlackboardComponent->SetValueAsBool(PlayAudioKeyName, true);
}
}
}
void AGuideController::GuideStopPlayAudio()
{
if (BlackboardComponent)
{
BlackboardComponent->SetValueAsBool(StopPlayAudioKeyName, true);
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
Unreal 4引擎中,使用BehaviorTree控制AI行为的C++实现
共35个文件
cpp:16个
h:16个
cs:3个
需积分: 50 23 下载量 124 浏览量
2017-02-15
19:24:48
上传
评论 1
收藏 24KB ZIP 举报
温馨提示
保证可用,对应文档见http://blog.csdn.net/winfield821/article/details/55212031。
资源推荐
资源详情
资源评论
收起资源包目录
Source.zip (35个子文件)
Source
Tooth.Target.cs 550B
Tooth
Tooth.Build.cs 773B
Tooth.h 222B
Public
GuideCine.h 1KB
Guide.h 698B
MyActor.h 545B
GuideUtils.h 735B
GuideController.h 2KB
GuideBehaviorTree
TurnToByRelativeAngle.h 2KB
GuideStopPlayAudio.h 761B
InitializeMoveToActorSpline.h 1KB
InitializeTurnToEndpoint.h 1KB
MoveAlongSpline.h 1KB
InitializeMoveToLocationSpline.h 1KB
UncheckMoveToActor.h 705B
UncheckMoveToLocation.h 729B
GuidePlayAudio.h 1KB
CineActor.h 2KB
Tooth.cpp 212B
Private
MyActor.cpp 970B
GuideUtils.cpp 2KB
CineActor.cpp 3KB
Guide.cpp 714B
GuideCine.cpp 4KB
GuideBehaviorTree
GuideStopPlayAudio.cpp 1KB
UncheckMoveToLocation.cpp 723B
TurnToByRelativeAngle.cpp 2KB
GuidePlayAudio.cpp 2KB
UncheckMoveToActor.cpp 793B
MoveAlongSpline.cpp 2KB
InitializeMoveToLocationSpline.cpp 1KB
InitializeMoveToActorSpline.cpp 2KB
InitializeTurnToEndpoint.cpp 2KB
GuideController.cpp 4KB
ToothEditor.Target.cs 564B
共 35 条
- 1
资源评论
winfield821
- 粉丝: 44
- 资源: 7
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功