TestMainMapVM.cs 4.6 KB

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