物品属性数据结构
SlAiTypes
namespace EObjectType
{
enum Type
{
Normal=0,
Food,
Tool,
Weapon
};
}
struct ObjectAttribute
{
FText EN;
FText ZH;
EObjectType::Type ObjectType;
int PlantAttack;
int MetalAttack;
int AnimalAttack;
int AffectRange;
FString TexPath;
ObjectAttribute(const FText ENName, const FText ZHName, const EObjectType::Type OT, const int PA, const int MA, const int AA, const int AR, const FString TP)
{
EN = ENName;
ZH = ZHName;
ObjectType = OT;
PlantAttack = PA;
MetalAttack = MA;
AnimalAttack = AA;
AffectRange = AR;
TexPath = TP;
}
FString ToString() {
return EN.ToString() + FString("--") + ZH.ToString() + FString("--") + FString::FromInt((int)ObjectType) + FString("--") +
FString::FromInt(PlantAttack) + FString("--") + FString::FromInt(MetalAttack) + FString("--") +
FString::FromInt(AnimalAttack) + FString("--") + FString::FromInt(AffectRange) + FString("--") + TexPath;
}
};
解析物品属性和另一种StringToObject方法
SlAiJsonHandle
public:
void ObjectAttrJsonRead(TMap<int, TSharedPtr<ObjectAttribute>>& ObjectAttrMap);
private:
EObjectType::Type FStringToObjectType(const FString ArgStr);
private:
FString ObjectAttrFileName;
#include "SlAiJsonHandle.h"
#include"SlAiHelper.h"
SlAiJsonHandle::SlAiJsonHandle()
{
RecordDataFileName = FString("RecordData.json");
ObjectAttrFileName = FString("ObjectAttribute.json");
RelativePath = FString("Res/ConfigData/");
}
void SlAiJsonHandle::ObjectAttrJsonRead(TMap<int, TSharedPtr<ObjectAttribute>>& ObjectAttrMap)
{
FString JsonValue;
LoadStringFromFile(ObjectAttrFileName, 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>> ObjectAttrArray = JsonParsed[i]->AsObject()->GetArrayField(FString::FromInt(i));
FText EN = FText::FromString(ObjectAttrArray[0]->AsObject()->GetStringField("EN"));
FText ZH = FText::FromString(ObjectAttrArray[1]->AsObject()->GetStringField("ZH"));
FString ObjectTypeStr = ObjectAttrArray[2]->AsObject()->GetStringField("ObjectType");
int PlantAttack = ObjectAttrArray[3]->AsObject()->GetIntegerField("PlantAttack");
int MetalAttack = ObjectAttrArray[4]->AsObject()->GetIntegerField("MetalAttack");
int AnimalAttack = ObjectAttrArray[5]->AsObject()->GetIntegerField("AnimalAttack");
int AffectRange = ObjectAttrArray[6]->AsObject()->GetIntegerField("AffectRange");
FString TexPath = ObjectAttrArray[7]->AsObject()->GetStringField("TexPath");
EObjectType::Type ObjectType = FStringToObjectType(ObjectTypeStr);
TSharedPtr<ObjectAttribute> ObjectAttrPtr = MakeShareable(new ObjectAttribute(EN, ZH, ObjectType, PlantAttack, MetalAttack, AnimalAttack, AffectRange, TexPath));
ObjectAttrMap.Add(i, ObjectAttrPtr);
}
}
}
bool SlAiJsonHandle::LoadStringFromFile(const FString& FileName, const FString& RelaPath, FString& ResultString)
{
if (!FileName.IsEmpty()) {
FString AbsoPath = FPaths::ProjectContentDir() + RelaPath + FileName;
if (FPaths::FileExists(AbsoPath))
{
if (FFileHelper::LoadFileToString(ResultString, *AbsoPath))
{
return true;
}
else
{
SlAiHelper::Debug(FString("Load Error") + AbsoPath);
}
}
else
{
SlAiHelper::Debug(FString("File Not Exist") + AbsoPath);
}
}
return false;
}
EObjectType::Type SlAiJsonHandle::FStringToObjectType(const FString ArgStr)
{
if (ArgStr.Equals(FString("Normal"))) return EObjectType::Normal;
if (ArgStr.Equals(FString("Food"))) return EObjectType::Food;
if (ArgStr.Equals(FString("Tool"))) return EObjectType::Tool;
if (ArgStr.Equals(FString("Weapon"))) return EObjectType::Weapon;
return EObjectType::Normal;
}
物品属性初始化
在GameMode的BeginPlay中初始化
SlAiDataHandle
public:
void InitializeGameData();
public:
TMap<int, TSharedPtr<ObjectAttribute>>ObjectAttrMap;
private:
void InitObjectAttr();
void SlAiDataHandle::InitObjectAttr()
{
SlAiSingleton<SlAiJsonHandle>::Get()->ObjectAttrJsonRead(ObjectAttrMap);
}
void SlAiDataHandle::InitializeGameData()
{
InitObjectAttr();
for (TMap<int, TSharedPtr<ObjectAttribute>>::TIterator It(ObjectAttrMap); It; ++It)
{
SlAiHelper::Debug(It->Value->ToString(), 120);
}
}
SlAiGameMode
void ASlAiGameMode::BeginPlay()
{
SlAiDataHandle::Get()->InitializeGameData();
}
运行
|