DiceEntity.cs 3.1 KB

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