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