119 lines
3.2 KiB
C#
119 lines
3.2 KiB
C#
using UnityEngine;
|
|
|
|
/// <summary>
|
|
/// ScriptableObject defining item properties.
|
|
/// Implements IUsableItem and IEquippableItem for extensibility.
|
|
/// </summary>
|
|
[CreateAssetMenu(fileName = "New Item", menuName = "Inventory/Item")]
|
|
public class ItemData : ScriptableObject, IUsableItem, IEquippableItem
|
|
{
|
|
[Header("Basic Info")]
|
|
public int itemID;
|
|
public string itemName;
|
|
public Sprite icon;
|
|
[TextArea] public string description;
|
|
|
|
[Header("Stack & Weight")]
|
|
public float weight = 1f;
|
|
public int maxStack = 99;
|
|
|
|
[Header("Visual Source")]
|
|
[Tooltip("Original prefab for dropped item visuals")]
|
|
public GameObject originalBlockPrefab;
|
|
|
|
[Header("Item Behavior")]
|
|
[Tooltip("Defines what happens when the item is used")]
|
|
public ItemBehavior behavior;
|
|
|
|
[Header("Equipment Settings")]
|
|
[Tooltip("Whether this item can be equipped (shows in hand)")]
|
|
public bool isEquippable;
|
|
[Tooltip("Prefab spawned in player's hand when equipped")]
|
|
public GameObject equipmentPrefab;
|
|
public Vector3 equipPositionOffset;
|
|
public Vector3 equipRotationOffset;
|
|
[Tooltip("Name of the transform to attach equipment to")]
|
|
public string attachmentPointName = "ToolAnchor";
|
|
|
|
#region IUsableItem Implementation
|
|
|
|
public bool IsConsumable => behavior != null && behavior.IsConsumable;
|
|
|
|
public bool CanUse(GameObject user, GameObject target)
|
|
{
|
|
return behavior != null && behavior.CanUse(user, target);
|
|
}
|
|
|
|
public void Use(GameObject user, GameObject target)
|
|
{
|
|
if (behavior != null)
|
|
{
|
|
behavior.Use(user, target);
|
|
}
|
|
}
|
|
|
|
public ActionDescriptor GetUseAction()
|
|
{
|
|
return behavior?.GetActionDescriptor();
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region IEquippableItem Implementation
|
|
|
|
public GameObject GetEquipmentPrefab() => equipmentPrefab;
|
|
public Vector3 GetPositionOffset() => equipPositionOffset;
|
|
public Vector3 GetRotationOffset() => equipRotationOffset;
|
|
public string GetAttachmentPointName() => attachmentPointName;
|
|
|
|
public Transform FindAttachmentPoint(GameObject user)
|
|
{
|
|
if (string.IsNullOrEmpty(attachmentPointName))
|
|
return user.transform;
|
|
|
|
var transforms = user.GetComponentsInChildren<Transform>();
|
|
foreach (var t in transforms)
|
|
{
|
|
if (t.name == attachmentPointName)
|
|
return t;
|
|
}
|
|
|
|
return user.transform;
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Utility Properties
|
|
|
|
/// <summary>
|
|
/// Check if this item has any usable behavior.
|
|
/// </summary>
|
|
public bool HasBehavior => behavior != null;
|
|
|
|
/// <summary>
|
|
/// Check if this item can be equipped.
|
|
/// </summary>
|
|
public bool CanBeEquipped => isEquippable && equipmentPrefab != null;
|
|
|
|
#endregion
|
|
|
|
#region Utility Methods
|
|
|
|
/// <summary>
|
|
/// Get display name for UI.
|
|
/// </summary>
|
|
public string GetDisplayName() => string.IsNullOrEmpty(itemName) ? name : itemName;
|
|
|
|
/// <summary>
|
|
/// Get description for UI.
|
|
/// </summary>
|
|
public string GetDescription() => description;
|
|
|
|
/// <summary>
|
|
/// Calculate total weight for a stack.
|
|
/// </summary>
|
|
public float GetStackWeight(int count) => weight * count;
|
|
|
|
#endregion
|
|
}
|