4bf678b937
新增: - EffectManager: Effect Timeline调度 + Action执行分发 - SystemDriver: AInfo全局驱动器, 持有FInteractionManager/FEffectManager - InteractionPromptComponent: 独立Prompt UI组件(只读Manager缓存) - DoorActor/DoorInteractableComponent: 门派生类 修改: - InteractionManager: 从UActorComponent重写为普通C++ FInteractionManager - Character: 替换旧InteractionManager为PromptComponent, 通过Driver访问Manager - GameInstance: 空壳化, 持久数据迁移至PersistenceSubsystem - InteractableComponent: 碰撞回调接入ASystemDriver注册路径 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
149 lines
4.5 KiB
C++
149 lines
4.5 KiB
C++
// ChildHood Project - 系统驱动器 实现(v1.1 Effect/Action 架构)
|
||
// 对应技术方案: docs/technical/交互系统-技术方案.md 第3节(类设计)
|
||
// 职责:全局唯一的 World 级驱动器,持有所有 Manager,负责初始化、Tick、跨 Manager 协调
|
||
|
||
#include "SystemDriver.h"
|
||
#include "ChildHood.h"
|
||
|
||
// FEffectManager 尚未创建时,此 include 会失败。
|
||
// 临时处理:条件编译,EffectManager.h 存在时才包含。
|
||
// 当 EffectManager 子代理完成后,去掉 __has_include 判断直接包含。
|
||
#if __has_include("Interaction/EffectManager.h")
|
||
#include "Interaction/EffectManager.h"
|
||
#define HAS_EFFECT_MANAGER 1
|
||
#else
|
||
#define HAS_EFFECT_MANAGER 0
|
||
#endif
|
||
|
||
#include "Engine/World.h"
|
||
#include "Kismet/GameplayStatics.h"
|
||
#include "GameFramework/PlayerController.h"
|
||
#include "GameFramework/Character.h"
|
||
|
||
// ============================================================================
|
||
// 构造函数
|
||
// ============================================================================
|
||
|
||
ASystemDriver::ASystemDriver()
|
||
{
|
||
// AInfo 默认不需要 Tick,我们在构造函数中开启
|
||
PrimaryActorTick.bCanEverTick = true;
|
||
PrimaryActorTick.bStartWithTickEnabled = true;
|
||
|
||
// 如果 FEffectManager 可用,在构造时创建实例
|
||
#if HAS_EFFECT_MANAGER
|
||
EffectManagerPtr = MakeUnique<FEffectManager>();
|
||
#endif
|
||
}
|
||
|
||
// ============================================================================
|
||
// 生命周期
|
||
// ============================================================================
|
||
|
||
void ASystemDriver::BeginPlay()
|
||
{
|
||
Super::BeginPlay();
|
||
|
||
// --- 初始化 InteractionManager ---
|
||
if (UWorld* World = GetWorld())
|
||
{
|
||
if (APlayerController* PC = World->GetFirstPlayerController())
|
||
{
|
||
if (ACharacter* Char = PC->GetCharacter())
|
||
{
|
||
InteractionManager.Initialize(Char);
|
||
UE_LOG(LogChildHood, Log, TEXT("SystemDriver: InteractionManager 初始化完成"));
|
||
}
|
||
else
|
||
{
|
||
UE_LOG(LogChildHood, Warning, TEXT("SystemDriver: PlayerController 无 Character,InteractionManager 未初始化"));
|
||
}
|
||
}
|
||
else
|
||
{
|
||
UE_LOG(LogChildHood, Warning, TEXT("SystemDriver: World 中无 PlayerController"));
|
||
}
|
||
}
|
||
|
||
// --- 初始化 EffectManager ---
|
||
#if HAS_EFFECT_MANAGER
|
||
if (EffectManagerPtr.IsValid())
|
||
{
|
||
// 绑定 Effect 完成回调
|
||
FOnEffectComplete Callback;
|
||
Callback.BindLambda([this]() { this->OnEffectComplete(); });
|
||
EffectManagerPtr->SetOnEffectComplete(MoveTemp(Callback));
|
||
|
||
UE_LOG(LogChildHood, Log, TEXT("SystemDriver: EffectManager 初始化完成"));
|
||
}
|
||
#else
|
||
UE_LOG(LogChildHood, Log, TEXT("SystemDriver: EffectManager 尚未创建(EffectManager.h 不存在)"));
|
||
#endif
|
||
}
|
||
|
||
void ASystemDriver::Tick(float DeltaTime)
|
||
{
|
||
Super::Tick(DeltaTime);
|
||
|
||
// --- Tick EffectManager ---
|
||
// InteractionManager 无 Tick(纯事件驱动)
|
||
#if HAS_EFFECT_MANAGER
|
||
if (EffectManagerPtr.IsValid())
|
||
{
|
||
EffectManagerPtr->Update(DeltaTime);
|
||
}
|
||
#endif
|
||
}
|
||
|
||
// ============================================================================
|
||
// 全局访问
|
||
// ============================================================================
|
||
|
||
ASystemDriver* ASystemDriver::Get(const UObject* WorldContextObject)
|
||
{
|
||
if (!WorldContextObject)
|
||
{
|
||
return nullptr;
|
||
}
|
||
|
||
UWorld* World = GEngine->GetWorldFromContextObject(WorldContextObject, EGetWorldErrorMode::LogAndReturnNull);
|
||
if (!World)
|
||
{
|
||
return nullptr;
|
||
}
|
||
|
||
// 使用 TActorIterator 查找场景中的 ASystemDriver
|
||
// 全局唯一,取第一个即可
|
||
for (TActorIterator<ASystemDriver> It(World); It; ++It)
|
||
{
|
||
return *It;
|
||
}
|
||
|
||
// 场景中不存在时返回 nullptr
|
||
// 应在关卡中放置一个 ASystemDriver,或在 GameMode::BeginPlay 中 Spawn
|
||
UE_LOG(LogChildHood, Verbose, TEXT("SystemDriver::Get: 场景中未找到 ASystemDriver"));
|
||
return nullptr;
|
||
}
|
||
|
||
// ============================================================================
|
||
// Manager 访问
|
||
// ============================================================================
|
||
|
||
FEffectManager& ASystemDriver::GetEffectManager()
|
||
{
|
||
checkf(EffectManagerPtr.IsValid(),
|
||
TEXT("ASystemDriver::GetEffectManager: EffectManager 未初始化。")
|
||
TEXT("请确保 EffectManager.h 已创建且 HAS_EFFECT_MANAGER == 1"));
|
||
return *EffectManagerPtr;
|
||
}
|
||
|
||
// ============================================================================
|
||
// Effect 完成回调
|
||
// ============================================================================
|
||
|
||
void ASystemDriver::OnEffectComplete()
|
||
{
|
||
// Effect 完成后通知 InteractionManager 恢复 Free 状态
|
||
InteractionManager.OnInteractionFinished();
|
||
}
|