DiceEntity.cs 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using DG.Tweening;
  5. using System.Linq;
  6. public class DiceEntity : MonoBehaviour {
  7. public InputM.KeyType key;
  8. private string currentLetter;
  9. private SpriteRenderer spriteText;
  10. private void Start() {
  11. spriteText = GetComponentsInChildren<SpriteRenderer>().Where(sp => sp.gameObject.name == "ImgText").First();
  12. GetComponentsInChildren<SpriteRenderer>().Where(sp => sp.gameObject.name == "ImgType").First().sprite = InputM.GetKeyTypeSprite(key);
  13. currentLetter = InputM.GetKeyRaw(key);
  14. UpdateSpriteText();
  15. }
  16. private void UpdateSpriteText() => spriteText.sprite = InputM.GetKeyTextSprite(currentLetter);
  17. const float rollTime = 3.5f, endTime = 1.5f;
  18. public DiceType diceType = DiceType.NoRestore; public bool pickedUp = false;
  19. public enum DiceType { Restore, NoRestore, Bad }
  20. private void OnCollisionEnter2D(Collision2D collision) {
  21. if (!collision.gameObject.CompareTag("Player") || pickedUp) return;
  22. pickedUp = true;
  23. AudioM.Play("DicePickup1", 0.8f);
  24. // 关闭其他移动
  25. GetComponent<Collider2D>().enabled = false;
  26. GetComponent<Rigidbody2D>().gravityScale = 0;
  27. GetComponent<Rigidbody2D>().velocity = Vector2.zero;
  28. // 开始旋转
  29. transform.DORotate(new Vector3(0, 0, 360f * Mathf.RoundToInt(rollTime * 1.5f)), rollTime, RotateMode.FastBeyond360);
  30. transform.DOScale(1, rollTime).SetRelative();
  31. transform.DOMoveY(transform.position.y + 2, rollTime);
  32. StartCoroutine(Roll());
  33. }
  34. IEnumerator Roll() {
  35. for (int i = 0; i < rollTime * 8; i++) {
  36. currentLetter = Modules.RandomOne("wasdlr".ToList()).ToString();
  37. UpdateSpriteText();
  38. yield return new WaitForSeconds(0.1f);
  39. }
  40. for (int i = 0; i < Mathf.RoundToInt(rollTime * 0.333f); i++) {
  41. currentLetter = Modules.RandomOne("wasdlr".ToList()).ToString();
  42. UpdateSpriteText();
  43. yield return new WaitForSeconds(0.333f);
  44. }
  45. // 最终确定
  46. string possible = InputM.GetPossibleResults(key, diceType);
  47. Debug.Log(possible);
  48. currentLetter = Modules.RandomOne(possible.ToCharArray().ToList()).ToString();
  49. UpdateSpriteText();
  50. yield return new WaitForSeconds(0.5f);
  51. // 强调动画
  52. AudioM.Play("DicePickup2");
  53. transform.DOScale(0.5f, 0.1f).SetRelative();
  54. yield return new WaitForSeconds(0.1f);
  55. transform.DOScale(-0.2f, 0.1f).SetRelative();
  56. yield return new WaitForSeconds(0.7f);
  57. // 结束动画
  58. transform.DOScale(-1.3f, endTime).SetRelative();
  59. GameObject endObj = InputM.GetDiceUI(key);
  60. Vector2 screenPos = RectTransformUtility.WorldToScreenPoint(AspectUtility.cam, endObj.transform.position);
  61. transform.DOMove((Vector2) AspectUtility.cam.ScreenToWorldPoint(screenPos), endTime);
  62. yield return new WaitForSeconds(endTime);
  63. // 设置
  64. InputM.SetKey(key, currentLetter);
  65. endObj.GetComponent<DiceUI>().isDropped = false;
  66. endObj.GetComponent<DiceUI>().UpdateColor();
  67. yield return new WaitForSeconds(0.1f);
  68. // 销毁
  69. Modules.DestroyGameObject(gameObject);
  70. }
  71. }