Well since you guys have hugely contributed to me coding an entire crafting system out of thin air, I’d like to contribute back, even if it’s with something insanely little in return (it’s best I can do). If anyone needs a code that gets your character to run to the table and automatically open the crafting UI on his own when he arrives, here it is, integrated with @bixarrio 's ‘CraftingTable.cs’ script:
using System;
using GameDevTV.Inventories;
using GameDevTV.UI;
using RPG.Control;
using RPG.Movement;
using UnityEngine;
namespace RPG.Crafting {
public class CraftingTable : MonoBehaviour, IRaycastable
{
[SerializeField] float acceptanceRadius;
[SerializeField] ShowHideUI showHideUI;
public static event Action<CraftingRecipe[]> OnCrafting;
private CraftingRecipe[] allRecipes;
private bool isMovingTowardsCraftingTable = true; // this variable checks if the player is near the crafting table, or is
public bool isInCraftingUI = false; // this variable is a check that automatically shuts our Crafting UI down if we are interrupted
private void Start() {
allRecipes = Resources.LoadAll<CraftingRecipe>("");
}
public CursorType GetCursorType()
{
return CursorType.Crafting;
}
public void Awake() {
acceptanceRadius = 3.0f;
}
public bool HandleRaycast(PlayerController callingController)
{
if (Input.GetMouseButtonDown(0)) {
float distanceToCraftingTable = Vector3.Distance(transform.position, callingController.transform.position);
if (distanceToCraftingTable > acceptanceRadius) {
Debug.Log("Moving towards Crafting UI");
callingController.GetComponent<Mover>().MoveTo(transform.position, 1.0f);
isMovingTowardsCraftingTable = true;
if (isMovingTowardsCraftingTable && distanceToCraftingTable <= acceptanceRadius) {
isMovingTowardsCraftingTable = false;
Debug.Log("Opening up the Crafting UI");
OnCrafting?.Invoke(allRecipes);
}
return true;
}
else if (distanceToCraftingTable <= acceptanceRadius) {
Debug.Log("Opening up the Crafting UI");
OnCrafting?.Invoke(allRecipes);
isMovingTowardsCraftingTable = false;
return true;
}
}
if (isMovingTowardsCraftingTable && Vector3.Distance(transform.position, callingController.transform.position) <= acceptanceRadius) {
Debug.Log("We are at the Crafting Table, opening up the UI now");
OnCrafting?.Invoke(allRecipes);
isMovingTowardsCraftingTable = false;
return true;
}
return true;
}
public static bool CanCraftRecipe(CraftingRecipe recipe) {
var playerInventory = Inventory.GetPlayerInventory();
foreach (var ingredientConfig in recipe.GetIngredients()) {
var hasIngredient = playerInventory.HasItem(ingredientConfig.Ingredient, out var inventoryAmount);
if (!hasIngredient) return false;
var hasEnough = inventoryAmount >= ingredientConfig.Amount;
if (!hasEnough) return false;
}
return true;
}
}
}