Newer
Older
DungeonShooting / DungeonShooting_Godot / src / game / role / Player.cs
@小李xl 小李xl on 7 Nov 2022 6 KB 添加Role攻击目标
  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. Vector2 mousePos = InputManager.GetMousePosition();
  82. //脸的朝向
  83. var gPos = GlobalPosition;
  84. if (mousePos.x > gPos.x && Face == FaceDirection.Left)
  85. {
  86. Face = FaceDirection.Right;
  87. }
  88. else if (mousePos.x < gPos.x && Face == FaceDirection.Right)
  89. {
  90. Face = FaceDirection.Left;
  91. }
  92. //枪口跟随鼠标
  93. MountPoint.SetLookAt(mousePos);
  94.  
  95. if (Input.IsActionJustPressed("exchange")) //切换武器
  96. {
  97. ExchangeNext();
  98. RefreshGunTexture();
  99. }
  100. else if (Input.IsActionJustPressed("throw")) //扔掉武器
  101. {
  102. ThrowWeapon();
  103. RefreshGunTexture();
  104. }
  105. else if (Input.IsActionJustPressed("interactive")) //互动物体
  106. {
  107. var item = TriggerInteractive();
  108. if (item is Weapon)
  109. {
  110. RefreshGunTexture();
  111. }
  112. }
  113. else if (Input.IsActionJustPressed("reload")) //换弹
  114. {
  115. Reload();
  116. }
  117. if (Input.IsActionPressed("fire")) //开火
  118. {
  119. Attack();
  120. }
  121. //刷新显示的弹药剩余量
  122. RefreshGunAmmunition();
  123.  
  124. var reloadBar = GameApplication.Instance.Ui.ReloadBar;
  125. if (Holster.ActiveWeapon != null && Holster.ActiveWeapon.Reloading)
  126. {
  127. reloadBar.ShowBar(gPos, 1 - Holster.ActiveWeapon.ReloadProgress);
  128. }
  129. else
  130. {
  131. reloadBar.HideBar();
  132. }
  133. }
  134.  
  135. public override void _PhysicsProcess(float delta)
  136. {
  137. base._PhysicsProcess(delta);
  138. Move(delta);
  139. //播放动画
  140. PlayAnim();
  141. //GlobalPosition = GlobalPosition.Round();
  142. //AnimatedSprite.Playing = false;
  143. }
  144.  
  145. protected override void OnChangeHp(int hp)
  146. {
  147. GameApplication.Instance.Ui.SetHp(hp);
  148. }
  149.  
  150. protected override void OnChangeMaxHp(int maxHp)
  151. {
  152. GameApplication.Instance.Ui.SetMaxHp(maxHp);
  153. }
  154.  
  155. protected override void ChangeInteractiveItem(CheckInteractiveResult result)
  156. {
  157. if (result == null)
  158. {
  159. //隐藏互动提示
  160. GameApplication.Instance.Ui.InteractiveTipBar.HideBar();
  161. }
  162. else
  163. {
  164. if (InteractiveItem is Weapon gun)
  165. {
  166. //显示互动提示
  167. GameApplication.Instance.Ui.InteractiveTipBar.ShowBar(result.Target, result.ShowIcon);
  168. }
  169. }
  170. }
  171.  
  172. protected void OnChangeShield(int shield)
  173. {
  174. GameApplication.Instance.Ui.SetShield(shield);
  175. }
  176.  
  177. protected void OnChangeMaxShield(int maxShield)
  178. {
  179. GameApplication.Instance.Ui.SetMaxShield(maxShield);
  180. }
  181.  
  182. /// <summary>
  183. /// 刷新 ui 上手持的物体
  184. /// </summary>
  185. private void RefreshGunTexture()
  186. {
  187. var gun = Holster.ActiveWeapon;
  188. if (gun != null)
  189. {
  190. GameApplication.Instance.Ui.SetGunTexture(gun.GetDefaultTexture());
  191. }
  192. else
  193. {
  194. GameApplication.Instance.Ui.SetGunTexture(null);
  195. }
  196. }
  197.  
  198. /// <summary>
  199. /// 刷新 ui 上显示的弹药量
  200. /// </summary>
  201. private void RefreshGunAmmunition()
  202. {
  203. var gun = Holster.ActiveWeapon;
  204. if (gun != null)
  205. {
  206. GameApplication.Instance.Ui.SetAmmunition(gun.CurrAmmo, gun.ResidueAmmo);
  207. }
  208. }
  209.  
  210. private void Move(float delta)
  211. {
  212. //角色移动
  213. // 得到输入的 vector2 getvector方法返回值已经归一化过了noemalized
  214. Vector2 dir = Input.GetVector("move_left", "move_right", "move_up", "move_down");
  215. // 移动. 如果移动的数值接近0(是用 摇杆可能出现 方向 可能会出现浮点),就fricition的值 插值 到 0
  216. // 如果 有输入 就以当前速度,用acceleration 插值到 对应方向 * 最大速度
  217. if (Mathf.IsZeroApprox(dir.x)) Velocity.x = Mathf.MoveToward(Velocity.x, 0, Friction * delta);
  218. else Velocity.x = Mathf.MoveToward(Velocity.x, dir.x * MoveSpeed, Acceleration * delta);
  219.  
  220. if (Mathf.IsZeroApprox(dir.y)) Velocity.y = Mathf.MoveToward(Velocity.y, 0, Friction * delta);
  221. else Velocity.y = Mathf.MoveToward(Velocity.y, dir.y * MoveSpeed, Acceleration * delta);
  222. Velocity = MoveAndSlide(Velocity);
  223. }
  224.  
  225. // 播放动画
  226. private void PlayAnim()
  227. {
  228. if (Velocity != Vector2.Zero)
  229. {
  230. if ((Face == FaceDirection.Right && Velocity.x >= 0) || Face == FaceDirection.Left && Velocity.x <= 0) //向前走
  231. {
  232. AnimatedSprite.Animation = AnimatorNames.Run;
  233. }
  234. else if ((Face == FaceDirection.Right && Velocity.x < 0) || Face == FaceDirection.Left && Velocity.x > 0) //向后走
  235. {
  236. AnimatedSprite.Animation = AnimatorNames.ReverseRun;
  237. }
  238. }
  239. else
  240. {
  241. AnimatedSprite.Animation = AnimatorNames.Idle;
  242. }
  243. }
  244. }