交互检测
动画类
public:
UFUNCTION(BlueprintCallable, Category = "SlAi")
void ChangeDetection(bool IsOpen);
void USlAiPlayerAnim::ChangeDetection(bool IsOpen)
{
if (!SPCharacter) return;
SPCharacter->ChangeHandObjectDetect(IsOpen);
}
角色类
public:
void ChangeHandObjectDetect(bool IsOpen);
void ASlAiPlayerCharacter::ChangeHandObjectDetect(bool IsOpen)
{
ASlAiHandObject* HandObjectClass = Cast<ASlAiHandObject>(HandObject->GetChildActor());
if (HandObjectClass) HandObjectClass->ChangeOverlayDetect(IsOpen);
}
物品类
public:
void ChangeOverlayDetect(bool IsOpen);
void ASlAiHandObject::ChangeOverlayDetect(bool IsOpen)
{
AffectCollision->bGenerateOverlapEvents = IsOpen;
}
在动画蓝图设置通知
运行
读取资源Json
先定义资源类型和属性结构体
namespace EResourceType {
enum Type
{
Plant=0,
Metal,
Animal
};
}
struct ResourceAttribute
{
FText EN;
FText ZH;
EResourceType::Type ResourceType;
int HP;
TArray<TArray<int>> FlobObjectInfo;
ResourceAttribute(const FText ENName, const FText ZHName, const EResourceType::Type RT, const int HPValue, TArray<TArray<int>>* FOI)
{
EN = ENName;
ZH = ZHName;
ResourceType = RT;
HP = HPValue;
for (TArray<TArray<int>>::TIterator It(*FOI); It; ++It)
{
TArray<int> FlobObjectInfoItem;
for (TArray<int>::TIterator Ih(*It); Ih; Ih++)
{
FlobObjectInfoItem.Add(*Ih);
}
FlobObjectInfo.Add(FlobObjectInfoItem);
}
}
FString ToString()
{
FString InfoStr;
for (TArray<TArray<int>>::TIterator It(FlobObjectInfo); It; ++It)
{
for (TArray<int>::TIterator Ih(*It); Ih; ++Ih)
{
InfoStr += FString::FromInt(*Ih) + FString(".");
}
InfoStr += FString("__");
}
return EN.ToString() + FString("--") + ZH.ToString() + FString("--") + FString::FromInt((int)ResourceType) + FString("--") + FString::FromInt(HP) + FString("--") + InfoStr;
}
};
解析资源文件
public:
...
void ResourceAttrJsonRead(TMap<int, TSharedPtr<ResourceAttribute>>& ResourceAttrMap);
private:
...
EResourceType::Type StringToResourceType(const FString ArgStr);
private:
FString RecordDataFileName;
FString ObjectAttrFileName;
FString ResourceAttrFileName;
FString RelativePath;
};
SlAiJsonHandle::SlAiJsonHandle()
{
RecordDataFileName = FString("RecordData.json");
ObjectAttrFileName = FString("ObjectAttribute.json");
ResourceAttrFileName = FString("ResourceAttribute.json");
RelativePath = FString("Res/ConfigData/");
}
void SlAiJsonHandle::ResourceAttrJsonRead(TMap<int, TSharedPtr<ResourceAttribute>>& ResourceAttrMap)
{
FString JsonValue;
LoadStringFromFile(ResourceAttrFileName, RelativePath, JsonValue);
TArray<TSharedPtr<FJsonValue>> JsonParsed;
TSharedRef<TJsonReader<TCHAR>> JsonReader = TJsonReaderFactory<TCHAR>::Create(JsonValue);
if (FJsonSerializer::Deserialize(JsonReader, JsonParsed))
{
for (int i = 0; i < JsonParsed.Num(); ++i)
{
TArray<TSharedPtr<FJsonValue>> ResourceAttr = JsonParsed[i]->AsObject()->GetArrayField(FString::FromInt(i + 1));
FText EN = FText::FromString(ResourceAttr[0]->AsObject()->GetStringField("EN"));
FText ZH = FText::FromString(ResourceAttr[1]->AsObject()->GetStringField("ZH"));
EResourceType::Type ResourceType = StringToResourceType(ResourceAttr[2]->AsObject()->GetStringField("ResourceType"));
int HP = ResourceAttr[3]->AsObject()->GetIntegerField("HP");
TArray<TArray<int>> FlobObjectInfoArray;
TArray<TSharedPtr<FJsonValue>> FlobObjectInfo = ResourceAttr[4]->AsObject()->GetArrayField(FString("FlobObjectInfo"));
for (int j = 0; j < FlobObjectInfo.Num(); ++j) {
FString FlobObjectInfoItem = FlobObjectInfo[j]->AsObject()->GetStringField(FString::FromInt(j));
FString ObjectIndexStr;
FString RangeStr;
FString RangeMinStr;
FString RangeMaxStr;
FlobObjectInfoItem.Split(FString("_"), &ObjectIndexStr, &RangeStr);
RangeStr.Split(FString(","), &RangeMinStr, &RangeMaxStr);
TArray<int> FlobObjectInfoList;
FlobObjectInfoList.Add(FCString::Atoi(*ObjectIndexStr));
FlobObjectInfoList.Add(FCString::Atoi(*RangeMinStr));
FlobObjectInfoList.Add(FCString::Atoi(*RangeMaxStr));
FlobObjectInfoArray.Add(FlobObjectInfoList);
}
TSharedPtr<ResourceAttribute> ResourceAttrPtr = MakeShareable(new ResourceAttribute(EN, ZH, ResourceType, HP, &FlobObjectInfoArray));
ResourceAttrMap.Add(i + 1, ResourceAttrPtr);
}
}
else
{
SlAiHelper::Debug(FString("Deserialize Failed"), 10.f);
}
}
初始化资源属性Map
public:
TMap<int, TSharedPtr<ResourceAttribute>> ResourceAttrMap;
private:
void InitResourceAttrMap();
void SlAiDataHandle::InitResourceAttrMap()
{
SlAiSingleton<SlAiJsonHandle>::Get()->ResourceAttrJsonRead(ResourceAttrMap);
for (TMap<int, TSharedPtr<ResourceAttribute>>::TIterator It(ResourceAttrMap); It; ++It)
{
SlAiHelper::Debug((It->Value)->ToString(), 120.f);
}
}
void SlAiDataHandle::InitializeGameData()
{
InitObjectAttr();
InitResourceAttrMap();
ObjectBrushList.Add(&SlAiStyle::GetGameStyle()->EmptyBrush);
ObjectBrushList.Add(&SlAiStyle::GetGameStyle()->ObjectBrush_1);
ObjectBrushList.Add(&SlAiStyle::GetGameStyle()->ObjectBrush_2);
ObjectBrushList.Add(&SlAiStyle::GetGameStyle()->ObjectBrush_3);
ObjectBrushList.Add(&SlAiStyle::GetGameStyle()->ObjectBrush_4);
ObjectBrushList.Add(&SlAiStyle::GetGameStyle()->ObjectBrush_5);
ObjectBrushList.Add(&SlAiStyle::GetGameStyle()->ObjectBrush_6);
ObjectBrushList.Add(&SlAiStyle::GetGameStyle()->ObjectBrush_7);
}
运行
|