Ejemplo n.º 1
0
        /// <summary>
        /// Adds improvement to specified hex if it meets the rule requirements.
        /// </summary>
        /// <param name="hex">Hex to attempt to add the improvement upon</param>
        /// <param name="improvementIndex">Index of the improvement within the improvement manager to attemp to add</param>
        /// <example>
        /// The following code attempts to add the first improvement to every tile.
        /// <code>
        /// using System;
        /// using UnityEngine;
        /// using CivGrid;
        ///
        /// public class ExampleClass : MonoBehaviour
        /// {
        ///    WorldManager worldManager;
        ///    ImprovementManager improvementManager;
        ///
        ///    void Start()
        ///    {
        ///        worldManager = GameObject.FindObjectOfType&lt;WorldManager&gt;();
        ///        improvementManager = GameObject.FindObjectOfType&lt;ImprovementManager&gt;();
        ///
        ///        //check again for each hex if it should spawn a resource
        ///        foreach (HexChunk chunk in worldManager.hexChunks)
        ///        {
        ///            foreach (HexInfo hex in chunk.hexArray)
        ///            {
        ///                //tries to add first improvement
        ///                improvementManager.TestedAddImprovementToTile(hex, 0);
        ///            }
        ///        }
        ///    }
        /// }
        /// </code>
        /// </example>
        /// <remarks>
        /// The index system should be based off of the inspector indexes at startup. The automatically generated "None" improvement
        /// is not included in the index numbering.
        /// </remarks>
        public void TestedAddImprovementToTile(Hex hex, int improvementIndex)
        {
            //if it's possible to spawn the improvement according to it's rules
            bool possible = false;

            //gets improvement from it's index
            Improvement improvement = improvements[improvementIndex + 1];

            //runs through the tests and if any return false, we can not spawn the improvement
            if (RuleTest.Test(hex, improvement.rule, tileManager))
            {
                possible = true;
            }
            else
            {
                possible = false;
            }

            //spawn the improvement on the tile
            if (possible)
            {
                hex.currentImprovement = improvement;
                hex.parentChunk.worldManager.improvementManager.InitiateImprovementsOnHexs(hex, improvement);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Checks if a resource should be spawned on a hexagon.
        /// </summary>
        /// <param name="hex">The hexagon to check</param>
        /// <example>
        /// The following code changes the possible resources and then re-checks each hex for the resource.
        /// <code>
        /// using System;
        /// using UnityEngine;
        /// using CivGrid;
        ///
        /// public class ExampleClass : MonoBehaviour
        /// {
        ///    WorldManager worldManager;
        ///    ResourceManager resourceManager;
        ///
        ///    void Start()
        ///    {
        ///        worldManager = GameObject.FindObjectOfType&lt;WorldManager&gt;();
        ///        resourceManager = GameObject.FindObjectOfType&lt;ResourceManager&gt;();
        ///
        ///        //creates a new resource to possibly spawn
        ///        resourceManager.AddResource(new Resource("SpecialNewResource", 15, 1, null, null, false, new HexRule(new int[] { 3, 4 }, new Feature[] { Feature.Flat })));
        ///
        ///        //check again for each hex if it should spawn a resource
        ///        foreach (HexChunk chunk in worldManager.hexChunks)
        ///        {
        ///            foreach (HexInfo hex in chunk.hexArray)
        ///            {
        ///                resourceManager.CheckForResource(hex);
        ///            }
        ///        }
        ///    }
        /// }
        /// </code>
        /// </example>
        public void CheckForResource(Hex hex)
        {
            //loop through all resources
            for (int i = 0; i < internalResources.Length; i++)
            {
                //get each resource and check if we can spawn them
                Resource r = internalResources[i];
                if (r.rule != null)
                {
                    //runs through the tests and if any return false, we can not spawn this resource; check the next
                    if (RuleTest.Test(hex, r.rule, tileManager))
                    {
                        //we can spawn it, but should we?
                        int number = (int)Random.Range(0, r.rarity);
                        if (number == 0)
                        {
                            //spawn resource
                            hex.currentResource = r;
                            SpawnResource(hex, hex.currentResource, false);
                            return;
                        }
                    }
                }
            }

            //no resource spawned; return "None"
            hex.currentResource = internalResources[0];
        }