| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- using System.Collections;
- using TMPro;
- using UnityEngine;
- using UnityEngine.UI;
- namespace Fateforge
- {
- public sealed partial class FateforgeApp
- {
- string defeatedRun;
- bool ExclusiveMotion => MotionPhase=="ticket" || MotionPhase=="game-over";
- RectTransform MotionOverlay(string name, Color tint)
- {
- var layer=Ui.Rect(motionLayer,name,0,0,1,1);
- Ui.Stretch(layer);
- Ui.Image(layer,null,tint,true);
- motionLayer.SetAsLastSibling();
- return layer;
- }
- void UseTicketFromSlot(int index, Button slot)
- {
- if (!CanAct || !string.IsNullOrEmpty(Game.Board.operation) || index>=Game.State.tickets.Count) return;
- var art=slot.transform.Find("Art_"+Game.State.tickets[index].id)?.GetComponent<Image>();
- var picture=SnapshotTicket(art);
- if (!picture.sprite) return;
- if (!Game.UseTicket(index)) return;
- Save();
- int token=++epoch;
- Busy=true;
- MotionPhase="ticket";
- UpdateEquipment();
- UpdateHeader();
- StartCoroutine(TicketFlight(picture,token,true));
- }
- struct TicketPicture
- {
- public Sprite sprite;
- public Vector2 start,size;
- }
- TicketPicture SnapshotTicket(Image image)
- {
- if (!image || !image.sprite) return default;
- var art=image.rectTransform;
- var size=image.sprite.rect.size;
- float scale=Mathf.Min(art.rect.width/size.x,art.rect.height/size.y);
- return new TicketPicture {sprite=image.sprite,start=MotionPoint(art),
- size=motionLayer.InverseTransformVector(art.TransformVector(size*scale))};
- }
- IEnumerator TicketFlight(TicketPicture picture, int token, bool resumeBoard)
- {
- var sprite=picture.sprite; var size=picture.size;
- var overlay=MotionOverlay("TicketFlight",new Color(.06f,.02f,.07f,.18f));
- var rect=Ui.Rect(overlay,"Ticket",0,0,size.x,size.y);
- rect.anchorMin=rect.anchorMax=rect.pivot=Vector2.one*.5f;
- // MotionPoint uses local coordinates of the same stretched presentation layer.
- Vector2 origin=picture.start-motionLayer.rect.center;
- var top=Half(true); var bottom=Half(false);
- TornTicket Half(bool upper)
- {
- var part=Ui.Rect(rect,upper?"Upper":"Lower",0,0,size.x,size.y);
- part.anchorMin=part.anchorMax=part.pivot=Vector2.one*.5f;
- part.anchoredPosition=Vector2.zero;
- part.gameObject.AddComponent<CanvasRenderer>();
- var graphic=part.gameObject.AddComponent<TornTicket>();
- graphic.sprite=sprite; graphic.upper=upper; graphic.raycastTarget=false;
- return graphic;
- }
- bool sounded=false;
- for (float t=0; t<.90f && token==epoch; t+=Delta)
- {
- float fly=Mathf.SmoothStep(0,1,Mathf.Clamp01(t/.35f));
- rect.anchoredPosition=Vector2.Lerp(origin,Vector2.zero,fly)+Vector2.up*(Mathf.Sin(fly*Mathf.PI)*40);
- rect.localScale=Vector3.one*(1+fly);
- float rip=Mathf.Clamp01((t-.44f)/.46f);
- if (rip>0 && !sounded) { PlayEffect(view.theme.ticketRip); sounded=true; }
- top.rectTransform.anchoredPosition=new Vector2(-38,46)*rip;
- bottom.rectTransform.anchoredPosition=new Vector2(40,-58)*rip;
- top.rectTransform.localRotation=Quaternion.Euler(0,0,17*rip);
- bottom.rectTransform.localRotation=Quaternion.Euler(0,0,-21*rip);
- top.color=bottom.color=new Color(1,1,1,1-rip*rip);
- yield return null;
- }
- if (overlay) Destroy(overlay.gameObject);
- if (token!=epoch || !resumeBoard) yield break;
- Busy=false;
- AfterBoardAction(false);
- }
- void PlayEffect(AudioClip clip,float volume=1)
- {
- if (Settings.sound && clip) effects.PlayOneShot(clip,volume);
- }
- void BeginGameOver()
- {
- if (defeatedRun==Game.State.id)
- {
- if (MotionPhase!="game-over" && !view.modal.gameObject.activeSelf) ShowEnd();
- return;
- }
- defeatedRun=Game.State.id;
- SyncBoard();
- Busy=true;
- MotionPhase="game-over";
- StartCoroutine(GameOverMotion(++epoch));
- }
- IEnumerator GameOverMotion(int token)
- {
- var layer=MotionOverlay("GameOver",Color.clear);
- var dimmer=layer.GetComponent<Image>();
- float center=-motionLayer.rect.center.y;
- var title=Ui.Text(layer,"GameOverTitle","GAME OVER",0,center-86,650,172,93,view.theme);
- title.font=view.theme.titleFont;
- title.fontStyle=FontStyles.Bold;
- title.outlineColor=new Color32(54,22,13,255); title.outlineWidth=.16f;
- PlayEffect(view.theme.defeatStinger);
- for (float t=0;t<1.3f && token==epoch;t+=Delta)
- {
- dimmer.color=new Color(.035f,.011f,.029f,.78f*Mathf.SmoothStep(0,1,t/.30f));
- float enter=Mathf.Clamp01((t-.20f)/.43f);
- float fall=1-Mathf.Pow(1-enter,3);
- title.rectTransform.anchoredPosition=new Vector2(0,-center+86+Mathf.Lerp(140,0,fall)-Mathf.Sin(enter*Mathf.PI)*12);
- title.color=new Color(1,.82f,.49f,enter);
- yield return null;
- }
- if (layer) Destroy(layer.gameObject);
- if (token!=epoch) yield break;
- MotionPhase="idle"; Busy=false; ShowEnd();
- }
- }
- }
|