Newer
Older
DungeonShooting / DungeonShooting_Godot / src / game / role / Player.cs
@小李xl 小李xl on 7 Nov 2022 6 KB 敌人锁定玩家并自动开火
  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. AttackLayer = PhysicsLayer.Wall | PhysicsLayer.Props | PhysicsLayer.Enemy;
  54. Camp = CampEnum.Camp1;
  55. }
  56.  
  57. public override void _Ready()
  58. {
  59. base._Ready();
  60.  
  61. //让相机跟随玩家
  62. // var remoteTransform = new RemoteTransform2D();
  63. // AddChild(remoteTransform);
  64. // MainCamera.Main.GlobalPosition = GlobalPosition;
  65. // MainCamera.Main.ResetSmoothing();
  66. // remoteTransform.RemotePath = remoteTransform.GetPathTo(MainCamera.Main);
  67. Holster.SlotList[2].Enable = true;
  68. Holster.SlotList[3].Enable = true;
  69. RefreshGunTexture();
  70.  
  71. MaxHp = 50;
  72. Hp = 50;
  73. MaxShield = 30;
  74. Shield = 30;
  75. }
  76.  
  77. public override void _Process(float delta)
  78. {
  79. base._Process(delta);
  80.  
  81. //脸的朝向
  82. var gPos = GlobalPosition;
  83. if (LookTarget == null)
  84. {
  85. Vector2 mousePos = InputManager.GetMousePosition();
  86. if (mousePos.x > gPos.x && Face == FaceDirection.Left)
  87. {
  88. Face = FaceDirection.Right;
  89. }
  90. else if (mousePos.x < gPos.x && Face == FaceDirection.Right)
  91. {
  92. Face = FaceDirection.Left;
  93. }
  94. //枪口跟随鼠标
  95. MountPoint.SetLookAt(mousePos);
  96. }
  97.  
  98. if (Input.IsActionJustPressed("exchange")) //切换武器
  99. {
  100. ExchangeNext();
  101. RefreshGunTexture();
  102. }
  103. else if (Input.IsActionJustPressed("throw")) //扔掉武器
  104. {
  105. ThrowWeapon();
  106. RefreshGunTexture();
  107. }
  108. else if (Input.IsActionJustPressed("interactive")) //互动物体
  109. {
  110. var item = TriggerInteractive();
  111. if (item is Weapon)
  112. {
  113. RefreshGunTexture();
  114. }
  115. }
  116. else if (Input.IsActionJustPressed("reload")) //换弹
  117. {
  118. Reload();
  119. }
  120. if (Input.IsActionPressed("fire")) //开火
  121. {
  122. Attack();
  123. }
  124. //刷新显示的弹药剩余量
  125. RefreshGunAmmunition();
  126.  
  127. var reloadBar = GameApplication.Instance.Ui.ReloadBar;
  128. if (Holster.ActiveWeapon != null && Holster.ActiveWeapon.Reloading)
  129. {
  130. reloadBar.ShowBar(gPos, 1 - Holster.ActiveWeapon.ReloadProgress);
  131. }
  132. else
  133. {
  134. 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. GameApplication.Instance.Ui.SetHp(hp);
  151. }
  152.  
  153. protected override void OnChangeMaxHp(int maxHp)
  154. {
  155. GameApplication.Instance.Ui.SetMaxHp(maxHp);
  156. }
  157.  
  158. protected override void ChangeInteractiveItem(CheckInteractiveResult result)
  159. {
  160. if (result == null)
  161. {
  162. //隐藏互动提示
  163. GameApplication.Instance.Ui.InteractiveTipBar.HideBar();
  164. }
  165. else
  166. {
  167. if (InteractiveItem is Weapon gun)
  168. {
  169. //显示互动提示
  170. GameApplication.Instance.Ui.InteractiveTipBar.ShowBar(result.Target, result.ShowIcon);
  171. }
  172. }
  173. }
  174.  
  175. protected void OnChangeShield(int shield)
  176. {
  177. GameApplication.Instance.Ui.SetShield(shield);
  178. }
  179.  
  180. protected void OnChangeMaxShield(int maxShield)
  181. {
  182. GameApplication.Instance.Ui.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. GameApplication.Instance.Ui.SetGunTexture(gun.GetDefaultTexture());
  194. }
  195. else
  196. {
  197. GameApplication.Instance.Ui.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. GameApplication.Instance.Ui.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. Velocity = MoveAndSlide(Velocity);
  226. }
  227.  
  228. // 播放动画
  229. private void PlayAnim()
  230. {
  231. if (Velocity != Vector2.Zero)
  232. {
  233. if ((Face == FaceDirection.Right && Velocity.x >= 0) || Face == FaceDirection.Left && Velocity.x <= 0) //向前走
  234. {
  235. AnimatedSprite.Animation = AnimatorNames.Run;
  236. }
  237. else if ((Face == FaceDirection.Right && Velocity.x < 0) || Face == FaceDirection.Left && Velocity.x > 0) //向后走
  238. {
  239. AnimatedSprite.Animation = AnimatorNames.ReverseRun;
  240. }
  241. }
  242. else
  243. {
  244. AnimatedSprite.Animation = AnimatorNames.Idle;
  245. }
  246. }
  247. }