美文网首页
十一、RPGType.h

十一、RPGType.h

作者: 珏_Gray | 来源:发表于2019-06-18 10:06 被阅读0次

通常,我们会在一个文件中定义一些枚举和结构体等供其他类使用来避免递归引用。

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

这个文件里包含:

  1. 2个结构体:(这个两个数据结构是背包系统的核心)
  • FRPGItemSlot
  • FRPGItemData
  1. 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);

关于这些数据结构如何使用,到后期应该会从整体再看。现在,暂不考虑。

相关文章

  • 十一、RPGType.h

    通常,我们会在一个文件中定义一些枚举和结构体等供其他类使用来避免递归引用。 这个文件里包含: 2个结构体:(这个两...

  • 十一,十一

    这是米多多第52篇原创文章 文:米多多 图:来自于网络 莫扎特说过,世上最可贵的是时间,世上最奢靡的是挥霍时光。 ...

  • 十一,十一

    十一月,如期来临。 做完功课,和小熊打声招呼。我紧赶慢赶追上了最后一班城乡的巴士,我戴上耳机,临车窗外,星火点点,...

  • 十一月的四季

    啊猪说:“十一月即将结束,我们就写十一月吧,十一月的景,十一月的人和事。”十一月,十一月,十一月在我心中...

  • 十一

    我叫十一 一二三四五六七 什么时候到十一 十一十一还没起 跳过七六五四三二一 叫我十一

  • 愿有岁月可回首  且以深情共白头

    我亲爱的十一 我依然爱唤你十一 有时在扣扣上深深浅浅的唤着十一十一十一十一 当然那是我们初识不久之后的事 ...

  • 我们不一样真爱酒吧

    双十一,买十一,送十一 各种德啤,任你选!

  • 看起来弯的吗?点赞会变直

    女大十一变女大十一变女大十一变女大十一变女大十一变女大十一变女大十一变一十大女变一十大女变一十大女变一十大女变一十...

  • 十一月

    十一月,天上飘下水的指纹 十一月,上帝烹饪天鹅 十一月,距离十二月只剩一月 十一月,距离十月还有十一月 十一月,雪...

  • 十一 十一 十二

    有人说,朋友十二画,恋人十二画,爱人十二画,家人十二画。所以,十二的名字,叫做难忘。于我而言,她是十一。不是恋人,...

网友评论

      本文标题:十一、RPGType.h

      本文链接:https://www.haomeiwen.com/subject/qosxqctx.html