ArrowBoard.cs 14 KB

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