53 lines
1.2 KiB
C#
53 lines
1.2 KiB
C#
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public static class SpikeAreaGenerator
|
|
{
|
|
/// <summary>
|
|
/// 生成不重叠的触手突刺落点
|
|
/// </summary>
|
|
public static List<Vector3> Generate(
|
|
Vector3 playerPos,
|
|
int count,
|
|
float minRadius = 2.5f,
|
|
float maxRadius = 5f,
|
|
float minDistance = 1.5f
|
|
)
|
|
{
|
|
List<Vector3> points = new List<Vector3>();
|
|
|
|
int safeGuard = 0;
|
|
int maxTry = 100;
|
|
|
|
while (points.Count < count && safeGuard < maxTry)
|
|
{
|
|
safeGuard++;
|
|
|
|
Vector2 rand = Random.insideUnitCircle.normalized *
|
|
Random.Range(minRadius, maxRadius);
|
|
|
|
Vector3 pos = new Vector3(
|
|
playerPos.x + rand.x,
|
|
playerPos.y,
|
|
playerPos.z + rand.y
|
|
);
|
|
|
|
if (IsFarEnough(pos, points, minDistance))
|
|
{
|
|
points.Add(pos);
|
|
}
|
|
}
|
|
|
|
return points;
|
|
}
|
|
|
|
private static bool IsFarEnough(Vector3 pos, List<Vector3> list, float minDis)
|
|
{
|
|
foreach (var p in list)
|
|
{
|
|
if (Vector3.Distance(pos, p) < minDis)
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
} |