Newer
Older
DungeonShooting / DungeonShooting_Godot / src / game / camera / GameCamera.cs
@小李xl 小李xl on 12 Mar 2023 5 KB 修复相机抖动存在的问题
  1. using System;
  2. using System.Collections.Generic;
  3. using Godot;
  4.  
  5. /// <summary>
  6. /// 游戏相机
  7. /// </summary>
  8. public partial class GameCamera : Camera2D
  9. {
  10. /// <summary>
  11. /// 当前场景的相机对象
  12. /// </summary>
  13. public static GameCamera Main { get; private set; }
  14.  
  15. /// <summary>
  16. /// 相机坐标更新完成事件
  17. /// </summary>
  18. public event Action<float> OnPositionUpdateEvent;
  19.  
  20. /// <summary>
  21. /// 恢复系数
  22. /// </summary>
  23. [Export]
  24. public float RecoveryCoefficient = 25f;
  25. /// <summary>
  26. /// 抖动开关
  27. /// </summary>
  28. public bool EnableShake { get; set; } = true;
  29.  
  30. /// <summary>
  31. /// 相机跟随目标
  32. /// </summary>
  33. private Role _followTarget;
  34. // 3.5
  35. //public Vector2 SubPixelPosition { get; private set; }
  36.  
  37. private long _index = 0;
  38. private Vector2 _processDistanceSquared = Vector2.Zero;
  39. private Vector2 _processDirection = Vector2.Zero;
  40. //抖动数据
  41. private readonly Dictionary<long, Vector2> _shakeMap = new Dictionary<long, Vector2>();
  42. private Vector2 _camPos;
  43. private Vector2 _shakeOffset = Vector2.Zero;
  44.  
  45. public GameCamera()
  46. {
  47. Main = this;
  48. }
  49. public override void _Ready()
  50. {
  51. _camPos = GlobalPosition;
  52. }
  53.  
  54. public override void _Process(double delta)
  55. {
  56. var newDelta = (float)delta;
  57. _Shake(newDelta);
  58. // 3.5 写法
  59. // var player = GameApplication.Instance.RoomManager.Player;
  60. // var viewportContainer = GameApplication.Instance.SubViewportContainer;
  61. // var camPos = player.GlobalPosition;
  62. // _camPos = _camPos.Lerp(camPos, Mathf.Min(6 * newDelta, 1)) + _shakeOffset;
  63. // SubPixelPosition = _camPos.Round() - _camPos;
  64. // (viewportContainer.Material as ShaderMaterial)?.SetShaderParameter("offset", SubPixelPosition);
  65. // GlobalPosition = _camPos.Round();
  66.  
  67. if (_followTarget != null)
  68. {
  69. var mousePosition = InputManager.GetViewportMousePosition();
  70. var targetPosition = _followTarget.GlobalPosition;
  71. Vector2 targetPos;
  72. if (targetPosition.DistanceSquaredTo(mousePosition) >= (60 / 0.3f) * (60 / 0.3f))
  73. {
  74. targetPos = targetPosition.MoveToward(mousePosition, 60);
  75. }
  76. else
  77. {
  78. targetPos = targetPosition.Lerp(mousePosition, 0.3f);
  79. }
  80. _camPos = (_camPos.Lerp(targetPos, Mathf.Min(6 * newDelta, 1))).Round();
  81. GlobalPosition = _camPos;
  82. Offset = _shakeOffset.Round();
  83. //_temp = _camPos - targetPosition;
  84.  
  85. //调用相机更新事件
  86. if (OnPositionUpdateEvent != null)
  87. {
  88. OnPositionUpdateEvent(newDelta);
  89. }
  90. }
  91. }
  92.  
  93. /// <summary>
  94. /// 设置相机跟随目标
  95. /// </summary>
  96. public void SetFollowTarget(Role target)
  97. {
  98. _followTarget = target;
  99. GlobalPosition = target.GlobalPosition;
  100. }
  101.  
  102. /// <summary>
  103. /// 获取相机跟随目标
  104. /// </summary>
  105. public Role GetFollowTarget()
  106. {
  107. return _followTarget;
  108. }
  109. /// <summary>
  110. /// 设置帧抖动, 结束后自动清零, 需要每一帧调用
  111. /// </summary>
  112. /// <param name="value">抖动的力度</param>
  113. public void Shake(Vector2 value)
  114. {
  115. if (value.LengthSquared() > _processDistanceSquared.LengthSquared())
  116. {
  117. _processDistanceSquared = value;
  118. }
  119. }
  120. /// <summary>
  121. /// 添加一个单方向上的抖动, 该帧结束后自动清零
  122. /// </summary>
  123. public void DirectionalShake(Vector2 value)
  124. {
  125. _processDirection += value;
  126. }
  127. /// <summary>
  128. /// 创建一个抖动, 并设置抖动时间
  129. /// </summary>
  130. public async void CreateShake(Vector2 value, float time)
  131. {
  132. if (time > 0)
  133. {
  134. value.X = Mathf.Abs(value.X);
  135. value.Y = Mathf.Abs(value.Y);
  136. var tempIndex = _index++;
  137. var sceneTreeTimer = GetTree().CreateTimer(time);
  138. _shakeMap[tempIndex] = value;
  139. await ToSignal(sceneTreeTimer, Timer.SignalName.Timeout);
  140. _shakeMap.Remove(tempIndex);
  141. }
  142. }
  143.  
  144. //抖动调用
  145. private void _Shake(float delta)
  146. {
  147. if (EnableShake)
  148. {
  149. var distance = _CalculateDistanceSquared();
  150. distance = new Vector2(Mathf.Sqrt(distance.X), Mathf.Sqrt(distance.Y));
  151. _shakeOffset += _processDirection + new Vector2(
  152. (float)GD.RandRange(-distance.X, distance.X) - Offset.X,
  153. (float)GD.RandRange(-distance.Y, distance.Y) - Offset.Y
  154. );
  155. _processDistanceSquared = Vector2.Zero;
  156. _processDirection = _processDirection.Lerp(Vector2.Zero, RecoveryCoefficient * delta);
  157. }
  158. else
  159. {
  160. _shakeOffset = _shakeOffset.Lerp(Vector2.Zero, RecoveryCoefficient * delta);
  161. }
  162. }
  163.  
  164. //计算相机需要抖动的值
  165. private Vector2 _CalculateDistanceSquared()
  166. {
  167. var temp = Vector2.Zero;
  168. float length = 0;
  169. foreach (var keyValuePair in _shakeMap)
  170. {
  171. var tempLenght = keyValuePair.Value.LengthSquared();
  172. if (tempLenght > length)
  173. {
  174. length = tempLenght;
  175. temp = keyValuePair.Value;
  176. }
  177. }
  178. return _processDistanceSquared.LengthSquared() > length ? _processDistanceSquared : temp;
  179. }
  180. }