Newer
Older
DungeonShooting / DungeonShooting_Godot / src / game / role / Player.cs
  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. // Acceleration = 3000;
  49. // Friction = 3000;
  50. // MoveSpeed = 800;
  51. }
  52.  
  53. protected override void Process(float delta)
  54. {
  55. base.Process(delta);
  56. //脸的朝向
  57. var gPos = GlobalPosition;
  58. if (LookTarget == null)
  59. {
  60. Vector2 mousePos = InputManager.GetViewportMousePosition();
  61. if (mousePos.X > gPos.X && Face == FaceDirection.Left)
  62. {
  63. Face = FaceDirection.Right;
  64. }
  65. else if (mousePos.X < gPos.X && Face == FaceDirection.Right)
  66. {
  67. Face = FaceDirection.Left;
  68. }
  69. //枪口跟随鼠标
  70. MountPoint.SetLookAt(mousePos);
  71. }
  72.  
  73. if (Input.IsActionJustPressed("exchange")) //切换武器
  74. {
  75. ExchangeNext();
  76. }
  77. else if (Input.IsActionJustPressed("throw")) //扔掉武器
  78. {
  79. ThrowWeapon();
  80. }
  81. else if (Input.IsActionJustPressed("interactive")) //互动物体
  82. {
  83. var item = TriggerInteractive();
  84. if (item != null)
  85. {
  86. RefreshGunTexture();
  87. }
  88. }
  89. else if (Input.IsActionJustPressed("reload")) //换弹
  90. {
  91. Reload();
  92. }
  93. if (Input.IsActionPressed("fire")) //开火
  94. {
  95. Attack();
  96. }
  97. //刷新显示的弹药剩余量
  98. RefreshGunAmmunition();
  99.  
  100. var reloadBar = GameApplication.Instance.Ui.ReloadBar;
  101. if (Holster.ActiveWeapon != null && Holster.ActiveWeapon.Reloading)
  102. {
  103. reloadBar.ShowBar(gPos, 1 - Holster.ActiveWeapon.ReloadProgress);
  104. }
  105. else
  106. {
  107. reloadBar.HideBar();
  108. }
  109. }
  110.  
  111. protected override void PhysicsProcess(float delta)
  112. {
  113. base.PhysicsProcess(delta);
  114. HandleMoveInput(delta);
  115. //播放动画
  116. PlayAnim();
  117. }
  118.  
  119. public override void ExchangeNext()
  120. {
  121. base.ExchangeNext();
  122. RefreshGunTexture();
  123. }
  124.  
  125. public override void ExchangePrev()
  126. {
  127. base.ExchangePrev();
  128. RefreshGunTexture();
  129. }
  130.  
  131. public override void ThrowWeapon(int index)
  132. {
  133. base.ThrowWeapon(index);
  134. RefreshGunTexture();
  135. }
  136.  
  137. public override void ThrowWeapon()
  138. {
  139. base.ThrowWeapon();
  140. RefreshGunTexture();
  141. }
  142.  
  143. public override bool PickUpWeapon(Weapon weapon, bool exchange = true)
  144. {
  145. var v = base.PickUpWeapon(weapon, exchange);
  146. if (v)
  147. {
  148. RefreshGunTexture();
  149. }
  150. return v;
  151. }
  152.  
  153. protected override void OnChangeHp(int hp)
  154. {
  155. GameApplication.Instance.Ui.SetHp(hp);
  156. }
  157.  
  158. protected override void OnChangeMaxHp(int maxHp)
  159. {
  160. GameApplication.Instance.Ui.SetMaxHp(maxHp);
  161. }
  162.  
  163. protected override void ChangeInteractiveItem(CheckInteractiveResult result)
  164. {
  165. if (result == null)
  166. {
  167. //隐藏互动提示
  168. GameApplication.Instance.Ui.InteractiveTipBar.HideBar();
  169. }
  170. else
  171. {
  172. if (InteractiveItem is Weapon gun)
  173. {
  174. //显示互动提示
  175. GameApplication.Instance.Ui.InteractiveTipBar.ShowBar(result.Target, result.ShowIcon);
  176. }
  177. }
  178. }
  179.  
  180. protected override void OnChangeShield(int shield)
  181. {
  182. GameApplication.Instance.Ui.SetShield(shield);
  183. }
  184.  
  185. protected override void OnChangeMaxShield(int maxShield)
  186. {
  187. GameApplication.Instance.Ui.SetMaxShield(maxShield);
  188. }
  189.  
  190. /// <summary>
  191. /// 刷新 ui 上手持的物体
  192. /// </summary>
  193. private void RefreshGunTexture()
  194. {
  195. var gun = Holster.ActiveWeapon;
  196. if (gun != null)
  197. {
  198. GameApplication.Instance.Ui.SetGunTexture(gun.GetDefaultTexture());
  199. }
  200. else
  201. {
  202. GameApplication.Instance.Ui.SetGunTexture(null);
  203. }
  204. }
  205.  
  206. /// <summary>
  207. /// 刷新 ui 上显示的弹药量
  208. /// </summary>
  209. private void RefreshGunAmmunition()
  210. {
  211. var gun = Holster.ActiveWeapon;
  212. if (gun != null)
  213. {
  214. GameApplication.Instance.Ui.SetAmmunition(gun.CurrAmmo, gun.ResidueAmmo);
  215. }
  216. }
  217.  
  218. //处理角色移动的输入
  219. private void HandleMoveInput(float delta)
  220. {
  221. //角色移动
  222. // 得到输入的 vector2 getvector方法返回值已经归一化过了noemalized
  223. Vector2 dir = Input.GetVector("move_left", "move_right", "move_up", "move_down");
  224. // 移动. 如果移动的数值接近0(是用 摇杆可能出现 方向 可能会出现浮点),就friction的值 插值 到 0
  225. // 如果 有输入 就以当前速度,用acceleration 插值到 对应方向 * 最大速度
  226. if (Mathf.IsZeroApprox(dir.X))
  227. {
  228. BasisVelocity = new Vector2(Mathf.MoveToward(BasisVelocity.X, 0, Friction * delta), BasisVelocity.Y);
  229. }
  230. else
  231. {
  232. BasisVelocity = new Vector2(Mathf.MoveToward(BasisVelocity.X, dir.X * MoveSpeed, Acceleration * delta),
  233. BasisVelocity.Y);
  234. }
  235.  
  236. if (Mathf.IsZeroApprox(dir.Y))
  237. {
  238. BasisVelocity = new Vector2(BasisVelocity.X, Mathf.MoveToward(BasisVelocity.Y, 0, Friction * delta));
  239. }
  240. else
  241. {
  242. BasisVelocity = new Vector2(BasisVelocity.X,
  243. Mathf.MoveToward(BasisVelocity.Y, dir.Y * MoveSpeed, Acceleration * delta));
  244. }
  245. }
  246.  
  247. // 播放动画
  248. private void PlayAnim()
  249. {
  250. if (BasisVelocity != Vector2.Zero)
  251. {
  252. if ((Face == FaceDirection.Right && BasisVelocity.X >= 0) || Face == FaceDirection.Left && BasisVelocity.X <= 0) //向前走
  253. {
  254. AnimatedSprite.Play(AnimatorNames.Run);
  255. }
  256. else if ((Face == FaceDirection.Right && BasisVelocity.X < 0) || Face == FaceDirection.Left && BasisVelocity.X > 0) //向后走
  257. {
  258. AnimatedSprite.Play(AnimatorNames.ReverseRun);
  259. }
  260. }
  261. else
  262. {
  263. AnimatedSprite.Play(AnimatorNames.Idle);
  264. }
  265. }
  266. }