Compare commits

..

No commits in common. "a963437069112e0ebf9d8ddd64e76b06ed930365" and "7aac1a15441f856a45de98c6b20b9c480a889795" have entirely different histories.

27 changed files with 56 additions and 131 deletions

Binary file not shown.

View File

@ -3,12 +3,36 @@
#include "ContainerInfo.h"
#include "IDetailTreeNode.h"
#include "PlayerInfoSaveGame.h"
#include "Chaos/PBDRigidClusteringAlgo.h"
#include "Engine/AssetManager.h"
#include "Engine/StreamableManager.h"
// void UContainerInfo::InitContainer(FPrimaryAssetId ContainerShapeId)
// {
// //加载形状资源
// TSharedPtr<FStreamableHandle> LoadHandle = GEngine->AssetManager->LoadPrimaryAsset(ContainerShapeId);
// if (LoadHandle.IsValid())
// {
// // 等待加载完成,这会阻塞线程
// LoadHandle->WaitUntilComplete();
//
// // 加载完成后,通过句柄获取资源
// UObject* LoadedObject = LoadHandle->GetLoadedAsset();
// if (LoadedObject)
// {
// ContainerShape = Cast<UShapeAsset>(LoadedObject);
// for (int x = 0; x < ContainerShape->GetShapeWidth(); x++)
// {
// for (int y = 0; y < ContainerShape->GetShapeHeight(); y++)
// {
// SlotInUsing.Add(FIntPoint(x, y), false);
// }
// }
// }
// LoadHandle->ReleaseHandle();
// }
//
// }
void UContainerInfo::InitContainerByShape(UShapeAsset* InContainerShape)
{
@ -22,19 +46,18 @@ void UContainerInfo::InitContainerByShape(UShapeAsset* InContainerShape)
}
}
bool UContainerInfo::AddContainerItem(FContainerItem Item, int32& ItemID)
bool UContainerInfo::AddContainerItem(FContainerItem Item, FIntPoint Position)
{
if (IsItemInContainerShape(Item))
if (IsItemInContainerShape(Item, Position))
{
return false;
}
if (IsItemOverlapOtherItem(Item))
if (IsItemOverlapOtherItem(Item, Position))
{
return false;
}
ItemID = NextItemID;
Items.Add(NextItemID, Item);
++NextItemID;
Items.Add(Position, Item);
//设置容器指定位置的占用状态
for (int x = 0; x < Item.GetItemWIdth(); x++)
{
@ -42,46 +65,25 @@ bool UContainerInfo::AddContainerItem(FContainerItem Item, int32& ItemID)
{
if (Item.IsSlotUsing(FIntPoint(x, y)))
{
SlotInUsing.Add(FIntPoint(x + Item.ContainerStartPos.X, y + Item.ContainerStartPos.Y), true);
SlotInUsing.Add(FIntPoint(x + Position.X, y + Position.Y), true);
}
}
}
return true;
}
bool UContainerInfo::RemoveContainerItem(const int32& ItemID)
{
if (Items.Contains(ItemID))
{
FContainerItem Item = Items[ItemID];
for (int x = 0; x < Item.GetItemWIdth(); x++)
{
for (int y = 0; y < Item.GetItemHeight(); y++)
{
if (Item.IsSlotUsing(FIntPoint(x + Item.ContainerStartPos.X, y + Item.ContainerStartPos.Y)))
{
SlotInUsing[FIntPoint(x + Item.ContainerStartPos.X, y + Item.ContainerStartPos.Y)] = false;
}
}
}
Items.Remove(ItemID);
return true;
}
return false;
}
bool UContainerInfo::IsItemOverlapOtherItem(FContainerItem NewItem)
bool UContainerInfo::IsItemOverlapOtherItem(FContainerItem NewItem, FIntPoint Position)
{
for (int x = 0; x < NewItem.GetItemWIdth(); x++)
{
for (int y = 0; y < NewItem.GetItemHeight(); y++)
{
if (NewItem.IsSlotUsing(FIntPoint(x, y)))
if (NewItem.IsSlotUsing(Position))
{
if (!ContainerShape->IsSlotActive(NewItem.ContainerStartPos.X + x, NewItem.ContainerStartPos.Y + y) ||
SlotInUsing[FIntPoint(x + NewItem.ContainerStartPos.X, y + NewItem.ContainerStartPos.Y)])
if (!ContainerShape->IsSlotActive(Position.X + x, Position.Y + y) ||
SlotInUsing[FIntPoint(x + Position.X, y + Position.Y)])
{
return true;
}
@ -92,17 +94,17 @@ bool UContainerInfo::IsItemOverlapOtherItem(FContainerItem NewItem)
return false;
}
bool UContainerInfo::IsItemInContainerShape(FContainerItem NewItem)
bool UContainerInfo::IsItemInContainerShape(FContainerItem NewItem, FIntPoint Position)
{
for (int x = 0; x < NewItem.GetItemWIdth(); x++)
{
for (int y = 0; y < NewItem.GetItemHeight(); y++)
{
if ( (x + NewItem.ContainerStartPos.X) > 0 && (x + NewItem.ContainerStartPos.X) < ContainerShape->GetShapeWidth() &&
(y + NewItem.ContainerStartPos.Y) > 0 && (y + NewItem.ContainerStartPos.Y) < ContainerShape->GetShapeHeight()
if ( (x + Position.X) > 0 && (x + Position.X) < ContainerShape->GetShapeWidth() &&
(y + Position.Y) > 0 && (y + Position.Y) < ContainerShape->GetShapeHeight()
)
{
if (!ContainerShape->IsSlotActive(NewItem.ContainerStartPos.X + x, NewItem.ContainerStartPos.Y + y))
if (!ContainerShape->IsSlotActive(Position.X + x, Position.Y + y))
{
return false;
}
@ -123,8 +125,8 @@ TArray<FContainerItemSaveData> UContainerInfo::GetSaveData() const
for (const auto& ItemPair : Items)
{
FContainerItemSaveData SaveData;
SaveData.PosX = ItemPair.Value.ContainerStartPos.X;
SaveData.PosY = ItemPair.Value.ContainerStartPos.Y;
SaveData.PosX = ItemPair.Key.X;
SaveData.PosY = ItemPair.Key.Y;
SaveData.DegreeType = ItemPair.Value.DegreeType;
if (ItemPair.Value.RewardItem)
@ -138,71 +140,28 @@ TArray<FContainerItemSaveData> UContainerInfo::GetSaveData() const
return SaveDataArray;
}
bool UContainerInfo::LoadFromSaveData(FPrimaryAssetId ShapeID, const TArray<FContainerItemSaveData>& ContainerItems)
void UContainerInfo::LoadFromSaveData(UPlayerInfoSaveGame* SaveGameData)
{
// 清空当前数据
Items.Empty();
SlotInUsing.Empty();
//从存档中加载仓库形状
FSoftObjectPath ShipShapePath = UAssetManager::Get().GetPrimaryAssetPath(ShapeID);
UShapeAsset* ShapeAsset = Cast<UShapeAsset>(ShipShapePath.TryLoad());
if (!IsValid(ShapeAsset))
if (IsValid(SaveGameData))
{
return false;
}
InitContainerByShape(ShapeAsset);
FSoftObjectPath ShipShapePath = UAssetManager::Get().GetPrimaryAssetPath(SaveGameData->ShipContainerShapeID);
InitContainerByShape(Cast<UShapeAsset>(ShipShapePath.TryLoad()));
;}
// 获得仓库中的物品信息
TArray<FPrimaryAssetId> AssetsToLoad;
for (const FContainerItemSaveData& ItemData : ContainerItems)
for (const FContainerItemSaveData& ItemData : SaveGameData->ShipContainerItems)
{
FContainerItem NewItem;
NewItem.ContainerStartPos = FIntPoint(ItemData.PosX, ItemData.PosY);
NewItem.DegreeType = ItemData.DegreeType;
//获得物品形状
FSoftObjectPath RewardItemPath = UAssetManager::Get().GetPrimaryAssetPath(ItemData.RewardItemAssetId);
NewItem.RewardItem = Cast<UFishingRewardDataAsset>(RewardItemPath.TryLoad());
//添加物品到容器中
FIntPoint Position(ItemData.PosX, ItemData.PosY);
int32 ItemID;
AddContainerItem(NewItem, ItemID);
AddContainerItem(NewItem, Position);
}
return true;
}
// bool UContainerInfo::LoadFromSaveData(UPlayerInfoSaveGame* SaveGameData)
// {
// // 清空当前数据
// Items.Empty();
// SlotInUsing.Empty();
// //从存档中加载仓库形状
// if (IsValid(SaveGameData))
// {
// FSoftObjectPath ShipShapePath = UAssetManager::Get().GetPrimaryAssetPath(SaveGameData->ShipContainerShapeID);
// UShapeAsset* ShapeAsset = Cast<UShapeAsset>(ShipShapePath.TryLoad());
// if (!IsValid(ShapeAsset))
// {
// return false;
// }
// InitContainerByShape(ShapeAsset);
// // 获得仓库中的物品信息
// TArray<FPrimaryAssetId> AssetsToLoad;
// for (const FContainerItemSaveData& ItemData : SaveGameData->ShipContainerItems)
// {
// FContainerItem NewItem;
// NewItem.DegreeType = ItemData.DegreeType;
// //获得物品形状
// FSoftObjectPath RewardItemPath = UAssetManager::Get().GetPrimaryAssetPath(ItemData.RewardItemAssetId);
// NewItem.RewardItem = Cast<UFishingRewardDataAsset>(RewardItemPath.TryLoad());
// //添加物品到容器中
// FIntPoint Position(ItemData.PosX, ItemData.PosY);
// AddContainerItem(NewItem, Position);
// }
// }
// else
// {
// return false;
// }
//
// return true;
// }

View File

@ -44,8 +44,6 @@ struct FContainerItem
{
GENERATED_BODY()
public:
UPROPERTY(EditAnywhere, BlueprintReadWrite)
FIntPoint ContainerStartPos;
UPROPERTY(EditAnywhere, BlueprintReadWrite)
EItemDegreeType DegreeType;
UPROPERTY(EditAnywhere, BlueprintReadWrite)
@ -112,34 +110,28 @@ public:
void InitContainerByShape(UShapeAsset* InContainerShape);
UFUNCTION(BlueprintPure)
bool AddContainerItem(FContainerItem Item, int32& ItemID);
UFUNCTION(BlueprintPure)
bool RemoveContainerItem(const int32& ItemID);
bool AddContainerItem(FContainerItem Item, FIntPoint Position);
// 获取存档数据
UFUNCTION(BlueprintCallable, Category = "ContainerInfo")
TArray<FContainerItemSaveData> GetSaveData() const;
// 从存档数据加载
// UFUNCTION(BlueprintCallable, Category = "ContainerInfo")
// bool LoadFromSaveData(UPlayerInfoSaveGame* SaveGameData);
UFUNCTION(BlueprintCallable, Category = "ContainerInfo")
bool LoadFromSaveData(FPrimaryAssetId ShapeID, const TArray<FContainerItemSaveData>& ContainerItems);
void LoadFromSaveData(UPlayerInfoSaveGame* SaveGameData);
private:
//要添加的物品是否会覆盖其他物品
bool IsItemOverlapOtherItem(FContainerItem Item);
bool IsItemOverlapOtherItem(FContainerItem Item, FIntPoint Position);
//要添加的物品是否在容器范围内
bool IsItemInContainerShape(FContainerItem Item);
bool IsItemInContainerShape(FContainerItem Item, FIntPoint Position);
public:
UPROPERTY(EditAnywhere, BlueprintReadWrite)
TMap<int32, FContainerItem> Items;
TMap<FIntPoint, FContainerItem> Items;
UPROPERTY(EditAnywhere, BlueprintReadWrite)
UShapeAsset* ContainerShape;
private:
int32 NextItemID = 0;
TMap<FIntPoint, bool> SlotInUsing;
};

View File

@ -44,33 +44,10 @@ bool UGameInfoManager::LoadGameInfo()
return false;
}
void UGameInfoManager::CreateGameInfo(UContainerInfo* ShipContainer, UContainerInfo* PlayerContainer)
void UGameInfoManager::CreateGameInfo(UContainerInfo* ShipContainer)
{
PlayerInfo = NewObject<UPlayerInfoSaveGame>(this);
PlayerInfo->ShipContainerItems = ShipContainer->GetSaveData();
PlayerInfo->ShipContainerShapeID = ShipContainer->ContainerShape->GetPrimaryAssetId();
PlayerInfo->PlayerContainerItems = PlayerContainer->GetSaveData();
PlayerInfo->PlayerContainerShapeID = PlayerContainer->ContainerShape->GetPrimaryAssetId();
SaveGameInfo();
}
void UGameInfoManager::CreateGameInfoAndSave(UShapeAsset* ShipContainerShape, UShapeAsset* PlayerContainerShape)
{
//创建仓库信息
UContainerInfo* ShipContainer = NewObject<UContainerInfo>();
ShipContainer->InitContainerByShape(ShipContainerShape);
UContainerInfo* PlayerContainer = NewObject<UContainerInfo>();
PlayerContainer->InitContainerByShape(PlayerContainerShape);
//创建要保存的额存档信息
PlayerInfo = NewObject<UPlayerInfoSaveGame>(this);
PlayerInfo->ShipContainerItems = ShipContainer->GetSaveData();
PlayerInfo->ShipContainerShapeID = ShipContainer->ContainerShape->GetPrimaryAssetId();
PlayerInfo->PlayerContainerItems = PlayerContainer->GetSaveData();
PlayerInfo->PlayerContainerShapeID = PlayerContainer->ContainerShape->GetPrimaryAssetId();
SaveGameInfo();
}

View File

@ -23,10 +23,7 @@ protected:
UFUNCTION(BlueprintCallable)
bool LoadGameInfo();
UFUNCTION(BlueprintCallable)
void CreateGameInfo(UContainerInfo* ShipContainer, UContainerInfo* PlayerContainer);
UFUNCTION(BlueprintCallable)
void CreateGameInfoAndSave(UShapeAsset* ShipContainerShape, UShapeAsset* PlayerContainerShape);
void CreateGameInfo(UContainerInfo* ShipContainer);
protected:
UPROPERTY(BlueprintReadOnly)
TObjectPtr<class UPlayerInfoSaveGame> PlayerInfo;