CardView.cs 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. using System;
  2. using TMPro;
  3. using UnityEngine;
  4. using UnityEngine.EventSystems;
  5. using UnityEngine.UI;
  6. namespace Fateforge
  7. {
  8. public sealed class CardView : MonoBehaviour,IPointerClickHandler
  9. {
  10. public Image symbol,frame,sticker;
  11. public UiGlyph outline;
  12. public TMP_Text value,mark;
  13. public int CardId {get;private set;}
  14. public Action<int,bool> Click;
  15. public RectTransform Rect => (RectTransform)transform;
  16. public Vector2 Target {get;set;}
  17. float lastTap=-10,spawnAge=1;
  18. public void Bind(Cell cell,Game game,ArtLibrary art,bool animate=false)
  19. {
  20. CardId=cell.id;symbol.sprite=art.Sprite(cell.symbol);value.text=English.Number(game.CardValue(cell));string tag=game.Sticker(cell);sticker.sprite=art.Sprite(tag);sticker.enabled=sticker.sprite!=null;
  21. bool fresh=game.Board.operation=="discard"&&!game.Board.eligible.Contains(cell.id);mark.text=fresh?"NEW":game.Board.selected>=0&&game.Board.cells[game.Board.selected].id==cell.id?"+":"";
  22. if(animate)spawnAge=0;
  23. }
  24. UiGlyph selectedCorners;
  25. public bool DiscardFlash {get;private set;}
  26. public void ShowDiscardPick(UiTheme theme)
  27. {
  28. DiscardFlash=true;
  29. var matches=GetComponent<CardMatchEffects>();if(matches)matches.ResetVisuals();
  30. SetSelected(true,theme);
  31. }
  32. public void SetSelected(bool active,UiTheme theme)
  33. {
  34. if(active && !selectedCorners)
  35. {
  36. selectedCorners=Ui.Glyph(transform,"SelectedCard",UiGlyph.Shape.SelectionCorners,0,0,1,1,theme.gold);
  37. Ui.Stretch(selectedCorners.rectTransform);
  38. }
  39. if(selectedCorners){selectedCorners.gameObject.SetActive(active);if(active)selectedCorners.transform.SetAsLastSibling();}
  40. if(active){if(outline)outline.color=theme.gold;frame.color=new Color(1,.76f,.27f,.18f);mark.text="";}
  41. }
  42. public void Highlight(Color color,float alpha=1){frame.color=new Color(color.r,color.g,color.b,alpha*.08f);if(outline)outline.color=new Color(color.r,color.g,color.b,Mathf.Max(.18f,alpha));}
  43. public void OnPointerClick(PointerEventData eventData){bool twice=eventData.clickCount>=2||Time.unscaledTime-lastTap<.3f;lastTap=Time.unscaledTime;Click?.Invoke(CardId,twice);}
  44. void Update(){if(spawnAge<1){spawnAge+=Time.unscaledDeltaTime*5;float t=Mathf.Clamp01(spawnAge);float scale=1+Mathf.Sin(t*Mathf.PI)*.13f;transform.localScale=Vector3.one*Mathf.Lerp(.55f,scale,Mathf.Min(1,t*4));}else transform.localScale=Vector3.one;}
  45. }
  46. }