十一、RPGType.h

通常,我們會(huì)在一個(gè)文件中定義一些枚舉和結(jié)構(gòu)體等供其他類使用來(lái)避免遞歸引用。

// ----------------------------------------------------------------------------------------------------------------
// 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
// ----------------------------------------------------------------------------------------------------------------

這個(gè)文件里包含:

  1. 2個(gè)結(jié)構(gòu)體:(這個(gè)兩個(gè)數(shù)據(jù)結(jié)構(gòu)是背包系統(tǒng)的核心)
  • FRPGItemSlot
  • FRPGItemData
  1. 6個(gè)委托聲明:
  • 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()


// 構(gòu)造函數(shù)
// ---------------------------------------------------------------------------------------------------
    /** Constructor, -1 means an invalid slot */
    FRPGItemSlot()
        : SlotNumber(-1)
    {}

    FRPGItemSlot(const FPrimaryAssetType& InItemType, int32 InSlotNumber)
        : ItemType(InItemType)
        , SlotNumber(InSlotNumber)
    {}
//-------------------------------------------------------------------------------------------------------


// 數(shù)據(jù)
// -------------------------------------------------------------------------------------------------------
    /** 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()

//構(gòu)造函數(shù)
// ---------------------------------------------------------------------------------------------------
    /** 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)
    {}
// ---------------------------------------------------------------------------------------------------


//數(shù)據(jù)
// ---------------------------------------------------------------------------------------------------
    /** 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);

關(guān)于這些數(shù)據(jù)結(jié)構(gòu)如何使用,到后期應(yīng)該會(huì)從整體再看。現(xiàn)在,暫不考慮。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容