59 lines
2.0 KiB
C#
59 lines
2.0 KiB
C#
using UnityEngine;
|
|
|
|
[CreateAssetMenu(
|
|
fileName = "ItemProcessingRecipe",
|
|
menuName = "FORT-BO/Workstations/Item Processing Recipe")]
|
|
public sealed class ItemProcessingRecipe : ScriptableObject
|
|
{
|
|
[SerializeField] private string recipeId;
|
|
[SerializeField] private ItemDefinition inputItem;
|
|
[SerializeField] private ItemDefinition outputItem;
|
|
[SerializeField, Min(0.05f)] private float processingDuration = 3f;
|
|
|
|
public string RecipeId => recipeId;
|
|
public ItemDefinition InputItem => inputItem;
|
|
public ItemDefinition OutputItem => outputItem;
|
|
public float ProcessingDuration => processingDuration;
|
|
|
|
public bool Accepts(NetworkItem item)
|
|
{
|
|
if (item == null || item.Definition == null || inputItem == null)
|
|
return false;
|
|
|
|
return item.Definition == inputItem ||
|
|
(!string.IsNullOrEmpty(inputItem.ItemId) && item.ItemId == inputItem.ItemId);
|
|
}
|
|
|
|
public bool TryValidateOutputPrefab(out string error)
|
|
{
|
|
error = string.Empty;
|
|
if (outputItem == null || outputItem.WorldPrefab == null)
|
|
return true;
|
|
|
|
NetworkItem prefabItem = outputItem.WorldPrefab.GetComponent<NetworkItem>();
|
|
if (prefabItem == null)
|
|
{
|
|
error = $"Output prefab '{outputItem.WorldPrefab.name}' has no NetworkItem.";
|
|
return false;
|
|
}
|
|
|
|
if (prefabItem.Definition == outputItem)
|
|
return true;
|
|
|
|
string actualOutput = prefabItem.Definition != null
|
|
? prefabItem.Definition.name
|
|
: "None";
|
|
error = $"Output item '{outputItem.name}' points to prefab " +
|
|
$"'{outputItem.WorldPrefab.name}', but that prefab uses definition " +
|
|
$"'{actualOutput}'.";
|
|
return false;
|
|
}
|
|
|
|
private void OnValidate()
|
|
{
|
|
recipeId = recipeId?.Trim().ToLowerInvariant();
|
|
if (!TryValidateOutputPrefab(out string error))
|
|
Debug.LogError($"Invalid processing recipe '{name}': {error}", this);
|
|
}
|
|
}
|