Adventure Creator Wikia
Advertisement

AC's crafting system allows for multiple Inventory items to be combined to create a single resulting item.  Such crafting typically occurs instantly - once the correct items are in the Crafting slots, or when the user presses the "Create" button, the result appears in the Output box.

The following script causes the result to appear after a delay - so that the Crafting process appears to take time to "cook" the resulting item.  It also allows for the creation time to be displayed visually in a UI Slider component.

To use it:

  • Copy the script below into a C# file named CookingTimer.cs.
  • Attach the new Cooking Timer component to a GameObject in the scene.  This can be a Unity UI prefab spawned in by the Menu Manager
  • Fill in its Inspector, entering the name of the Crafting menu, and the Crafting element that has its "Crafting element type" property set to Output.
  • Find the same "Output" Crafting element in the Menu Manager, and uncheck "Result is automatic?" in its list of properties
  • (Optional) Assign a UI Slider component that represents the cooking time.
using UnityEngine;
using UnityEngine.UI;
using AC;

public class CookingTimer : MonoBehaviour
{

	public string menuName = "Crafting";
	public string craftingOutputName = "Crafting";
	public Slider slider;
	public readonly float cookingTimer;
	public readonly float maxCookingTime;

	void OnEnable ()
	{
		EventManager.OnInventoryAdd_Alt += OnInventoryUpdate;
		EventManager.OnInventoryRemove_Alt += OnInventoryUpdate;
	}

	void OnEnable ()
	{
		EventManager.OnInventoryAdd_Alt -= OnInventoryUpdate;
		EventManager.OnInventoryRemove_Alt -= OnInventoryUpdate;
	}

	void OnInventoryUpdate (InvCollection invCollection, InvInstance invInstance, int amount)
	{
		MenuCrafting outputBox = PlayerMenus.GetElementWithName (menuName, craftingOutputName) as MenuCrafting;
		InvCollection ingredients = outputBox.IngredientsInvCollection;
		if (ingredients == invCollection)
		{
			Recipe activeRecipe = KickStarter.runtimeInventory.CalculateRecipe (ingredients);
			if (activeRecipe != null)
			{
				// Calculate cooking time here
				cookingTimer = 1f;
			}
			else
			{
				cookingTimer = 0f;
			}
			maxCookingTime = cookingTimer;
		}
	}

	void Update ()
	{
		if (cookingTimer > 0f)
		{
			cookingTimer -= Time.deltaTime;
			if (cookingTimer <= 0f)
			{
				MenuCrafting outputBox = PlayerMenus.GetElementWithName (menuName, craftingOutputName) as MenuCrafting;
				outputBox.SetOutput ();
			}
		}

		if (slider)
		{
			slider.value = cookingTimer / maxCookingTime;
		}
	}

}
Advertisement