Guest User

Untitled

a guest
May 1st, 2020
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.00 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using DG.Tweening;
  5. using Sirenix.OdinInspector;
  6. using TheraBytes.BetterUi;
  7. using UnityEngine;
  8. using UnityEngine.Serialization;
  9. using UnityEngine.UI;
  10. using UnityEngine.UI.Extensions;
  11.  
  12. public class Hunt : MonoBehaviour
  13. {
  14. [PreviewField] [SerializeField] private List<Sprite> availableSprites;
  15. [SerializeField] private List<Sprite> frames;
  16. [SerializeField] private Button btn;
  17. [SerializeField] private RectTransform huntRT;
  18. [SerializeField] private BetterImage coin;
  19. [SerializeField] private ParticleSystem sparkles_ps;
  20. [SerializeField] private ParticleSystem glow_ps;
  21.  
  22. [NonSerialized] public Action<Hunt> onItemClicked;
  23. [NonSerialized] public Vector2 pos;
  24. private Tween fadeTween;
  25. private int nextFrame;
  26. private int totalFrames;
  27. private Color col;
  28. private ParticleSystem.MainModule mainPs;
  29. private Tweener punchScaleTween;
  30.  
  31. private Sequence m_FinalTweenSequence = null;
  32.  
  33. void Start()
  34. {
  35. btn.onClick.AddListener(showFoundAnimation);
  36.  
  37. totalFrames = frames.Count;
  38.  
  39. var rnd = GuessItemsPanel.rnd;
  40. var rndInt = rnd.Next(0, availableSprites.Count);
  41. var randomSprite = availableSprites[rndInt];
  42. coin.sprite = randomSprite;
  43. nextFrame = int.Parse(randomSprite.name);
  44.  
  45. var wid = Screen.width;
  46. var webWid = wid * .1f;
  47.  
  48. //resize
  49. huntRT.sizeDelta = new Vector2(webWid, webWid);
  50.  
  51. //first color
  52. var randomAlpha = rnd.Next(25, 65);
  53. col = Color.white;
  54. col.a = randomAlpha / 100f;
  55. coin.color = col;
  56.  
  57. //second color
  58. randomAlpha = rnd.Next(30, 60);
  59. col = Color.white;
  60. col.a = randomAlpha / 100f;
  61. coin.SecondColor = col;
  62.  
  63. //Particle Setup
  64. //resize the radius and startsizes
  65. var containWid = huntRT.rect.width;
  66.  
  67. //set aprticle radius
  68. mainPs = sparkles_ps.main;
  69. var shape = sparkles_ps.shape;
  70. shape.radius = containWid * .5f;
  71.  
  72. //set particle size
  73. mainPs.startSize = new ParticleSystem.MinMaxCurve(containWid * .2f, containWid * .5f); //10 - 50 ?
  74. glow_ps.startSize = wid * .25f;
  75.  
  76. pos = transform.position;
  77. }
  78.  
  79. private IEnumerator RotateCoin_Coroutine()
  80. {
  81. var timeout = new WaitForSeconds(0.015f);
  82. nextFrame = 0;
  83.  
  84. while (nextFrame < 50)
  85. {
  86. coin.sprite = frames[(nextFrame) % totalFrames];
  87. nextFrame++;
  88. yield return timeout;
  89. }
  90. }
  91.  
  92. private void showFoundAnimation()
  93. {
  94. // zajisti, ze nekliknem dvakrat:
  95. btn.onClick.RemoveListener(showFoundAnimation);
  96.  
  97. //change alpha to 1
  98. col.a = 1;
  99. coin.ColoringMode = ColorMode.Color;
  100. coin.color = col;
  101.  
  102. //start 3d rotation
  103. // InvokeRepeating(nameof(rotate3D), 0, .015f); - NIKDY NEBO NAVZDY!!!!
  104.  
  105. sparkles_ps.Simulate(0);
  106. sparkles_ps.Play();
  107.  
  108. glow_ps.Simulate(0);
  109. glow_ps.Play();
  110.  
  111. int vibrato = 4;
  112. float duration = 1.2f;
  113. float elasticity = 0.2f;
  114. int maxFrames = 50;
  115. Vector3 targetScale = new Vector3(1.2f, 1.2f, 1.2f);
  116.  
  117. // zarotuj si coinem - at tech 50 framu obstara solo coroutina, nemusim se starat o ukoncovani:
  118. // sama chcipne pri disablu nebo az odtoci co potrebuje...
  119. StartCoroutine(RotateCoin_Coroutine());
  120.  
  121. m_FinalTweenSequence = DOTween.Sequence();
  122. m_FinalTweenSequence.Append(huntRT.DOPunchScale(targetScale, duration, vibrato, elasticity));
  123. m_FinalTweenSequence.InsertCallback(0.4f, () => { onItemClicked?.Invoke(this); });
  124. m_FinalTweenSequence.Insert(0.4f, coin.DOFade(0, .4f).SetEase(Ease.OutQuad));
  125. m_FinalTweenSequence.OnComplete(() => { m_FinalTweenSequence = null; });
  126. }
  127.  
  128.  
  129. private void OnDestroy()
  130. {
  131. // Tohle nevim no. Bud bych mazal cele gameobjekty, nebo particly vypinal - tohle mi divne smrdi, ale nevim jaky mas config...
  132. if (sparkles_ps)
  133. {
  134. sparkles_ps.Clear();
  135. Destroy(sparkles_ps.GetComponent<UIParticleSystem>());
  136. Destroy(sparkles_ps);
  137. }
  138.  
  139. if (glow_ps)
  140. {
  141. glow_ps.Clear();
  142. Destroy(glow_ps.GetComponent<UIParticleSystem>());
  143. Destroy(glow_ps);
  144. }
  145.  
  146. // CancelInvoke(nameof(rotate3D)); -- NIKDY NIKDY !!!
  147.  
  148. //fadeTween?.Kill();
  149. //punchScaleTween?.Kill();
  150. if (m_FinalTweenSequence != null)
  151. {
  152. // jestli furt bezime, tak to pojdme dokoncit...
  153. m_FinalTweenSequence.Complete(true); // dokonci tween sequence a odpal vsechny nalinkovane callbacky!
  154. }
  155.  
  156. // unregister i tady je dobrej napad - kdyby se neodklinkovalo uz klikem, tak to nakonec stejne uvolnime
  157. btn.onClick.RemoveListener(showFoundAnimation);
  158. }
  159. }
Add Comment
Please, Sign In to add comment