1.
打开UE引擎,创建TargetPoint C++类,命名为MMOARPGAISpawnPoint
?打开MMOARPGAISpawnPoint.h
#pragma once
#include "CoreMinimal.h"
#include "Engine/TargetPoint.h"
#include "MMOARPGAISpawnPoint.generated.h"
/**
*
*/
UCLASS()
class MMOARPGGAME_API AMMOARPGAISpawnPoint : public ATargetPoint
{
GENERATED_BODY()
public:
UPROPERTY(EditAnywhere, Category = "AISpawn")
int32 CharacterID;//角色ID
UPROPERTY(EditAnywhere, Category = "AISpawn")
int32 Lv;//等级
UPROPERTY(EditAnywhere, Category = "AISpawn")
bool bScopeSpawnAI;//是否范围性生成
UPROPERTY(EditAnywhere, Category = "AISpawn", meta = (EditCondition = "bScopeSpawnAI"))
float SpawnAIRadius;//生成半径
UPROPERTY(EditAnywhere, Category = "AISpawn", meta = (EditCondition = "bScopeSpawnAI"))
int32 SpawnAINumberInRange;//范围内怪物生成数量
public:
AMMOARPGAISpawnPoint();
virtual void BeginPlay() override;
virtual void EndPlay(const EEndPlayReason::Type EndPlayReason);
virtual void Tick(float DeltaTime) override;
protected:
void SpawnAICharacter(int32 CharacterID,int32 InLV);
};
进行实现
// Fill out your copyright notice in the Description page of Project Settings.
#include "MMOARPGAISpawnPoint.h"
#include "../Character/Core/MMOARPGCharacterBase.h"
#include "../MMOARPGGameState.h"
AMMOARPGAISpawnPoint::AMMOARPGAISpawnPoint()
{
CharacterID = INDEX_NONE;
Lv = 1;
bScopeSpawnAI = false;
SpawnAIRadius = 500.f;
SpawnAINumberInRange = 3;
}
void AMMOARPGAISpawnPoint::BeginPlay()
{
Super::BeginPlay();
if (GetWorld() && GetWorld()->IsServer())
{
//在BeginPlay中生成
SpawnAICharacter(CharacterID, Lv);
}
}
void AMMOARPGAISpawnPoint::EndPlay(const EEndPlayReason::Type EndPlayReason)
{
Super::EndPlay(EndPlayReason);
}
void AMMOARPGAISpawnPoint::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
void AMMOARPGAISpawnPoint::SpawnAICharacter(int32 InCharacterID, int32 InLV)
{
if (GetWorld())
{
if (AMMOARPGGameState* InGameState = GetWorld()->GetGameState<AMMOARPGGameState>())
{
//通过角色ID获取角色属性数据表
if (FCharacterAttributeTable* InAttributeTable = InGameState->GetCharacterAttributeTable(InCharacterID))
{
//通过角色ID获取角色类数据表
if (FCharacterStyleTable* InCharacterTable = InGameState->GetCharacterStyleTable(InCharacterID))
{
TArray<FVector>Locations;
if (bScopeSpawnAI)//是否范围生成怪物
{
for (int32 i=0;i<SpawnAINumberInRange;i++)
{
FVector Origin = GetActorLocation();
FVector2D Point = FMath::RandPointInCircle(SpawnAIRadius);//在范围内随机生成一个点,用于怪物随机生成
Locations.Add(Origin + FVector(Point.X, Point.Y, 0.f));
}
}
else
{
Locations.Add(GetActorLocation());
}
//遍历生成位置
for (auto& Location : Locations)
{
if (AMMOARPGCharacterBase* InCharacterBase = GetWorld()->SpawnActor<AMMOARPGCharacterBase>(
InCharacterTable->MMOARPGChearacterClass,//要生成的角色类
Location,//生成位置
FRotator::ZeroRotator))//旋转
{
}
}
}
}
}
}
}
2.
打开UE引擎,在文件夹中找到生成点
然后将其拖拽到场景,进行配置
|