| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- using UnityEngine;
- using UnityEngine.UI;
- namespace Fateforge
- {
- // Presentation only. The application supplies the animation clock and match membership.
- [DisallowMultipleComponent]
- public sealed class CardMatchEffects : MonoBehaviour
- {
- CardView card;
- UiTheme theme;
- Image tile,flash;
- RectTransform layer;
- readonly RectTransform[] clips=new RectTransform[3];
- readonly UiGlyph[] borders=new UiGlyph[3];
- readonly Image[] sparks=new Image[3];
- readonly Color[] familyColors={new Color(.25f,.96f,1),new Color(.62f,.94f,.25f),new Color(1,.64f,.12f)};
- Color tileColor,symbolColor,valueColor,frameColor;
- Vector3 symbolScale;
- Vector2 lastSize;
- int families;
- float lastPulse,lastDim=1;
- bool configured;
- public void Configure(CardView target,UiTheme palette,Sprite spark)
- {
- if(target==null||palette==null)return;
- if(configured){
- if(card!=target)return;
- theme=palette;
- for(int i=0;i<sparks.Length;i++)sparks[i].sprite=spark;
- Sample(lastPulse,lastDim);
- return;
- }
- card=target;theme=palette;tile=target.GetComponent<Image>();
- tileColor=tile?tile.color:Color.white;symbolColor=card.symbol.color;valueColor=card.value.color;frameColor=card.frame?card.frame.color:Color.clear;symbolScale=card.symbol.rectTransform.localScale;
- layer=Ui.Rect(card.transform,"MatchEffects",0,0,100,100);Ui.Stretch(layer);
- flash=Ui.Image(Ui.Rect(layer,"GlassFlash",0,0,100,100),theme.panelSprite,Color.clear);Ui.Stretch(flash.rectTransform,4,4,4,4);flash.type=Image.Type.Sliced;flash.pixelsPerUnitMultiplier=2.4f;
- for(int i=0;i<3;i++){
- clips[i]=Ui.Rect(layer,"FamilyBand_"+i,0,0,100,100);clips[i].gameObject.AddComponent<RectMask2D>();
- borders[i]=Ui.Glyph(clips[i],"FamilyBorder_"+i,UiGlyph.Shape.CardOutline,0,0,96,96,familyColors[i]);
- }
- for(int i=0;i<sparks.Length;i++){
- sparks[i]=Ui.Image(Ui.Rect(layer,"MatchSpark_"+i,0,0,24,24),spark,Color.clear);sparks[i].preserveAspect=true;
- }
- configured=true;LayoutBands();Sample(0,1);
- }
- public void SetFamilies(int mask)
- {
- mask&=7;if(families==mask)return;families=mask;
- if(!configured)return;LayoutBands();Sample(lastPulse,lastDim);
- }
- public void Sample(float pulse,float dim=1)
- {
- if(!configured||card==null)return;
- pulse=families==0?0:Mathf.Clamp01(pulse);dim=Mathf.Clamp01(dim);lastPulse=pulse;lastDim=dim;
- if(card.Rect.rect.size!=lastSize)LayoutBands();
- if(tile)tile.color=WithAlpha(tileColor,tileColor.a*dim);
- card.symbol.color=WithAlpha(symbolColor,symbolColor.a*dim);card.value.color=WithAlpha(valueColor,valueColor.a*dim);
- card.symbol.rectTransform.localScale=symbolScale*(1+.075f*pulse);
- if(card.outline){card.outline.enabled=families==0;if(families==0)card.outline.color=WithAlpha(theme.gold,.18f*dim);}
- Color tint=Color.clear;int count=0;
- for(int i=0;i<3;i++)if((families&(1<<i))!=0){tint+=familyColors[i];count++;borders[i].color=WithAlpha(familyColors[i],(.78f+.2f*pulse)*dim);}
- if(count>0)tint/=count;
- if(card.frame)card.frame.color=pulse>0?WithAlpha(tint,Mathf.Max(frameColor.a,.10f*pulse)*dim):WithAlpha(frameColor,frameColor.a*dim);
- flash.enabled=pulse>.001f;flash.color=WithAlpha(Color.Lerp(Color.white,tint,.22f),.42f*pulse*dim);
- float sparkSize=Mathf.Clamp(Mathf.Min(lastSize.x,lastSize.y)*.30f,12,38)*(1+.35f*pulse);
- for(int i=0;i<sparks.Length;i++){
- var image=sparks[i];image.enabled=image.sprite!=null&&pulse>.001f&&i<(count>1?3:2);
- image.color=WithAlpha(Color.Lerp(Color.white,tint,.18f),pulse*.88f*dim);
- float x=i==0?.39f:i==1?-.38f:.37f,y=i==0?.11f:i==1?.84f:.77f;
- Ui.Place(image.rectTransform,lastSize.x*x,lastSize.y*y-sparkSize*.5f,sparkSize,sparkSize);
- image.rectTransform.localRotation=Quaternion.Euler(0,0,(i%2==0?1:-1)*18*pulse);
- }
- }
- public void ResetVisuals()
- {families=0;if(configured){LayoutBands();Sample(0,1);}}
- void OnDisable()
- {
- // Children may already have been destroyed when a scene or Play Mode closes.
- families=0;lastPulse=0;lastDim=1;
- foreach(var clip in clips)if(clip)clip.gameObject.SetActive(false);
- foreach(var spark in sparks)if(spark)spark.enabled=false;
- if(flash)flash.enabled=false;if(tile)tile.color=tileColor;
- if(card){if(card.symbol){card.symbol.color=symbolColor;card.symbol.rectTransform.localScale=symbolScale;}if(card.value)card.value.color=valueColor;if(card.frame)card.frame.color=frameColor;if(card.outline)card.outline.enabled=true;}
- }
- void LayoutBands()
- {
- if(!configured||card==null)return;
- lastSize=card.Rect.rect.size;int count=0;for(int i=0;i<3;i++)if((families&(1<<i))!=0)count++;
- int band=0;
- for(int i=0;i<3;i++){
- bool active=(families&(1<<i))!=0;clips[i].gameObject.SetActive(active);if(!active)continue;
- // Each clip sees the same full-card border, exposing only its top/middle/bottom band.
- float from=band/(float)count,to=(band+1)/(float)count;
- clips[i].anchorMin=new Vector2(0,1-to);clips[i].anchorMax=new Vector2(1,1-from);clips[i].offsetMin=clips[i].offsetMax=Vector2.zero;
- Ui.Place(borders[i].rectTransform,0,3-from*lastSize.y,Mathf.Max(1,lastSize.x-6),Mathf.Max(1,lastSize.y-6));band++;
- }
- }
- static Color WithAlpha(Color color,float alpha){color.a=alpha;return color;}
- }
- }
|