HpAddEntityView.cs 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. using DG.Tweening;
  2. using System;
  3. using TMPro;
  4. using UnityEngine;
  5. using XGame.Framework.Map;
  6. using XGame.Framework.ObjectCollection;
  7. using XGame.Framework.Utils;
  8. namespace FL.Map
  9. {
  10. public class HpAddEntityView : EntityView<HpAddEntityVM>
  11. {
  12. protected override void OnEnable(object intent)
  13. {
  14. }
  15. protected override void OnDisable()
  16. {
  17. }
  18. protected override void OnDispose()
  19. {
  20. VM.Tr.DOKill();
  21. VM.Root.DOKill();
  22. }
  23. public void ShowValue(int value, Vector3 position, Action callback)
  24. {
  25. VM.HpText.text = value.ToString();
  26. VM.HpText.color = value > 0 ? Color.green : Color.red;
  27. Position = position;
  28. var endPosition = position + new Vector3(2, 4, 0);// new Vector3(UnityEngine.Random.Range(0, 2), UnityEngine.Random.Range(4, 5), 0);
  29. var timeScale = UnityEngine.Random.Range(0.2f, 0.8f);
  30. var linePoint = Vector3.Lerp(position, endPosition, timeScale);
  31. var dir = Vector3.Cross(endPosition - position, Vector3.forward).normalized; //运动方向的法线
  32. var disOffset = UnityEngine.Random.Range(-4f, -1f);
  33. var center = linePoint + dir * disOffset;
  34. var totalTime = 2f;
  35. var timeTween = DOTween.To(() => 0, (t) =>
  36. {
  37. var tNormal = t / totalTime;
  38. //var pos = Vector3.Lerp(position, endPosition, tNormal);
  39. //Position = pos + dir * disOffset * Mathf.Sin(180 * tNormal * Mathf.Deg2Rad);
  40. Position = position.BezierPoint(center, endPosition, tNormal);
  41. }, totalTime, totalTime);
  42. timeTween.SetEase(Ease.OutQuad);
  43. timeTween.onComplete = () =>
  44. {
  45. callback.SafeInvoke();
  46. };
  47. //var jumpTween = VM.Tr.DOJump(endPosition, 2, 1, 3);
  48. //jumpTween.onComplete = () =>
  49. //{
  50. // callback?.Invoke();
  51. //};
  52. }
  53. }
  54. public class HpAddEntityVM : EntityViewModel
  55. {
  56. public TextMeshPro HpText { get; private set; }
  57. public Transform Root { get; private set; }
  58. protected override void OnInit(IObjectCollector collector)
  59. {
  60. HpText = collector.GetComponent<TextMeshPro>("HpText");
  61. Root = collector.GetGameObject("Root").transform;
  62. }
  63. }
  64. }