DiceEntity.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 bool canRestore = true, pickedUp = false;
  19. private void OnCollisionEnter2D(Collision2D collision) {
  20. if (!collision.gameObject.CompareTag("Player") || pickedUp) return;
  21. pickedUp = true;
  22. AudioM.Play("DicePickup1", 0.8f);
  23. // 关闭其他移动
  24. GetComponent<Collider2D>().enabled = false;
  25. GetComponent<Rigidbody2D>().gravityScale = 0;
  26. GetComponent<Rigidbody2D>().velocity = Vector2.zero;
  27. // 开始旋转
  28. transform.DORotate(new Vector3(0, 0, 360f * Mathf.RoundToInt(rollTime * 1.5f)), rollTime, RotateMode.FastBeyond360);
  29. transform.DOScale(1, rollTime).SetRelative();
  30. transform.DOMoveY(transform.position.y + 2, rollTime);
  31. StartCoroutine(Roll());
  32. }
  33. IEnumerator Roll() {
  34. for (int i = 0; i < rollTime * 8; i++) {
  35. currentLetter = Modules.RandomOne("wasdlr".ToList()).ToString();
  36. UpdateSpriteText();
  37. yield return new WaitForSeconds(0.1f);
  38. }
  39. for (int i = 0; i < Mathf.RoundToInt(rollTime * 0.333f); i++) {
  40. currentLetter = Modules.RandomOne("wasdlr".ToList()).ToString();
  41. UpdateSpriteText();
  42. yield return new WaitForSeconds(0.333f);
  43. }
  44. // 最终确定
  45. string possible = InputM.GetPossibleResults(key, canRestore);
  46. Debug.Log(possible);
  47. currentLetter = Modules.RandomOne(possible.ToCharArray().ToList()).ToString();
  48. UpdateSpriteText();
  49. yield return new WaitForSeconds(0.5f);
  50. // 强调动画
  51. AudioM.Play("DicePickup2");
  52. transform.DOScale(0.5f, 0.1f).SetRelative();
  53. yield return new WaitForSeconds(0.1f);
  54. transform.DOScale(-0.2f, 0.1f).SetRelative();
  55. yield return new WaitForSeconds(0.7f);
  56. // 结束动画
  57. transform.DOScale(-1.3f, endTime).SetRelative();
  58. GameObject endObj = InputM.GetDiceUI(key);
  59. Vector2 screenPos = RectTransformUtility.WorldToScreenPoint(AspectUtility.cam, endObj.transform.position);
  60. transform.DOMove((Vector2) AspectUtility.cam.ScreenToWorldPoint(screenPos), endTime);
  61. yield return new WaitForSeconds(endTime);
  62. // 设置
  63. InputM.SetKey(key, currentLetter);
  64. endObj.GetComponent<DiceUI>().isDropped = false;
  65. endObj.GetComponent<DiceUI>().UpdateColor();
  66. yield return new WaitForSeconds(0.1f);
  67. // 销毁
  68. Modules.DestroyGameObject(gameObject);
  69. }
  70. }