Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question Hello guys, I want to add one feature in my game

Discussion in 'Code Editors & IDEs' started by Gurwinder6911, Apr 21, 2022.

  1. Gurwinder6911

    Gurwinder6911

    Joined:
    Apr 21, 2022
    Posts:
    1
    The feature is : Point system (x for completed order, y for wrong order, z for when you run out of time)
    How can i add it?
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. //New as of Feb.25rd
    6.  
    7. public class Service : MonoBehaviour
    8. {
    9.     public int numOrderCompleted;
    10.  
    11.     public GameObject customerInService;
    12.     public GameObject GameController;
    13.     public Text currentOrderText;
    14.     public Transform customerExitPlace;
    15.  
    16.     public float serviceRateAsCustomersPerHour = 20; // customer/hour
    17.     public float interServiceTimeInHours; // = 1.0 / Service time of customer Per Hour;
    18.     private float interServiceTimeInMinutes;
    19.     private float interServiceTimeInSeconds;
    20.  
    21.     public bool generateServices = false;
    22.     // minimum and maximum interservice time in seconds
    23.     public float minInterServiceTimeInSeconds = 3;
    24.     public float maxInterServiceTimeInSeconds = 60;
    25.     public System.Array sizeValues, baseValues, additionsValues;
    26.     public System.Type type;
    27.     public Size orderedSize;
    28.     public Base orderedBase;
    29.     public List<Additions> orderedAdditions;
    30.     public Order order;
    31.     Queue queueManager;
    32.  
    33.     //public Text Timer;
    34.     public Text orderText;
    35.     public float elapsedSeconds = 0f;
    36.  
    37.     public float timeScale = 1;
    38.  
    39.     public Slider sliderTScale;
    40.     public Dropdown sizeDropDown;
    41.     public Dropdown baseDropDown;
    42.     public Button caramelBtn, chocolateBtn, strawberryBtn, vanillaBtn, mapleBtn, peppermintBtn, submitBtn;
    43.  
    44.     private string UserCreatedOrder = "";
    45.     private string RandOrderItems = "";
    46.     private string orderMessage = "Order is Correct!";
    47.  
    48.     public enum ServiceIntervalTimeStrategy
    49.     {
    50.         ConstantIntervalTime,
    51.         UniformIntervalTime,
    52.         ExponentialIntervalTime,
    53.         ObservedIntervalTime
    54.     }
    55.  
    56.     public ServiceIntervalTimeStrategy serviceIntervalTimeStrategy = ServiceIntervalTimeStrategy.UniformIntervalTime;
    57.  
    58.     // Start is called before the first frame update
    59.     void Start()
    60.     {
    61.         interServiceTimeInHours = 1.0f / serviceRateAsCustomersPerHour;
    62.         interServiceTimeInMinutes = interServiceTimeInHours * 60;
    63.         interServiceTimeInSeconds = interServiceTimeInMinutes * 60;
    64.  
    65.         submitBtn.onClick.AddListener(CompareOrder);
    66.  
    67.         sizeDropDown.value = 0;
    68.         baseDropDown.value = 0;
    69.  
    70.         caramelBtn.onClick.AddListener(CaramelOnClick);
    71.         chocolateBtn.onClick.AddListener(ChocolatelOnClick);
    72.         strawberryBtn.onClick.AddListener(StrawberryOnClick);
    73.         vanillaBtn.onClick.AddListener(VanillaOnClick);
    74.         mapleBtn.onClick.AddListener(MapleOnClick);
    75.         peppermintBtn.onClick.AddListener(PeppermintOnClick);
    76.         GameController = GameObject.FindGameObjectWithTag("GameController");
    77.     }
    78.  
    79.     private void Update()
    80.     {
    81.         //timeScale = sliderTScale.value;
    82.  
    83.     }
    84.  
    85.     private void FixedUpdate()
    86.     {
    87.         elapsedSeconds += Time.deltaTime;
    88.         //Timer.text = "Total time in seconds: " + elapsedSeconds.ToString();
    89.  
    90.         currentOrderText.text = UserCreatedOrder;
    91.  
    92.         if (elapsedSeconds > 100)
    93.         {
    94.             if(numOrderCompleted == 5)
    95.             {
    96.                 GameController.GetComponent<SceneController>().LoadVictory();
    97.             }
    98.             else
    99.             {
    100.                 GameController.GetComponent<SceneController>().LoadLose();
    101.             }
    102.         }
    103.         else if(numOrderCompleted == 5)
    104.         {
    105.             GameController.GetComponent<SceneController>().LoadVictory();
    106.         }
    107.     }
    108.     private void OnTriggerEnter(Collider other)
    109.     {
    110.         if (other.gameObject.CompareTag("Customer"))
    111.         {
    112.             if (customerInService != other.gameObject)
    113.             {
    114.                 customerInService = other.gameObject;
    115.                 customerInService.GetComponent<CustomerController>().SetInService(true);
    116.                 Debug.Log("Customer is Here: " + other.gameObject.name);
    117.                 GetRandomOrderValues();
    118.                 order = new Order(orderedAdditions, orderedSize, orderedBase);
    119.  
    120.                 //generateServices = true;
    121.                 //StartCoroutine(GenerateServices());
    122.             }
    123.         }
    124. #if DEBUG_SP
    125.         print("ServiceProcess.OnTriggerEnter:otherID=" + other.gameObject.GetInstanceID());
    126. #endif
    127.  
    128.  
    129.     }
    130.  
    131.     public void GetRandomOrderValues()
    132.     {
    133.         RandOrderItems = "";
    134.         orderedAdditions.Clear();
    135.         orderText.text = "Order: ";
    136.         int index;
    137.         System.Random random = new System.Random();
    138.         type = typeof(Size);
    139.         sizeValues = type.GetEnumValues();
    140.         index = random.Next(sizeValues.Length);
    141.         orderedSize = (Size)sizeValues.GetValue(index);
    142.         orderText.text += ("\nSize: " + orderedSize);
    143.  
    144.         type = typeof(Base);
    145.         baseValues = type.GetEnumValues();
    146.         index = random.Next(baseValues.Length);
    147.         orderedBase = (Base)baseValues.GetValue(index);
    148.         orderText.text += ("\nBase: " + orderedBase);
    149.  
    150.         type = typeof(Additions);
    151.         additionsValues = type.GetEnumValues();
    152.         int numberOfAdditions = random.Next(1, 5);
    153.         Additions addition;
    154.         for (int i = 0; i <= numberOfAdditions; i++)
    155.         {
    156.             // Get random num from the Additions enum
    157.             index = random.Next(additionsValues.Length);
    158.             addition = (Additions)additionsValues.GetValue(index);
    159.             orderedAdditions.Add(addition);
    160.  
    161.             RandOrderItems += addition + "\n";
    162.  
    163.         }
    164.  
    165.         RandOrderItems += orderedSize + "\n";
    166.         RandOrderItems += orderedBase;
    167.         Debug.Log(RandOrderItems);
    168.         orderText.text += ("\nAdditions: ");
    169.         foreach (var add in orderedAdditions)
    170.         {
    171.             orderText.text += "\n" + add;
    172.         }
    173.  
    174.         //orderList = orderText.text;
    175.     }
    176.  
    177.     IEnumerator GenerateServices()
    178.     {
    179.         //GetRandomOrderValues();
    180.         //order = new Order(orderedAdditions, orderedSize, orderedBase);
    181.  
    182.        
    183.         while (generateServices)
    184.         {
    185.             float timeToNextServiceInSec = interServiceTimeInSeconds;
    186.             switch (serviceIntervalTimeStrategy)
    187.             {
    188.                 case ServiceIntervalTimeStrategy.ConstantIntervalTime:
    189.                     timeToNextServiceInSec = interServiceTimeInSeconds;
    190.                     break;
    191.                 case ServiceIntervalTimeStrategy.UniformIntervalTime:
    192.                     timeToNextServiceInSec = Random.Range(minInterServiceTimeInSeconds, maxInterServiceTimeInSeconds);
    193.                     break;
    194.                 case ServiceIntervalTimeStrategy.ExponentialIntervalTime:
    195.                     float U = Random.value;
    196.                     float Lambda = 1 / serviceRateAsCustomersPerHour;
    197.                     timeToNextServiceInSec = GetExp(U, Lambda);
    198.                     break;
    199.                 case ServiceIntervalTimeStrategy.ObservedIntervalTime:
    200.                     timeToNextServiceInSec = interServiceTimeInSeconds;
    201.                     break;
    202.                 default:
    203.                     print("No acceptable ServiceIntervalTimeStrategy:" + serviceIntervalTimeStrategy);
    204.                     break;
    205.  
    206.             }
    207.  
    208.             generateServices = false;
    209.             yield return new WaitForSeconds(timeToNextServiceInSec / timeScale);
    210.         }
    211.         customerInService.GetComponent<CustomerController>().ExitService(customerExitPlace);
    212.         customerInService = null;
    213.  
    214.     }
    215.     private void OnDrawGizmos()
    216.     {
    217.         if (customerInService)
    218.         {
    219.             Renderer r = customerInService.GetComponent<Renderer>();
    220.             r.material.color = Color.green;
    221.         }
    222.     }
    223.  
    224.     static float GetExp(float u, float lambda)
    225.     {
    226.         //throw new NotImplementedException();
    227.         return -Mathf.Log(1 - u) / lambda;
    228.     }
    229.  
    230.  
    231.     void CaramelOnClick()
    232.     {
    233.         UserCreatedOrder += "Caramel\n";
    234.     }
    235.     void ChocolatelOnClick()
    236.     {
    237.         UserCreatedOrder += "Chocolate\n";
    238.     }
    239.  
    240.     void StrawberryOnClick()
    241.     {
    242.         UserCreatedOrder += "Strawberry\n";
    243.     }
    244.  
    245.     void VanillaOnClick()
    246.     {
    247.         UserCreatedOrder += "Vanilla\n";
    248.     }
    249.  
    250.     void MapleOnClick()
    251.     {
    252.         UserCreatedOrder += "Maple\n";
    253.     }
    254.  
    255.     void PeppermintOnClick()
    256.     {
    257.         UserCreatedOrder += "Peppermint\n";
    258.     }
    259.  
    260.  
    261.     public void CompareOrder()
    262.     {
    263.         UserCreatedOrder += sizeDropDown.options[sizeDropDown.value].text + "\n";
    264.         UserCreatedOrder += baseDropDown.options[baseDropDown.value].text;
    265.        
    266.         sizeDropDown.value = 0;
    267.         baseDropDown.value = 0;
    268.  
    269.         string[] splitOrder = UserCreatedOrder.Split(char.Parse("\n"));
    270.         string[] RandSplitList = RandOrderItems.Split(char.Parse("\n"));
    271.  
    272.         Debug.Log(splitOrder.Length);
    273.         Debug.Log(RandSplitList.Length);
    274.  
    275.         bool[] correct = new bool[RandSplitList.Length];
    276.  
    277.         if (splitOrder.Length == RandSplitList.Length)
    278.         {
    279.             Debug.Log(RandOrderItems);
    280.             Debug.Log(UserCreatedOrder);
    281.             for (int i = 0; i < RandSplitList.Length; i++)
    282.             {
    283.                 if (RandOrderItems.Contains(splitOrder[i]))
    284.                 {
    285.                     correct[i] = true;
    286.                 }
    287.                 else
    288.                 {
    289.                     correct[i] = false;
    290.                 }
    291.             }
    292.         }
    293.  
    294.         int x = 0;
    295.         for (int i = 0; i < correct.Length; i++)
    296.         {
    297.             //Debug.Log(correct[i]);
    298.             if (!correct[i])
    299.             {
    300.                 x += 1;
    301.                 orderText.text = "Order is wrong!";
    302.                 customerInService.GetComponent<CustomerController>().ChangeState(CustomerController.CustomerState.Serviced);
    303.                 UserCreatedOrder = "";
    304.                 break;
    305.             }
    306.            
    307.         }
    308.         if (x == 0)
    309.         {
    310.             orderText.text = "Order Completed!";
    311.             customerInService.GetComponent<CustomerController>().ChangeState(CustomerController.CustomerState.Serviced);
    312.             numOrderCompleted += 1;
    313.             UserCreatedOrder = "";
    314.         }
    315.  
    316.  
    317.  
    318.     }
    319.  
    320. }
    321.  
    322.