TestMainMapVM.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. using DG.Tweening;
  2. using System;
  3. using UnityEngine;
  4. using XGame.Framework.Map;
  5. using XGame.Framework.ObjectCollection;
  6. namespace FL.Map
  7. {
  8. public class TestMainMapVM : IMapViewModel
  9. {
  10. public MeshRenderer BgMesh { get; private set; }
  11. public MeshRenderer CloudMesh { get; private set; }
  12. public Transform MapRoot { get; private set; }
  13. public Transform PlayerRoot { get; private set; }
  14. public Transform EnemyRoot { get; private set; }
  15. public Transform HpRoot { get; private set; }
  16. public ParallelCamera Camera { get; private set; }
  17. public SpriteRenderer BlackMask { get; private set; }
  18. private Vector3 _cameraDefault;
  19. private Vector3 _mapRootDefault;
  20. public TestMainMapVM(IObjectCollector collector)
  21. {
  22. BgMesh = collector.GetComponent<MeshRenderer>("BgMesh");
  23. CloudMesh = collector.GetComponent<MeshRenderer>("CloudMesh");
  24. MapRoot = collector.GetGameObject("MapRoot").transform;
  25. PlayerRoot = collector.GetGameObject("PlayerRoot").transform;
  26. EnemyRoot = collector.GetGameObject("EnemyRoot").transform;
  27. HpRoot = collector.GetGameObject("HpRoot").transform;
  28. Camera = collector.GetComponent<ParallelCamera>("MainCamera");
  29. BlackMask = collector.GetComponent<SpriteRenderer>("BlackMask");
  30. _cameraDefault = Camera.transform.localPosition;
  31. _mapRootDefault = MapRoot.localPosition;
  32. _bgMaterial = BgMesh.material;
  33. _bgSpeed = _bgMaterial.GetFloat("_ScrollY");
  34. BgMesh.material = _bgMaterial;
  35. _cloudMat = CloudMesh.material;
  36. _cloudSpeed = _cloudMat.GetFloat("_ScrollY");
  37. CloudMesh.material = _cloudMat;
  38. }
  39. public void Reset()
  40. {
  41. //BlackMask.gameObject.SetActive(false);
  42. Camera.transform.localPosition = _cameraDefault;
  43. MapRoot.localPosition = _mapRootDefault;
  44. _moveTime = 0;
  45. _targetOffset = Vector3.zero;
  46. SetCloudSpeed(cloud_default_speed);
  47. }
  48. #region 地图滚动
  49. private const float cloud_default_speed = 0.01f;
  50. private Material _bgMaterial;
  51. private Material _cloudMat;
  52. private float _bgSpeed;
  53. private float _cloudSpeed = cloud_default_speed;
  54. private int _moveTime = 0;
  55. private Vector3 _targetOffset = Vector3.zero;
  56. public void SetFocusPosition(Vector3 position)
  57. {
  58. _targetOffset = MapRoot.position - position;
  59. }
  60. public void UpdateMapMoveTime(int millisecond)
  61. {
  62. if (Camera.Target?.IsMoving ?? false)
  63. {
  64. if (_targetOffset != Vector3.zero)
  65. {
  66. MapRoot.position = Camera.Target.Position + _targetOffset;
  67. }
  68. _moveTime += millisecond;
  69. _bgMaterial.SetFloat("_MoveTime", _moveTime * 0.001f);
  70. if (_cloudSpeed != _bgSpeed)
  71. {
  72. SetCloudSpeed(_bgSpeed);
  73. }
  74. }
  75. else if (_cloudSpeed == _bgSpeed)
  76. {
  77. SetCloudSpeed(cloud_default_speed);
  78. }
  79. }
  80. private void SetCloudSpeed(float speed)
  81. {
  82. _cloudSpeed = speed;
  83. _cloudMat.SetFloat("_ScrollY", speed);
  84. }
  85. #endregion
  86. /// <summary>
  87. /// 显示黑屏
  88. /// </summary>
  89. /// <param name="duration">单位:秒</param>
  90. /// <param name="callback"></param>
  91. public void ShowBlackMask(float duration, Action callback)
  92. {
  93. BlackMask.gameObject.SetActive(true);
  94. var color = Color.black;
  95. color.a = 0;
  96. //BlackMask.color = color;
  97. var tween = DOTween.ToAlpha(() => color, (a) => { BlackMask.color = a; }, 1, duration);
  98. tween.onComplete = () =>
  99. {
  100. callback.SafeInvoke();
  101. var tween1 = DOTween.ToAlpha(() => color, (a) => { BlackMask.color = a; }, 0, duration);
  102. tween1.onComplete = () =>
  103. {
  104. BlackMask.gameObject.SetActive(false);
  105. };
  106. };
  107. }
  108. }
  109. }