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