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