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