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