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