Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* A short script to enable having various amount of resources, that can change later on.
- * In this case we are talking about resources needed in a game (gold, lumber etc.).
- * If for an instance the game needs to have more resources in a later version, or if some
- * of the resources needs to be changed, one only has to change the enum filled with resources.
- * The code was specically written for Unity3D (C#).
- */
- using System.Collections.Generic;
- using System.Collections;
- using System;
- namespace ResourceTest
- {
- public enum TypeOfResource
- {
- GOLD,
- LUMBER,
- GAS
- }
- public class ResourceHandler
- {
- private static Dictionary<TypeOfResource, int> amountOfResources;
- public static void ResetResources()
- { // set all resources to 0
- amountOfResources = new Dictionary<TypeOfResource, int>();
- foreach(TypeOfResource resource in Enum.GetValues(typeof(TypeOfResource)))
- {
- amountOfResources.Add(resource, 0);
- }
- }
- public static int GetResource(TypeOfResource resource)
- { // get current amount of resource
- if(amountOfResources == null)
- {
- ResetResources();
- }
- if(amountOfResources.ContainsKey(resource))
- {
- return amountOfResources[resource];
- }
- else
- {
- amountOfResources.Add(resource, 0);
- }
- return 0;
- }
- public static void SetResource(TypeOfResource resource, int integer)
- { // override current amount with new integer
- if(amountOfResources == null)
- {
- ResetResources();
- }
- if(amountOfResources.ContainsKey(resource))
- {
- amountOfResources[resource] = integer;
- }
- else
- {
- amountOfResources.Add(resource, integer);
- }
- }
- public static void AddResource(TypeOfResource resource, int integer)
- { // add to current amount
- int currentValue = GetResource(resource);
- amountOfResources[resource] = currentValue + integer;
- }
- public static bool CheckIfEnoughResources(Dictionary<TypeOfResource, int> requiredAmount)
- { // true if you have enough resources
- foreach(KeyValuePair<TypeOfResource, int> resourceAmount in requiredAmount)
- {
- if(resourceAmount.Value > GetResource(resourceAmount.Key))
- {
- return false;
- }
- }
- return true;
- }
- public static bool CheckIfEnoughResources(RequiredResource[] requiredAmount)
- { // the same but with a different parameter
- foreach(RequiredResource resourceAmount in requiredAmount)
- {
- if(resourceAmount.AmountRequired > GetResource(resourceAmount.TypeOfResource))
- {
- return false;
- }
- }
- return true;
- }
- }
- }
- using UnityEngine;
- using System.Collections;
- namespace ResourceTest
- {
- public class Product : ScriptableObject
- { // a scriptable object containing all information per product that you can buy with the resources (only for Unity3D)
- [SerializeField]private string nameOfProduct;
- public string NameOfProduct { get { return nameOfProduct; } }
- [SerializeField]private RequiredResource[] requiredResources;
- public RequiredResource[] RequiredResources { get { return requiredResources; } }
- }
- [System.Serializable]
- public class RequiredResource
- { // a helper class to make it easier to make additional products using the editor (only for Unity3D)
- [SerializeField]private TypeOfResource typeOfResource;
- public TypeOfResource TypeOfResource { get { return typeOfResource; } }
- [SerializeField]private int amountRequired;
- public int AmountRequired { get { return amountRequired; } }
- }
- }
- using UnityEngine;
- using System.Collections;
- namespace ResourceTest
- {
- public class Test : MonoBehaviour
- {
- public Product[] AllProducts;
- private void Start()
- {
- // set some initial values for the resources, these should be determined by the course of the game
- ResourceHandler.SetResource(TypeOfResource.LUMBER, 5);
- ResourceHandler.SetResource(TypeOfResource.GAS, 4);
- ResourceHandler.SetResource(TypeOfResource.GOLD, 3);
- // test if you have enough resources for a certain product, make sure you have made some products in the editor (only for Unity3D)
- foreach(Product p in AllProducts)
- {
- bool canBeBought = ResourceHandler.CheckIfEnoughResources(p.RequiredResources);
- if(canBeBought == true)
- {
- Debug.Log(p.NameOfProduct + " can be bought/build with your current resources.");
- }
- else
- {
- Debug.Log(p.NameOfProduct + " can not be bought/build with your current resources.");
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement