/// <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<WorldManager>(); /// improvementManager = GameObject.FindObjectOfType<ImprovementManager>(); /// /// //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); } }
/// <summary> /// Creates the improvement GameObject and switches the hex texture. /// </summary> /// <param name="hex">Hex to create the improvement on</param> /// <param name="i">Improvement to add</param> private void InitiateImprovementsOnHexs(Hex hex, Improvement i) { //get the parent chunk object; this is where we will parent the improvement objects to GameObject resourceHolder = hex.parentChunk.gameObject; if (resourceHolder == null) { Debug.LogError("Could not find the resource holder!"); } //remove current improvements Destroy(hex.iObject); //remove current resource gameobjects Destroy(hex.rObject); //switch the hex's texture to this improvement's ground texture hex.ChangeTextureToImprovement(); //spawn gameObject if there is a mesh to spawn if (i.meshToSpawn != null) { float y = (hex.worldPosition.y + hex.hexExt.y) - ((hex.hexExt.y) / 5f); if (y == 0) { y -= ((hex.worldPosition.y + hex.hexExt.y) / Random.Range(4, 8)); } GameObject holder = new GameObject(i.name + " at " + hex.AxialCoordinates, typeof(MeshFilter), typeof(MeshRenderer)); holder.GetComponent<MeshFilter>().mesh = hex.currentImprovement.meshToSpawn; holder.transform.position = new Vector3((hex.worldPosition.x + hex.hexCenter.x + Random.Range(-0.2f, 0.2f)), y, (hex.worldPosition.z + hex.hexCenter.z + Random.Range(-0.2f, 0.2f))); holder.transform.rotation = Quaternion.identity; holder.renderer.material.mainTexture = i.meshTexture; holder.transform.parent = hex.parentChunk.transform; hex.iObject = holder; } }
/// <summary> /// This is the setup called from HexChunk when it's ready for us to generate our meshes. /// </summary> /// <example> /// The following code will start hex operations on a new hex provided that the hexagon has a valid parent chunk and world manager. /// <code> /// class HexTest : MonoBehaviour /// { /// HexInfo hex; /// /// void Start() /// { /// hex = new HexInfo(); /// /// hex.Start(); /// } /// } /// </code> /// </example> public void Start() { //set tile type to the mountain tile if the feature is a mountain and the type exists if (terrainFeature == Feature.Mountain) { Tile mountain = parentChunk.worldManager.tileManager.TryGetMountain(); if (mountain != null) { terrainType = mountain; } } //get the texture atlas from world manager worldTextureAtlas = parentChunk.worldManager.textureAtlas; parentChunk.worldManager.axialToHexDictionary.Add(AxialCoordinates, (Hex)this); //cache neighbors of this hexagon neighbors = parentChunk.worldManager.GetNeighborsOfHex((Hex)this); //generate local mesh MeshSetup(); //if we are NOT loading a map if (parentChunk.worldManager.generateNewValues == true) { //check for resources and default to no improvement currentImprovement = improvementManager.improvements[0]; resourceManager.CheckForResource((Hex)this); } }
/// <summary> /// Checks if a key exists in the array. /// </summary> /// <param name="list">The list to check if the key exists within</param> /// <param name="key">The key to look for within this array</param> /// <returns>If the key was found</returns> public static bool ContainsKey(this ImprovementItem[] list, Improvement key) { //cycle through each key in the array and return true if we find one matching the supplied key; otherwise return false foreach (ImprovementItem item in list) { if (item.Key == key) { return(true); } } return(false); }
/// <summary> /// Attempts to return the texture atlas location of this Improvement from the array based on a key. /// </summary> /// <param name="list">The array to get the value from</param> /// <param name="key">The key to search for a match within the array</param> /// <param name="location">A out reference of the Rect location of this Improvement within the texture atlas</param> /// <returns>If the key was found in the array</returns> public static bool TryGetValue(this ImprovementItem[] list, Improvement key, out Rect location) { //list to hold each possible return value List <Rect> tempList = new List <Rect>(); //add each possible return value to the tempList foreach (ImprovementItem item in list) { if (item.Key.name == key.name) { tempList.Add(item.Value); } } //if we have more than one that is returnable if (tempList.Count > 1) { //select one at random and return it int index = UnityEngine.Random.Range(0, tempList.Count); location = tempList[index]; return(true); } //only have one that is returnable; return that one else if (tempList.Count == 1) { location = tempList[0]; return(true); } //couldn't find the key else { //not found Debug.LogError("Couldn't get a value from the given key: " + key.name); location = new Rect(); return(false); } }
/// <summary> /// Creates the improvement GameObject and switches the hex texture. /// </summary> /// <param name="hex">Hex to create the improvement on</param> /// <param name="i">Improvement to add</param> private void InitiateImprovementsOnHexs(Hex hex, Improvement i) { //get the parent chunk object; this is where we will parent the improvement objects to GameObject resourceHolder = hex.parentChunk.gameObject; if (resourceHolder == null) { Debug.LogError("Could not find the resource holder!"); } //remove current improvements Destroy(hex.iObject); //remove current resource gameobjects Destroy(hex.rObject); //switch the hex's texture to this improvement's ground texture hex.ChangeTextureToImprovement(); //spawn gameObject if there is a mesh to spawn if (i.meshToSpawn != null) { float y = (hex.worldPosition.y + hex.hexExt.y) - ((hex.hexExt.y) / 5f); if (y == 0) { y -= ((hex.worldPosition.y + hex.hexExt.y) / Random.Range(4, 8)); } GameObject holder = new GameObject(i.name + " at " + hex.AxialCoordinates, typeof(MeshFilter), typeof(MeshRenderer)); holder.GetComponent <MeshFilter>().mesh = hex.currentImprovement.meshToSpawn; holder.transform.position = new Vector3((hex.worldPosition.x + hex.hexCenter.x + Random.Range(-0.2f, 0.2f)), y, (hex.worldPosition.z + hex.hexCenter.z + Random.Range(-0.2f, 0.2f))); holder.transform.rotation = Quaternion.identity; holder.GetComponent <Renderer>().material.mainTexture = i.meshTexture; holder.transform.parent = hex.parentChunk.transform; hex.iObject = holder; } }
/// <summary> /// Removes an improvement from the improvement array. /// </summary> /// <param name="i">Improvement to remove</param> /// <remarks> /// Removing a improvement that is referenced elsewhere will cause null reference errors. Only use this /// method if you are personally managing the specific improvement memory lifetime. /// </remarks> public void DeleteImprovement(Improvement i) { improvements.Remove(i); internalImprovements = improvements.ToArray(); UpdateImprovementNames(); }
/// <summary> /// Adds an improvement to the improvement array at the provided index. /// </summary> /// <param name="i">Improvement to add</param> /// <param name="index">Index in which to add the improvement</param> /// <remarks> /// This method is safe to use after world generation. However, the improvement must be present on /// a world load to be safe./n /// For example if you add an improvment and the user adds it a tile. Saves the game. And then loads it, /// you must re-add the same improvement at the same index before world generation for it to safely load. /// </remarks> public void AddImprovementAtIndex(Improvement i, int index) { improvements.Insert(index, i); internalImprovements = improvements.ToArray(); UpdateImprovementNames(); }
/// <summary> /// Adds an improvement to the improvement array. /// </summary> /// <param name="i">Improvement to add</param> /// <remarks> /// This method is safe to use after world generation. However, the improvement must be present on /// a world load to be safe./n /// For example if you add an improvment and the user adds it a tile. Saves the game. And then loads it, /// you must re-add the same improvement at the same index before world generation for it to safely load. /// </remarks> public void AddImprovement(Improvement i) { improvements.Add(i); internalImprovements = improvements.ToArray(); UpdateImprovementNames(); }
public ImprovementItem(Improvement key, Rect value) { this.key = key; this.value = value; }
public void TestedAddImprovementToTile(Hex hex, Improvement improvement) { //if it's possible to spawn the improvement according to it's rules bool possible = false; //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); } }
/// <summary> /// Adds a new entry of a key and matching value to this array. /// </summary> /// <param name="list">List to add the improvement to</param> /// <param name="improvementToAdd">Improvement to add as the key</param> /// <param name="rectToAdd">Rect to add as the value</param>s public static void Add(this List <ImprovementItem> list, Improvement improvementToAdd, Rect rectToAdd) { list.Add(new ImprovementItem(improvementToAdd, rectToAdd)); }