十一、RPGType.h
2019-06-18 本文已影响0人
珏_Gray
通常,我们会在一个文件中定义一些枚举和结构体等供其他类使用来避免递归引用。
// ----------------------------------------------------------------------------------------------------------------
// This header is for enums and structs used by classes and blueprints accross the game
// Collecting these in a single header helps avoid problems with recursive header includes
// It's also a good place to put things like data table row structs
// ----------------------------------------------------------------------------------------------------------------
这个文件里包含:
- 2个结构体:(这个两个数据结构是背包系统的核心)
- FRPGItemSlot
- FRPGItemData
- 6个委托声明:
- FOnInventoryItemChanged
- FOnInventoryItemChangedNative
- FOnSlottedItemChanged
- FOnSlottedItemChangedNative
- FOnInventoryLoaded
- FOnInventoryLoadedNative
FRPGItemSlot
/** Struct representing a slot for an item, shown in the UI */
USTRUCT(BlueprintType)
struct ACTIONRPG_API FRPGItemSlot
{
GENERATED_BODY()
// 构造函数
// ---------------------------------------------------------------------------------------------------
/** Constructor, -1 means an invalid slot */
FRPGItemSlot()
: SlotNumber(-1)
{}
FRPGItemSlot(const FPrimaryAssetType& InItemType, int32 InSlotNumber)
: ItemType(InItemType)
, SlotNumber(InSlotNumber)
{}
//-------------------------------------------------------------------------------------------------------
// 数据
// -------------------------------------------------------------------------------------------------------
/** The type of items that can go in this slot */
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Item)
FPrimaryAssetType ItemType;
/** The number of this slot, 0 indexed */
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Item)
int32 SlotNumber;
//-------------------------------------------------------------------------------------------------------
// 方法
//-------------------------------------------------------------------------------------------------------
/** Equality operators */
bool operator==(const FRPGItemSlot& Other) const
{
return ItemType == Other.ItemType && SlotNumber == Other.SlotNumber;
}
bool operator!=(const FRPGItemSlot& Other) const
{
return !(*this == Other);
}
/** Implemented so it can be used in Maps/Sets */
friend inline uint32 GetTypeHash(const FRPGItemSlot& Key)
{
uint32 Hash = 0;
Hash = HashCombine(Hash, GetTypeHash(Key.ItemType));
Hash = HashCombine(Hash, (uint32)Key.SlotNumber);
return Hash;
}
/** Returns true if slot is valid */
bool IsValid() const
{
return ItemType.IsValid() && SlotNumber >= 0;
}
//-------------------------------------------------------------------------------------------------------
};
FRPGItemData
/** Extra information about a URPGItem that is in a player's inventory */
USTRUCT(BlueprintType)
struct ACTIONRPG_API FRPGItemData
{
GENERATED_BODY()
//构造函数
// ---------------------------------------------------------------------------------------------------
/** Constructor, default to count/level 1 so declaring them in blueprints gives you the expected behavior */
FRPGItemData()
: ItemCount(1)
, ItemLevel(1)
{}
FRPGItemData(int32 InItemCount, int32 InItemLevel)
: ItemCount(InItemCount)
, ItemLevel(InItemLevel)
{}
// ---------------------------------------------------------------------------------------------------
//数据
// ---------------------------------------------------------------------------------------------------
/** The number of instances of this item in the inventory, can never be below 1 */
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Item)
int32 ItemCount;
/** The level of this item. This level is shared for all instances, can never be below 1 */
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Item)
int32 ItemLevel;
// ---------------------------------------------------------------------------------------------------
//方法
// ---------------------------------------------------------------------------------------------------
/** Equality operators */
bool operator==(const FRPGItemData& Other) const
{
return ItemCount == Other.ItemCount && ItemLevel == Other.ItemLevel;
}
bool operator!=(const FRPGItemData& Other) const
{
return !(*this == Other);
}
/** Returns true if count is greater than 0 */
bool IsValid() const
{
return ItemCount > 0;
}
/** Append an item data, this adds the count and overrides everything else */
void UpdateItemData(const FRPGItemData& Other, int32 MaxCount, int32 MaxLevel)
{
if (MaxCount <= 0)
{
MaxCount = MAX_int32;
}
if (MaxLevel <= 0)
{
MaxLevel = MAX_int32;
}
ItemCount = FMath::Clamp(ItemCount + Other.ItemCount, 1, MaxCount);
ItemLevel = FMath::Clamp(Other.ItemLevel, 1, MaxLevel);
}
// ---------------------------------------------------------------------------------------------------
};
Delegates 委托
/** Delegate called when an inventory item changes */
DECLARE_DYNAMIC_MULTICAST_DELEGATE_TwoParams(FOnInventoryItemChanged, bool, bAdded, URPGItem*, Item);
DECLARE_MULTICAST_DELEGATE_TwoParams(FOnInventoryItemChangedNative, bool, URPGItem*);
/** Delegate called when the contents of an inventory slot change */
DECLARE_DYNAMIC_MULTICAST_DELEGATE_TwoParams(FOnSlottedItemChanged, FRPGItemSlot, ItemSlot, URPGItem*, Item);
DECLARE_MULTICAST_DELEGATE_TwoParams(FOnSlottedItemChangedNative, FRPGItemSlot, URPGItem*);
/** Delegate called when the entire inventory has been loaded, all items may have been replaced */
DECLARE_DYNAMIC_MULTICAST_DELEGATE(FOnInventoryLoaded);
DECLARE_MULTICAST_DELEGATE(FOnInventoryLoadedNative);
关于这些数据结构如何使用,到后期应该会从整体再看。现在,暂不考虑。