public ResourceItem(Resource key, Rect value) { this.key = key; this.value = value; }
/// <summary> /// Adds a resource to the resource array. /// </summary> /// <param name="r">Resource to add</param> /// <remarks> /// This method should only be used before world generation as it will /// not have an effect unless you re-check for resources on each hex. /// </remarks> public void AddResource(Resource r) { resources.Add(r); internalResources = resources.ToArray(); UpdateResourceNames(); }
/// <summary> /// Removes a resource from the resource array. /// </summary> /// <param name="r">Resource to remove</param> /// <remarks> /// Removing a resource that is referenced elsewhere will cause null reference errors. Only use this /// method if you are personally managing the specific resources memory lifetime. /// </remarks> public void DeleteResource(Resource r) { resources.Remove(r); internalResources = resources.ToArray(); UpdateResourceNames(); }
/// <summary> /// Spawns the provided resource on the tile. /// Optional to regenerate the chunk. /// </summary> /// <remarks> /// This can be used to force a resource to spawn, even if against it's rules. /// </remarks> /// <param name="hex">Hex to spawn the resource on</param> /// <param name="r">Resource to spawn</param> /// <param name="regenerateChunk">If the parent chunk should be regenerated</param> public void SpawnResource(Hex hex, Resource r, bool regenerateChunk) { //reset resource locations hex.resourceLocations.Clear(); //destroy previous resource objects if (hex.rObject != null) { Destroy(hex.rObject); } //if the resource has a mesh to spawn if (r.meshToSpawn != null) { //calculate y position to spawn the resources float y; if (hex.localMesh == null) { y = (worldManager.hexExt.y); if (y == 0) { y -= ((hex.worldPosition.y + worldManager.hexExt.y) / Random.Range(4, 8)); } else { y = hex.worldPosition.y + worldManager.hexExt.y + hex.currentResource.meshToSpawn.bounds.extents.y; } } else { y = (hex.localMesh.bounds.extents.y); if (y == 0) { y -= ((hex.worldPosition.y + hex.localMesh.bounds.extents.y) / Random.Range(4, 8)); } else { y = hex.worldPosition.y + hex.localMesh.bounds.extents.y + hex.currentResource.meshToSpawn.bounds.extents.y; } } //spawn a resource for each spawn amount for (int i = 0; i < r.meshSpawnAmount; i++) { //position setting float x = (worldManager.hexCenter.x + Random.Range(-0.2f, 0.2f)); float z = (worldManager.hexCenter.z + Random.Range(-0.2f, 0.2f)); hex.resourceLocations.Add(new Vector3(x, y, z)); } //number of resources int size = hex.resourceLocations.Count; //number of resources to combine if (size > 0) { //combine instances CombineInstance[] combine = new CombineInstance[size]; Matrix4x4 matrix = new Matrix4x4(); matrix.SetTRS(Vector3.zero, Quaternion.identity, Vector3.one); //skip first combine instance due to presetting for (int k = 0; k < size; k++) { combine[k].mesh = hex.currentResource.meshToSpawn; matrix.SetTRS(hex.resourceLocations[k], Quaternion.identity, Vector3.one); combine[k].transform = matrix; } //create gameobject to hold the resource meshes GameObject holder = new GameObject(r.name + " at " + hex.AxialCoordinates, typeof(MeshFilter), typeof(MeshRenderer)); //set the gameobject position to the hex position holder.transform.position = hex.worldPosition; holder.transform.parent = hex.parentChunk.transform; //set the resource mesh texture holder.renderer.material.mainTexture = r.meshTexture; //assign the combined mesh to the resource holder gameobject MeshFilter filter = holder.GetComponent<MeshFilter>(); filter.mesh = new Mesh(); filter.mesh.CombineMeshes(combine); //set the hex's resource object to the resource holder hex.rObject = holder; //UV mapping Rect rectArea; worldManager.textureAtlas.resourceLocations.TryGetValue(r, out rectArea); //temp UV data Vector2[] uv; uv = new Vector2[filter.mesh.vertexCount]; //calculate the combined UV data for (int i = 0; i < filter.mesh.vertexCount; i++) { uv[i] = new Vector2(filter.mesh.uv[i].x * rectArea.width + rectArea.x, filter.mesh.uv[i].y * rectArea.height + rectArea.y); } //assign the resource holder's UV data filter.mesh.uv = uv; } } //if needed; regenerate the chunk and it's UV data if (regenerateChunk) { hex.ChangeTextureToResource(); hex.parentChunk.RegenerateMesh(); } }
/// <summary> /// Adds a resource to the resource array at the provided index. /// </summary> /// <param name="r">Resource to add</param> /// <param name="index">Index in which to add the resource</param> /// <remarks> /// This method should only be used before world generation as it will /// not have an effect unless you re-check for resources on each hex. /// </remarks> public void AddResourceAtIndex(Resource r, int index) { resources.Insert(index, r); internalResources = resources.ToArray(); UpdateResourceNames(); }