Newer
Older
DungeonShooting / DungeonShooting_Godot / src / game / camera / GameCamera.cs
@lijincheng lijincheng on 24 May 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.  
  68. var world = GameApplication.Instance.World;
  69. if (world != null && !world.Pause && _followTarget != null)
  70. {
  71. var mousePosition = InputManager.CursorPosition;
  72. var targetPosition = _followTarget.GlobalPosition;
  73. Vector2 targetPos;
  74. if (targetPosition.DistanceSquaredTo(mousePosition) >= (60 / 0.3f) * (60 / 0.3f))
  75. {
  76. targetPos = targetPosition.MoveToward(mousePosition, 60);
  77. }
  78. else
  79. {
  80. targetPos = targetPosition.Lerp(mousePosition, 0.3f);
  81. }
  82. _camPos = (_camPos.Lerp(targetPos, Mathf.Min(6 * newDelta, 1))).Round();
  83. GlobalPosition = _camPos;
  84. Offset = _shakeOffset.Round();
  85. //_temp = _camPos - targetPosition;
  86.  
  87. //调用相机更新事件
  88. if (OnPositionUpdateEvent != null)
  89. {
  90. OnPositionUpdateEvent(newDelta);
  91. }
  92. }
  93. }
  94.  
  95. /// <summary>
  96. /// 设置相机跟随目标
  97. /// </summary>
  98. public void SetFollowTarget(Role target)
  99. {
  100. _followTarget = target;
  101. if (target != null)
  102. {
  103. _camPos = target.GlobalPosition;
  104. GlobalPosition = _camPos;
  105. }
  106. }
  107.  
  108. /// <summary>
  109. /// 获取相机跟随目标
  110. /// </summary>
  111. public Role GetFollowTarget()
  112. {
  113. return _followTarget;
  114. }
  115. /// <summary>
  116. /// 设置帧抖动, 结束后自动清零, 需要每一帧调用
  117. /// </summary>
  118. /// <param name="value">抖动的力度</param>
  119. public void Shake(Vector2 value)
  120. {
  121. if (value.LengthSquared() > _processDistanceSquared.LengthSquared())
  122. {
  123. _processDistanceSquared = value;
  124. }
  125. }
  126. /// <summary>
  127. /// 添加一个单方向上的抖动, 该帧结束后自动清零
  128. /// </summary>
  129. public void DirectionalShake(Vector2 value)
  130. {
  131. _processDirection += value;
  132. }
  133. /// <summary>
  134. /// 创建一个抖动, 并设置抖动时间
  135. /// </summary>
  136. public async void CreateShake(Vector2 value, float time)
  137. {
  138. if (time > 0)
  139. {
  140. value.X = Mathf.Abs(value.X);
  141. value.Y = Mathf.Abs(value.Y);
  142. var tempIndex = _index++;
  143. var sceneTreeTimer = GetTree().CreateTimer(time);
  144. _shakeMap[tempIndex] = value;
  145. await ToSignal(sceneTreeTimer, Timer.SignalName.Timeout);
  146. _shakeMap.Remove(tempIndex);
  147. }
  148. }
  149.  
  150. /// <summary>
  151. /// 播放玩家死亡特写镜头
  152. /// </summary>
  153. public void PlayPlayerDieFeatures()
  154. {
  155. }
  156.  
  157. //抖动调用
  158. private void _Shake(float delta)
  159. {
  160. if (EnableShake)
  161. {
  162. var distance = _CalculateDistanceSquared();
  163. distance = new Vector2(Mathf.Sqrt(distance.X), Mathf.Sqrt(distance.Y));
  164. _shakeOffset += _processDirection + new Vector2(
  165. (float)GD.RandRange(-distance.X, distance.X) - Offset.X,
  166. (float)GD.RandRange(-distance.Y, distance.Y) - Offset.Y
  167. );
  168. _processDistanceSquared = Vector2.Zero;
  169. _processDirection = _processDirection.Lerp(Vector2.Zero, RecoveryCoefficient * delta);
  170. }
  171. else
  172. {
  173. _shakeOffset = _shakeOffset.Lerp(Vector2.Zero, RecoveryCoefficient * delta);
  174. }
  175. }
  176.  
  177. //计算相机需要抖动的值
  178. private Vector2 _CalculateDistanceSquared()
  179. {
  180. var temp = Vector2.Zero;
  181. float length = 0;
  182. foreach (var keyValuePair in _shakeMap)
  183. {
  184. var tempLenght = keyValuePair.Value.LengthSquared();
  185. if (tempLenght > length)
  186. {
  187. length = tempLenght;
  188. temp = keyValuePair.Value;
  189. }
  190. }
  191. return _processDistanceSquared.LengthSquared() > length ? _processDistanceSquared : temp;
  192. }
  193. }