320 lines
9.8 KiB
C#
320 lines
9.8 KiB
C#
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class GearFollower : MonoBehaviour
|
|
{
|
|
[Header("参数")]
|
|
public SpriteRenderer targetSpriteRenderer;
|
|
public float snapDistance = 0.3f;
|
|
public float detachDistance = 1.0f;
|
|
public float tractionSpeed = 2f;
|
|
|
|
[Header("切割效果参数")]
|
|
public float cutEdgeExpandDuration = 0.5f; // 卷边展开时间
|
|
public float cutEdgeCoolDuration = 3f; // 冷却时间
|
|
public float cutEdgeMaxOffset = 0.05f; // 卷边最大偏移量
|
|
|
|
public Gradient cutEdgeColorGradient; // 颜色渐变(从高温到冷却)
|
|
|
|
public Material cutEdgeMaterial; // 用于绘制卷边的材质
|
|
|
|
private List<Vector2> edgePoints;
|
|
private int currentEdgeIndex = 0;
|
|
private int tractionDirection = 1;
|
|
|
|
private enum State { Follow, Traction }
|
|
private State currentState = State.Follow;
|
|
|
|
private Camera mainCamera;
|
|
|
|
// 记录切割的边段及其动画状态
|
|
private class CutSegment
|
|
{
|
|
public int startIndex;
|
|
public float timeElapsed; // 0~卷边展开+冷却总时长
|
|
public LineRenderer lineRenderer;
|
|
}
|
|
private List<CutSegment> cutSegments = new List<CutSegment>();
|
|
|
|
void Start()
|
|
{
|
|
mainCamera = Camera.main;
|
|
LoadEdgePointsFromSprite();
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
Vector2 mouseWorldPos = mainCamera.ScreenToWorldPoint(Input.mousePosition);
|
|
|
|
if (Input.GetMouseButton(0))
|
|
{
|
|
transform.Rotate(Vector3.forward, -360f * Time.deltaTime);
|
|
}
|
|
|
|
switch (currentState)
|
|
{
|
|
case State.Follow:
|
|
UpdateFollowState(mouseWorldPos);
|
|
break;
|
|
case State.Traction:
|
|
UpdateTractionState(mouseWorldPos);
|
|
break;
|
|
}
|
|
|
|
UpdateCutSegments();
|
|
}
|
|
|
|
private void LoadEdgePointsFromSprite()
|
|
{
|
|
edgePoints = new List<Vector2>();
|
|
if (targetSpriteRenderer == null)
|
|
{
|
|
Debug.LogError("targetSpriteRenderer 为空");
|
|
return;
|
|
}
|
|
|
|
List<Vector2> shapePoints = new List<Vector2>();
|
|
targetSpriteRenderer.sprite.GetPhysicsShape(0, shapePoints);
|
|
|
|
if (shapePoints.Count == 0)
|
|
{
|
|
Debug.LogError("目标Sprite没有物理形状点");
|
|
return;
|
|
}
|
|
|
|
float desiredSegmentLength = 0.1f; // 你可以根据需要调整这个值
|
|
|
|
edgePoints.Clear();
|
|
|
|
for (int i = 0; i < shapePoints.Count; i++)
|
|
{
|
|
Vector2 p1 = targetSpriteRenderer.transform.TransformPoint(shapePoints[i]);
|
|
Vector2 p2 = targetSpriteRenderer.transform.TransformPoint(shapePoints[(i + 1) % shapePoints.Count]);
|
|
|
|
float segmentLength = Vector2.Distance(p1, p2);
|
|
int steps = Mathf.Max(1, Mathf.CeilToInt(segmentLength / desiredSegmentLength));
|
|
|
|
for (int s = 0; s < steps; s++)
|
|
{
|
|
float t = (float)s / steps;
|
|
Vector2 interpolated = Vector2.Lerp(p1, p2, t);
|
|
edgePoints.Add(interpolated);
|
|
}
|
|
}
|
|
|
|
// 确保闭环
|
|
if ((edgePoints[0] - edgePoints[edgePoints.Count - 1]).sqrMagnitude > 0.0001f)
|
|
{
|
|
edgePoints.Add(edgePoints[0]);
|
|
}
|
|
|
|
Debug.Log($"边缘点数量:{edgePoints.Count}");
|
|
}
|
|
|
|
private void UpdateFollowState(Vector2 mousePos)
|
|
{
|
|
transform.position = mousePos;
|
|
|
|
int nearestIndex = FindNearestEdgePointIndex(mousePos, out float dist);
|
|
if (dist < snapDistance)
|
|
{
|
|
currentEdgeIndex = nearestIndex;
|
|
|
|
// 判断应该向前还是向后吸附
|
|
Vector2 prev = edgePoints[WrapIndex(currentEdgeIndex - 1)];
|
|
Vector2 next = edgePoints[WrapIndex(currentEdgeIndex + 1)];
|
|
|
|
float distToPrev = Vector2.Distance(mousePos, prev);
|
|
float distToNext = Vector2.Distance(mousePos, next);
|
|
|
|
tractionDirection = (distToNext < distToPrev) ? 1 : -1;
|
|
|
|
transform.position = edgePoints[currentEdgeIndex];
|
|
currentState = State.Traction;
|
|
}
|
|
}
|
|
|
|
private float directionSwitchCooldown = 0.2f;
|
|
private float directionSwitchTimer = 0f;
|
|
|
|
private void UpdateTractionState(Vector2 mousePos)
|
|
{
|
|
Vector2 gearPos = transform.position;
|
|
|
|
if (Vector2.Distance(mousePos, gearPos) > detachDistance)
|
|
{
|
|
currentState = State.Follow;
|
|
return;
|
|
}
|
|
|
|
if (!Input.GetMouseButton(0))
|
|
{
|
|
// 不按左键不移动
|
|
return;
|
|
}
|
|
|
|
int nextIndex = WrapIndex(currentEdgeIndex + tractionDirection);
|
|
Vector2 from = edgePoints[currentEdgeIndex];
|
|
Vector2 to = edgePoints[nextIndex];
|
|
|
|
// 方向判断优化:使用角度判断是否反方向
|
|
Vector2 forward = (to - from).normalized;
|
|
Vector2 toMouse = (mousePos - from).normalized;
|
|
float angle = Vector2.SignedAngle(forward, toMouse); // -180 ~ 180
|
|
|
|
// 当角度大于120度,说明鼠标方向明显是反的
|
|
if (Mathf.Abs(angle) > 120f)
|
|
{
|
|
directionSwitchTimer += Time.deltaTime;
|
|
if (directionSwitchTimer > directionSwitchCooldown)
|
|
{
|
|
tractionDirection *= -1;
|
|
directionSwitchTimer = 0f;
|
|
|
|
// 更新前进段
|
|
nextIndex = WrapIndex(currentEdgeIndex + tractionDirection);
|
|
from = edgePoints[currentEdgeIndex];
|
|
to = edgePoints[nextIndex];
|
|
forward = (to - from).normalized;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
directionSwitchTimer = 0f;
|
|
}
|
|
|
|
Vector2 targetPoint = GetClosestPointOnSegment(from, to, mousePos);
|
|
Vector2 moveDir = (targetPoint - gearPos);
|
|
float distance = moveDir.magnitude;
|
|
|
|
float maxStep = tractionSpeed * Time.deltaTime;
|
|
|
|
if (distance <= maxStep)
|
|
{
|
|
transform.position = targetPoint;
|
|
|
|
// 如果到达段末,前进一段
|
|
if (Vector2.Distance(targetPoint, to) < 0.01f)
|
|
{
|
|
// 记录切割痕迹
|
|
AddCutSegment(currentEdgeIndex);
|
|
|
|
currentEdgeIndex = nextIndex;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
transform.position += (Vector3)(moveDir.normalized * maxStep);
|
|
}
|
|
}
|
|
|
|
// 添加一个切割段,生成卷边视觉效果
|
|
private void AddCutSegment(int startIndex)
|
|
{
|
|
// 防止重复添加同一段(可选)
|
|
foreach (var seg in cutSegments)
|
|
{
|
|
if (seg.startIndex == startIndex)
|
|
return;
|
|
}
|
|
|
|
GameObject go = new GameObject($"CutSegment_{startIndex}");
|
|
go.transform.parent = transform.parent; // 放在GearFollower同级方便管理
|
|
|
|
LineRenderer lr = go.AddComponent<LineRenderer>();
|
|
lr.positionCount = 4; // 画一个四点的矩形条表示卷边
|
|
lr.material = cutEdgeMaterial != null ? cutEdgeMaterial : new Material(Shader.Find("Sprites/Default"));
|
|
lr.widthCurve = AnimationCurve.Constant(0, 1, 0.05f);
|
|
lr.numCapVertices = 4;
|
|
lr.useWorldSpace = true;
|
|
|
|
CutSegment newSeg = new CutSegment
|
|
{
|
|
startIndex = startIndex,
|
|
timeElapsed = 0f,
|
|
lineRenderer = lr
|
|
};
|
|
|
|
cutSegments.Add(newSeg);
|
|
}
|
|
|
|
// 每帧更新所有切割段动画
|
|
private void UpdateCutSegments()
|
|
{
|
|
for (int i = cutSegments.Count - 1; i >= 0; i--)
|
|
{
|
|
var seg = cutSegments[i];
|
|
seg.timeElapsed += Time.deltaTime;
|
|
|
|
float expandT = Mathf.Clamp01(seg.timeElapsed / cutEdgeExpandDuration);
|
|
float coolT = Mathf.Clamp01((seg.timeElapsed - cutEdgeExpandDuration) / cutEdgeCoolDuration);
|
|
|
|
// 获取该段两个端点
|
|
int nextIndex = WrapIndex(seg.startIndex + 1);
|
|
Vector2 p1 = edgePoints[seg.startIndex];
|
|
Vector2 p2 = edgePoints[nextIndex];
|
|
|
|
Vector2 segmentDir = (p2 - p1).normalized;
|
|
Vector2 normal = new Vector2(-segmentDir.y, segmentDir.x);
|
|
|
|
float offset = cutEdgeMaxOffset * expandT;
|
|
|
|
// 四个点形成的卷边矩形
|
|
Vector3[] positions = new Vector3[4];
|
|
// 主边
|
|
positions[0] = p1;
|
|
positions[1] = p2;
|
|
// 卷边偏移边(沿法线方向偏移offset)
|
|
positions[2] = p2 + normal * offset;
|
|
positions[3] = p1 + normal * offset;
|
|
|
|
seg.lineRenderer.positionCount = 4;
|
|
seg.lineRenderer.SetPositions(positions);
|
|
|
|
// 颜色渐变
|
|
Color color = cutEdgeColorGradient.Evaluate(1f - coolT);
|
|
seg.lineRenderer.startColor = color;
|
|
seg.lineRenderer.endColor = color;
|
|
|
|
// 冷却结束删除该段
|
|
if (seg.timeElapsed > cutEdgeExpandDuration + cutEdgeCoolDuration)
|
|
{
|
|
Destroy(seg.lineRenderer.gameObject);
|
|
cutSegments.RemoveAt(i);
|
|
}
|
|
}
|
|
}
|
|
|
|
private int FindNearestEdgePointIndex(Vector2 pos, out float minDist)
|
|
{
|
|
int nearestIndex = 0;
|
|
minDist = float.MaxValue;
|
|
|
|
for (int i = 0; i < edgePoints.Count; i++)
|
|
{
|
|
float dist = Vector2.Distance(pos, edgePoints[i]);
|
|
if (dist < minDist)
|
|
{
|
|
minDist = dist;
|
|
nearestIndex = i;
|
|
}
|
|
}
|
|
return nearestIndex;
|
|
}
|
|
|
|
private Vector2 GetClosestPointOnSegment(Vector2 a, Vector2 b, Vector2 p)
|
|
{
|
|
Vector2 ab = b - a;
|
|
float t = Vector2.Dot(p - a, ab) / ab.sqrMagnitude;
|
|
t = Mathf.Clamp01(t);
|
|
return a + ab * t;
|
|
}
|
|
|
|
private int WrapIndex(int index)
|
|
{
|
|
if (index < 0) return edgePoints.Count - 2;
|
|
if (index >= edgePoints.Count - 1) return 0;
|
|
return index;
|
|
}
|
|
}
|