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