目录
打地鼠,中间有炸弹。
避免高频率创建和销毁,提高重复利用,此时可以使用享元模式
- // 抽象享元类
- UCLASS(Abstract)
- class DESIGNPATTERNS_API AFlyweightObject : public AActor
- {
- GENERATED_BODY()
- public:
- // 用于每次重新调用时,初始化各个参数
- virtual void Init(FVector pInitPos) {
- bIsLiving = true;
- UE_LOG(LogTemp, Warning, TEXT("----------------------------------"));
- UE_LOG(LogTemp, Warning, TEXT(__FUNCTION__" %s is used. Pos%s"),
- *this->GetName(),
- *UKismetStringLibrary::Conv_VectorToString(pInitPos));
-
- GetWorld()->GetTimerManager().SetTimer(m_pTimerHandle, this, &AFlyweightObject::End, 1.0f, false);
-
- }
-
- // 自定义销毁程序,不是真正的销毁实例
- virtual void End() {
- if (bIsLiving)
- {
- UE_LOG(LogTemp, Warning, TEXT(__FUNCTION__" ******** %s is destroyed."), *this->GetName());
- bIsLiving = false;
- GetWorld()->GetTimerManager().ClearTimer(m_pTimerHandle);
- }
- }
-
- // 击中
- virtual void Hit() PURE_VIRTUAL(AFlyweightObject::IsValid, );
-
- // 用于判断是否已经在使用、空闲状态
- bool IsLiving() {
- return bIsLiving;
- }
-
- protected:
- bool bIsLiving;
- FTimerHandle m_pTimerHandle;
- };
- // 具体享元类 —— 地鼠
- UCLASS()
- class DESIGNPATTERNS_API AMole : public AFlyweightObject
- {
- GENERATED_BODY()
- public:
- virtual void Hit() {
- UE_LOG(LogTemp, Warning, TEXT(__FUNCTION__" %s is hit. Add 10 score"), *this->GetName());
- End();
- }
-
- };
- // 具体享元类 —— 炸弹
- UCLASS()
- class DESIGNPATTERNS_API ABomb : public AFlyweightObject
- {
- GENERATED_BODY()
- public:
- virtual void Hit() {
- UE_LOG(LogTemp, Warning, TEXT(__FUNCTION__" %s is hit. Add -5 score"), *this->GetName());
- End();
- }
- };
内部可使用享元池存储多个享元对象
- // 享元工厂类
- UCLASS()
- class DESIGNPATTERNS_API AFlyweightFactory : public AActor
- {
- GENERATED_BODY()
- public:
- // 初始化多个地鼠和炸弹实例以供使用
- void Init() {
- for (int32 i = 0; i < 3; i++)
- {
- AMole* mole = GetWorld()->SpawnActor
(FVector::ZeroVector, FRotator::ZeroRotator); - m_pMolePool.Add(mole->GetName(), mole);
-
- ABomb* bomb = GetWorld()->SpawnActor
(FVector::ZeroVector, FRotator::ZeroRotator); - m_pBombPool.Add(bomb->GetName(), bomb);
- }
- }
-
- // 获取未被使用的地鼠实例
- AMole* GetMole() {
- for (TTuple
& item : m_pMolePool) - {
- if (!(item.Value)->IsLiving())
- {
- return item.Value;
- }
- }
- return nullptr;
- }
-
- // 获取未被使用的炸弹实例
- ABomb* GetBomb() {
- for (TTuple
& item : m_pBombPool) - {
- if (!(item.Value)->IsLiving())
- {
- return item.Value;
- }
- }
- return nullptr;
- }
-
- // 随机获取未被使用的地鼠或炸弹实例
- AFlyweightObject* GetRandomElement() {
- if (UKismetMathLibrary::RandomFloat() < 0.7f)
- {
- return GetMole();
- }
- else
- {
- return GetBomb();
- }
-
- }
-
- private:
- // 存储的地鼠对象池
- UPROPERTY()
- TMap
m_pMolePool; -
- // 存储的炸弹对象池
- UPROPERTY()
- TMap
m_pBombPool; - };
测试
- UCLASS()
- class DESIGNPATTERNS_API AFlyweightTestActor : public AActor
- {
- GENERATED_BODY()
-
- public:
- // Sets default values for this actor's properties
- AFlyweightTestActor();
- // Called every frame
- virtual void Tick(float DeltaTime) override;
-
- protected:
- // Called when the game starts or when spawned
- virtual void BeginPlay() override;
-
- public:
- UPROPERTY()
- AFlyweightFactory* m_pFlyweightFactory;
-
- UPROPERTY()
- FTimerHandle TimerHandle;
-
- // 定时生成地鼠或炸弹
- UFUNCTION()
- void AutoSpawner();
-
-
- };
- // Called when the game starts or when spawned
- void AFlyweightTestActor::BeginPlay()
- {
- Super::BeginPlay();
-
- // 享元工厂实例
- m_pFlyweightFactory = GetWorld()->SpawnActor
(AFlyweightFactory::StaticClass()); - m_pFlyweightFactory->Init();
-
- // 定时器
- GetWorld()->GetTimerManager().SetTimer(TimerHandle, this, &AFlyweightTestActor::AutoSpawner, 0.4f, true);
- }
-
- void AFlyweightTestActor::AutoSpawner()
- {
- // 随机位置
- int32 RandomInt = UKismetMathLibrary::RandomInteger(16);
- FVector RandomPos = FVector::ZeroVector + FVector(1, 0, 0) * (RandomInt / 4) + FVector(0, 1, 0) * (RandomInt % 4);
- // 获取随机空闲的地鼠或炸弹
- AFlyweightObject* FlyweightObj = m_pFlyweightFactory->GetRandomElement();
- if (FlyweightObj)
- {
- // 初始化享元对象
- FlyweightObj->Init(RandomPos);
-
- // 假定一定概率击中
- if (UKismetMathLibrary::RandomFloat()<0.2)
- {
- FLatentActionInfo LatentActionInfo;
- UKismetSystemLibrary::Delay(GetWorld(), 0.1, LatentActionInfo);
- FlyweightObj->Hit();
- }
-
- }
-
- }
- LogTemp: Warning: ----------------------------------
- LogTemp: Warning: AFlyweightObject::Init Mole_0 is used. PosX=3.000 Y=1.000 Z=0.000
- LogTemp: Warning: ----------------------------------
- LogTemp: Warning: AFlyweightObject::Init Mole_1 is used. PosX=2.000 Y=3.000 Z=0.000
- LogTemp: Warning: ----------------------------------
- LogTemp: Warning: AFlyweightObject::Init Mole_2 is used. PosX=1.000 Y=1.000 Z=0.000
- LogTemp: Warning: AMole::Hit Mole_2 is hit. Add 10 score
- LogTemp: Warning: AFlyweightObject::End ******** Mole_2 is destroyed.
- LogTemp: Warning: AFlyweightObject::End ******** Mole_0 is destroyed.
- LogTemp: Warning: ----------------------------------
- LogTemp: Warning: AFlyweightObject::Init Mole_0 is used. PosX=3.000 Y=3.000 Z=0.000
- LogTemp: Warning: AFlyweightObject::End ******** Mole_1 is destroyed.
- LogTemp: Warning: ----------------------------------
- LogTemp: Warning: AFlyweightObject::Init Mole_1 is used. PosX=3.000 Y=2.000 Z=0.000
- LogTemp: Warning: ----------------------------------
- LogTemp: Warning: AFlyweightObject::Init Bomb_0 is used. PosX=3.000 Y=1.000 Z=0.000
- LogTemp: Warning: AFlyweightObject::End ******** Mole_0 is destroyed.
- LogTemp: Warning: ----------------------------------
- LogTemp: Warning: AFlyweightObject::Init Bomb_1 is used. PosX=3.000 Y=2.000 Z=0.000
- LogTemp: Warning: ABomb::Hit Bomb_1 is hit. Add -5 score
- LogTemp: Warning: AFlyweightObject::End ******** Bomb_1 is destroyed.
- LogTemp: Warning: AFlyweightObject::End ******** Mole_1 is destroyed.
- LogTemp: Warning: ----------------------------------
- LogTemp: Warning: AFlyweightObject::Init Bomb_1 is used. PosX=2.000 Y=2.000 Z=0.000
- LogTemp: Warning: AFlyweightObject::End ******** Bomb_0 is destroyed.
- LogTemp: Warning: ----------------------------------
- LogTemp: Warning: AFlyweightObject::Init Bomb_0 is used. PosX=2.000 Y=2.000 Z=0.000
- LogTemp: Warning: ----------------------------------
- LogTemp: Warning: AFlyweightObject::Init Bomb_2 is used. PosX=0.000 Y=3.000 Z=0.000
- LogTemp: Warning: AFlyweightObject::End ******** Bomb_1 is destroyed.
- LogTemp: Warning: ----------------------------------
- LogTemp: Warning: AFlyweightObject::Init Bomb_1 is used. PosX=1.000 Y=2.000 Z=0.000
- LogTemp: Warning: ABomb::Hit Bomb_1 is hit. Add -5 score
- LogTemp: Warning: AFlyweightObject::End ******** Bomb_1 is destroyed.
- LogTemp: Warning: AFlyweightObject::End ******** Bomb_0 is destroyed.
- LogTemp: Warning: ----------------------------------
- LogTemp: Warning: AFlyweightObject::Init Mole_0 is used. PosX=1.000 Y=2.000 Z=0.000
- LogTemp: Warning: AMole::Hit Mole_0 is hit. Add 10 score
- LogTemp: Warning: AFlyweightObject::End ******** Mole_0 is destroyed.
- LogTemp: Warning: AFlyweightObject::End ******** Bomb_2 is destroyed.
- LogTemp: Warning: ----------------------------------
- LogTemp: Warning: AFlyweightObject::Init Mole_0 is used. PosX=2.000 Y=3.000 Z=0.000
- LogTemp: Warning: ----------------------------------
- LogTemp: Warning: AFlyweightObject::Init Mole_1 is used. PosX=1.000 Y=0.000 Z=0.000
- LogTemp: Warning: ----------------------------------
- LogTemp: Warning: AFlyweightObject::Init Bomb_0 is used. PosX=3.000 Y=3.000 Z=0.000
- LogTemp: Warning: ABomb::Hit Bomb_0 is hit. Add -5 score
- LogTemp: Warning: AFlyweightObject::End ******** Bomb_0 is destroyed.
- LogTemp: Warning: AFlyweightObject::End ******** Mole_0 is destroyed.