Untitled
unknown
csharp
3 years ago
2.8 kB
11
Indexable
private void tilePolygon()
{
if(_points.Count < 3){ return; }
// Variable for counting clockwise turns in a row
short clockwiseTurnsCount = 0;
// Store points list with info on turns
List<Vector3> pointsWithTurns = generateTurns(_points);
// We go through each point
for (int pointIndex = 0; pointIndex < _points.Count; pointIndex++)
{
// If this point represents a clockwise turn...
if (pointsWithTurns[pointIndex].z > 0f)
{
// We count it
clockwiseTurnsCount++;
//If it's the third one
if (clockwiseTurnsCount == 3)
{
// Generate 4th point and place a mask tile (this updates points list)
placeMaskTile(pointIndex);
// Call function recursively for updated list
tilePolygon();
break;
}
}
// If this point represents a counterclockwise turn
else
{
// Zero the counter
clockwiseTurnsCount = 0;
}
}
}
private void placeMaskTile(int pointIndex)
{
// Calculate the fourth point to make a rectangle
Vector2 fourthPoint = calculateFourthRectVertex(
_points[pointIndex - 2],
_points[pointIndex - 1],
_points[pointIndex]
);
// Make a list of all rectangle points
List<Vector2> rectangle = new List<Vector2>();
rectangle.Add(_points[pointIndex - 2]);
rectangle.Add(_points[pointIndex - 1]);
rectangle.Add(_points[pointIndex]);
rectangle.Add(fourthPoint);
// Calculate points positions
Vector2[] pointsSorted = sortClockwise(rectangle);
// Instantiate and scale a mask tile based on these 4 points
GameObject maskTile = Instantiate(
maskSpritePrefab,
new Vector3(
pointsSorted[0].x + ((pointsSorted[1].x - pointsSorted[0].x) / 2),
pointsSorted[0].y - ((pointsSorted[0].y - pointsSorted[3].y) / 2),
0f
),
transform.rotation
);
maskTile.transform.localScale = new Vector3(
pointsSorted[1].x - pointsSorted[0].x,
pointsSorted[0].y - pointsSorted[3].y,
0f
);
// Insert fourth point in the list, rewrite current
_points[pointIndex] = fourthPoint;
// Delete 2 previous points
_points.RemoveAt(pointIndex - 1);
_points.RemoveAt(pointIndex - 2);
}Editor is loading...