第一次提交

This commit is contained in:
不明不惑
2026-03-03 01:23:02 +08:00
commit 3e434877e8
1053 changed files with 102411 additions and 0 deletions

View File

@@ -0,0 +1,105 @@
// Copyright 2025 https://yuewu.dev/en All Rights Reserved.
#pragma once
#include "CoreMinimal.h"
#include "Net/Serialization/FastArraySerializer.h"
#include "GameplayTagContainer.h"
#include "GIS_CurrencyEntry.h"
#include "GIS_CurrencyContainer.generated.h"
class UGIS_CurrencySystemComponent;
class UGIS_CurrencyDefinition;
struct FGIS_CurrencyContainer;
/**
* Container for storing currency entries.
* 用于存储货币条目的容器。
*/
USTRUCT()
struct GENERICINVENTORYSYSTEM_API FGIS_CurrencyContainer : public FFastArraySerializer
{
GENERATED_BODY()
/**
* Default constructor for the currency container.
* 货币容器的默认构造函数。
*/
FGIS_CurrencyContainer()
{
};
/**
* Constructor for the currency container with an owning component.
* 使用拥有组件构造货币容器。
* @param InOwner The owning currency system component. 拥有此容器的货币系统组件。
*/
FGIS_CurrencyContainer(UGIS_CurrencySystemComponent* InOwner)
: OwningComponent(InOwner)
{
}
//~FFastArraySerializer contract
/**
* Called before entries are removed during replication.
* 复制期间移除条目前调用。
* @param RemovedIndices The indices of entries to remove. 要移除的条目索引。
* @param FinalSize The final size of the entries array after removal. 移除后条目数组的最终大小。
*/
void PreReplicatedRemove(const TArrayView<int32> RemovedIndices, int32 FinalSize);
/**
* Called after entries are added during replication.
* 复制期间添加条目后调用。
* @param AddedIndices The indices of added entries. 添加的条目索引。
* @param FinalSize The final size of the entries array after addition. 添加后条目数组的最终大小。
*/
void PostReplicatedAdd(const TArrayView<int32> AddedIndices, int32 FinalSize);
/**
* Called after entries are changed during replication.
* 复制期间条目更改后调用。
* @param ChangedIndices The indices of changed entries. 更改的条目索引。
* @param FinalSize The final size of the entries array after change. 更改后条目数组的最终大小。
*/
void PostReplicatedChange(const TArrayView<int32> ChangedIndices, int32 FinalSize);
//~End of FFastArraySerializer contract
/**
* Handles delta serialization for network replication.
* 处理网络复制的增量序列化。
* @param DeltaParms The serialization parameters. 序列化参数。
* @return True if serialization was successful, false otherwise. 如果序列化成功则返回true否则返回false。
*/
bool NetDeltaSerialize(FNetDeltaSerializeInfo& DeltaParms)
{
return FastArrayDeltaSerialize<FGIS_CurrencyEntry, FGIS_CurrencyContainer>(Entries, DeltaParms, *this);
}
/**
* The owning currency system component.
* 拥有此容器的货币系统组件。
*/
UPROPERTY()
TObjectPtr<UGIS_CurrencySystemComponent> OwningComponent{nullptr};
/**
* Array of currency entries.
* 货币条目数组。
*/
UPROPERTY(VisibleAnywhere, Category="Currency", meta=(DisplayName="Currencies", TitleProperty="{Definition}->{Value}"))
TArray<FGIS_CurrencyEntry> Entries;
};
/**
* Traits for the currency container to enable network delta serialization.
* 货币容器的特性,用于启用网络增量序列化。
*/
template <>
struct TStructOpsTypeTraits<FGIS_CurrencyContainer> : TStructOpsTypeTraitsBase2<FGIS_CurrencyContainer>
{
enum
{
WithNetDeltaSerializer = true,
};
};

View File

@@ -0,0 +1,127 @@
// Copyright 2025 https://yuewu.dev/en All Rights Reserved.
#pragma once
#include "CoreMinimal.h"
#include "Engine/DataAsset.h"
#include "Engine/Texture2D.h"
#include "GIS_CurrencyDefinition.generated.h"
class UGIS_CurrencyDefinition;
/**
* Struct to represent a currency exchange rate.
* 表示货币汇率的结构体。
*/
struct FGIS_CurrencyExchangeRate
{
/**
* The currency definition for the exchange rate.
* 汇率的货币定义。
*/
TObjectPtr<const UGIS_CurrencyDefinition> Currency;
/**
* The exchange rate value.
* 汇率值。
*/
float ExchangeRate;
/**
* Constructor for the currency exchange rate.
* 货币汇率的构造函数。
* @param InCurrency The currency definition. 货币定义。
* @param InExchangeRate The exchange rate value. 汇率值。
*/
FGIS_CurrencyExchangeRate(const UGIS_CurrencyDefinition* InCurrency, float InExchangeRate);
};
/**
* Defines properties for a currency type in the inventory system.
* 定义库存系统中货币类型的属性。
*/
UCLASS(BlueprintType)
class GENERICINVENTORYSYSTEM_API UGIS_CurrencyDefinition : public UDataAsset
{
GENERATED_BODY()
public:
/**
* The display name of the currency for UI purposes.
* 货币的UI显示名称。
*/
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category="Common")
FText DisplayName;
/**
* The description of the currency for UI purposes.
* 货币的UI描述。
*/
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category="Common")
FText Description;
/**
* The icon for the currency for UI display.
* 货币的UI图标。
*/
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category="Common")
TSoftObjectPtr<UTexture2D> Icon;
/**
* The maximum amount allowed for this currency (0 for unlimited).
* 货币的最大数量0表示无限制
* @details If the amount exceeds this value, it will attempt to convert to another currency.
* @细节 如果数量超过此值,将尝试转换为其他货币。
*/
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category="Common", meta=(ClampMin=0))
float MaxAmount{0};
/**
* The parent currency used to compute exchange rates.
* 用于计算汇率的父级货币。
*/
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category="Exchange")
TObjectPtr<const UGIS_CurrencyDefinition> ParentCurrency;
/**
* The exchange rate to the parent currency (e.g., 100 cents = 1 dollar).
* 相对于父级货币的汇率例如100分=1美元
* @details If this currency is a 10-unit note and the parent is a 100-unit note, set to 10 for 1:10 exchange.
* @细节 如果此货币是10元钞父级是100元钞设置值为10表示1个100元钞可兑换10个10元钞。
*/
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category="Exchange", meta=(ClampMin=1))
float ExchangeRateToParent{1};
/**
* The currency to convert to when this currency exceeds MaxAmount.
* 当货币数量超过最大值时转换到的溢出货币。
*/
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category="Exchange")
TObjectPtr<const UGIS_CurrencyDefinition> OverflowCurrency;
/**
* The currency to convert fractional remainders to.
* 分数余数转换到的分数货币。
*/
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category="Exchange")
TObjectPtr<const UGIS_CurrencyDefinition> FractionCurrency;
/**
* Attempts to get the exchange rate to another currency.
* 尝试获取针对其他货币的汇率。
* @param OtherCurrency The currency to compare with. 要比较的其他货币。
* @param ExchangeRate The resulting exchange rate (output). 输出的汇率。
* @return True if the exchange rate was found, false otherwise. 如果找到汇率则返回true否则返回false。
* @details Returns the rate as "1 this currency = ExchangeRate other currency".
* @细节 以“1个当前货币 = 汇率个其他货币”的形式返回汇率。
*/
bool TryGetExchangeRateTo(const UGIS_CurrencyDefinition* OtherCurrency, double& ExchangeRate) const;
/**
* Gets the exchange rate to the root currency.
* 获取相对于根货币的汇率。
* @param AdditionalExchangeRate An external exchange rate for recursive calculations. 用于递归计算的外部汇率。
* @return The exchange rate to the root currency. 相对于根货币的汇率。
*/
FGIS_CurrencyExchangeRate GetRootExchangeRate(double AdditionalExchangeRate = 1) const;
};

View File

@@ -0,0 +1,94 @@
// Copyright 2025 https://yuewu.dev/en All Rights Reserved.
#pragma once
#include "CoreMinimal.h"
#include "Net/Serialization/FastArraySerializer.h"
#include "UObject/Object.h"
#include "GIS_CurrencyEntry.generated.h"
class UGIS_CurrencyDefinition;
/**
* Represents a currency and its associated amount.
* 表示一种货币及其数量。
*/
USTRUCT(BlueprintType)
struct GENERICINVENTORYSYSTEM_API FGIS_CurrencyEntry : public FFastArraySerializerItem
{
GENERATED_BODY()
/**
* Default constructor for the currency entry.
* 货币条目的默认构造函数。
*/
FGIS_CurrencyEntry();
/**
* Constructor for the currency entry with specified definition and amount.
* 使用指定货币定义和数量构造货币条目。
* @param InDefinition The currency definition. 货币定义。
* @param InAmount The amount of the currency. 货币数量。
*/
FGIS_CurrencyEntry(const TObjectPtr<const UGIS_CurrencyDefinition>& InDefinition, float InAmount);
/**
* Constructor for the currency entry with specified amount and definition (alternative order).
* 使用指定数量和货币定义构造货币条目(参数顺序相反)。
* @param InAmount The amount of the currency. 货币数量。
* @param InDefinition The currency definition. 货币定义。
*/
FGIS_CurrencyEntry(float InAmount, const TObjectPtr<const UGIS_CurrencyDefinition>& InDefinition);
/**
* Referenced currency definition.
* 引用的货币定义。
*/
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category="GIS")
TObjectPtr<const UGIS_CurrencyDefinition> Definition;
/**
* The amount of the currency.
* 货币的数量。
*/
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category="GIS")
float Amount;
/**
* The previous amount of the currency (not replicated).
* 货币的前一数量(不复制)。
*/
UPROPERTY(NotReplicated)
float PrevAmount = 0;
/**
* Checks if this currency entry is equal to another.
* 检查此货币条目是否与另一个相等。
* @param Other The other currency entry to compare with. 要比较的另一个货币条目。
* @return True if the entries are equal, false otherwise. 如果条目相等则返回true否则返回false。
*/
bool Equals(const FGIS_CurrencyEntry& Other) const;
/**
* Converts the currency entry to a string representation.
* 将货币条目转换为字符串表示。
* @return The string representation of the currency entry. 货币条目的字符串表示。
*/
FString ToString() const;
/**
* Equality operator to compare two currency entries.
* 比较两个货币条目的相等性运算符。
* @param Rhs The right-hand side currency entry to compare with. 要比较的右侧货币条目。
* @return True if the entries are equal, false otherwise. 如果条目相等则返回true否则返回false。
*/
bool operator==(const FGIS_CurrencyEntry& Rhs) const;
/**
* Inequality operator to compare two currency entries.
* 比较两个货币条目的不等性运算符。
* @param Rhs The right-hand side currency entry to compare with. 要比较的右侧货币条目。
* @return True if the entries are not equal, false otherwise. 如果条目不相等则返回true否则返回false。
*/
bool operator!=(const FGIS_CurrencyEntry& Rhs) const;
};

View File

@@ -0,0 +1,375 @@
// Copyright 2025 https://yuewu.dev/en All Rights Reserved.
#pragma once
#include "CoreMinimal.h"
#include "GIS_CurrencyContainer.h"
#include "GIS_CurrencyDefinition.h"
#include "Components/ActorComponent.h"
#include "GIS_CurrencySystemComponent.generated.h"
class UGIS_InventorySubsystem;
/**
* Delegate triggered when a currency amount changes.
* 货币数量变化时触发的委托。
* @param Currency The currency definition that changed. 发生变化的货币定义。
* @param OldAmount The previous amount of the currency. 之前的货币数量。
* @param NewAmount The new amount of the currency. 新的货币数量。
*/
DECLARE_DYNAMIC_MULTICAST_DELEGATE_ThreeParams(FGIS_CurrencyChangedSignature, const UGIS_CurrencyDefinition*, Currency, float, OldAmount, float, NewAmount);
/**
* Currency system component for managing an actor's currencies.
* 管理演员货币的货币系统组件。
* @details This component serves as a wrapper for the CurrencyContainer, handling currency-related operations.
* @细节 该组件作为CurrencyContainer的包装器处理与货币相关的操作。
*/
UCLASS(ClassGroup=(GIS), BlueprintType, Blueprintable, meta=(BlueprintSpawnableComponent))
class GENERICINVENTORYSYSTEM_API UGIS_CurrencySystemComponent : public UActorComponent
{
GENERATED_BODY()
friend FGIS_CurrencyContainer;
public:
/**
* Constructor for the currency system component.
* 货币系统组件的构造函数。
*/
UGIS_CurrencySystemComponent();
/**
* Gets the currency system component from an actor.
* 从一个演员获取货币系统组件。
* @param Actor The actor to query for the currency system component. 要查询货币系统组件的演员。
* @return The currency system component, or nullptr if not found. 货币系统组件如果未找到则返回nullptr。
*/
UFUNCTION(BlueprintCallable, BlueprintPure, Category="GIS|Inventory", Meta = (DefaultToSelf="Actor"))
static UGIS_CurrencySystemComponent* GetCurrencySystemComponent(const AActor* Actor);
/**
* Gets the properties that should be replicated for this component.
* 获取需要为此组件复制的属性。
* @param OutLifetimeProps Array to store the replicated properties. 存储复制属性的数组。
*/
virtual void GetLifetimeReplicatedProps(TArray<class FLifetimeProperty>& OutLifetimeProps) const override;
/**
* Initializes the component.
* 初始化组件。
*/
virtual void InitializeComponent() override;
/**
* Gets all currency information.
* 获取所有货币信息。
* @return Array of currency entries containing currency types and amounts. 包含货币类型和数量的货币条目数组。
*/
UFUNCTION(BlueprintCallable, Category="GIS|CurrencySystem")
virtual TArray<FGIS_CurrencyEntry> GetAllCurrencies() const;
/**
* Sets all currency information.
* 设置所有货币信息。
* @param InCurrencyInfos The array of currency entries to set. 要设置的货币条目数组。
*/
virtual void SetCurrencies(const TArray<FGIS_CurrencyEntry>& InCurrencyInfos);
/**
* Clears all currency information.
* 清空所有货币信息。
*/
UFUNCTION(BlueprintCallable, Category="GIS|CurrencySystem")
virtual void EmptyCurrencies();
/**
* Gets information for a specific currency.
* 获取指定货币的信息。
* @param CurrencyDefinition The currency definition to query. 要查询的货币定义。
* @param OutCurrencyInfo The currency information (output). 货币信息(输出)。
* @return True if the currency is found, false otherwise. 如果找到货币则返回true否则返回false。
*/
UFUNCTION(BlueprintCallable, BlueprintPure=false, Category="GIS|CurrencySystem", meta=(ExpandBoolAsExecs="ReturnValue"))
virtual bool GetCurrency(TSoftObjectPtr<const UGIS_CurrencyDefinition> CurrencyDefinition, FGIS_CurrencyEntry& OutCurrencyInfo) const;
/**
* Gets information for multiple specified currencies.
* 获取多个指定货币的信息。
* @param CurrencyDefinitions The array of currency definitions to query. 要查询的货币定义数组。
* @param OutCurrencyInfos The array of currency information (output). 货币信息数组(输出)。
* @return True if all specified currencies are found, false otherwise. 如果找到所有指定货币则返回true否则返回false。
*/
UFUNCTION(BlueprintCallable, BlueprintPure=false, Category="GIS|CurrencySystem", meta=(ExpandBoolAsExecs="ReturnValue"))
virtual bool GetCurrencies(TArray<TSoftObjectPtr<UGIS_CurrencyDefinition>> CurrencyDefinitions, TArray<FGIS_CurrencyEntry>& OutCurrencyInfos) const;
/**
* Adds a currency and its amount.
* 添加货币及其数量。
* @param CurrencyInfo The currency information to add. 要添加的货币信息。
* @return True if the currency was added successfully, false otherwise. 如果货币添加成功则返回true否则返回false。
* @details Adds the specified amount to an existing currency or creates a new entry if the currency doesn't exist.
* @细节 如果当前没有该货币,则以指定数量新增货币;如果已有,则在原始数量上累加。
*/
UFUNCTION(BlueprintCallable, Category="GIS|CurrencySystem")
virtual bool AddCurrency(FGIS_CurrencyEntry CurrencyInfo);
/**
* Removes a currency and its amount.
* 移除指定数量的货币。
* @param CurrencyInfo The currency information to remove. 要移除的货币信息。
* @return True if the currency was removed successfully, false if the currency doesn't exist. 如果货币移除成功则返回true如果货币不存在则返回false。
*/
UFUNCTION(BlueprintCallable, Category="GIS|CurrencySystem")
virtual bool RemoveCurrency(FGIS_CurrencyEntry CurrencyInfo);
/**
* Checks if the specified currency amount is available.
* 检查是否拥有指定数量的货币。
* @param CurrencyInfo The currency information to check. 要检查的货币信息。
* @return True if the specified amount is available, false otherwise. 如果有足够数量的货币则返回true否则返回false。
*/
UFUNCTION(BlueprintCallable, Category="GIS|CurrencySystem")
virtual bool HasCurrency(FGIS_CurrencyEntry CurrencyInfo) const;
/**
* Checks if multiple specified currency amounts are available.
* 检查是否拥有多个指定数量的货币。
* @param CurrencyInfos The array of currency information to check. 要检查的货币信息数组。
* @return True if all specified amounts are available, false otherwise. 如果所有货币数量都满足则返回true否则返回false。
*/
UFUNCTION(BlueprintCallable, Category="GIS|CurrencySystem")
virtual bool HasCurrencies(const TArray<FGIS_CurrencyEntry>& CurrencyInfos);
/**
* Adds multiple currencies.
* 添加多个货币。
* @param CurrencyInfos The array of currency information to add. 要添加的货币信息数组。
* @return True if all currencies were added successfully, false otherwise. 如果所有货币都添加成功则返回true否则返回false。
*/
UFUNCTION(BlueprintCallable, Category="GIS|CurrencySystem")
virtual bool AddCurrencies(const TArray<FGIS_CurrencyEntry>& CurrencyInfos);
/**
* Removes multiple currencies.
* 移除多个货币。
* @param CurrencyInfos The array of currency information to remove. 要移除的货币信息数组。
* @return True if all currencies were removed successfully, false otherwise. 如果所有货币都移除成功则返回true否则返回false。
*/
UFUNCTION(BlueprintCallable, Category="GIS|CurrencySystem")
virtual bool RemoveCurrencies(const TArray<FGIS_CurrencyEntry>& CurrencyInfos);
/**
* Called when the actor begins play.
* 演员开始播放时调用。
*/
virtual void BeginPlay() override;
/**
* Called when the actor ends play.
* 演员结束播放时调用。
* @param EndPlayReason The reason for ending play. 结束播放的原因。
*/
virtual void EndPlay(const EEndPlayReason::Type EndPlayReason) override;
/**
* Event triggered when a currency amount changes.
* 货币数量变化时触发的事件。
*/
UPROPERTY(BlueprintAssignable)
FGIS_CurrencyChangedSignature OnCurrencyChangedEvent;
#pragma region Static Calculations
#if 0
/**
* Finds the index of a currency in a list.
* 查找包含指定货币的索引。
* @param Currency The currency definition to find. 要查找的货币定义。
* @param List The list of currency entries to search. 要搜索的货币条目列表。
* @return The index of the currency, or -1 if not found. 货币的索引,如果未找到则返回-1。
*/
static int32 FindIndexWithCurrency(const UGIS_CurrencyDefinition* Currency, const TArray<FGIS_CurrencyEntry>& List);
/**
* Finds or creates an index for a currency in a list.
* 查找或创建包含指定货币的索引。
* @param Currency The currency definition to find or create. 要查找或创建的货币定义。
* @param Result The list of currency entries (modified). 货币条目列表(可修改)。
* @return The index of the currency. 货币的索引。
*/
static int32 FindOrCreateCurrencyIndex(const UGIS_CurrencyDefinition* Currency, TArray<FGIS_CurrencyEntry>& Result);
/**
* Performs discrete addition of two currency lists.
* 执行两个货币列表的离散加法。
* @param Lhs The first currency list. 第一个货币列表。
* @param Rhs The second currency list. 第二个货币列表。
* @return The combined currency list. 合并后的货币列表。
*/
static TArray<FGIS_CurrencyEntry> DiscreteAddition(const TArray<FGIS_CurrencyEntry>& Lhs, const TArray<FGIS_CurrencyEntry>& Rhs);
/**
* Adds a single currency and amount to a currency list.
* 将单一货币和数量添加到货币列表。
* @param CurrencyAmounts The currency list to modify. 要修改的货币列表。
* @param Currency The currency definition to add. 要添加的货币定义。
* @param Amount The amount to add. 要添加的数量。
* @return The updated currency list. 更新后的货币列表。
*/
static TArray<FGIS_CurrencyEntry> DiscreteAddition(const TArray<FGIS_CurrencyEntry>& CurrencyAmounts,
const UGIS_CurrencyDefinition* Currency, float Amount);
/**
* Sets the fractional part of a currency to its maximum value.
* 将货币的小数部分设置为最大值。
* @param CurrencyAmounts The currency list to modify. 要修改的货币列表。
* @param Currency The currency definition to adjust. 要调整的货币定义。
* @return The updated currency list. 更新后的货币列表。
*/
static TArray<FGIS_CurrencyEntry> SetAllFractionToMax(const TArray<FGIS_CurrencyEntry>& CurrencyAmounts, const UGIS_CurrencyDefinition* Currency);
/**
* Converts an amount to discrete currency units.
* 将数量转换为离散的货币单位。
* @param Currency The currency definition to convert. 要转换的货币定义。
* @param Amount The amount to convert. 要转换的数量。
* @return The discrete currency entries. 离散的货币条目。
*/
static TArray<FGIS_CurrencyEntry> ConvertToDiscrete(const UGIS_CurrencyDefinition* Currency, double Amount);
/**
* Converts overflow amounts, ignoring fractional parts.
* 转换溢出金额,忽略小数部分。
* @param Currency The currency definition to convert. 要转换的货币定义。
* @param Amount The amount to convert. 要转换的数量。
* @return The overflow currency entries. 溢出的货币条目。
*/
static TArray<FGIS_CurrencyEntry> ConvertOverflow(const UGIS_CurrencyDefinition* Currency, double Amount);
/**
* Converts the fractional part of an amount to currency units.
* 将金额的小数部分转换为货币单位。
* @param Currency The currency definition to convert. 要转换的货币定义。
* @param Amount The amount to convert. 要转换的数量。
* @return The fractional currency entries. 小数部分的货币条目。
*/
static TArray<FGIS_CurrencyEntry> ConvertFraction(const UGIS_CurrencyDefinition* Currency, double Amount);
/**
* Gets the maximum amount for a currency in discrete units.
* 获取货币的最大金额(以离散单位计)。
* @param Currency The currency definition to query. 要查询的货币定义。
* @return The maximum currency entries. 最大货币条目。
*/
static TArray<FGIS_CurrencyEntry> MaxedOutAmount(const UGIS_CurrencyDefinition* Currency);
#endif
#pragma endregion
protected:
/**
* Called when a currency entry is added.
* 货币条目添加时调用。
* @param Entry The added currency entry. 添加的货币条目。
* @param Idx The index of the added entry. 添加条目的索引。
*/
virtual void OnCurrencyEntryAdded(const FGIS_CurrencyEntry& Entry, int32 Idx);
/**
* Called when a currency entry is removed.
* 货币条目移除时调用。
* @param Entry The removed currency entry. 移除的货币条目。
* @param Idx The index of the removed entry. 移除条目的索引。
*/
virtual void OnCurrencyEntryRemoved(const FGIS_CurrencyEntry& Entry, int32 Idx);
/**
* Called when a currency entry is updated.
* 货币条目更新时调用。
* @param Entry The updated currency entry. 更新的货币条目。
* @param Idx The index of the updated entry. 更新条目的索引。
* @param OldAmount The previous amount of the currency. 之前的货币数量。
* @param NewAmount The new amount of the currency. 新的货币数量。
*/
virtual void OnCurrencyEntryUpdated(const FGIS_CurrencyEntry& Entry, int32 Idx, float OldAmount, float NewAmount);
/**
* Called when a currency amount changes.
* 货币数量变化时调用。
* @param Tag The currency definition that changed. 发生变化的货币定义。
* @param OldValue The previous amount of the currency. 之前的货币数量。
* @param NewValue The new amount of the currency. 新的货币数量。
*/
virtual void OnCurrencyChanged(TObjectPtr<const UGIS_CurrencyDefinition> Tag, float OldValue, float NewValue);
/**
* Adds initial currencies to the component.
* 向组件添加初始货币。
*/
UFUNCTION(BlueprintNativeEvent, Category="GIS|CurrencySystem")
void AddInitialCurrencies();
/**
* Internal function to get information for a specific currency.
* 获取指定货币信息的内部函数。
* @param CurrencyTag The currency definition to query. 要查询的货币定义。
* @param OutCurrencyInfo The currency information (output). 货币信息(输出)。
* @return True if the currency is found, false otherwise. 如果找到货币则返回true否则返回false。
*/
virtual bool GetCurrencyInternal(const TObjectPtr<const UGIS_CurrencyDefinition>& CurrencyTag, FGIS_CurrencyEntry& OutCurrencyInfo) const;
/**
* Internal function to get information for multiple currencies.
* 获取多个货币信息的内部函数。
* @param Currencies The array of currency definitions to query. 要查询的货币定义数组。
* @param OutCurrencies The array of currency information (output). 货币信息数组(输出)。
* @return True if all currencies are found, false otherwise. 如果找到所有货币则返回true否则返回false。
*/
virtual bool GetCurrenciesInternal(const TArray<TObjectPtr<const UGIS_CurrencyDefinition>>& Currencies, TArray<FGIS_CurrencyEntry>& OutCurrencies) const;
/**
* Internal function to add a currency.
* 添加货币的内部函数。
* @param CurrencyInfo The currency information to add. 要添加的货币信息。
* @param bNotify Whether to trigger notifications for the addition. 是否触发添加通知。
* @return True if the currency was added successfully, false otherwise. 如果货币添加成功则返回true否则返回false。
*/
virtual bool AddCurrencyInternal(const FGIS_CurrencyEntry& CurrencyInfo, bool bNotify = true);
/**
* Internal function to remove a currency.
* 移除货币的内部函数。
* @param CurrencyInfo The currency information to remove. 要移除的货币信息。
* @param bNotify Whether to trigger notifications for the removal. 是否触发移除通知。
* @return True if the currency was removed successfully, false otherwise. 如果货币移除成功则返回true否则返回false。
*/
virtual bool RemoveCurrencyInternal(const FGIS_CurrencyEntry& CurrencyInfo, bool bNotify = true);
/**
* Internal function to check if a currency amount is available.
* 检查货币数量是否可用的内部函数。
* @param CurrencyInfo The currency information to check. 要检查的货币信息。
* @return True if the specified amount is available, false otherwise. 如果有足够数量的货币则返回true否则返回false。
*/
virtual bool HasCurrencyInternal(const FGIS_CurrencyEntry& CurrencyInfo) const;
/**
* Default currencies to initialize the component with.
* 初始化组件的默认货币。
*/
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category="Currency")
TArray<FGIS_CurrencyEntry> DefaultCurrencies;
/**
* Container for currency entries.
* 货币条目的容器。
*/
UPROPERTY(VisibleAnywhere, Replicated, Category="Currency", meta=(ShowOnlyInnerProperties))
FGIS_CurrencyContainer Container;
/**
* Local cache mapping currency definitions to their amounts.
* 货币定义到其数量的本地缓存映射。
*/
UPROPERTY()
TMap<TObjectPtr<const UGIS_CurrencyDefinition>, float> CurrencyMap;
};

View File

@@ -0,0 +1,82 @@
// Copyright 2025 https://yuewu.dev/en All Rights Reserved.
#pragma once
#include "CoreMinimal.h"
#include "Items/GIS_ItemInfo.h"
#include "UObject/Interface.h"
#include "GIS_ShopCondition.generated.h"
class UGIS_CurrencySystemComponent;
class UGIS_ShopSystemComponent;
class UGIS_InventorySystemComponent;
// This class does not need to be modified.
/**
* Interface for defining custom buy condition checks.
* 定义自定义购买条件检查的接口。
*/
UINTERFACE()
class UGIS_ShopBuyCondition : public UInterface
{
GENERATED_BODY()
};
/**
* Interface for actors or components to implement custom buy condition logic.
* 供Actor或组件实现自定义购买条件逻辑的接口。
* @details Allows checking if an item can be bought based on shop, inventory, and currency conditions.
* @细节 允许根据商店、库存和货币条件检查道具是否可以购买。
*/
class GENERICINVENTORYSYSTEM_API IGIS_ShopBuyCondition
{
GENERATED_BODY()
public:
/**
* Checks if an item can be bought.
* 检查道具是否可以购买。
* @param Shop The shop system component. 商店系统组件。
* @param BuyerInventory The inventory of the buyer. 购买者的库存。
* @param CurrencySystem The currency system component. 货币系统组件。
* @param ItemInfo Information about the item to buy. 要购买的道具信息。
* @return True if the item can be bought, false otherwise. 如果道具可以购买则返回true否则返回false。
*/
virtual bool CanBuy(const UGIS_ShopSystemComponent* Shop, UGIS_InventorySystemComponent* BuyerInventory, UGIS_CurrencySystemComponent* CurrencySystem,
FGIS_ItemInfo ItemInfo) = 0;
};
// This class does not need to be modified.
/**
* Interface for defining custom sell condition checks.
* 定义自定义出售条件检查的接口。
*/
UINTERFACE()
class UGIS_ShopSellCondition : public UInterface
{
GENERATED_BODY()
};
/**
* Interface for actors or components to implement custom sell condition logic.
* 供Actor或组件实现自定义出售条件逻辑的接口。
* @details Allows checking if an item can be sold based on shop, inventory, and currency conditions.
* @细节 允许根据商店、库存和货币条件检查道具是否可以出售。
*/
class GENERICINVENTORYSYSTEM_API IGIS_ShopSellCondition
{
GENERATED_BODY()
public:
/**
* Checks if an item can be sold.
* 检查道具是否可以出售。
* @param Shop The shop system component. 商店系统组件。
* @param SellerInventory The inventory of the seller. 出售者的库存。
* @param CurrencySystem The currency system component. 货币系统组件。
* @param ItemInfo Information about the item to sell. 要出售的道具信息。
* @return True if the item can be sold, false otherwise. 如果道具可以出售则返回true否则返回false。
*/
virtual bool CanSell(const UGIS_ShopSystemComponent* Shop, UGIS_InventorySystemComponent* SellerInventory, UGIS_CurrencySystemComponent* CurrencySystem,
FGIS_ItemInfo ItemInfo) = 0;
};

View File

@@ -0,0 +1,254 @@
// Copyright 2025 https://yuewu.dev/en All Rights Reserved.
#pragma once
#include "CoreMinimal.h"
#include "GIS_CurrencyEntry.h"
#include "GIS_InventoryTags.h"
#include "Items/GIS_ItemInfo.h"
#include "Components/ActorComponent.h"
#include "GIS_ShopSystemComponent.generated.h"
class UGIS_CurrencySystemComponent;
class IGIS_ShopSellCondition;
class IGIS_ShopBuyCondition;
/**
* Component responsible for managing shop functionality, including buying and selling items.
* 负责管理商店功能的组件,包括购买和出售道具。
*/
UCLASS(ClassGroup=(GIS), meta=(BlueprintSpawnableComponent))
class GENERICINVENTORYSYSTEM_API UGIS_ShopSystemComponent : public UActorComponent
{
GENERATED_BODY()
public:
/**
* Constructor for the shop system component.
* 商店系统组件的构造函数。
*/
UGIS_ShopSystemComponent();
/**
* Gets the shop system component from an actor.
* 从一个演员获取商店系统组件。
* @param Actor The actor to query for the shop system component. 要查询商店系统组件的演员。
* @return The shop system component, or nullptr if not found. 商店系统组件如果未找到则返回nullptr。
*/
UFUNCTION(BlueprintCallable, BlueprintPure, Category="GIS|ShopSystem", Meta = (DefaultToSelf="Actor"))
static UGIS_ShopSystemComponent* GetShopSystemComponent(const AActor* Actor);
/**
* Gets the shop's inventory.
* 获取商店的库存。
* @return The shop's inventory component, or nullptr if not set. 商店的库存组件如果未设置则返回nullptr。
*/
UFUNCTION(BlueprintCallable, BlueprintPure, Category="GIS|ShopSystem")
virtual UGIS_InventorySystemComponent* GetInventory() const;
/**
* Attempts to buy an item from the shop.
* 尝试从商店购买道具。
* @param BuyerInventory The buyer's inventory component. 买家的库存组件。
* @param CurrencySystem The buyer's currency system component (to deduct currency). 买家的货币系统组件(用于扣除货币)。
* @param ItemInfo The item information to buy. 要购买的道具信息。
* @return True if the purchase was successful, false otherwise. 如果购买成功则返回true否则返回false。
*/
UFUNCTION(BlueprintCallable, Category="GIS|ShopSystem")
virtual bool BuyItem(UGIS_InventorySystemComponent* BuyerInventory, UGIS_CurrencySystemComponent* CurrencySystem, const FGIS_ItemInfo& ItemInfo);
/**
* Attempts to sell an item to the shop.
* 尝试向商店出售道具。
* @param SellerInventory The seller's inventory component. 卖家的库存组件。
* @param CurrencySystem The seller's currency system component (to add currency). 卖家的货币系统组件(用于添加货币)。
* @param ItemInfo The item information to sell. 要出售的道具信息。
* @return True if the sale was successful, false otherwise. 如果出售成功则返回true否则返回false。
*/
UFUNCTION(BlueprintCallable, Category="GIS|ShopSystem")
virtual bool SellItem(UGIS_InventorySystemComponent* SellerInventory, UGIS_CurrencySystemComponent* CurrencySystem, const FGIS_ItemInfo& ItemInfo);
/**
* Checks if a buyer can purchase an item from the shop.
* 检查买家是否可以从商店购买道具。
* @param BuyerInventory The buyer's inventory component. 买家的库存组件。
* @param CurrencySystem The buyer's currency system component (to check currency availability). 买家的货币系统组件(用于检查货币是否足够)。
* @param ItemInfo The item information to buy. 要购买的道具信息。
* @return True if the buyer can purchase the item, false otherwise. 如果买家可以购买道具则返回true否则返回false。
* @details Used for custom checks such as content locking or other game mechanics. 可用于自定义检查,如内容锁定或其他游戏机制。
*/
UFUNCTION(BlueprintCallable, Category="GIS|ShopSystem")
virtual bool CanBuyerBuyItem(UGIS_InventorySystemComponent* BuyerInventory, UGIS_CurrencySystemComponent* CurrencySystem, const FGIS_ItemInfo& ItemInfo) const;
/**
* Checks if a seller can sell an item to the shop.
* 检查卖家是否可以向商店出售道具。
* @param SellerInventory The seller's inventory component. 卖家的库存组件。
* @param CurrencySystem The seller's currency system component. 卖家的货币系统组件。
* @param ItemInfo The item information to sell. 要出售的道具信息。
* @return True if the seller can sell the item, false otherwise. 如果卖家可以出售道具则返回true否则返回false。
* @details Used for custom checks such as content locking or other game mechanics. 可用于自定义检查,如内容锁定或其他游戏机制。
*/
UFUNCTION(BlueprintCallable, Category="GIS|ShopSystem")
virtual bool CanSellerSellItem(UGIS_InventorySystemComponent* SellerInventory, UGIS_CurrencySystemComponent* CurrencySystem, const FGIS_ItemInfo& ItemInfo) const;
/**
* Checks if the specified item is available for purchase from the shop.
* 检查指定道具是否可从商店购买。
* @param ItemInfo The item information to check. 要检查的道具信息。
* @return True if the item is buyable, false otherwise. 如果道具可购买则返回true否则返回false。
*/
UFUNCTION(BlueprintCallable, Category="GIS|ShopSystem")
virtual bool IsItemBuyable(const FGIS_ItemInfo& ItemInfo) const;
/**
* Checks if the shop can buy the specified item from a seller.
* 检查商店是否可以从卖家购买指定道具。
* @param ItemInfo The item information to check. 要检查的道具信息。
* @return True if the item is sellable, false otherwise. 如果道具可出售则返回true否则返回false。
*/
UFUNCTION(BlueprintCallable, Category="GIS|ShopSystem")
virtual bool IsItemSellable(const FGIS_ItemInfo& ItemInfo) const;
/**
* Gets the buy price modifier for the buyer.
* 获取买家的购买价格浮动。
* @param BuyerInventory The buyer's inventory component. 买家的库存组件。
* @return The buy price modifier. 购买价格浮动值。
* @details Can be overridden to implement a dynamic pricing system based on game mechanics. 可覆写以基于游戏机制实现动态价格系统。
*/
UFUNCTION(BlueprintCallable, BlueprintNativeEvent, Category="GIS|ShopSystem")
float GetBuyModifierForBuyer(UGIS_InventorySystemComponent* BuyerInventory) const;
/**
* Gets the sell price modifier for the seller.
* 获取卖家的出售价格浮动。
* @param SellerInventory The seller's inventory component. 卖家的库存组件。
* @return The sell price modifier. 出售价格浮动值。
* @details Can be overridden to implement a dynamic pricing system based on game mechanics, such as supply shortages. 可覆写以基于游戏机制(如缺货)实现动态价格系统。
*/
UFUNCTION(BlueprintCallable, BlueprintNativeEvent, Category="GIS|ShopSystem")
float GetSellModifierForSeller(UGIS_InventorySystemComponent* SellerInventory) const;
/**
* Gets the buy value (currency cost) of an item for the buyer.
* 获取买家购买道具的货币价格。
* @param Buyer The buyer's inventory component. 买家的库存组件。
* @param ItemInfo The item information to buy. 要购买的道具信息。
* @param BuyValue The required currency for the purchase (output). 购买所需的货币(输出)。
* @return True if the buyer can afford the item, false otherwise. 如果买家买得起则返回true否则返回false。
*/
UFUNCTION(BlueprintCallable, BlueprintPure=false, BlueprintNativeEvent, Category="GIS|ShopSystem", meta=(ExpandBoolAsExecs="ReturnValue"))
bool TryGetBuyValueForBuyer(UGIS_InventorySystemComponent* Buyer, const FGIS_ItemInfo& ItemInfo, TArray<FGIS_CurrencyEntry>& BuyValue) const;
/**
* Gets the sell value (currency gained) of an item for the seller.
* 获取卖家出售道具的货币价格。
* @param Seller The seller's inventory component. 卖家的库存组件。
* @param ItemInfo The item information to sell. 要出售的道具信息。
* @param SellValue The currency gained from the sale (output). 出售获得的货币(输出)。
* @return True if the currency value is valid, false otherwise. 如果货币价格有效则返回true否则返回false。
*/
UFUNCTION(BlueprintCallable, BlueprintPure=false, BlueprintNativeEvent, Category="GIS|ShopSystem", meta=(ExpandBoolAsExecs="ReturnValue"))
bool TryGetSellValueForSeller(UGIS_InventorySystemComponent* Seller, const FGIS_ItemInfo& ItemInfo, TArray<FGIS_CurrencyEntry>& SellValue) const;
/**
* Called when the actor begins play.
* 演员开始播放时调用。
*/
virtual void BeginPlay() override;
/**
* Called when the actor ends play.
* 演员结束播放时调用。
* @param EndPlayReason The reason for ending play. 结束播放的原因。
*/
virtual void EndPlay(const EEndPlayReason::Type EndPlayReason) override;
protected:
/**
* Internal check for whether a seller can sell an item to the shop.
* 检查卖家是否可以向商店出售道具的内部函数。
* @param SellerInventory The seller's inventory component. 卖家的库存组件。
* @param CurrencySystem The seller's currency system component. 卖家的货币系统组件。
* @param ItemInfo The item information to sell. 要出售的道具信息。
* @return True if the seller can sell the item, false otherwise. 如果卖家可以出售道具则返回true否则返回false。
*/
UFUNCTION(BlueprintNativeEvent, Category="GIS|ShopSystem")
bool CanSellerSellItemInternal(UGIS_InventorySystemComponent* SellerInventory, UGIS_CurrencySystemComponent* CurrencySystem, const FGIS_ItemInfo& ItemInfo) const;
/**
* Internal check for whether a buyer can purchase an item from the shop.
* 检查买家是否可以从商店购买道具的内部函数。
* @param BuyerInventory The buyer's inventory component. 买家的库存组件。
* @param CurrencySystem The buyer's currency system component. 买家的货币系统组件。
* @param ItemInfo The item information to buy. 要购买的道具信息。
* @return True if the buyer can purchase the item, false otherwise. 如果买家可以购买道具则返回true否则返回false。
*/
UFUNCTION(BlueprintNativeEvent, Category="GIS|ShopSystem")
bool CanBuyerBuyItemInternal(UGIS_InventorySystemComponent* BuyerInventory, UGIS_CurrencySystemComponent* CurrencySystem, const FGIS_ItemInfo& ItemInfo) const;
/**
* Internal function to handle selling an item to the shop.
* 处理向商店出售道具的内部函数。
* @param SellerInventory The seller's inventory component. 卖家的库存组件。
* @param CurrencySystem The seller's currency system component. 卖家的货币系统组件。
* @param ItemInfo The item information to sell. 要出售的道具信息。
* @return True if the sale was successful, false otherwise. 如果出售成功则返回true否则返回false。
*/
UFUNCTION(BlueprintNativeEvent, Category="GIS|ShopSystem")
bool SellItemInternal(UGIS_InventorySystemComponent* SellerInventory, UGIS_CurrencySystemComponent* CurrencySystem, const FGIS_ItemInfo& ItemInfo);
/**
* Internal function to handle buying an item from the shop.
* 处理从商店购买道具的内部函数。
* @param BuyerInventory The buyer's inventory component. 买家的库存组件。
* @param CurrencySystem The buyer's currency system component. 买家的货币系统组件。
* @param ItemInfo The item information to buy. 要购买的道具信息。
* @return True if the purchase was successful, false otherwise. 如果购买成功则返回true否则返回false。
*/
UFUNCTION(BlueprintNativeEvent, Category="GIS|ShopSystem")
bool BuyItemInternal(UGIS_InventorySystemComponent* BuyerInventory, UGIS_CurrencySystemComponent* CurrencySystem, const FGIS_ItemInfo& ItemInfo);
/**
* The target item collection to add items to when purchased.
* 购买时道具添加到的目标道具集合。
*/
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category="Shop", meta=(Categories="GIS.Collection"))
FGameplayTag TargetItemCollectionToAddOnBuy = GIS_CollectionTags::Main;
/**
* Controls the buy price modifier for this shop.
* 控制此商店的购买价格浮动。
*/
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category="Shop", meta=(ClampMin=0))
float BuyPriceModifier = 0;
/**
* Controls the sell price modifier for this shop.
* 控制此商店的出售价格浮动。
*/
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category="Shop", meta=(ClampMin=0))
float SellPriceModifer = 0;
/**
* The inventory component associated with the shop.
* 与商店关联的库存组件。
*/
UPROPERTY()
TObjectPtr<UGIS_InventorySystemComponent> OwningInventory;
/**
* List of conditions for buying items from the shop.
* 商店购买道具的条件列表。
*/
UPROPERTY()
TArray<TScriptInterface<IGIS_ShopBuyCondition>> BuyConditions;
/**
* List of conditions for selling items to the shop.
* 向商店出售道具的条件列表。
*/
UPROPERTY()
TArray<TScriptInterface<IGIS_ShopSellCondition>> SellConditions;
};