TestMainMapVM.cs 4.7 KB

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