| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202 |
- using System;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.EventSystems;
- using UnityEngine.UI;
- namespace DiamondArrow
- {
- [RequireComponent(typeof(CanvasRenderer))]
- public sealed class ArrowBoard : MaskableGraphic, IPointerClickHandler
- {
- public LevelData level;
- public GameSession session;
- public Action<int> clicked;
- public int highlight=-1,blocked=-1,blocker=-1;
- public int TutorialArrow { get; set; } = -1;
- public float highlightUntil,flashUntil;
- public bool interactive;
- Vector2 contentCenter,cachedSize;
- float enteredAt,step;
- ArrowMotionPath[] paths;
- float[] bumpedAt,hitAt,bumpDistance;
- int[,] occupants;
- readonly Dictionary<int,Exit> exits=new Dictionary<int,Exit>();
- readonly List<Vector2> moving=new List<Vector2>(90);
- readonly List<Vector2> highlightLine=new List<Vector2>(90);
- struct Exit { public float started,duration,distance;public bool smash; }
- public bool HasActiveExits
- {
- get {foreach(var e in exits.Values)if(Now-e.started<e.duration)return true;return false;}
- }
- // Capture can sample deterministic timestamps without changing gameplay or save state.
- public float PreviewTime=-1;
- float Now=>PreviewTime>=0?PreviewTime:Time.unscaledTime;
- protected override void OnEnable(){base.OnEnable();if(level!=null&&occupants==null)Configure(level,session);}
- public void Configure(LevelData data,GameSession run=null)
- {
- TutorialArrow = -1;
- level=data;session=run;exits.Clear();paths=null;raycastTarget=run!=null;interactive=run!=null;enteredAt=Now;
- contentCenter=Vector2.one*((level.size-1)*.5f);
- if(run==null)
- {
- Vector2 min=new Vector2(float.MaxValue,float.MaxValue),max=new Vector2(float.MinValue,float.MinValue);
- 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;
- }
- 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];
- for(int y=0;y<data.size;y++)for(int x=0;x<data.size;x++)occupants[x,y]=-1;
- 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;}
- EnsurePaths();SetVerticesDirty();
- }
- void EnsurePaths()
- {
- 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;
- paths=new ArrowMotionPath[level.arrows.Length];for(int i=0;i<paths.Length;i++)paths[i]=new ArrowMotionPath(RoundedPath(level.arrows[i]));
- }
- public float AnimateExit(int index,bool smash)
- {
- EnsurePaths();float distance=paths[index].Length+DistanceToEdge(paths[index].Points[paths[index].Points.Length-1],paths[index].Direction)+step;
- float duration=smash?.52f:Mathf.Clamp(distance/1500f,.48f,.92f);
- exits[index]=new Exit{started=Now,duration=duration,distance=distance,smash=smash};SetVerticesDirty();return duration;
- }
- float DistanceToEdge(Vector2 p,Vector2 d)
- {
- 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;
- }
- public void Flash(int index,int other)
- {
- EnsurePaths();blocked=index;blocker=other;flashUntil=Now+.45f;bumpedAt[index]=Now;if(other>=0)hitAt[other]=Now;
- // Move only up to the first occupied cell, then recoil along the same path.
- 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;
- 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;}
- bumpDistance[index]=Mathf.Min(step*.68f,Mathf.Max(step*.12f,(gap-.5f)*step));SetVerticesDirty();
- }
- void Update(){if(level!=null&&Application.isPlaying)SetVerticesDirty();}
- Vector2 Point(Cell c){var r=rectTransform.rect;return r.center+new Vector2((c.x-contentCenter.x)*step,(contentCenter.y-c.y)*step);}
- List<Vector2> RoundedPath(ArrowData arrow)
- {
- var src=new List<Vector2>();foreach(var c in arrow.cells)src.Add(Point(c));var output=new List<Vector2>{src[0]};
- for(int i=1;i<src.Count-1;i++)
- {
- var incoming=(src[i]-src[i-1]).normalized;var outgoing=(src[i+1]-src[i]).normalized;
- if(Vector2.Dot(incoming,outgoing)>.99f){output.Add(src[i]);continue;}
- float radius=step*.36f;var a=src[i]-incoming*radius;var b=src[i]+outgoing*radius;output.Add(a);
- 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);}
- }
- output.Add(src[src.Count-1]);return output;
- }
- protected override void OnPopulateMesh(VertexHelper vh)
- {
- vh.Clear();if(level==null||occupants==null)return;EnsurePaths();float now=Now;
- for(int x=0;x<level.size;x++)for(int y=0;y<level.size;y++)
- {
- int owner=occupants[x,y];float pulse=0;
- if(owner>=0&&exits.TryGetValue(owner,out var ex))
- {
- var path=paths[owner];float cellDistance=0;float best=float.MaxValue;var point=Point(new Cell(x,y));
- 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];}}
- // Invert the travel curve: pulse a socket when the tail actually passes it.
- float fraction=Mathf.Clamp01(cellDistance/ex.distance);
- float delay=ex.smash?0:(Mathf.Sqrt(.04f+3.2f*fraction)-.2f)/1.6f*ex.duration;
- float age=now-ex.started-delay;if(age>=0)pulse=Mathf.Exp(-age*6)*Mathf.Sin(Mathf.Min(1,age*8)*Mathf.PI*.5f);
- }
- Circle(vh,Point(new Cell(x,y)),1.2f+pulse*step*.065f,new Color(.63f,.58f,.8f,.18f+pulse*.36f));
- }
- float pulseStrength = TutorialArrow >= 0 ? .5f - .5f * Mathf.Cos((now - enteredAt) * 4f) : 0f;
- for(int i=0;i<paths.Length;i++)
- {
- bool exiting=exits.TryGetValue(i,out var exit);float age=exiting?now-exit.started:0;
- if(session!=null&&!session.Alive[i]&&(!exiting||age>=exit.duration))continue;
- var path=paths[i];float alpha=session==null?1:Mathf.Clamp01((now-enteredAt-i*.008f)/.25f);
- Color color=Neon.ArrowColors[i%8];float blockedAge=now-bumpedAt[i];
- if(blockedAge<1.7f)color=Color.Lerp(color,new Color(1,.13f,.25f),1-Mathf.SmoothStep(0,1,(blockedAge-.65f)/1.05f));
- if(now-hitAt[i]<.32f)color=Color.Lerp(color,Color.white,Mathf.Exp(-(now-hitAt[i])*10));
- if(exiting&&exit.smash){DrawFragments(vh,path,color,age,exit.duration,i);continue;}
- float travel=exiting?ArrowMotionPath.Travel(age,exit.duration,exit.distance):ArrowMotionPath.Bump(blockedAge,bumpDistance[i]);
- path.Window(travel,moving);
- if(exiting)
- {
- // A faint afterimage follows the same track; the arrow itself keeps its length.
- path.Window(ArrowMotionPath.Travel(Mathf.Max(0,age-.035f),exit.duration,exit.distance),highlightLine);
- Stroke(vh,highlightLine,step*.23f,new Color(color.r,color.g,color.b,.10f),true);
- alpha*=1-Mathf.SmoothStep(0,1,(age/exit.duration-.91f)/.09f);
- }
- float width=step*.235f;
- bool guided = i == TutorialArrow;
- if (guided)
- {
- color = new Color(1f, .70f, .15f);
- width *= 1.12f;
- Stroke(vh, moving, width + step * (.16f + .06f * pulseStrength), new Color(1f, .58f, .08f, alpha * .32f), true);
- Stroke(vh, moving, width + step * .085f, new Color(1f, .92f, .60f, alpha), true);
- }
- else if (TutorialArrow >= 0) alpha *= .28f;
- if(i==highlight&&now<highlightUntil)Stroke(vh,moving,width+9,new Color(1,.78f,.18f,.20f+.10f*Mathf.Sin(now*8)),true);
- Stroke(vh,moving,width+4,new Color(.015f,.004f,.035f,alpha*.9f),true);
- color.a=alpha;Stroke(vh,moving,width,color,true);
- Stroke(vh,moving,width*.48f,new Color(1,1,1,alpha*.16f),false);
- // Off-center highlight gives the enamel pipe a cylindrical cross section.
- 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);
- 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);}
- var dir=path.Direction;var head=moving[moving.Count-1];DrawHead(vh,head,dir,guided ? step*1.15f : step,color);
- if (guided) DrawTapRing(vh, head, step * (.34f + .06f * pulseStrength), step * .028f, new Color(1f, .90f, .56f, alpha));
- }
- }
- static void DrawTapRing(VertexHelper vh, Vector2 center, float radius, float thickness, Color color)
- {
- const int segments = 48;
- int first = vh.currentVertCount;
- for (int i = 0; i <= segments; i++)
- {
- float angle = i * 2f * Mathf.PI / segments;
- var direction = new Vector2(Mathf.Cos(angle), Mathf.Sin(angle));
- vh.AddVert(center + direction * radius, color, Vector2.zero);
- vh.AddVert(center + direction * (radius - thickness), color, Vector2.zero);
- if (i == 0) continue;
- int outer = first + 2 * i;
- vh.AddTriangle(outer - 2, outer, outer - 1);
- vh.AddTriangle(outer, outer + 1, outer - 1);
- }
- }
- void DrawGlint(VertexHelper vh,ArrowMotionPath path,float start,float length,float width,float alpha)
- {
- 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);
- }
- static void DrawHead(VertexHelper vh,Vector2 head,Vector2 dir,float step,Color color)
- {
- Vector2 side=new Vector2(-dir.y,dir.x)*step*.19f;var tip=head+dir*step*.24f;var back=head-dir*step*.15f;
- 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);
- Triangle(vh,tip-dir*1.7f,back+side*.77f,back,new Color(1,1,1,color.a*.36f));
- }
- static void DrawFragments(VertexHelper vh,ArrowMotionPath path,Color color,float age,float duration,int seed)
- {
- float alpha=Mathf.Clamp01(1-age/duration);int count=Mathf.Clamp(Mathf.CeilToInt(path.Length/14),8,32);
- 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));}
- }
- 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);}
- static void Circle(VertexHelper vh,Vector2 p,float radius,Color color)
- {
- 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);
- 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);}}
- }
- static void Stroke(VertexHelper vh,IList<Vector2> points,float width,Color color,bool antialias)
- {
- 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;
- for(int i=0;i<points.Count;i++)
- {
- 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);
- if(antialias)vh.AddVert(points[i]+n*(width*.5f+.8f),clear,Vector2.zero);
- vh.AddVert(points[i]+n*width*.5f,color,Vector2.zero);vh.AddVert(points[i]-n*width*.5f,color,Vector2.zero);
- if(antialias)vh.AddVert(points[i]-n*(width*.5f+.8f),clear,Vector2.zero);
- 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);}
- }
- Circle(vh,points[0],width*.5f,color);
- }
- public void OnPointerClick(PointerEventData e)
- {
- if(!interactive||session==null)return;EnsurePaths();RectTransformUtility.ScreenPointToLocalPointInRectangle(rectTransform,e.position,e.pressEventCamera,out var point);int best=-1;float distance=float.MaxValue;
- 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;}}}
- if(best>=0&&distance<step*.34f)clicked?.Invoke(best);
- }
- }
- }
|