/// <summary> /// Gets the created worlds. /// </summary> /// <returns>The created worlds.</returns> public static List<WorldSpecs> GetCreatedWorlds() { if(File.Exists(directory + "CreatedWorlds.xml")) { List<WorldSpecs> existingSpecs = new List<WorldSpecs> (); XmlTextReader reader = new XmlTextReader(directory + "CreatedWorlds.xml"); while(reader.Read()) { if(reader.IsStartElement() && reader.NodeType == XmlNodeType.Element) { switch(reader.Name) { case "WorldSpec": if(reader.AttributeCount >= 10) { WorldSpecs tempSpec = new WorldSpecs(); tempSpec.spaceName = reader.GetAttribute(0); tempSpec.spaceArea = int.Parse(reader.GetAttribute(1)); tempSpec.mapLength = int.Parse(reader.GetAttribute(2)); tempSpec.cellLength = float.Parse(reader.GetAttribute(3)); tempSpec.start = new Vector2(float.Parse(reader.GetAttribute(4)),float.Parse(reader.GetAttribute(5))); tempSpec.degreeJumpStep = float.Parse(reader.GetAttribute(6)); tempSpec.subdivisions = int.Parse(reader.GetAttribute(7)); tempSpec.totalNumberOfCells = int.Parse(reader.GetAttribute(8)); tempSpec.seed = int.Parse(reader.GetAttribute(9)); tempSpec.planetPositions = new Vector2[(reader.AttributeCount - 10) / 2]; if(reader.AttributeCount > 11) { float maxPosition = (reader.AttributeCount - 10)/2.0f; int maxP = Mathf.CeilToInt(maxPosition); for(int i = 0, j = 0; j < maxP; j++) { tempSpec.planetPositions[j].Set(float.Parse(reader.GetAttribute(i+10)), float.Parse(reader.GetAttribute(i+11))); i+= 2; } } existingSpecs.Add(tempSpec); } else { Debug.Log("Data is missing from 1 of the worlds. Not Saving it anymore"); } break; case "Root": break; default: Debug.Log(reader.Name + " : possible invalid data in save file ignoring, please review file"); break; } } } reader.Close(); return existingSpecs; } else { return new List<WorldSpecs>(1); } }
/// <summary> /// Use to Procedurally generate a space world with a random seed /// length x width /// </summary> public void GenerateSpace( int mapLength , int numberOfSubdivisions, Vector2 startingVector, string SpaceName, int seed) { worldspec = default(WorldSpecs); worldspec.spaceName = SpaceName; worldspec.spaceArea = mapLength * mapLength; worldspec.mapLength = mapLength; worldspec.cellLength = SquareMathCalculations.FindSmallestDivisionSideLength(worldspec.mapLength, numberOfSubdivisions); worldspec.start = startingVector; worldspec.degreeJumpStep = 1.0f; worldspec.seed = seed; worldspec.subdivisions = numberOfSubdivisions; worldspec.invalidSpawnPoints = new List<Vector2>(); worldspec.totalNumberOfCells = numberOfSubdivisions * numberOfSubdivisions; if(planets.Count > 0) { worldspec.planetPositions = new Vector2[planets.Count]; float jumpStep = ((float)worldspec.mapLength) / (float)planets.Count; float jump = 0f; for(int i = 0 ; i < planets.Count; i++, jump += jumpStep) { // r = distance + previous distance reroll: float r = jump; float theta = UnityEngine.Random.Range( 1f, 89f); Vector2 tempPos = new Vector2(-(float)mapLength/2.0f, -(float)worldspec.mapLength/2.0f); tempPos.x = tempPos.x + (Mathf.Abs(Mathf.Cos(theta) * r)); tempPos.y = tempPos.x + (Mathf.Abs(Mathf.Sin(theta) * r)); float offsetFromCenter = (tempPos.x % (worldspec.cellLength)); if(offsetFromCenter != (worldspec.cellLength/2.0f)) { tempPos.x = (tempPos.x - (offsetFromCenter)) + (worldspec.cellLength/2.0f); } offsetFromCenter = (tempPos.y % (worldspec.cellLength)); if(offsetFromCenter != (worldspec.cellLength/2.0f)) { tempPos.y = (tempPos.y - (offsetFromCenter)) + (worldspec.cellLength/2.0f); } if(isPlanetInRange(tempPos) || Vector2.Distance(Vector2.zero,tempPos) <= worldspec.cellLength) { goto reroll; } worldspec.planetPositions[i] = tempPos; } } for(float degree = 0; degree < 360; degree+= worldspec.degreeJumpStep) { float r0 = 5 * Mathf.Cos( 9 * degree) + (worldspec.mapLength * 0.20f); //degree / 4; //3*Mathf.Cos(6 * degree) + 15.0f; worldspec.invalidSpawnPoints.Add(new Vector2( r0 * Mathf.Cos(degree), r0 * Mathf.Sin(degree))); } SpawnPlanets (); CreateCells (worldspec); SaveSpace(); }
/// <summary> /// Creates the cells. /// </summary> /// <param name="details">Details.</param> private void CreateCells(WorldSpecs details) { Vector2 startPoint = new Vector2(details.start.x - (details.mapLength * 0.5f), details.start.y - (details.mapLength * 0.5f)); GameObject parent = new GameObject (details.spaceName); parent.transform.position = Vector2.zero; for(float i = 0, x = 0; i < details.mapLength ; i += details.cellLength, x++) { for(float j = 0, y = 0; j < details.mapLength; j += details.cellLength, y++) { Vector2 positon = new Vector2(startPoint.x + i + (details.cellLength/2.0f) , startPoint.y + j + (details.cellLength / 2.0f)); if(isValidCellPosition(positon) && !isPlanetInRange(positon)) { GameObject cell = new GameObject ( x + "," + y ); cell.transform.parent = parent.transform; cell.transform.localScale = new Vector3(details.cellLength ,details.cellLength,1.0f); cell.transform.position = positon; cell.layer = 11; WorldCell temp = cell.AddComponent<WorldCell>(); ObjectPool.Pool.AddCell(temp); cell.AddComponent<BoxCollider2D>().isTrigger = true; cell.gameObject.isStatic = true; } } } StartCoroutine(WorldCell.GenerateALLXMLData(preLoadRange)); ObjectPool.Pool.LinkCells(); if(worldDoneLoading != null) { worldDoneLoading(ActionType.load); } }
/// <summary> /// Generates the space. /// </summary> /// <param name="details">Details.</param> public void GenerateSpace( WorldSpecs details) { worldspec = details; CreateCells (worldspec); SpawnPlanets (); }