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