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 { get; private set; }
  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. /// <summary>
  24. /// 设置当前操作的玩家对象
  25. /// </summary>
  26. public static void SetCurrentPlayer(Player player)
  27. {
  28. Current = player;
  29. }
  30. public override void OnInit()
  31. {
  32. base.OnInit();
  33. AttackLayer = PhysicsLayer.Wall | PhysicsLayer.Props | PhysicsLayer.Enemy;
  34. Camp = CampEnum.Camp1;
  35.  
  36. //让相机跟随玩家
  37. // var remoteTransform = new RemoteTransform2D();
  38. // AddChild(remoteTransform);
  39. // MainCamera.Main.GlobalPosition = GlobalPosition;
  40. // MainCamera.Main.ResetSmoothing();
  41. // remoteTransform.RemotePath = remoteTransform.GetPathTo(MainCamera.Main);
  42. RefreshWeaponTexture();
  43.  
  44. MaxHp = 50;
  45. Hp = 50;
  46. MaxShield = 30;
  47. Shield = 30;
  48.  
  49. // // debug用
  50. // Acceleration = 3000;
  51. // Friction = 3000;
  52. // MoveSpeed = 500;
  53. // CollisionLayer = 0;
  54. // CollisionMask = 0;
  55. // GameCamera.Main.Zoom = new Vector2(0.5f, 0.5f);
  56. }
  57.  
  58. protected override void Process(float delta)
  59. {
  60. base.Process(delta);
  61. //脸的朝向
  62. if (LookTarget == null)
  63. {
  64. var gPos = GlobalPosition;
  65. Vector2 mousePos = InputManager.GetViewportMousePosition();
  66. if (mousePos.X > gPos.X && Face == FaceDirection.Left)
  67. {
  68. Face = FaceDirection.Right;
  69. }
  70. else if (mousePos.X < gPos.X && Face == FaceDirection.Right)
  71. {
  72. Face = FaceDirection.Left;
  73. }
  74. //枪口跟随鼠标
  75. MountPoint.SetLookAt(mousePos);
  76. }
  77.  
  78. if (Input.IsActionJustPressed("exchange")) //切换武器
  79. {
  80. ExchangeNext();
  81. }
  82. else if (Input.IsActionJustPressed("throw")) //扔掉武器
  83. {
  84. ThrowWeapon();
  85. }
  86. else if (Input.IsActionJustPressed("interactive")) //互动物体
  87. {
  88. var item = TriggerInteractive();
  89. if (item != null)
  90. {
  91. RefreshWeaponTexture();
  92. }
  93. }
  94. else if (Input.IsActionJustPressed("reload")) //换弹
  95. {
  96. Reload();
  97. }
  98. if (Input.IsActionPressed("fire")) //开火
  99. {
  100. Attack();
  101. }
  102. }
  103.  
  104. protected override void PhysicsProcess(float delta)
  105. {
  106. base.PhysicsProcess(delta);
  107. HandleMoveInput(delta);
  108. //播放动画
  109. PlayAnim();
  110. }
  111.  
  112. public override void ExchangeNext()
  113. {
  114. base.ExchangeNext();
  115. RefreshWeaponTexture();
  116. }
  117.  
  118. public override void ExchangePrev()
  119. {
  120. base.ExchangePrev();
  121. RefreshWeaponTexture();
  122. }
  123.  
  124. public override void ThrowWeapon(int index)
  125. {
  126. base.ThrowWeapon(index);
  127. RefreshWeaponTexture();
  128. }
  129.  
  130. public override void ThrowWeapon()
  131. {
  132. base.ThrowWeapon();
  133. RefreshWeaponTexture();
  134. }
  135.  
  136. public override bool PickUpWeapon(Weapon weapon, bool exchange = true)
  137. {
  138. var v = base.PickUpWeapon(weapon, exchange);
  139. if (v)
  140. {
  141. RefreshWeaponTexture();
  142. }
  143. return v;
  144. }
  145.  
  146. protected override void OnChangeHp(int hp)
  147. {
  148. //GameApplication.Instance.Ui.SetHp(hp);
  149. EventManager.EmitEvent(EventEnum.OnPlayerHpChange, hp);
  150. }
  151.  
  152. protected override void OnChangeMaxHp(int maxHp)
  153. {
  154. //GameApplication.Instance.Ui.SetMaxHp(maxHp);
  155. EventManager.EmitEvent(EventEnum.OnPlayerMaxHpChange, maxHp);
  156. }
  157.  
  158. protected override void ChangeInteractiveItem(CheckInteractiveResult result)
  159. {
  160. //派发互动对象改变事件
  161. EventManager.EmitEvent(EventEnum.OnPlayerChangeInteractiveItem, result);
  162. }
  163.  
  164. protected override void OnChangeShield(int shield)
  165. {
  166. //GameApplication.Instance.Ui.SetShield(shield);
  167. EventManager.EmitEvent(EventEnum.OnPlayerShieldChange, shield);
  168. }
  169.  
  170. protected override void OnChangeMaxShield(int maxShield)
  171. {
  172. //GameApplication.Instance.Ui.SetMaxShield(maxShield);
  173. EventManager.EmitEvent(EventEnum.OnPlayerMaxShieldChange, maxShield);
  174. }
  175.  
  176. /// <summary>
  177. /// 刷新 ui 上手持的物体
  178. /// </summary>
  179. private void RefreshWeaponTexture()
  180. {
  181. EventManager.EmitEvent(EventEnum.OnPlayerRefreshWeaponTexture, Holster.ActiveWeapon?.GetDefaultTexture());
  182. }
  183.  
  184. //处理角色移动的输入
  185. private void HandleMoveInput(float delta)
  186. {
  187. //角色移动
  188. // 得到输入的 vector2 getvector方法返回值已经归一化过了noemalized
  189. Vector2 dir = Input.GetVector("move_left", "move_right", "move_up", "move_down");
  190. // 移动. 如果移动的数值接近0(是用 摇杆可能出现 方向 可能会出现浮点),就friction的值 插值 到 0
  191. // 如果 有输入 就以当前速度,用acceleration 插值到 对应方向 * 最大速度
  192. if (Mathf.IsZeroApprox(dir.X))
  193. {
  194. BasisVelocity = new Vector2(Mathf.MoveToward(BasisVelocity.X, 0, Friction * delta), BasisVelocity.Y);
  195. }
  196. else
  197. {
  198. BasisVelocity = new Vector2(Mathf.MoveToward(BasisVelocity.X, dir.X * MoveSpeed, Acceleration * delta),
  199. BasisVelocity.Y);
  200. }
  201.  
  202. if (Mathf.IsZeroApprox(dir.Y))
  203. {
  204. BasisVelocity = new Vector2(BasisVelocity.X, Mathf.MoveToward(BasisVelocity.Y, 0, Friction * delta));
  205. }
  206. else
  207. {
  208. BasisVelocity = new Vector2(BasisVelocity.X,
  209. Mathf.MoveToward(BasisVelocity.Y, dir.Y * MoveSpeed, Acceleration * delta));
  210. }
  211. }
  212.  
  213. // 播放动画
  214. private void PlayAnim()
  215. {
  216. if (BasisVelocity != Vector2.Zero)
  217. {
  218. if ((Face == FaceDirection.Right && BasisVelocity.X >= 0) || Face == FaceDirection.Left && BasisVelocity.X <= 0) //向前走
  219. {
  220. AnimatedSprite.Play(AnimatorNames.Run);
  221. }
  222. else if ((Face == FaceDirection.Right && BasisVelocity.X < 0) || Face == FaceDirection.Left && BasisVelocity.X > 0) //向后走
  223. {
  224. AnimatedSprite.Play(AnimatorNames.ReverseRun);
  225. }
  226. }
  227. else
  228. {
  229. AnimatedSprite.Play(AnimatorNames.Idle);
  230. }
  231. }
  232. }