Newer
Older
DungeonShooting / DungeonShooting_Godot / src / game / role / Player.cs
  1. using Godot;
  2.  
  3.  
  4. /// <summary>
  5. /// 玩家角色基类, 所有角色都必须继承该类
  6. /// </summary>
  7. public class Player : Role
  8. {
  9. /// <summary>
  10. /// 获取当前操作的角色
  11. /// </summary>
  12. public static Player Current => GameApplication.Instance.Room.Player;
  13. /// <summary>
  14. /// 移动加速度
  15. /// </summary>
  16. public float Acceleration { get; set; } = 1500f;
  17. /// <summary>
  18. /// 移动摩擦力
  19. /// </summary>
  20. public float Friction { get; set; } = 800f;
  21.  
  22. public Player(): base(ResourcePath.prefab_role_Player_tscn)
  23. {
  24. AttackLayer = PhysicsLayer.Wall | PhysicsLayer.Props | PhysicsLayer.Enemy;
  25. Camp = CampEnum.Camp1;
  26. Holster.SlotList[2].Enable = true;
  27. Holster.SlotList[3].Enable = true;
  28. }
  29.  
  30. public override void _Ready()
  31. {
  32. base._Ready();
  33.  
  34. //让相机跟随玩家
  35. // var remoteTransform = new RemoteTransform2D();
  36. // AddChild(remoteTransform);
  37. // MainCamera.Main.GlobalPosition = GlobalPosition;
  38. // MainCamera.Main.ResetSmoothing();
  39. // remoteTransform.RemotePath = remoteTransform.GetPathTo(MainCamera.Main);
  40. RefreshGunTexture();
  41.  
  42. MaxHp = 50;
  43. Hp = 50;
  44. MaxShield = 30;
  45. Shield = 30;
  46. }
  47.  
  48. protected override void Process(float delta)
  49. {
  50. base.Process(delta);
  51. //脸的朝向
  52. var gPos = GlobalPosition;
  53. if (LookTarget == null)
  54. {
  55. Vector2 mousePos = InputManager.GetViewportMousePosition();
  56. if (mousePos.x > gPos.x && Face == FaceDirection.Left)
  57. {
  58. Face = FaceDirection.Right;
  59. }
  60. else if (mousePos.x < gPos.x && Face == FaceDirection.Right)
  61. {
  62. Face = FaceDirection.Left;
  63. }
  64. //枪口跟随鼠标
  65. MountPoint.SetLookAt(mousePos);
  66. }
  67.  
  68. if (Input.IsActionJustPressed("exchange")) //切换武器
  69. {
  70. ExchangeNext();
  71. RefreshGunTexture();
  72. }
  73. else if (Input.IsActionJustPressed("throw")) //扔掉武器
  74. {
  75. ThrowWeapon();
  76. RefreshGunTexture();
  77. }
  78. else if (Input.IsActionJustPressed("interactive")) //互动物体
  79. {
  80. var item = TriggerInteractive();
  81. if (item is Weapon)
  82. {
  83. RefreshGunTexture();
  84. }
  85. }
  86. else if (Input.IsActionJustPressed("reload")) //换弹
  87. {
  88. Reload();
  89. }
  90. if (Input.IsActionPressed("fire")) //开火
  91. {
  92. Attack();
  93. }
  94. //刷新显示的弹药剩余量
  95. RefreshGunAmmunition();
  96.  
  97. var reloadBar = GameApplication.Instance.Ui.ReloadBar;
  98. if (Holster.ActiveWeapon != null && Holster.ActiveWeapon.Reloading)
  99. {
  100. reloadBar.ShowBar(gPos, 1 - Holster.ActiveWeapon.ReloadProgress);
  101. }
  102. else
  103. {
  104. reloadBar.HideBar();
  105. }
  106. }
  107.  
  108. protected override void PhysicsProcess(float delta)
  109. {
  110. base.PhysicsProcess(delta);
  111. HandleMoveInput(delta);
  112. //播放动画
  113. PlayAnim();
  114. }
  115.  
  116. protected override void OnChangeHp(int hp)
  117. {
  118. GameApplication.Instance.Ui.SetHp(hp);
  119. }
  120.  
  121. protected override void OnChangeMaxHp(int maxHp)
  122. {
  123. GameApplication.Instance.Ui.SetMaxHp(maxHp);
  124. }
  125.  
  126. protected override void ChangeInteractiveItem(CheckInteractiveResult result)
  127. {
  128. if (result == null)
  129. {
  130. //隐藏互动提示
  131. GameApplication.Instance.Ui.InteractiveTipBar.HideBar();
  132. }
  133. else
  134. {
  135. if (InteractiveItem is Weapon gun)
  136. {
  137. //显示互动提示
  138. GameApplication.Instance.Ui.InteractiveTipBar.ShowBar(result.Target, result.ShowIcon);
  139. }
  140. }
  141. }
  142.  
  143. protected override void OnChangeShield(int shield)
  144. {
  145. GameApplication.Instance.Ui.SetShield(shield);
  146. }
  147.  
  148. protected override void OnChangeMaxShield(int maxShield)
  149. {
  150. GameApplication.Instance.Ui.SetMaxShield(maxShield);
  151. }
  152.  
  153. /// <summary>
  154. /// 刷新 ui 上手持的物体
  155. /// </summary>
  156. private void RefreshGunTexture()
  157. {
  158. var gun = Holster.ActiveWeapon;
  159. if (gun != null)
  160. {
  161. GameApplication.Instance.Ui.SetGunTexture(gun.GetDefaultTexture());
  162. }
  163. else
  164. {
  165. GameApplication.Instance.Ui.SetGunTexture(null);
  166. }
  167. }
  168.  
  169. /// <summary>
  170. /// 刷新 ui 上显示的弹药量
  171. /// </summary>
  172. private void RefreshGunAmmunition()
  173. {
  174. var gun = Holster.ActiveWeapon;
  175. if (gun != null)
  176. {
  177. GameApplication.Instance.Ui.SetAmmunition(gun.CurrAmmo, gun.ResidueAmmo);
  178. }
  179. }
  180.  
  181. //处理角色移动的输入
  182. private void HandleMoveInput(float delta)
  183. {
  184. //角色移动
  185. // 得到输入的 vector2 getvector方法返回值已经归一化过了noemalized
  186. Vector2 dir = Input.GetVector("move_left", "move_right", "move_up", "move_down");
  187. // 移动. 如果移动的数值接近0(是用 摇杆可能出现 方向 可能会出现浮点),就friction的值 插值 到 0
  188. // 如果 有输入 就以当前速度,用acceleration 插值到 对应方向 * 最大速度
  189. if (Mathf.IsZeroApprox(dir.x))
  190. {
  191. BasisVelocity = new Vector2(Mathf.MoveToward(BasisVelocity.x, 0, Friction * delta), BasisVelocity.y);
  192. }
  193. else
  194. {
  195. BasisVelocity = new Vector2(Mathf.MoveToward(BasisVelocity.x, dir.x * MoveSpeed, Acceleration * delta),
  196. BasisVelocity.y);
  197. }
  198.  
  199. if (Mathf.IsZeroApprox(dir.y))
  200. {
  201. BasisVelocity = new Vector2(BasisVelocity.x, Mathf.MoveToward(BasisVelocity.y, 0, Friction * delta));
  202. }
  203. else
  204. {
  205. BasisVelocity = new Vector2(BasisVelocity.x,
  206. Mathf.MoveToward(BasisVelocity.y, dir.y * MoveSpeed, Acceleration * delta));
  207. }
  208. }
  209.  
  210. // 播放动画
  211. private void PlayAnim()
  212. {
  213. if (BasisVelocity != Vector2.Zero)
  214. {
  215. if ((Face == FaceDirection.Right && BasisVelocity.x >= 0) || Face == FaceDirection.Left && BasisVelocity.x <= 0) //向前走
  216. {
  217. AnimatedSprite.Animation = AnimatorNames.Run;
  218. }
  219. else if ((Face == FaceDirection.Right && BasisVelocity.x < 0) || Face == FaceDirection.Left && BasisVelocity.x > 0) //向后走
  220. {
  221. AnimatedSprite.Animation = AnimatorNames.ReverseRun;
  222. }
  223. }
  224. else
  225. {
  226. AnimatedSprite.Animation = AnimatorNames.Idle;
  227. }
  228. }
  229. }