Newer
Older
DungeonShooting / DungeonShooting_Godot / src / game / activity / role / player / Player.cs
@小李xl 小李xl on 7 Nov 2023 10 KB 修复近战无法消除弹幕的bug
  1. using System;
  2. using Godot;
  3.  
  4.  
  5. /// <summary>
  6. /// 玩家角色基类, 所有角色都必须继承该类
  7. /// </summary>
  8. [Tool]
  9. public partial class Player : Role
  10. {
  11. /// <summary>
  12. /// 获取当前操作的角色
  13. /// </summary>
  14. public static Player Current { get; private set; }
  15. /// <summary>
  16. /// 玩家身上的状态机控制器
  17. /// </summary>
  18. public StateController<Player, PlayerStateEnum> StateController { get; private set; }
  19.  
  20. /// <summary>
  21. /// 设置当前操作的玩家对象
  22. /// </summary>
  23. public static void SetCurrentPlayer(Player player)
  24. {
  25. Current = player;
  26. //设置相机和鼠标跟随玩家
  27. GameCamera.Main.SetFollowTarget(player);
  28. GameApplication.Instance.Cursor.SetMountRole(player);
  29. }
  30. public override void OnInit()
  31. {
  32. base.OnInit();
  33.  
  34. IsAi = false;
  35. StateController = AddComponent<StateController<Player, PlayerStateEnum>>();
  36. AttackLayer = PhysicsLayer.Wall | PhysicsLayer.Enemy;
  37. EnemyLayer = EnemyLayer = PhysicsLayer.Enemy;
  38. Camp = CampEnum.Camp1;
  39.  
  40. MaxHp = 6;
  41. Hp = 6;
  42. MaxShield = 0;
  43. Shield = 0;
  44.  
  45. // debug用
  46. // RoleState.Acceleration = 3000;
  47. // RoleState.Friction = 3000;
  48. // RoleState.MoveSpeed = 500;
  49. // CollisionLayer = 0;
  50. // CollisionMask = 0;
  51. //GameCamera.Main.Zoom = new Vector2(0.2f, 0.2f);
  52. //GameCamera.Main.Zoom = new Vector2(0.5f, 0.5f);
  53. // this.CallDelay(0.5f, () =>
  54. // {
  55. // var weapon = Create<Weapon>(Ids.Id_weapon0009);
  56. // PickUpWeapon(weapon);
  57. // });
  58. //注册状态机
  59. StateController.Register(new PlayerIdleState());
  60. StateController.Register(new PlayerMoveState());
  61. StateController.Register(new PlayerRollState());
  62. //默认状态
  63. StateController.ChangeStateInstant(PlayerStateEnum.Idle);
  64.  
  65. //InitSubLine();
  66. }
  67.  
  68. protected override void Process(float delta)
  69. {
  70. base.Process(delta);
  71. if (IsDie)
  72. {
  73. return;
  74. }
  75. //脸的朝向
  76. if (LookTarget == null)
  77. {
  78. var gPos = GlobalPosition;
  79. Vector2 mousePos = InputManager.CursorPosition;
  80. if (mousePos.X > gPos.X && Face == FaceDirection.Left)
  81. {
  82. Face = FaceDirection.Right;
  83. }
  84. else if (mousePos.X < gPos.X && Face == FaceDirection.Right)
  85. {
  86. Face = FaceDirection.Left;
  87. }
  88.  
  89. if (MountLookTarget)
  90. {
  91. //枪口跟随鼠标
  92. MountPoint.SetLookAt(mousePos);
  93. }
  94. }
  95.  
  96. if (InputManager.ExchangeWeapon) //切换武器
  97. {
  98. ExchangeNextWeapon();
  99. }
  100. else if (InputManager.ThrowWeapon) //扔掉武器
  101. {
  102. ThrowWeapon();
  103.  
  104. // //测试用的, 所有敌人也扔掉武器
  105. // if (Affiliation != null)
  106. // {
  107. // var enemies = Affiliation.FindIncludeItems(o =>
  108. // {
  109. // return o.CollisionWithMask(PhysicsLayer.Enemy);
  110. // });
  111. // foreach (var activityObject in enemies)
  112. // {
  113. // if (activityObject is Enemy enemy)
  114. // {
  115. // enemy.ThrowWeapon();
  116. // }
  117. // }
  118. // }
  119. }
  120. else if (InputManager.Interactive) //互动物体
  121. {
  122. TriggerInteractive();
  123. }
  124. else if (InputManager.Reload) //换弹
  125. {
  126. Reload();
  127. }
  128.  
  129. var meleeAttackFlag = false;
  130. if (InputManager.MeleeAttack) //近战攻击
  131. {
  132. if (StateController.CurrState != PlayerStateEnum.Roll) //不能是翻滚状态
  133. {
  134. if (WeaponPack.ActiveItem != null && WeaponPack.ActiveItem.Attribute.CanMeleeAttack)
  135. {
  136. meleeAttackFlag = true;
  137. MeleeAttack();
  138. }
  139. }
  140. }
  141. if (!meleeAttackFlag && InputManager.Fire) //正常开火
  142. {
  143. if (StateController.CurrState != PlayerStateEnum.Roll) //不能是翻滚状态
  144. {
  145. Attack();
  146. // //测试用,触发房间内地上的武器开火
  147. // var weaponArray = AffiliationArea.FindEnterItems(o => o is Weapon);
  148. // foreach (Weapon activityObject in weaponArray)
  149. // {
  150. // activityObject.Trigger(this);
  151. // }
  152. }
  153. }
  154.  
  155. if (InputManager.UseActiveProp) //使用道具
  156. {
  157. UseActiveProp();
  158. }
  159. else if (InputManager.RemoveProp) //扔掉道具
  160. {
  161. ThrowActiveProp();
  162. }
  163.  
  164. if (Input.IsKeyPressed(Key.P)) //测试用, 自杀
  165. {
  166. //Hurt(1000, 0);
  167. Hp = 0;
  168. Hurt(1000, 0);
  169. }
  170. else if (Input.IsKeyPressed(Key.O)) //测试用, 消灭房间内所有敌人
  171. {
  172. var enemyList = AffiliationArea.FindIncludeItems(o => o.CollisionWithMask(PhysicsLayer.Enemy));
  173. foreach (var enemy in enemyList)
  174. {
  175. ((Enemy)enemy).Hurt(1000, 0);
  176. }
  177. }
  178. // //测试用
  179. // if (InputManager.Roll) //鼠标处触发互动物体
  180. // {
  181. // var now = DateTime.Now;
  182. // var mousePosition = GetGlobalMousePosition();
  183. // var freezeSprites = AffiliationArea.RoomInfo.StaticSprite.CollisionCircle(mousePosition, 25, true);
  184. // Debug.Log("检测数量: " + freezeSprites.Count + ", 用时: " + (DateTime.Now - now).TotalMilliseconds);
  185. // foreach (var freezeSprite in freezeSprites)
  186. // {
  187. // var temp = freezeSprite.Position - mousePosition;
  188. // freezeSprite.ActivityObject.MoveController.AddForce(temp.Normalized() * 300 * (25f - temp.Length()) / 25f);
  189. // }
  190. // }
  191. }
  192.  
  193. protected override void OnPickUpWeapon(Weapon weapon)
  194. {
  195. EventManager.EmitEvent(EventEnum.OnPlayerPickUpWeapon, weapon);
  196. }
  197.  
  198. protected override void OnThrowWeapon(Weapon weapon)
  199. {
  200. EventManager.EmitEvent(EventEnum.OnPlayerRemoveWeapon, weapon);
  201. }
  202.  
  203. protected override int OnHandlerHurt(int damage)
  204. {
  205. //修改受到的伤害, 每次只受到1点伤害
  206. return 1;
  207. }
  208.  
  209. protected override void OnHit(int damage, bool realHarm)
  210. {
  211. //进入无敌状态
  212. if (realHarm) //真实伤害
  213. {
  214. PlayInvincibleFlashing(RoleState.WoundedInvincibleTime);
  215. }
  216. else //护盾抵消掉的
  217. {
  218. PlayInvincibleFlashing(RoleState.ShieldInvincibleTime);
  219. }
  220. }
  221.  
  222. protected override void OnChangeHp(int hp)
  223. {
  224. //GameApplication.Instance.Ui.SetHp(hp);
  225. EventManager.EmitEvent(EventEnum.OnPlayerHpChange, hp);
  226. }
  227.  
  228. protected override void OnChangeMaxHp(int maxHp)
  229. {
  230. //GameApplication.Instance.Ui.SetMaxHp(maxHp);
  231. EventManager.EmitEvent(EventEnum.OnPlayerMaxHpChange, maxHp);
  232. }
  233.  
  234. protected override void ChangeInteractiveItem(CheckInteractiveResult prev, CheckInteractiveResult result)
  235. {
  236. if (prev != null && prev.Target.ShowOutline)
  237. {
  238. prev.Target.OutlineColor = Colors.Black;
  239. }
  240. if (result != null && result.Target.ShowOutline)
  241. {
  242. result.Target.OutlineColor = Colors.White;
  243. }
  244. //派发互动对象改变事件
  245. EventManager.EmitEvent(EventEnum.OnPlayerChangeInteractiveItem, result);
  246. }
  247.  
  248. protected override void OnChangeShield(int shield)
  249. {
  250. //GameApplication.Instance.Ui.SetShield(shield);
  251. EventManager.EmitEvent(EventEnum.OnPlayerShieldChange, shield);
  252. }
  253.  
  254. protected override void OnChangeMaxShield(int maxShield)
  255. {
  256. //GameApplication.Instance.Ui.SetMaxShield(maxShield);
  257. EventManager.EmitEvent(EventEnum.OnPlayerMaxShieldChange, maxShield);
  258. }
  259.  
  260. protected override void OnDie()
  261. {
  262. StateController.Enable = false;
  263. GameCamera.Main.SetFollowTarget(null);
  264. BasisVelocity = Vector2.Zero;
  265. MoveController.ClearForce();
  266.  
  267. //暂停游戏
  268. GameApplication.Instance.World.Pause = true;
  269. //弹出结算面板
  270. GameApplication.Instance.Cursor.SetGuiMode(true);
  271. UiManager.Open_Settlement();
  272. }
  273.  
  274. protected override void OnPickUpActiveProp(ActiveProp activeProp)
  275. {
  276. EventManager.EmitEvent(EventEnum.OnPlayerPickUpProp, activeProp);
  277. }
  278.  
  279. protected override void OnRemoveActiveProp(ActiveProp activeProp)
  280. {
  281. EventManager.EmitEvent(EventEnum.OnPlayerRemoveProp, activeProp);
  282. }
  283.  
  284. protected override void OnPickUpBuffProp(BuffProp buffProp)
  285. {
  286. EventManager.EmitEvent(EventEnum.OnPlayerPickUpProp, buffProp);
  287. }
  288.  
  289. protected override void OnRemoveBuffProp(BuffProp buffProp)
  290. {
  291. EventManager.EmitEvent(EventEnum.OnPlayerRemoveProp, buffProp);
  292. }
  293.  
  294. /// <summary>
  295. /// 处理角色移动的输入
  296. /// </summary>
  297. public void HandleMoveInput(float delta)
  298. {
  299. var dir = InputManager.MoveAxis;
  300. // 移动. 如果移动的数值接近0(是用 摇杆可能出现 方向 可能会出现浮点),就friction的值 插值 到 0
  301. // 如果 有输入 就以当前速度,用acceleration 插值到 对应方向 * 最大速度
  302. if (Mathf.IsZeroApprox(dir.X))
  303. {
  304. BasisVelocity = new Vector2(Mathf.MoveToward(BasisVelocity.X, 0, RoleState.Friction * delta), BasisVelocity.Y);
  305. }
  306. else
  307. {
  308. BasisVelocity = new Vector2(Mathf.MoveToward(BasisVelocity.X, dir.X * RoleState.MoveSpeed, RoleState.Acceleration * delta), BasisVelocity.Y);
  309. }
  310.  
  311. if (Mathf.IsZeroApprox(dir.Y))
  312. {
  313. BasisVelocity = new Vector2(BasisVelocity.X, Mathf.MoveToward(BasisVelocity.Y, 0, RoleState.Friction * delta));
  314. }
  315. else
  316. {
  317. BasisVelocity = new Vector2(BasisVelocity.X, Mathf.MoveToward(BasisVelocity.Y, dir.Y * RoleState.MoveSpeed, RoleState.Acceleration * delta));
  318. }
  319. }
  320.  
  321. // protected override void DebugDraw()
  322. // {
  323. // base.DebugDraw();
  324. // DrawArc(GetLocalMousePosition(), 25, 0, Mathf.Pi * 2f, 20, Colors.Red, 1);
  325. // }
  326. }