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. MoveSpeed = 300;
  48. }
  49.  
  50. protected override void Process(float delta)
  51. {
  52. base.Process(delta);
  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. protected 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. BasisVelocity = new Vector2(Mathf.MoveToward(BasisVelocity.x, 0, Friction * delta), BasisVelocity.y);
  194. }
  195. else
  196. {
  197. BasisVelocity = new Vector2(Mathf.MoveToward(BasisVelocity.x, dir.x * MoveSpeed, Acceleration * delta),
  198. BasisVelocity.y);
  199. }
  200.  
  201. if (Mathf.IsZeroApprox(dir.y))
  202. {
  203. BasisVelocity = new Vector2(BasisVelocity.x, Mathf.MoveToward(BasisVelocity.y, 0, Friction * delta));
  204. }
  205. else
  206. {
  207. BasisVelocity = new Vector2(BasisVelocity.x,
  208. Mathf.MoveToward(BasisVelocity.y, dir.y * MoveSpeed, Acceleration * delta));
  209. }
  210. }
  211.  
  212. // 播放动画
  213. private void PlayAnim()
  214. {
  215. if (BasisVelocity != Vector2.Zero)
  216. {
  217. if ((Face == FaceDirection.Right && BasisVelocity.x >= 0) || Face == FaceDirection.Left && BasisVelocity.x <= 0) //向前走
  218. {
  219. AnimatedSprite.Animation = AnimatorNames.Run;
  220. }
  221. else if ((Face == FaceDirection.Right && BasisVelocity.x < 0) || Face == FaceDirection.Left && BasisVelocity.x > 0) //向后走
  222. {
  223. AnimatedSprite.Animation = AnimatorNames.ReverseRun;
  224. }
  225. }
  226. else
  227. {
  228. AnimatedSprite.Animation = AnimatorNames.Idle;
  229. }
  230. }
  231. }