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 OnInit()
  23. {
  24. base.OnInit();
  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. RefreshWeaponTexture();
  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. if (LookTarget == null)
  56. {
  57. var gPos = GlobalPosition;
  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. RefreshWeaponTexture();
  85. }
  86. }
  87. else if (Input.IsActionJustPressed("reload")) //换弹
  88. {
  89. Reload();
  90. }
  91. if (Input.IsActionPressed("fire")) //开火
  92. {
  93. Attack();
  94. }
  95. }
  96.  
  97. protected override void PhysicsProcess(float delta)
  98. {
  99. base.PhysicsProcess(delta);
  100. HandleMoveInput(delta);
  101. //播放动画
  102. PlayAnim();
  103. }
  104.  
  105. public override void ExchangeNext()
  106. {
  107. base.ExchangeNext();
  108. RefreshWeaponTexture();
  109. }
  110.  
  111. public override void ExchangePrev()
  112. {
  113. base.ExchangePrev();
  114. RefreshWeaponTexture();
  115. }
  116.  
  117. public override void ThrowWeapon(int index)
  118. {
  119. base.ThrowWeapon(index);
  120. RefreshWeaponTexture();
  121. }
  122.  
  123. public override void ThrowWeapon()
  124. {
  125. base.ThrowWeapon();
  126. RefreshWeaponTexture();
  127. }
  128.  
  129. public override bool PickUpWeapon(Weapon weapon, bool exchange = true)
  130. {
  131. var v = base.PickUpWeapon(weapon, exchange);
  132. if (v)
  133. {
  134. RefreshWeaponTexture();
  135. }
  136. return v;
  137. }
  138.  
  139. protected override void OnChangeHp(int hp)
  140. {
  141. //GameApplication.Instance.Ui.SetHp(hp);
  142. EventManager.EmitEvent(EventEnum.OnPlayerHpChange, hp);
  143. }
  144.  
  145. protected override void OnChangeMaxHp(int maxHp)
  146. {
  147. //GameApplication.Instance.Ui.SetMaxHp(maxHp);
  148. EventManager.EmitEvent(EventEnum.OnPlayerMaxHpChange, maxHp);
  149. }
  150.  
  151. protected override void ChangeInteractiveItem(CheckInteractiveResult result)
  152. {
  153. //派发互动对象改变事件
  154. EventManager.EmitEvent(EventEnum.OnPlayerChangeInteractiveItem, result);
  155. }
  156.  
  157. protected override void OnChangeShield(int shield)
  158. {
  159. //GameApplication.Instance.Ui.SetShield(shield);
  160. EventManager.EmitEvent(EventEnum.OnPlayerShieldChange, shield);
  161. }
  162.  
  163. protected override void OnChangeMaxShield(int maxShield)
  164. {
  165. //GameApplication.Instance.Ui.SetMaxShield(maxShield);
  166. EventManager.EmitEvent(EventEnum.OnPlayerMaxShieldChange, maxShield);
  167. }
  168.  
  169. /// <summary>
  170. /// 刷新 ui 上手持的物体
  171. /// </summary>
  172. private void RefreshWeaponTexture()
  173. {
  174. EventManager.EmitEvent(EventEnum.OnPlayerRefreshWeaponTexture, Holster.ActiveWeapon?.GetDefaultTexture());
  175. }
  176.  
  177. //处理角色移动的输入
  178. private void HandleMoveInput(float delta)
  179. {
  180. //角色移动
  181. // 得到输入的 vector2 getvector方法返回值已经归一化过了noemalized
  182. Vector2 dir = Input.GetVector("move_left", "move_right", "move_up", "move_down");
  183. // 移动. 如果移动的数值接近0(是用 摇杆可能出现 方向 可能会出现浮点),就friction的值 插值 到 0
  184. // 如果 有输入 就以当前速度,用acceleration 插值到 对应方向 * 最大速度
  185. if (Mathf.IsZeroApprox(dir.X))
  186. {
  187. BasisVelocity = new Vector2(Mathf.MoveToward(BasisVelocity.X, 0, Friction * delta), BasisVelocity.Y);
  188. }
  189. else
  190. {
  191. BasisVelocity = new Vector2(Mathf.MoveToward(BasisVelocity.X, dir.X * MoveSpeed, Acceleration * delta),
  192. BasisVelocity.Y);
  193. }
  194.  
  195. if (Mathf.IsZeroApprox(dir.Y))
  196. {
  197. BasisVelocity = new Vector2(BasisVelocity.X, Mathf.MoveToward(BasisVelocity.Y, 0, Friction * delta));
  198. }
  199. else
  200. {
  201. BasisVelocity = new Vector2(BasisVelocity.X,
  202. Mathf.MoveToward(BasisVelocity.Y, dir.Y * MoveSpeed, Acceleration * delta));
  203. }
  204. }
  205.  
  206. // 播放动画
  207. private void PlayAnim()
  208. {
  209. if (BasisVelocity != Vector2.Zero)
  210. {
  211. if ((Face == FaceDirection.Right && BasisVelocity.X >= 0) || Face == FaceDirection.Left && BasisVelocity.X <= 0) //向前走
  212. {
  213. AnimatedSprite.Play(AnimatorNames.Run);
  214. }
  215. else if ((Face == FaceDirection.Right && BasisVelocity.X < 0) || Face == FaceDirection.Left && BasisVelocity.X > 0) //向后走
  216. {
  217. AnimatedSprite.Play(AnimatorNames.ReverseRun);
  218. }
  219. }
  220. else
  221. {
  222. AnimatedSprite.Play(AnimatorNames.Idle);
  223. }
  224. }
  225. }