209 lines
5.6 KiB
C++

// Fill out your copyright notice in the Description page of Project Settings.
#include "ContainerInfo.h"
#include "IDetailTreeNode.h"
#include "PlayerInfoSaveGame.h"
#include "Chaos/PBDRigidClusteringAlgo.h"
#include "Engine/AssetManager.h"
#include "Engine/StreamableManager.h"
void UContainerInfo::InitContainerByShape(UShapeAsset* InContainerShape)
{
ContainerShape = InContainerShape;
for (int x = 0; x < ContainerShape->GetShapeWidth(); x++)
{
for (int y = 0; y < ContainerShape->GetShapeHeight(); y++)
{
SlotInUsing.Add(FIntPoint(x, y), false);
}
}
}
bool UContainerInfo::AddContainerItem(FContainerItem Item, int32& ItemID)
{
if (!IsItemInContainerShape(Item))
{
return false;
}
if (IsItemOverlapOtherItem(Item))
{
return false;
}
ItemID = NextItemID;
Items.Add(NextItemID, Item);
++NextItemID;
//设置容器指定位置的占用状态
for (int x = 0; x < Item.GetItemWIdth(); x++)
{
for (int y = 0; y < Item.GetItemHeight(); y++)
{
if (Item.IsSlotUsing(FIntPoint(x, y)))
{
SlotInUsing.Add(FIntPoint(x + Item.ContainerStartPos.X, y + Item.ContainerStartPos.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)
{
for (int x = 0; x < NewItem.GetItemWIdth(); x++)
{
for (int y = 0; y < NewItem.GetItemHeight(); y++)
{
if (NewItem.IsSlotUsing(FIntPoint(x, y)))
{
if (!ContainerShape->IsSlotActive(NewItem.ContainerStartPos.X + x, NewItem.ContainerStartPos.Y + y) ||
SlotInUsing[FIntPoint(x + NewItem.ContainerStartPos.X, y + NewItem.ContainerStartPos.Y)])
{
return true;
}
}
}
}
return false;
}
bool UContainerInfo::IsItemInContainerShape(FContainerItem NewItem)
{
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 (!ContainerShape->IsSlotActive(NewItem.ContainerStartPos.X + x, NewItem.ContainerStartPos.Y + y))
{
return false;
}
}
else
{
return false;
}
}
}
return true;
}
TArray<FContainerItemSaveData> UContainerInfo::GetSaveData() const
{
TArray<FContainerItemSaveData> SaveDataArray;
FPrimaryAssetId TestID = ContainerShape->GetPrimaryAssetId();
for (const auto& ItemPair : Items)
{
FContainerItemSaveData SaveData;
SaveData.PosX = ItemPair.Value.ContainerStartPos.X;
SaveData.PosY = ItemPair.Value.ContainerStartPos.Y;
SaveData.DegreeType = ItemPair.Value.DegreeType;
if (ItemPair.Value.RewardItem)
{
SaveData.RewardItemAssetId = ItemPair.Value.RewardItem->GetPrimaryAssetId();
}
SaveDataArray.Add(SaveData);
}
return SaveDataArray;
}
bool UContainerInfo::LoadFromSaveData(FPrimaryAssetId ShapeID, const TArray<FContainerItemSaveData>& ContainerItems)
{
// 清空当前数据
Items.Empty();
SlotInUsing.Empty();
//从存档中加载仓库形状
FSoftObjectPath ShipShapePath = UAssetManager::Get().GetPrimaryAssetPath(ShapeID);
UShapeAsset* ShapeAsset = Cast<UShapeAsset>(ShipShapePath.TryLoad());
if (!IsValid(ShapeAsset))
{
return false;
}
InitContainerByShape(ShapeAsset);
// 获得仓库中的物品信息
TArray<FPrimaryAssetId> AssetsToLoad;
for (const FContainerItemSaveData& ItemData : ContainerItems)
{
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);
}
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;
// }