Newer
Older
DungeonShooting / DungeonShooting_Godot / src / game / role / Player.cs
@lijincheng lijincheng on 5 Jun 2023 6 KB 初始化ActivityObject
  1. using Godot;
  2.  
  3.  
  4. /// <summary>
  5. /// 玩家角色基类, 所有角色都必须继承该类
  6. /// </summary>
  7. [Tool, GlobalClass]
  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. GameCamera.Main.SetFollowTarget(player);
  31. GameApplication.Instance.Cursor.SetMountRole(player);
  32. }
  33. public override void OnInit()
  34. {
  35. base.OnInit();
  36. AttackLayer = PhysicsLayer.Wall | PhysicsLayer.Props | PhysicsLayer.Enemy;
  37. Camp = CampEnum.Camp1;
  38.  
  39. //让相机跟随玩家
  40. // var remoteTransform = new RemoteTransform2D();
  41. // AddChild(remoteTransform);
  42. // MainCamera.Main.GlobalPosition = GlobalPosition;
  43. // MainCamera.Main.ResetSmoothing();
  44. // remoteTransform.RemotePath = remoteTransform.GetPathTo(MainCamera.Main);
  45.  
  46. MaxHp = 50;
  47. Hp = 50;
  48. MaxShield = 30;
  49. Shield = 30;
  50.  
  51. // debug用
  52. // Acceleration = 3000;
  53. // Friction = 3000;
  54. // MoveSpeed = 500;
  55. // CollisionLayer = 0;
  56. // CollisionMask = 0;
  57. // GameCamera.Main.Zoom = new Vector2(0.5f, 0.5f);
  58. }
  59.  
  60. protected override void Process(float delta)
  61. {
  62. if (IsDie)
  63. {
  64. return;
  65. }
  66. base.Process(delta);
  67. //脸的朝向
  68. if (LookTarget == null)
  69. {
  70. var gPos = GlobalPosition;
  71. Vector2 mousePos = InputManager.CursorPosition;
  72. if (mousePos.X > gPos.X && Face == FaceDirection.Left)
  73. {
  74. Face = FaceDirection.Right;
  75. }
  76. else if (mousePos.X < gPos.X && Face == FaceDirection.Right)
  77. {
  78. Face = FaceDirection.Left;
  79. }
  80. //枪口跟随鼠标
  81. MountPoint.SetLookAt(mousePos);
  82. }
  83.  
  84. if (InputManager.Exchange) //切换武器
  85. {
  86. ExchangeNext();
  87. }
  88. else if (InputManager.Throw) //扔掉武器
  89. {
  90. ThrowWeapon();
  91.  
  92. // //测试用的, 所有敌人也扔掉武器
  93. // if (Affiliation != null)
  94. // {
  95. // var enemies = Affiliation.FindIncludeItems(o =>
  96. // {
  97. // return o.CollisionWithMask(PhysicsLayer.Enemy);
  98. // });
  99. // foreach (var activityObject in enemies)
  100. // {
  101. // if (activityObject is Enemy enemy)
  102. // {
  103. // enemy.ThrowWeapon();
  104. // }
  105. // }
  106. // }
  107. }
  108. else if (InputManager.Interactive) //互动物体
  109. {
  110. TriggerInteractive();
  111. }
  112. else if (InputManager.Reload) //换弹
  113. {
  114. Reload();
  115. }
  116. if (InputManager.Fire) //开火
  117. {
  118. Attack();
  119. }
  120.  
  121. if (Input.IsKeyPressed(Key.P))
  122. {
  123. Hurt(1000, 0);
  124. }
  125. }
  126.  
  127. protected override void PhysicsProcess(float delta)
  128. {
  129. if (IsDie)
  130. {
  131. return;
  132. }
  133.  
  134. base.PhysicsProcess(delta);
  135. HandleMoveInput(delta);
  136. //播放动画
  137. PlayAnim();
  138. }
  139.  
  140. protected override void OnChangeHp(int hp)
  141. {
  142. //GameApplication.Instance.Ui.SetHp(hp);
  143. EventManager.EmitEvent(EventEnum.OnPlayerHpChange, hp);
  144. }
  145.  
  146. protected override void OnChangeMaxHp(int maxHp)
  147. {
  148. //GameApplication.Instance.Ui.SetMaxHp(maxHp);
  149. EventManager.EmitEvent(EventEnum.OnPlayerMaxHpChange, maxHp);
  150. }
  151.  
  152. protected override void ChangeInteractiveItem(CheckInteractiveResult result)
  153. {
  154. //派发互动对象改变事件
  155. EventManager.EmitEvent(EventEnum.OnPlayerChangeInteractiveItem, result);
  156. }
  157.  
  158. protected override void OnChangeShield(int shield)
  159. {
  160. //GameApplication.Instance.Ui.SetShield(shield);
  161. EventManager.EmitEvent(EventEnum.OnPlayerShieldChange, shield);
  162. }
  163.  
  164. protected override void OnChangeMaxShield(int maxShield)
  165. {
  166. //GameApplication.Instance.Ui.SetMaxShield(maxShield);
  167. EventManager.EmitEvent(EventEnum.OnPlayerMaxShieldChange, maxShield);
  168. }
  169.  
  170. protected override void OnDie()
  171. {
  172. GameCamera.Main.SetFollowTarget(null);
  173. UiManager.Open_Settlement();
  174. //GameApplication.Instance.World.ProcessMode = ProcessModeEnum.WhenPaused;
  175. }
  176.  
  177. //处理角色移动的输入
  178. private void HandleMoveInput(float delta)
  179. {
  180. //角色移动
  181. Vector2 dir = InputManager.MoveAxis;
  182. // 移动. 如果移动的数值接近0(是用 摇杆可能出现 方向 可能会出现浮点),就friction的值 插值 到 0
  183. // 如果 有输入 就以当前速度,用acceleration 插值到 对应方向 * 最大速度
  184. if (Mathf.IsZeroApprox(dir.X))
  185. {
  186. BasisVelocity = new Vector2(Mathf.MoveToward(BasisVelocity.X, 0, Friction * delta), BasisVelocity.Y);
  187. }
  188. else
  189. {
  190. BasisVelocity = new Vector2(Mathf.MoveToward(BasisVelocity.X, dir.X * MoveSpeed, Acceleration * delta),
  191. BasisVelocity.Y);
  192. }
  193.  
  194. if (Mathf.IsZeroApprox(dir.Y))
  195. {
  196. BasisVelocity = new Vector2(BasisVelocity.X, Mathf.MoveToward(BasisVelocity.Y, 0, Friction * delta));
  197. }
  198. else
  199. {
  200. BasisVelocity = new Vector2(BasisVelocity.X,
  201. Mathf.MoveToward(BasisVelocity.Y, dir.Y * MoveSpeed, Acceleration * delta));
  202. }
  203. }
  204.  
  205. // 播放动画
  206. private void PlayAnim()
  207. {
  208. if (BasisVelocity != Vector2.Zero)
  209. {
  210. if ((Face == FaceDirection.Right && BasisVelocity.X >= 0) || Face == FaceDirection.Left && BasisVelocity.X <= 0) //向前走
  211. {
  212. AnimatedSprite.Play(AnimatorNames.Run);
  213. }
  214. else if ((Face == FaceDirection.Right && BasisVelocity.X < 0) || Face == FaceDirection.Left && BasisVelocity.X > 0) //向后走
  215. {
  216. AnimatedSprite.Play(AnimatorNames.ReverseRun);
  217. }
  218. }
  219. else
  220. {
  221. AnimatedSprite.Play(AnimatorNames.Idle);
  222. }
  223. }
  224. }