Newer
Older
DungeonShooting / DungeonShooting_Godot / src / game / activity / role / Player.cs
@小李xl 小李xl on 9 Jul 2023 8 KB 修复拾起道具的bug
  1. using Godot;
  2.  
  3.  
  4. /// <summary>
  5. /// 玩家角色基类, 所有角色都必须继承该类
  6. /// </summary>
  7. [Tool]
  8. public partial class Player : Role
  9. {
  10. /// <summary>
  11. /// 获取当前操作的角色
  12. /// </summary>
  13. public static Player Current { get; private set; }
  14.  
  15. /// <summary>
  16. /// 设置当前操作的玩家对象
  17. /// </summary>
  18. public static void SetCurrentPlayer(Player player)
  19. {
  20. Current = player;
  21. //设置相机和鼠标跟随玩家
  22. GameCamera.Main.SetFollowTarget(player);
  23. GameApplication.Instance.Cursor.SetMountRole(player);
  24. }
  25. public override void OnInit()
  26. {
  27. base.OnInit();
  28. AttackLayer = PhysicsLayer.Wall | PhysicsLayer.Prop | PhysicsLayer.Enemy;
  29. Camp = CampEnum.Camp1;
  30.  
  31. //让相机跟随玩家
  32. // var remoteTransform = new RemoteTransform2D();
  33. // AddChild(remoteTransform);
  34. // MainCamera.Main.GlobalPosition = GlobalPosition;
  35. // MainCamera.Main.ResetSmoothing();
  36. // remoteTransform.RemotePath = remoteTransform.GetPathTo(MainCamera.Main);
  37.  
  38. MaxHp = 6;
  39. Hp = 6;
  40. MaxShield = 0;
  41. Shield = 0;
  42.  
  43. // debug用
  44. // Acceleration = 3000;
  45. // Friction = 3000;
  46. // MoveSpeed = 500;
  47. // CollisionLayer = 0;
  48. // CollisionMask = 0;
  49. // GameCamera.Main.Zoom = new Vector2(0.5f, 0.5f);
  50. }
  51.  
  52. protected override void Process(float delta)
  53. {
  54. if (IsDie)
  55. {
  56. return;
  57. }
  58. base.Process(delta);
  59. //脸的朝向
  60. if (LookTarget == null)
  61. {
  62. var gPos = GlobalPosition;
  63. Vector2 mousePos = InputManager.CursorPosition;
  64. if (mousePos.X > gPos.X && Face == FaceDirection.Left)
  65. {
  66. Face = FaceDirection.Right;
  67. }
  68. else if (mousePos.X < gPos.X && Face == FaceDirection.Right)
  69. {
  70. Face = FaceDirection.Left;
  71. }
  72. //枪口跟随鼠标
  73. MountPoint.SetLookAt(mousePos);
  74. }
  75.  
  76. if (InputManager.ExchangeWeapon) //切换武器
  77. {
  78. ExchangeNextWeapon();
  79. }
  80. else if (InputManager.ThrowWeapon) //扔掉武器
  81. {
  82. ThrowWeapon();
  83.  
  84. // //测试用的, 所有敌人也扔掉武器
  85. // if (Affiliation != null)
  86. // {
  87. // var enemies = Affiliation.FindIncludeItems(o =>
  88. // {
  89. // return o.CollisionWithMask(PhysicsLayer.Enemy);
  90. // });
  91. // foreach (var activityObject in enemies)
  92. // {
  93. // if (activityObject is Enemy enemy)
  94. // {
  95. // enemy.ThrowWeapon();
  96. // }
  97. // }
  98. // }
  99. }
  100. else if (InputManager.Interactive) //互动物体
  101. {
  102. TriggerInteractive();
  103. }
  104. else if (InputManager.Reload) //换弹
  105. {
  106. Reload();
  107. }
  108.  
  109. if (InputManager.Fire) //开火
  110. {
  111. Attack();
  112. }
  113.  
  114. if (InputManager.UseActiveProp) //使用道具
  115. {
  116. UseActiveProp();
  117. }
  118. else if (InputManager.RemoveProp) //扔掉道具
  119. {
  120. ThrowActiveProp();
  121. }
  122.  
  123. if (Input.IsKeyPressed(Key.P))
  124. {
  125. Hurt(1000, 0);
  126. }
  127. }
  128.  
  129. protected override void PhysicsProcess(float delta)
  130. {
  131. if (IsDie)
  132. {
  133. return;
  134. }
  135.  
  136. base.PhysicsProcess(delta);
  137. HandleMoveInput(delta);
  138. //播放动画
  139. PlayAnim();
  140. }
  141.  
  142. protected override void OnPickUpWeapon(Weapon weapon)
  143. {
  144. EventManager.EmitEvent(EventEnum.OnPlayerPickUpWeapon, weapon);
  145. }
  146.  
  147. protected override void OnThrowWeapon(Weapon weapon)
  148. {
  149. EventManager.EmitEvent(EventEnum.OnPlayerRemoveWeapon, weapon);
  150. }
  151.  
  152. protected override int OnHandlerHurt(int damage)
  153. {
  154. //修改受到的伤害, 每次只受到1点伤害
  155. return 1;
  156. }
  157.  
  158. protected override void OnHit(int damage, bool realHarm)
  159. {
  160. //进入无敌状态
  161. if (realHarm) //真实伤害
  162. {
  163. PlayInvincibleFlashing(RoleState.WoundedInvincibleTime);
  164. }
  165. else //护盾抵消掉的
  166. {
  167. PlayInvincibleFlashing(RoleState.ShieldInvincibleTime);
  168. }
  169. }
  170.  
  171. protected override void OnChangeHp(int hp)
  172. {
  173. //GameApplication.Instance.Ui.SetHp(hp);
  174. EventManager.EmitEvent(EventEnum.OnPlayerHpChange, hp);
  175. }
  176.  
  177. protected override void OnChangeMaxHp(int maxHp)
  178. {
  179. //GameApplication.Instance.Ui.SetMaxHp(maxHp);
  180. EventManager.EmitEvent(EventEnum.OnPlayerMaxHpChange, maxHp);
  181. }
  182.  
  183. protected override void ChangeInteractiveItem(CheckInteractiveResult prev, CheckInteractiveResult result)
  184. {
  185. if (prev != null && prev.Target.ShowOutline)
  186. {
  187. prev.Target.OutlineColor = Colors.Black;
  188. }
  189. if (result != null && result.Target.ShowOutline)
  190. {
  191. result.Target.OutlineColor = Colors.White;
  192. }
  193. //派发互动对象改变事件
  194. EventManager.EmitEvent(EventEnum.OnPlayerChangeInteractiveItem, result);
  195. }
  196.  
  197. protected override void OnChangeShield(int shield)
  198. {
  199. //GameApplication.Instance.Ui.SetShield(shield);
  200. EventManager.EmitEvent(EventEnum.OnPlayerShieldChange, shield);
  201. }
  202.  
  203. protected override void OnChangeMaxShield(int maxShield)
  204. {
  205. //GameApplication.Instance.Ui.SetMaxShield(maxShield);
  206. EventManager.EmitEvent(EventEnum.OnPlayerMaxShieldChange, maxShield);
  207. }
  208.  
  209. protected override void OnDie()
  210. {
  211. GameCamera.Main.SetFollowTarget(null);
  212. BasisVelocity = Vector2.Zero;
  213. MoveController.ClearForce();
  214. UiManager.Open_Settlement();
  215. }
  216.  
  217. protected override void OnPickUpActiveProp(ActiveProp activeProp)
  218. {
  219. EventManager.EmitEvent(EventEnum.OnPlayerPickUpProp, activeProp);
  220. }
  221.  
  222. protected override void OnRemoveActiveProp(ActiveProp activeProp)
  223. {
  224. EventManager.EmitEvent(EventEnum.OnPlayerRemoveProp, activeProp);
  225. }
  226.  
  227. protected override void OnPickUpBuffProp(BuffProp buffProp)
  228. {
  229. EventManager.EmitEvent(EventEnum.OnPlayerPickUpProp, buffProp);
  230. }
  231.  
  232. protected override void OnRemoveBuffProp(BuffProp buffProp)
  233. {
  234. EventManager.EmitEvent(EventEnum.OnPlayerRemoveProp, buffProp);
  235. }
  236.  
  237. //处理角色移动的输入
  238. private void HandleMoveInput(float delta)
  239. {
  240. //角色移动
  241. Vector2 dir = InputManager.MoveAxis;
  242. // 移动. 如果移动的数值接近0(是用 摇杆可能出现 方向 可能会出现浮点),就friction的值 插值 到 0
  243. // 如果 有输入 就以当前速度,用acceleration 插值到 对应方向 * 最大速度
  244. if (Mathf.IsZeroApprox(dir.X))
  245. {
  246. BasisVelocity = new Vector2(Mathf.MoveToward(BasisVelocity.X, 0, RoleState.Friction * delta), BasisVelocity.Y);
  247. }
  248. else
  249. {
  250. BasisVelocity = new Vector2(Mathf.MoveToward(BasisVelocity.X, dir.X * RoleState.MoveSpeed, RoleState.Acceleration * delta),
  251. BasisVelocity.Y);
  252. }
  253.  
  254. if (Mathf.IsZeroApprox(dir.Y))
  255. {
  256. BasisVelocity = new Vector2(BasisVelocity.X, Mathf.MoveToward(BasisVelocity.Y, 0, RoleState.Friction * delta));
  257. }
  258. else
  259. {
  260. BasisVelocity = new Vector2(BasisVelocity.X,
  261. Mathf.MoveToward(BasisVelocity.Y, dir.Y * RoleState.MoveSpeed, RoleState.Acceleration * delta));
  262. }
  263. }
  264.  
  265. // 播放动画
  266. private void PlayAnim()
  267. {
  268. if (BasisVelocity != Vector2.Zero)
  269. {
  270. if ((Face == FaceDirection.Right && BasisVelocity.X >= 0) || Face == FaceDirection.Left && BasisVelocity.X <= 0) //向前走
  271. {
  272. AnimatedSprite.Play(AnimatorNames.Run);
  273. }
  274. else if ((Face == FaceDirection.Right && BasisVelocity.X < 0) || Face == FaceDirection.Left && BasisVelocity.X > 0) //向后走
  275. {
  276. AnimatedSprite.Play(AnimatorNames.ReverseRun);
  277. }
  278. }
  279. else
  280. {
  281. AnimatedSprite.Play(AnimatorNames.Idle);
  282. }
  283. }
  284. }