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