ArrowBoard.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.EventSystems;
  5. using UnityEngine.UI;
  6. namespace DiamondArrow
  7. {
  8. [RequireComponent(typeof(CanvasRenderer))]
  9. public sealed class ArrowBoard : MaskableGraphic, IPointerClickHandler
  10. {
  11. public LevelData level;
  12. public GameSession session;
  13. public Action<int> clicked;
  14. public int highlight=-1,blocked=-1,blocker=-1;
  15. public float highlightUntil,flashUntil;
  16. public bool interactive;
  17. Vector2 contentCenter,cachedSize;
  18. float enteredAt,step;
  19. ArrowMotionPath[] paths;
  20. float[] bumpedAt,hitAt,bumpDistance;
  21. int[,] occupants;
  22. readonly Dictionary<int,Exit> exits=new Dictionary<int,Exit>();
  23. readonly List<Vector2> moving=new List<Vector2>(90);
  24. readonly List<Vector2> highlightLine=new List<Vector2>(90);
  25. struct Exit { public float started,duration,distance;public bool smash; }
  26. public bool HasActiveExits
  27. {
  28. get {foreach(var e in exits.Values)if(Now-e.started<e.duration)return true;return false;}
  29. }
  30. // Capture can sample deterministic timestamps without changing gameplay or save state.
  31. public float PreviewTime=-1;
  32. float Now=>PreviewTime>=0?PreviewTime:Time.unscaledTime;
  33. protected override void OnEnable(){base.OnEnable();if(level!=null&&occupants==null)Configure(level,session);}
  34. public void Configure(LevelData data,GameSession run=null)
  35. {
  36. level=data;session=run;exits.Clear();paths=null;raycastTarget=run!=null;interactive=run!=null;enteredAt=Now;
  37. contentCenter=Vector2.one*((level.size-1)*.5f);
  38. if(run==null)
  39. {
  40. Vector2 min=new Vector2(float.MaxValue,float.MaxValue),max=new Vector2(float.MinValue,float.MinValue);
  41. foreach(var a in level.arrows){foreach(var c in a.cells){var p=new Vector2(c.x,c.y);min=Vector2.Min(min,p);max=Vector2.Max(max,p);}var h=a.cells[a.cells.Length-1];var prev=a.cells[a.cells.Length-2];var tip=new Vector2(h.x,h.y)+new Vector2(h.x-prev.x,h.y-prev.y)*.22f;min=Vector2.Min(min,tip);max=Vector2.Max(max,tip);}contentCenter=(min+max)*.5f;
  42. }
  43. bumpedAt=new float[data.arrows.Length];hitAt=new float[data.arrows.Length];bumpDistance=new float[data.arrows.Length];occupants=new int[data.size,data.size];
  44. for(int y=0;y<data.size;y++)for(int x=0;x<data.size;x++)occupants[x,y]=-1;
  45. for(int i=0;i<data.arrows.Length;i++){bumpedAt[i]=hitAt[i]=-999;foreach(var cell in data.arrows[i].cells)occupants[cell.x,cell.y]=i;}
  46. EnsurePaths();SetVerticesDirty();
  47. }
  48. void EnsurePaths()
  49. {
  50. if(level==null)return;var size=rectTransform.rect.size;if(paths!=null&&cachedSize==size)return;cachedSize=size;step=(Mathf.Min(size.x,size.y)-44)/level.size;
  51. paths=new ArrowMotionPath[level.arrows.Length];for(int i=0;i<paths.Length;i++)paths[i]=new ArrowMotionPath(RoundedPath(level.arrows[i]));
  52. }
  53. public float AnimateExit(int index,bool smash)
  54. {
  55. EnsurePaths();float distance=paths[index].Length+DistanceToEdge(paths[index].Points[paths[index].Points.Length-1],paths[index].Direction)+step;
  56. float duration=smash?.52f:Mathf.Clamp(distance/1500f,.48f,.92f);
  57. exits[index]=new Exit{started=Now,duration=duration,distance=distance,smash=smash};SetVerticesDirty();return duration;
  58. }
  59. float DistanceToEdge(Vector2 p,Vector2 d)
  60. {
  61. Rect r=rectTransform.rect;return d.x>.5f?r.xMax-p.x:d.x<-.5f?p.x-r.xMin:d.y>.5f?r.yMax-p.y:p.y-r.yMin;
  62. }
  63. public void Flash(int index,int other)
  64. {
  65. EnsurePaths();blocked=index;blocker=other;flashUntil=Now+.45f;bumpedAt[index]=Now;if(other>=0)hitAt[other]=Now;
  66. // Move only up to the first occupied cell, then recoil along the same path.
  67. var a=level.arrows[index].cells;var h=a[a.Length-1];var prev=a[a.Length-2];int dx=h.x-prev.x,dy=h.y-prev.y;int gap=1;
  68. for(int x=h.x+dx,y=h.y+dy;x>=0&&y>=0&&x<level.size&&y<level.size;x+=dx,y+=dy,gap++){if(occupants[x,y]==other)break;}
  69. bumpDistance[index]=Mathf.Min(step*.68f,Mathf.Max(step*.12f,(gap-.5f)*step));SetVerticesDirty();
  70. }
  71. void Update(){if(level!=null&&Application.isPlaying)SetVerticesDirty();}
  72. Vector2 Point(Cell c){var r=rectTransform.rect;return r.center+new Vector2((c.x-contentCenter.x)*step,(contentCenter.y-c.y)*step);}
  73. List<Vector2> RoundedPath(ArrowData arrow)
  74. {
  75. var src=new List<Vector2>();foreach(var c in arrow.cells)src.Add(Point(c));var output=new List<Vector2>{src[0]};
  76. for(int i=1;i<src.Count-1;i++)
  77. {
  78. var incoming=(src[i]-src[i-1]).normalized;var outgoing=(src[i+1]-src[i]).normalized;
  79. if(Vector2.Dot(incoming,outgoing)>.99f){output.Add(src[i]);continue;}
  80. float radius=step*.36f;var a=src[i]-incoming*radius;var b=src[i]+outgoing*radius;output.Add(a);
  81. for(int k=1;k<=12;k++){float t=k/12f;output.Add((1-t)*(1-t)*a+2*(1-t)*t*src[i]+t*t*b);}
  82. }
  83. output.Add(src[src.Count-1]);return output;
  84. }
  85. protected override void OnPopulateMesh(VertexHelper vh)
  86. {
  87. vh.Clear();if(level==null||occupants==null)return;EnsurePaths();float now=Now;
  88. for(int x=0;x<level.size;x++)for(int y=0;y<level.size;y++)
  89. {
  90. int owner=occupants[x,y];float pulse=0;
  91. if(owner>=0&&exits.TryGetValue(owner,out var ex))
  92. {
  93. var path=paths[owner];float cellDistance=0;float best=float.MaxValue;var point=Point(new Cell(x,y));
  94. for(int p=0;p<path.Points.Length;p++){float d=(point-path.Points[p]).sqrMagnitude;if(d<best){best=d;cellDistance=path.Distances[p];}}
  95. // Invert the travel curve: pulse a socket when the tail actually passes it.
  96. float fraction=Mathf.Clamp01(cellDistance/ex.distance);
  97. float delay=ex.smash?0:(Mathf.Sqrt(.04f+3.2f*fraction)-.2f)/1.6f*ex.duration;
  98. float age=now-ex.started-delay;if(age>=0)pulse=Mathf.Exp(-age*6)*Mathf.Sin(Mathf.Min(1,age*8)*Mathf.PI*.5f);
  99. }
  100. Circle(vh,Point(new Cell(x,y)),1.2f+pulse*step*.065f,new Color(.63f,.58f,.8f,.18f+pulse*.36f));
  101. }
  102. for(int i=0;i<paths.Length;i++)
  103. {
  104. bool exiting=exits.TryGetValue(i,out var exit);float age=exiting?now-exit.started:0;
  105. if(session!=null&&!session.Alive[i]&&(!exiting||age>=exit.duration))continue;
  106. var path=paths[i];float alpha=session==null?1:Mathf.Clamp01((now-enteredAt-i*.008f)/.25f);
  107. Color color=Neon.ArrowColors[i%8];float blockedAge=now-bumpedAt[i];
  108. if(blockedAge<1.7f)color=Color.Lerp(color,new Color(1,.13f,.25f),1-Mathf.SmoothStep(0,1,(blockedAge-.65f)/1.05f));
  109. if(now-hitAt[i]<.32f)color=Color.Lerp(color,Color.white,Mathf.Exp(-(now-hitAt[i])*10));
  110. if(exiting&&exit.smash){DrawFragments(vh,path,color,age,exit.duration,i);continue;}
  111. float travel=exiting?ArrowMotionPath.Travel(age,exit.duration,exit.distance):ArrowMotionPath.Bump(blockedAge,bumpDistance[i]);
  112. path.Window(travel,moving);
  113. if(exiting)
  114. {
  115. // A faint afterimage follows the same track; the arrow itself keeps its length.
  116. path.Window(ArrowMotionPath.Travel(Mathf.Max(0,age-.035f),exit.duration,exit.distance),highlightLine);
  117. Stroke(vh,highlightLine,step*.23f,new Color(color.r,color.g,color.b,.10f),true);
  118. alpha*=1-Mathf.SmoothStep(0,1,(age/exit.duration-.91f)/.09f);
  119. }
  120. float width=step*.235f;
  121. if(i==highlight&&now<highlightUntil)Stroke(vh,moving,width+9,new Color(1,.78f,.18f,.20f+.10f*Mathf.Sin(now*8)),true);
  122. Stroke(vh,moving,width+4,new Color(.015f,.004f,.035f,alpha*.9f),true);
  123. color.a=alpha;Stroke(vh,moving,width,color,true);
  124. Stroke(vh,moving,width*.48f,new Color(1,1,1,alpha*.16f),false);
  125. // Off-center highlight gives the enamel pipe a cylindrical cross section.
  126. highlightLine.Clear();foreach(var p in moving)highlightLine.Add(p+new Vector2(-.8f,1.0f));Stroke(vh,highlightLine,width*.17f,new Color(1,1,1,alpha*.54f),false);
  127. if(!exiting&&travel<.001f){float start=Mathf.Repeat(now*.38f+i*.137f,1)*path.Length;DrawGlint(vh,path,start,step*.42f,width*.16f,alpha*.65f);}
  128. var dir=path.Direction;var head=moving[moving.Count-1];DrawHead(vh,head,dir,step,color);
  129. }
  130. }
  131. void DrawGlint(VertexHelper vh,ArrowMotionPath path,float start,float length,float width,float alpha)
  132. {
  133. highlightLine.Clear();highlightLine.Add(path.Sample(start));float end=Mathf.Min(path.Length,start+length);for(int i=0;i<path.Points.Length;i++)if(path.Distances[i]>start&&path.Distances[i]<end)highlightLine.Add(path.Points[i]);highlightLine.Add(path.Sample(end));Stroke(vh,highlightLine,width,new Color(1,1,1,alpha),false);
  134. }
  135. static void DrawHead(VertexHelper vh,Vector2 head,Vector2 dir,float step,Color color)
  136. {
  137. Vector2 side=new Vector2(-dir.y,dir.x)*step*.19f;var tip=head+dir*step*.24f;var back=head-dir*step*.15f;
  138. Triangle(vh,tip+dir,back+side*1.10f,back-side*1.10f,new Color(.02f,0,.04f,color.a*.9f));Triangle(vh,tip,back+side,back-side,color);
  139. Triangle(vh,tip-dir*1.7f,back+side*.77f,back,new Color(1,1,1,color.a*.36f));
  140. }
  141. static void DrawFragments(VertexHelper vh,ArrowMotionPath path,Color color,float age,float duration,int seed)
  142. {
  143. float alpha=Mathf.Clamp01(1-age/duration);int count=Mathf.Clamp(Mathf.CeilToInt(path.Length/14),8,32);
  144. for(int p=0;p<count;p++){var v=path.Sample(path.Length*p/(count-1f))+new Vector2(Mathf.Sin(p*19+seed),Mathf.Cos(p*13+seed))*age*180+Vector2.down*age*age*250;float r=3+2*Mathf.Sin(p);Triangle(vh,v+Vector2.up*r,v+new Vector2(r,-r),v+new Vector2(-r,-r),new Color(color.r,color.g,color.b,alpha));}
  145. }
  146. static void Triangle(VertexHelper vh,Vector2 a,Vector2 b,Vector2 c,Color col){int i=vh.currentVertCount;vh.AddVert(a,col,Vector2.zero);vh.AddVert(b,col,Vector2.zero);vh.AddVert(c,col,Vector2.zero);vh.AddTriangle(i,i+1,i+2);}
  147. static void Circle(VertexHelper vh,Vector2 p,float radius,Color color)
  148. {
  149. const int segments=20;int first=vh.currentVertCount;vh.AddVert(p,color,Vector2.zero);var transparent=new Color(color.r,color.g,color.b,0);
  150. for(int i=0;i<=segments;i++){float a=i*Mathf.PI*2/segments;var n=new Vector2(Mathf.Cos(a),Mathf.Sin(a));vh.AddVert(p+n*radius,color,Vector2.zero);vh.AddVert(p+n*(radius+.75f),transparent,Vector2.zero);if(i>0){int v=first+1+i*2;vh.AddTriangle(first,v-2,v);vh.AddTriangle(v-2,v-1,v);vh.AddTriangle(v-1,v+1,v);}}
  151. }
  152. static void Stroke(VertexHelper vh,IList<Vector2> points,float width,Color color,bool antialias)
  153. {
  154. if(points.Count<2)return;int first=vh.currentVertCount;var clear=new Color(color.r,color.g,color.b,0);int stride=antialias?4:2;
  155. for(int i=0;i<points.Count;i++)
  156. {
  157. var tangent=(points[Mathf.Min(i+1,points.Count-1)]-points[Mathf.Max(0,i-1)]).normalized;var n=new Vector2(-tangent.y,tangent.x);
  158. if(antialias)vh.AddVert(points[i]+n*(width*.5f+.8f),clear,Vector2.zero);
  159. vh.AddVert(points[i]+n*width*.5f,color,Vector2.zero);vh.AddVert(points[i]-n*width*.5f,color,Vector2.zero);
  160. if(antialias)vh.AddVert(points[i]-n*(width*.5f+.8f),clear,Vector2.zero);
  161. if(i>0)for(int j=0;j<stride-1;j++){int a=first+(i-1)*stride+j,b=a+stride;vh.AddTriangle(a,b,a+1);vh.AddTriangle(a+1,b,b+1);}
  162. }
  163. Circle(vh,points[0],width*.5f,color);
  164. }
  165. public void OnPointerClick(PointerEventData e)
  166. {
  167. if(!interactive||session==null)return;EnsurePaths();RectTransformUtility.ScreenPointToLocalPointInRectangle(rectTransform,e.position,e.pressEventCamera,out var point);int best=-1;float distance=float.MaxValue;
  168. for(int i=0;i<session.Alive.Length;i++)if(session.Alive[i]){var path=paths[i].Points;for(int j=1;j<path.Length;j++){var a=path[j-1];var d=path[j]-a;float t=Mathf.Clamp01(Vector2.Dot(point-a,d)/Mathf.Max(.001f,d.sqrMagnitude));float dd=Vector2.Distance(point,a+d*t);if(dd<distance){distance=dd;best=i;}}}
  169. if(best>=0&&distance<step*.34f)clicked?.Invoke(best);
  170. }
  171. }
  172. }