Advertisement
Looksky

In Game Resources

Oct 22nd, 2015
323
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.44 KB | None | 0 0
  1. /* A short script to enable having various amount of resources, that can change later on.
  2.  * In this case we are talking about resources needed in a game (gold, lumber etc.).
  3.  * If for an instance the game needs to have more resources in a later version, or if some
  4.  * of the resources needs to be changed, one only has to change the enum filled with resources.
  5.  * The code was specically written for Unity3D (C#).
  6.  */
  7.  
  8. using System.Collections.Generic;
  9. using System.Collections;
  10. using System;
  11.  
  12. namespace ResourceTest
  13. {
  14.     public enum TypeOfResource
  15.     {
  16.         GOLD,
  17.         LUMBER,
  18.         GAS
  19.     }
  20.  
  21.     public class ResourceHandler
  22.     {
  23.  
  24.         private static Dictionary<TypeOfResource, int> amountOfResources;
  25.  
  26.         public static void ResetResources()
  27.         { // set all resources to 0
  28.             amountOfResources = new Dictionary<TypeOfResource, int>();
  29.            
  30.             foreach(TypeOfResource resource in Enum.GetValues(typeof(TypeOfResource)))
  31.             {
  32.                 amountOfResources.Add(resource, 0);
  33.             }
  34.         }
  35.  
  36.  
  37.         public static int GetResource(TypeOfResource resource)
  38.         { // get current amount of resource
  39.             if(amountOfResources == null)
  40.             {
  41.                 ResetResources();
  42.             }
  43.  
  44.             if(amountOfResources.ContainsKey(resource))
  45.             {
  46.                 return amountOfResources[resource];
  47.             }
  48.             else
  49.             {
  50.                 amountOfResources.Add(resource, 0);
  51.             }
  52.  
  53.             return 0;
  54.         }
  55.  
  56.         public static void SetResource(TypeOfResource resource, int integer)
  57.         { // override current amount with new integer
  58.             if(amountOfResources == null)
  59.             {
  60.                 ResetResources();
  61.             }
  62.            
  63.             if(amountOfResources.ContainsKey(resource))
  64.             {
  65.                 amountOfResources[resource] = integer;
  66.             }
  67.             else
  68.             {
  69.                 amountOfResources.Add(resource, integer);
  70.             }
  71.         }
  72.  
  73.         public static void AddResource(TypeOfResource resource, int integer)
  74.         { // add to current amount
  75.             int currentValue = GetResource(resource);
  76.             amountOfResources[resource] = currentValue + integer;
  77.         }
  78.  
  79.         public static bool CheckIfEnoughResources(Dictionary<TypeOfResource, int> requiredAmount)
  80.         { // true if you have enough resources
  81.             foreach(KeyValuePair<TypeOfResource, int> resourceAmount in requiredAmount)
  82.             {
  83.                 if(resourceAmount.Value > GetResource(resourceAmount.Key))
  84.                 {
  85.                     return false;
  86.                 }
  87.             }
  88.             return true;
  89.         }
  90.  
  91.         public static bool CheckIfEnoughResources(RequiredResource[] requiredAmount)
  92.         { // the same but with a different parameter
  93.             foreach(RequiredResource resourceAmount in requiredAmount)
  94.             {
  95.                 if(resourceAmount.AmountRequired > GetResource(resourceAmount.TypeOfResource))
  96.                 {
  97.                     return false;
  98.                 }
  99.             }
  100.             return true;
  101.         }
  102.     }
  103. }
  104.  
  105. using UnityEngine;
  106. using System.Collections;
  107.  
  108. namespace ResourceTest
  109. {
  110.     public class Product : ScriptableObject
  111.     { // a scriptable object containing all information per product that you can buy with the resources (only for Unity3D)
  112.         [SerializeField]private string nameOfProduct;
  113.         public string NameOfProduct { get { return nameOfProduct; } }
  114.  
  115.         [SerializeField]private RequiredResource[] requiredResources;
  116.         public RequiredResource[] RequiredResources { get { return requiredResources; } }
  117.     }
  118.  
  119.     [System.Serializable]
  120.     public class RequiredResource
  121.     { // a helper class to make it easier to make additional products using the editor (only for Unity3D)
  122.         [SerializeField]private TypeOfResource typeOfResource;
  123.         public TypeOfResource TypeOfResource { get { return typeOfResource; } }
  124.         [SerializeField]private int amountRequired;
  125.         public int AmountRequired { get { return amountRequired; } }
  126.     }
  127. }
  128.  
  129.  
  130. using UnityEngine;
  131. using System.Collections;
  132.  
  133. namespace ResourceTest
  134. {
  135.     public class Test : MonoBehaviour
  136.     {
  137.         public Product[] AllProducts;
  138.  
  139.         private void Start()
  140.         {
  141.             // set some initial values for the resources, these should be determined by the course of the game
  142.             ResourceHandler.SetResource(TypeOfResource.LUMBER, 5);
  143.             ResourceHandler.SetResource(TypeOfResource.GAS, 4);
  144.             ResourceHandler.SetResource(TypeOfResource.GOLD, 3);
  145.  
  146.             // test if you have enough resources for a certain product, make sure you have made some products in the editor (only for Unity3D)
  147.             foreach(Product p in AllProducts)
  148.             {
  149.                 bool canBeBought = ResourceHandler.CheckIfEnoughResources(p.RequiredResources);
  150.                 if(canBeBought == true)
  151.                 {
  152.                     Debug.Log(p.NameOfProduct + " can be bought/build with your current resources.");
  153.                 }
  154.                 else
  155.                 {
  156.                     Debug.Log(p.NameOfProduct + " can not be bought/build with your current resources.");
  157.                 }
  158.             }
  159.         }
  160.     }
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement