Guest User

Hunt

a guest
May 2nd, 2020
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.55 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using DG.Tweening;
  5. using DG.Tweening.Core;
  6. using DG.Tweening.Plugins.Options;
  7. using Sirenix.OdinInspector;
  8. using TheraBytes.BetterUi;
  9. using UnityEngine;
  10. using UnityEngine.Serialization;
  11. using UnityEngine.UI;
  12. using UnityEngine.UI.Extensions;
  13. using utils;
  14.  
  15. public class Hunt : MonoBehaviour
  16. {
  17.     [PreviewField] [SerializeField] private List<Sprite> availableSprites;
  18.     [SerializeField] private List<Sprite> frames;
  19.     [SerializeField] private Button btn;
  20.     [SerializeField] private RectTransform huntRT;
  21.     [SerializeField] private BetterImage coin;
  22.     [SerializeField] private ParticleSystem sparkles_ps;
  23.     [SerializeField] private ParticleSystem glow_ps;
  24.  
  25.     [NonSerialized] public Action<Hunt> onItemClicked;
  26.     [NonSerialized] public Vector2 pos;
  27.  
  28.     private int nextFrame;
  29.     private int totalFrames;
  30.     private Color col;
  31.     private Color col2;
  32.     private ParticleSystem.MainModule mainPs;
  33.     private Sequence foundSeq;
  34.     private Tween hintTween;
  35.     private readonly int max3DRotFrames = 50;
  36.  
  37.  
  38.     void Start()
  39.     {
  40.         btn.onClick.AddListener(showFoundAnimation);
  41.  
  42.         totalFrames = frames.Count;
  43.  
  44.         var rnd = GuessItemsPanel.rnd;
  45.         var rndInt = rnd.Next(0, availableSprites.Count);
  46.         var randomSprite = availableSprites[rndInt];
  47.         coin.sprite = randomSprite;
  48.         nextFrame = int.Parse(randomSprite.name);
  49.  
  50.         var wid = Screen.width;
  51.         var webWid = wid * .1f;
  52.  
  53.         //resize
  54.         huntRT.sizeDelta = new Vector2(webWid, webWid);
  55.  
  56.         //first color
  57.         var randomAlpha = rnd.Next(25, 65);
  58.         col = Color.white;
  59.         col.a = randomAlpha / 100f;
  60.         coin.color = col;
  61.  
  62.         //second color
  63.         randomAlpha = rnd.Next(30, 60);
  64.         col = Color.white;
  65.         col.a = randomAlpha / 100f;
  66.         coin.SecondColor = col;
  67.  
  68.         //define second color as yellow for hint animation
  69.         col2 = Color.yellow;
  70.        
  71.         //Particle Setup
  72.         //resize the radius and startsizes
  73.         var containWid = huntRT.rect.width;
  74.  
  75.         //set aprticle radius
  76.         mainPs = sparkles_ps.main;
  77.         var shape = sparkles_ps.shape;
  78.         shape.radius = containWid * .5f;
  79.  
  80.         //set particle size
  81.         mainPs.startSize = new ParticleSystem.MinMaxCurve(containWid * .2f, containWid * .5f); //10 - 50 ?      
  82.         glow_ps.startSize = wid * .25f;
  83.  
  84.         pos = new Vector2(transform.position.x,transform.position.y+containWid*.5f);
  85.     }
  86.  
  87.     private void showFoundAnimation()
  88.     {
  89.         btn.onClick.RemoveListener(showFoundAnimation);
  90.        
  91.         //in case hinting animation is running stop it
  92.         if (hintTween != null)
  93.         {
  94.             Debug.Log("kill HINT Tween!");
  95.             hintTween.Kill();
  96.         }
  97.  
  98.         //change alpha to 1
  99.         col.a = 1;
  100.         coin.ColoringMode = ColorMode.Color;
  101.         coin.color = col;
  102.  
  103.         //start 3d rotation
  104.         StartCoroutine(RotateCoin_Coroutine());
  105.        
  106.         sparkles_ps.Simulate(0);
  107.         sparkles_ps.Play();
  108.  
  109.         glow_ps.Simulate(0);
  110.         glow_ps.Play();
  111.  
  112.         int vibrato = 4;
  113.         float duration = 1.2f;
  114.         float elasticity = 0.2f;
  115.        
  116.         Vector3 targetScale = new Vector3(1.2f, 1.2f, 1.2f);
  117.  
  118.         //define starting sequence
  119.         foundSeq = DOTween.Sequence();
  120.         foundSeq.Append(huntRT.DOPunchScale(targetScale, duration, vibrato, elasticity));
  121.         foundSeq.InsertCallback(0.4f, () => { onItemClicked?.Invoke(this); });
  122.         foundSeq.Insert(0.4f, coin.DOFade(0, .4f).SetEase(Ease.OutQuad));
  123.         foundSeq.OnComplete(() =>
  124.         {
  125.             foundSeq = null;
  126.             Destroy(gameObject);
  127.         });
  128.  
  129.         AudioManager.Instance.playClick(AudioManager.CLICKS.COIN_TINKLE);
  130.     }
  131.  
  132.     private IEnumerator RotateCoin_Coroutine()
  133.     {
  134.         var timeout = new WaitForSeconds(0.02f);
  135.         nextFrame = 0;
  136.  
  137.         while (nextFrame < max3DRotFrames)
  138.         {
  139.             coin.sprite = frames[(nextFrame) % totalFrames];
  140.             nextFrame++;
  141.             yield return timeout;
  142.         }
  143.     }
  144.        
  145.     public void showHintAnimation()
  146.     {
  147.         coin.ColoringMode = ColorMode.Color;
  148.         hintTween = coin.DOColor(Color.yellow, 1.4f).SetEase(Ease.InOutQuad).SetLoops(4,LoopType.Yoyo);
  149.     }
  150.    
  151.     private void OnDestroy()
  152.     {
  153.         hintTween?.Kill();
  154.         foundSeq?.Complete(true);
  155.         btn.onClick.RemoveListener(showFoundAnimation);
  156.     }
  157. }
Add Comment
Please, Sign In to add comment