MeshManager.cs Version 1

Version for parts I - III using UnityEngine; using System.Collections; using System.Collections.Generic; using RTS; public class MeshManager {//for each meshfilter there should be an distinct meshManager private Mesh mesh; public Vector3 ParentPosition; public List Aliases; public int[] VerticleToAliasArray;//the index is vert number, in its there is the number of its alias, its size is the number of veerticles public List Triangles; float MinimumHeight; float MaximumHeight; public MeshManager (ref MeshFilter meshFilter){//constructor Initialise; ParentPosition = meshFilter.transform.position; mesh = meshFilter.mesh; ManageVerticles(mesh.vertices); ManageTriangles(mesh.triangles); CalcualateMinimumHeight; } 	private void Initialise { Aliases = new List; Triangles = new List; } 	private void ManageVerticles(Vector3[] verticles){//fnction find where many verticles are in the same positions and in that way produces its aliases Dictionary TempAliases = new Dictionary; //list of ints are numbers of verticles in the specific position (the Vector3) int i = 0; foreach (Vector3 verticle in verticles){ if(TempAliases.ContainsKey(verticle)){//we've arleady added a verticle of this position, so the alias will have multiple verticles TempAliases[verticle].Add(i); }else{//we have to add a number of our verticle to our list TempAliases[verticle] = new List ; TempAliases[verticle].Add(i); } 			i++; } 		VerticleToAliasArray = new int[i]; i = 0; foreach(var pair in TempAliases){//a Key-Value Pair Aliases.Add (new Verticle(this, i, pair.Value, pair.Key)); ManageVerticleToAliasArray(i,pair.Value); i++; } 	} 	private void ManageVerticleToAliasArray(int i, List list){//we are trying to fill the Verticle to Alias Array foreach(int k in list){ VerticleToAliasArray[k] = i; } 	} 	private void ManageTriangles(int[] triangles){//info about triangles is stored in an array. blblblba for(int i=0; i<triangles.Length; i+=3){ Triangles.Add(new Vector3(triangles[i], triangles[i+1], triangles[i+2]));//ok, we have a nice list of triangles, what now? } 		int j = 0; foreach(Vector3 vect in Triangles){//we pass info to verticle bout the triangles it particip in 			Aliases[VerticleToAliasArray[(int)vect.x]].AddTriangle(j,ChangeVerticleToAlias(vect)); Aliases[VerticleToAliasArray[(int)vect.y]].AddTriangle(j,ChangeVerticleToAlias(vect)); Aliases[VerticleToAliasArray[(int)vect.z]].AddTriangle(j,ChangeVerticleToAlias(vect)); j++; } 	} 	private Vector3 ChangeVerticleToAlias(Vector3 vect){ Vector3 x = new Vector3(VerticleToAliasArray[(int)vect.x], VerticleToAliasArray[(int)vect.y], VerticleToAliasArray[(int)vect.z]); return x; 	} public void CalcualateMinimumHeight{//Counting absolute minimum and maximum height of verticle in the mesh Vector3[] verticles = mesh.vertices; foreach(Verticle vert in Aliases){ if(vert.positionAbsolute.y < MinimumHeight){ MinimumHeight = vert.positionAbsolute.y;} if(vert.positionAbsolute.y > MaximumHeight){ MaximumHeight = vert.positionAbsolute.y;} } 	} 	public void Destroy (float percent){ float DestroyHeight	= MaximumHeight -(MaximumHeight - MinimumHeight)*percent;// ParentPosition.y + 0.66f; foreach(Verticle vert in Aliases){ vert.DestroyByHeight(DestroyHeight); } 		UpdateTrianglesList; } 	public void RemoveTriangle(int k){ Vector3 triangleNumbers = Triangles[k]; Vector3 aliasNumbers = ChangeVerticleToAlias(triangleNumbers); Triangles[k] = Vector3.zero; } 	public void UpdateTrianglesList{//gets the in-manager triangle list and then translates it to mesh.triangles format, and then sets it to be the mesh.triangles int LenghtOfTrianglesArray = Triangles.Count*3; int[] tempTrianglesArray = new int[LenghtOfTrianglesArray]; int i = 0; foreach(Vector3 vec in Triangles){ tempTrianglesArray[i] 	= (int)vec.x; 			tempTrianglesArray[i+1] = (int)vec.y; 			tempTrianglesArray[i+2] = (int)vec.z; 			i+=3; } 		mesh.triangles = tempTrianglesArray; } 	public void DestroyByNumber(int numb){ foreach(Verticle vert in Aliases){ vert.DestroyByNumber(numb); } 		UpdateTrianglesList; } }