数据驱动 组件系统允许通过JSON文件定义物品和方块的行为。这使得玩家和开发者可以更容易地添加或修改游戏内容,而不需要直接修改游戏代码。
模块化 组件系统是模块化的,意味着不同的组件可以被组合在一起,为物品和方块赋予多种行为。这种设计使得定制和扩展变得非常灵活。
易用性 通过组件系统,创建新物品和方块的过程更加直观和易于理解。玩家只需要编辑一些配置文件,就可以实现复杂的游戏机制。
Copy UCLASS ()
class CHARMINGCRAFT_API UNativeCraftComponent : public UObject
{
GENERATED_BODY ()
public :
UFUNCTION (BlueprintCallable)
bool IsAllowMultipleInstance () const ;
UFUNCTION ( BlueprintCallable )
void SetIsAllowMultipleInstance ( bool bIsAllowMultipleInstance);
UFUNCTION ( BlueprintCallable )
bool IsActive () const ;
UFUNCTION ( BlueprintCallable )
void SetIsActive ( bool bIsActive);
UPROPERTY ( BlueprintReadWrite , EditAnywhere )
FString Description;
public :
UNativeCraftComponent ();
private :
// Whether allow item or entity have multiple same class of components
UPROPERTY ( BlueprintReadWrite , EditAnywhere )
bool bIsAllowMultipleInstance = true ;
UPROPERTY ( BlueprintReadWrite , EditAnywhere )
bool bIsActive = false ;
};
Copy UCLASS (ClassGroup = (Custom) , meta = (BlueprintSpawnableComponent))
class CHARMINGCRAFT_API UItemStack : public UPersistentDataContainer , public IComponentizedObjectInterface
{
GENERATED_BODY ()
public :
// Sets default values for this component's properties
UItemStack ();
UPROPERTY ( BlueprintReadWrite , EditAnywhere , Category = "ItemStack Fields" )
int32 Amount;
UPROPERTY ( BlueprintReadWrite , EditAnywhere , Category = "ItemStack Fields" )
EMaterial Material; //
UPROPERTY ( BlueprintReadOnly , VisibleAnywhere , Category = "ItemStack InternalData" )
TSubclassOf < UItem > ItemClass;
UPROPERTY ( BlueprintReadWrite , EditAnywhere , Category = "ItemStack Fields" )
UItemMeta * ItemMeta;
UPROPERTY ()
TArray < UNativeCraftComponent *> Components;
virtual UNativeCraftComponent * AddComponents_Implementation ( UNativeCraftComponent * AddedComponents) override ;
virtual bool RemoveComponents_Implementation ( UNativeCraftComponent * RemovededComponents) override ;
virtual TArray < UNativeCraftComponent * > GetObjectComponentsByClass_Implementation ( TSubclassOf < UNativeCraftComponent > ComponentsClass) override ;
virtual TArray < UNativeCraftComponent * > GetObjectComponents_Implementation () override ;
};
ComponentizedObjectInterface.h
Copy UINTERFACE ()
class UComponentizedObjectInterface : public UInterface
{
GENERATED_BODY ()
};
class CHARMINGCRAFT_API IComponentizedObjectInterface
{
GENERATED_BODY ()
public :
UFUNCTION (BlueprintCallable , BlueprintNativeEvent)
TArray < UNativeCraftComponent * > GetObjectComponents ();
UFUNCTION ( BlueprintCallable , BlueprintNativeEvent )
TArray < UNativeCraftComponent * > GetObjectComponentsByClass ( TSubclassOf < UNativeCraftComponent > ComponentsClass);
UFUNCTION ( BlueprintCallable , BlueprintNativeEvent )
UNativeCraftComponent * AddComponents ( UNativeCraftComponent * AddedComponents);
UFUNCTION ( BlueprintCallable , BlueprintNativeEvent )
bool RemoveComponents ( UNativeCraftComponent * RemovededComponents);
};