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