public GlobalSectionCell DefineSection(int SectionId) { GlobalSectionCell definedSection = new GlobalSectionCell(Vector3.zero, 0, 0); if (SectionId == 1) { definedSection.Xsize = _mapData.StartingSecionSize; definedSection.Ysize = _mapData.StartingSecionSize; } else { definedSection.Xsize = UnityEngine.Random.Range(_mapData.MinSectionSize, _mapData.MaxSectionSize + 1); definedSection.Ysize = UnityEngine.Random.Range(_mapData.MinSectionSize, _mapData.MaxSectionSize + 1); } Vector2 localNewPosition = FindNearestLocalPlace(); MapCell localSectionPivot = new MapCell((int)localNewPosition.x, (int)localNewPosition.y); definedSection.Position = ConvertLocalToGlobal(localNewPosition); LocallSectionCell definedLocalSection = new LocallSectionCell(localSectionPivot, definedSection.Xsize, definedSection.Ysize); definedSection = BuildLocalSection(SectionId, definedLocalSection); ClearUpPlaces(); return(definedSection); }
void CalculateNextSpawnPoints(LocallSectionCell section) { Vector2 FirstSpawnPoint = Vector2.zero; Vector2 SecondSpawnPoint = Vector2.zero; List <MapCell> BlockPerimeter = TransormPerimeter(section); if (!IsInMapLimits(BlockPerimeter[0], EmptyId)) { return; } FirstSpawnPoint = new Vector2(BlockPerimeter[0].X, BlockPerimeter[0].Y); for (int i = 1; i < BlockPerimeter.Count; i++) { if (!IsInMapLimits(BlockPerimeter[i], EmptyId)) { SecondSpawnPoint = new Vector2(BlockPerimeter[i - 1].X, BlockPerimeter[i - 1].Y); break; } } if (FirstSpawnPoint != Vector2.zero) { AddPlace(FirstSpawnPoint); } if (SecondSpawnPoint != Vector2.zero) { AddPlace(SecondSpawnPoint); } }
GlobalSectionCell BuildLocalSection(int SectionId, LocallSectionCell section) { //Fix pivot -> topleft //Fix X size //Fix Y size LocallSectionCell newSection = FindProperSection(section); //Populate localMap with id of the Section according to direction for (int i = 0; i < newSection.Xsize; i++) { for (int k = 0; k < newSection.Ysize; k++) { localMap[newSection.Position.X + i, newSection.Position.Y + k] = SectionId; } } //Don't forget to mark perimeter with a gap width for (int g = 1; g <= Gap; g++) { MapCell focusCell = new MapCell(newSection.Position.X, newSection.Position.Y); focusCell.X -= g; focusCell.Y -= g; //Top Perimeter for (int i = 0; i < newSection.Xsize + (g * 2 - 1); i++) { if (IsInMapLimits(focusCell, EmptyId)) { localMap[focusCell.X, focusCell.Y] = GapId; } focusCell.X++; } //Right Perimeter for (int i = 0; i < newSection.Ysize + (g * 2 - 1); i++) { if (IsInMapLimits(focusCell, EmptyId)) { localMap[focusCell.X, focusCell.Y] = GapId; } focusCell.Y++; } //Bot Perimeter for (int i = 0; i < newSection.Xsize + (g * 2 - 1); i++) { if (IsInMapLimits(focusCell, EmptyId)) { localMap[focusCell.X, focusCell.Y] = GapId; } focusCell.X--; } //Left Perimeter for (int i = 0; i < newSection.Ysize + (g * 2 - 1); i++) { if (IsInMapLimits(focusCell, EmptyId)) { localMap[focusCell.X, focusCell.Y] = GapId; } focusCell.Y--; } } //If this is the first section -> add spawn Vector on the right side; if (SectionId == 1) { Vector2 nextSectionPosition = new Vector2(newSection.Position.X + newSection.Xsize + Gap, newSection.Position.Y); AddPlace(nextSectionPosition); } else { CalculateNextSpawnPoints(newSection); } return(ConvertLocalToGlobal(newSection)); }
GlobalSectionCell ConvertLocalToGlobal(LocallSectionCell localSection) { Vector3 Pivot = ConvertLocalToGlobal(localSection.Position); return(new GlobalSectionCell(Pivot, localSection.Xsize, localSection.Ysize)); }
//Check direction of the Section, and change Pivot to the top-left corner if needed LocallSectionCell FindProperSection(LocallSectionCell section) { //find proper pivot (left-top) //resize section, if needed //First, we need to define expand direction MapCell checkCell = new MapCell(section.Position.X, section.Position.Y); checkCell.X++; int Xdirection = 1; int Ydirection = 1; if (IsInMapLimits(checkCell, EmptyId)) { Xdirection = 1; } else { Xdirection = -1; } checkCell.X = section.Position.X; checkCell.Y = section.Position.Y + 1; if (IsInMapLimits(checkCell, EmptyId)) { Ydirection = 1; } else { Ydirection = -1; } //directions is determined //now we need to find new section position and size MapCell newLocalPos = new MapCell(section.Position.X, section.Position.Y); int newXsize = section.Xsize; int newYsize = section.Ysize; checkCell.X = newLocalPos.X; checkCell.Y = newLocalPos.Y; MapCell additionalCheckCell = new MapCell(checkCell.X, checkCell.Y); //Check X side if (Ydirection == 1) { additionalCheckCell.Y += (_mapData.MinSectionSize - 1); } else { additionalCheckCell.Y -= (_mapData.MinSectionSize - 1); } for (int i = 0; i < section.Xsize; i++) { if (!IsInMapLimits(checkCell, EmptyId) || !IsInMapLimits(additionalCheckCell, EmptyId)) { newXsize = i; break; } if (Xdirection == 1) { checkCell.X++; additionalCheckCell.X++; } else { checkCell.X--; additionalCheckCell.X--; } } if (Xdirection == -1) { newLocalPos.X = checkCell.X + 1; } checkCell.X = newLocalPos.X; additionalCheckCell.X = section.Position.X; additionalCheckCell.Y = section.Position.Y; //Check Y side if (Xdirection == 1) { additionalCheckCell.X += (newXsize - 1); } for (int i = 0; i < section.Ysize; i++) { if (!IsInMapLimits(checkCell, EmptyId) || !IsInMapLimits(additionalCheckCell, EmptyId)) { newYsize = i; break; } if (Ydirection == 1) { checkCell.Y++; additionalCheckCell.Y++; } else { checkCell.Y--; additionalCheckCell.Y--; } } if (Ydirection == -1) { newLocalPos.Y = checkCell.Y + 1; } if (localMap[newLocalPos.X, newLocalPos.Y] != EmptyId) { Debug.Log("AHTUNG: new pivot is occupied! " + newLocalPos); } return(new LocallSectionCell(newLocalPos, newXsize, newYsize)); }
List <MapCell> TransormPerimeter(LocallSectionCell section) { List <MapCell> Perimeter = new List <MapCell>(); MapCell focusCell = new MapCell(section.Position.X - Gap - 1, section.Position.Y - Gap - 1); int PerimeterShiftIndex = 0; //Go CLockWise -> add cell to the List //go Right for (int i = 0; i < (section.Xsize + Gap * 2 + 2); i++) { focusCell.X = section.Position.X - Gap - 1 + i; Perimeter.Add(focusCell); } //go Bot for (int i = 1; i < (section.Ysize + Gap * 2 + 2); i++) { focusCell.Y = section.Position.Y - Gap - 1 + i; Perimeter.Add(focusCell); } //go Left for (int i = 1; i < (section.Xsize + Gap * 2 + 2); i++) { focusCell.X = section.Position.X + section.Xsize + Gap - i; Perimeter.Add(focusCell); } //go Top for (int i = 1; i < (section.Ysize + Gap * 2 + 1); i++) { focusCell.Y = section.Position.Y + section.Ysize + Gap - i; Perimeter.Add(focusCell); } //First we need to find UNpropriate cell int searchIndex = 0; for (int i = 0; i < Perimeter.Count; i++) { if (!IsInMapLimits(Perimeter[i], EmptyId)) { searchIndex = i; break; } } //Then we continue our search until we find good Cell. In this place sometimes OutOfRangeExeption index appears... for (int i = searchIndex; i < Perimeter.Count; i++) { if (IsInMapLimits(Perimeter[i], EmptyId)) { PerimeterShiftIndex = i; break; } } if (PerimeterShiftIndex == 0) { return(Perimeter); } var ReorderedPerimeter = Perimeter.Skip(PerimeterShiftIndex).Concat(Perimeter.Take(PerimeterShiftIndex)); var ReorderedList = ReorderedPerimeter.ToList <MapCell>(); return(ReorderedList); }