Home Game Development c# – How can I make a crafting system in Unity?

c# – How can I make a crafting system in Unity?

0
c# – How can I make a crafting system in Unity?

[ad_1]

My impulse could be to create an summary Item class that derives from ScriptableObject, and from that derive CraftableItem and Element lessons.

Item would describe the fundamental properties and strategies of any stock merchandise, similar to fashions or sprites utilized by the UI, description, title price. Methods may embody features to generate runtime cases, to find out whether or not the participant can purchase, and so on.

CraftableItem would have properties to explain the elements, crafting time and every other stipulations, together with no matter strategies are wanted to craft, use and so on.

utilizing System.Collections;
utilizing System.Collections.Generic;
utilizing UnityEngine;
utilizing CraftingStuff;

public summary class Item : ScriptableObject
{
    [Tooltip("What is this thing called?")]
    public string title;
    [Tooltip("2D image for use in dialogs, etc")]
    public Sprite sprite;
    [Tooltip ("How many inventory slots does this item occupy")]
    public int measurement;
    [Tooltip("game object to be instantiated")]
    public GameObject prefab;
    public summary ItemType Type { get; }

    /// <abstract>
    /// Creates the derived runtime class occasion and populates it as required.
    /// </abstract>
    /// <returns>The derived class runtime, forged to ItemRuntime.</returns>
    public summary ItemRuntime GenerateRuntime();

    /// <abstract>
    /// Creates the weather wanted by the merchandise acquistion modal.
    /// </abstract>
    /// <returns>An ItemModalInfo object, which accommodates the mandatory information to populate the modal.</returns>
    /// <param title="runtime">Runtime.</param>
    public summary ItemModalInfo PopulateItemModal(ItemRuntime runtime);

    /// <abstract>
    /// Perform an mandatory actions to instantiate the item within the sport world.
    /// </abstract>
    /// <param title="runtime">Runtime.</param>
    public summary void InstantiateGameObject(ItemRuntime runtime);

    /// <abstract>
    /// Checks the participant can purchase the merchandise.
    /// </abstract>
    /// <returns>enum representing whether or not participant can purchase or motive why not (e.g. not sufficient cash, no room, and so on).</returns>
    /// <param title="runtime">ItemRuntime.</param>
    public summary AquisitionAbilityCheck CheckPlayerCanAcquire(ItemRuntime runtime);

    /// <abstract>
    /// Called when the participant chooses so as to add the merchandise. Performs no matter housekeeping is critical, together with deducting the value from the participant's 
    /// money, Instantiating or transferring gameobjects, creating loans, and so on.
    /// </abstract>
    /// <param title="runtime">the merchandise runtime.</param>
    public summary void AddItem(ItemRuntime runtime, ref string log);
    /// <abstract>
    /// Called if the participant declines or in any other case cannot settle for the merchandise.
    /// </abstract>
    /// <param title="runtime">Runtime.</param>
    public summary void DeclineItem(ItemRuntime runtime);

    /// <abstract>
    /// Called when the participant loses or sells the merchandise. Performs housekeeping similar to eradicating gameobjects, cancelling loans, and so on.
    /// </abstract>
    /// <returns>money accrued from promoting or repo'ing the merchandise</returns>
    /// <param title="runtime">The merchandise runtime.</param>
    public summary float Take awayItem(ItemRuntime runtime);
}

ItemRuntime and a few of these different issues could be described within the CraftingStuff namespace, together with a definition of the Ingredient class:

utilizing UnityEngine;

namespace CraftingStuff
{
   
     public enum AquisitionAbilityCheck
    {
        canAcquire,
        lackingIngredients,
        missingBench,
        noRoom,
        underSpell
    }

    public enum ItemType
    {
        ingredient,
        uncommonElement,
        craftable,
        weapon,
        device,
        meals,
        bench
    }

    public enum BenchType
    {
        tool1,
        forge1,
        food1,
        weapon1,
        tool2,
        forge2,
        weapon2
    }

    [System.Serializable]
    public class Ingredient
    {
        public Item merchandise;
        public int quantity;
    }

    
    [System.Serializable]
    public class ItemModalInfo
    {
        public string title;
        public string subtitle;
        public string description;
        public string extraInfo;
        public string acceptButtonText;
        public string declineButtonText;

        //you possibly can additionally outline callbacks right here
        /*
        public UIController.AfterModalClose afterModalClose = null;
        public UIController.AfterModalOpen afterModalOpen = null;
        public UIController.ModalButtonClose modalButtonClose = null;
        public bool showUIAfterModalClose = true;
        */
    }

    

    [System.Serializable]
    public summary class ItemRuntime
    {
        public int id;
        public summary Item Immutable { get; set; }
        public GameObject gameObject;
      

        public digital void SomeHandler(Transform t)
        {
           //do one thing with t
        }

        public digital void SomeOtherHandler(Weather climate)
        {
           //possibly climate impacts objects. Who is aware of? 
        }
    }

    public enum Weather
    {
        solar,
        rain,
        sleet,
        snow,
        storm,
        wind
    }
}

The Basic Element Class would seem like this:

utilizing System.Collections;
utilizing System.Collections.Generic;
utilizing CraftingStuff;
utilizing UnityEngine;

[CreateAssetMenu(menuName = "Item/ New Element")]
public class Element : Item
{
    public override ItemType Type { get { return ItemType.ingredient; } }

    public override void AddItem(ItemRuntime runtime, ref string log)
    {
        //do no matter wanted when participant provides this merchandise to stock
    }

    public override AquisitionAbilityCheck CheckPlayerCanAcquire(ItemRuntime runtime)
    {
        // examine if consumer can truly purchase.
        return AquisitionAbilityCheck.canAcquire;
    }

    public override void DeclineItem(ItemRuntime runtime)
    {
        //do any housekeeping after declining
    }

    public override ItemRuntime GenerateRuntime()
    {
        var element_rt = new ElementRuntime();
        element_rt.Immutable = this;
        // do different initialization for runtime occasion
        return element_rt;
    }

    public override void InstantiateGameObject(ItemRuntime runtime)
    {
        //create the in-game illustration, save reference in runtime
    }

    public override ItemModalInfo PopulateItemModal(ItemRuntime runtime)
    {
        var information = new ItemModalInfo();
        // populate
        return information;
    }

    public override float Take awayItem(ItemRuntime runtime)
    {
        return 0; // $ obtained for dropping merchandise
    }

}

public class ElementRuntime : ItemRuntime
{
    personal Element ingredient;
    public override Item Immutable { get { return ingredient; } set { ingredient = (Element)worth; } }
}

CraftableItem is much like Element, however provides stipulations within the type of an inventory of elements (Items with quantities) and a selected crafting bench that’s required. There can be a time property.

utilizing System.Collections;
utilizing System.Collections.Generic;
utilizing CraftingStuff;
utilizing UnityEngine;

[CreateAssetMenu(menuName = "Item/ New CraftableItem")]
public class CraftableItem : Item
{
    //new properties distinctive to craftables:
    public List<Ingredient> elements;
    public BenchType benchType;
    public float craftingTime;


    public override ItemType Type { get { return ItemType.craftable; } }

    public void CraftItem()
    {
        //if prereqs are met, start crafting
    }


    public override void AddItem(ItemRuntime runtime, ref string log)
    {
        //do no matter wanted when participant provides this merchandise to stock
    }

    public override AquisitionAbilityCheck CheckPlayerCanAcquire(ItemRuntime runtime)
    {
        // examine if consumer can truly purchase.
        // possibly participant would not have all of the elements
        return AquisitionAbilityCheck.canAcquire;
    }

    public override void DeclineItem(ItemRuntime runtime)
    {
        //do any housekeeping after declining
    }

    public override ItemRuntime GenerateRuntime()
    {
        var element_rt = new ElementRuntime();
        element_rt.Immutable = this;
        // do different initialization for runtime occasion
        return element_rt;
    }

    public override void InstantiateGameObject(ItemRuntime runtime)
    {
        //create the in-game illustration, save reference in runtime
    }

    public override ItemModalInfo PopulateItemModal(ItemRuntime runtime)
    {
        var information = new ItemModalInfo();
        // populate
        return information;
    }

    public override float Take awayItem(ItemRuntime runtime)
    {
        return 0; // $ obtained for dropping merchandise
    }

}

public class CraftableRuntime : ItemRuntime
{
    personal CraftableItem craftable;
    public override Item Immutable { get { return craftable; } set { craftable = (CraftableItem)worth; } }
}

Demo
(this can be a work in progress, I’ll replace as I transfer alongside.)

With our fundamental merchandise sorts outlined, we will begin creating issues. Let’s begin by defining some Elements and Some CraftableItems.

Either one might be created with the context menu by proper clicking within the mission pane:
Context Menu for CraftableItem

The outcome will likely be a brand new ScriptableObject that we will fill the main points in within the inspector. For occasion, that is the definition of an Element known as “Rock”:

Rock Element Inspector

(Note: I haven’t got time to provide you with precise sprites or fashions for these things, simply utilizing what’s laying round in my junk mission property)

Let’s additionally create an Element known as “Stick”:

Stick Element in Inspector

Next, let’s create a CraftableItem known as “Hammer.” Craftable objects have just a few extra fields to fill out. We’ll say a hammer prices one stick and one rock, requires a fundamental device bench, and takes 10 seconds to craft:

Hammer Craftable Item Inspector

Now let’s assemble the scene. This will simply be tremendous fundamental — Two UI grids, one to carry our present stock, and one other to carry objects that may be picked up or crafted. Start by including a Canvas Component (which ought to herald an OccasionSystem together with it). We’ll additionally want a brand new empty sport object that we’ll name Controller — it will maintain the singleton part that would be the glue for our demo.

Basic Hierarchy

Now, to the Canvas we’ll create two empty kids which we are going to name InventoryGrid and ItemGrid:

Grids added to Heirarchy

Each of those gameobjects will get a GridLayoutGroup part. They are configured equally, one occupying the highest half of the display, the opposite, the underside. Here’s the config for the highest grid object:

Inventory Grid configuration

You can rapidly take a look at populate these grids with dummy kids which have Image elements to see the way it appears to be like:
A bunch of dummy images added to the grids

Scene view of Grid Test

(…To be continued with demo implementation)

[ad_2]

LEAVE A REPLY

Please enter your comment!
Please enter your name here