Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using DG.Tweening;
- using DG.Tweening.Core;
- using DG.Tweening.Plugins.Options;
- using Sirenix.OdinInspector;
- using TheraBytes.BetterUi;
- using UnityEngine;
- using UnityEngine.Serialization;
- using UnityEngine.UI;
- using UnityEngine.UI.Extensions;
- using utils;
- public class Hunt : MonoBehaviour
- {
- [PreviewField] [SerializeField] private List<Sprite> availableSprites;
- [SerializeField] private List<Sprite> frames;
- [SerializeField] private Button btn;
- [SerializeField] private RectTransform huntRT;
- [SerializeField] private BetterImage coin;
- [SerializeField] private ParticleSystem sparkles_ps;
- [SerializeField] private ParticleSystem glow_ps;
- [NonSerialized] public Action<Hunt> onItemClicked;
- [NonSerialized] public Vector2 pos;
- private int nextFrame;
- private int totalFrames;
- private Color col;
- private Color col2;
- private ParticleSystem.MainModule mainPs;
- private Sequence foundSeq;
- private Tween hintTween;
- private readonly int max3DRotFrames = 50;
- void Start()
- {
- btn.onClick.AddListener(showFoundAnimation);
- totalFrames = frames.Count;
- var rnd = GuessItemsPanel.rnd;
- var rndInt = rnd.Next(0, availableSprites.Count);
- var randomSprite = availableSprites[rndInt];
- coin.sprite = randomSprite;
- nextFrame = int.Parse(randomSprite.name);
- var wid = Screen.width;
- var webWid = wid * .1f;
- //resize
- huntRT.sizeDelta = new Vector2(webWid, webWid);
- //first color
- var randomAlpha = rnd.Next(25, 65);
- col = Color.white;
- col.a = randomAlpha / 100f;
- coin.color = col;
- //second color
- randomAlpha = rnd.Next(30, 60);
- col = Color.white;
- col.a = randomAlpha / 100f;
- coin.SecondColor = col;
- //define second color as yellow for hint animation
- col2 = Color.yellow;
- //Particle Setup
- //resize the radius and startsizes
- var containWid = huntRT.rect.width;
- //set aprticle radius
- mainPs = sparkles_ps.main;
- var shape = sparkles_ps.shape;
- shape.radius = containWid * .5f;
- //set particle size
- mainPs.startSize = new ParticleSystem.MinMaxCurve(containWid * .2f, containWid * .5f); //10 - 50 ?
- glow_ps.startSize = wid * .25f;
- pos = new Vector2(transform.position.x,transform.position.y+containWid*.5f);
- }
- private void showFoundAnimation()
- {
- btn.onClick.RemoveListener(showFoundAnimation);
- //in case hinting animation is running stop it
- if (hintTween != null)
- {
- Debug.Log("kill HINT Tween!");
- hintTween.Kill();
- }
- //change alpha to 1
- col.a = 1;
- coin.ColoringMode = ColorMode.Color;
- coin.color = col;
- //start 3d rotation
- StartCoroutine(RotateCoin_Coroutine());
- sparkles_ps.Simulate(0);
- sparkles_ps.Play();
- glow_ps.Simulate(0);
- glow_ps.Play();
- int vibrato = 4;
- float duration = 1.2f;
- float elasticity = 0.2f;
- Vector3 targetScale = new Vector3(1.2f, 1.2f, 1.2f);
- //define starting sequence
- foundSeq = DOTween.Sequence();
- foundSeq.Append(huntRT.DOPunchScale(targetScale, duration, vibrato, elasticity));
- foundSeq.InsertCallback(0.4f, () => { onItemClicked?.Invoke(this); });
- foundSeq.Insert(0.4f, coin.DOFade(0, .4f).SetEase(Ease.OutQuad));
- foundSeq.OnComplete(() =>
- {
- foundSeq = null;
- Destroy(gameObject);
- });
- AudioManager.Instance.playClick(AudioManager.CLICKS.COIN_TINKLE);
- }
- private IEnumerator RotateCoin_Coroutine()
- {
- var timeout = new WaitForSeconds(0.02f);
- nextFrame = 0;
- while (nextFrame < max3DRotFrames)
- {
- coin.sprite = frames[(nextFrame) % totalFrames];
- nextFrame++;
- yield return timeout;
- }
- }
- public void showHintAnimation()
- {
- coin.ColoringMode = ColorMode.Color;
- hintTween = coin.DOColor(Color.yellow, 1.4f).SetEase(Ease.InOutQuad).SetLoops(4,LoopType.Yoyo);
- }
- private void OnDestroy()
- {
- hintTween?.Kill();
- foundSeq?.Complete(true);
- btn.onClick.RemoveListener(showFoundAnimation);
- }
- }
Add Comment
Please, Sign In to add comment