Newer
Older
DungeonShooting / DungeonShooting_Godot / src / game / activity / role / player / Player.cs
@小李xl 小李xl on 4 Apr 2024 12 KB 解决换阵营子弹碰撞问题
  1. using System;
  2. using Config;
  3. using Godot;
  4.  
  5.  
  6. /// <summary>
  7. /// 玩家角色基类, 所有角色都必须继承该类
  8. /// </summary>
  9. [Tool]
  10. public partial class Player : Role
  11. {
  12. /// <summary>
  13. /// 当玩家第一次进入房间时调用
  14. /// </summary>
  15. public event Action<RoomInfo> OnFirstEnterRoomEvent;
  16. /// <summary>
  17. /// 玩家身上的状态机控制器
  18. /// </summary>
  19. public StateController<Player, PlayerStateEnum> StateController { get; private set; }
  20.  
  21. public PlayerRoleState PlayerRoleState { get; private set; }
  22. /// <summary>
  23. /// 是否可以翻滚
  24. /// </summary>
  25. public bool CanRoll => _rollCoolingTimer <= 0;
  26. //翻滚冷却计时器
  27. private float _rollCoolingTimer = 0;
  28. private BrushImageData _brushData2;
  29. public override void OnInit()
  30. {
  31. base.OnInit();
  32.  
  33. IsAi = false;
  34. StateController = AddComponent<StateController<Player, PlayerStateEnum>>();
  35. Camp = CampEnum.Camp1;
  36.  
  37. MaxHp = 6;
  38. Hp = 6;
  39. MaxShield = 0;
  40. Shield = 0;
  41.  
  42. WeaponPack.SetCapacity(4);
  43. ActivePropsPack.SetCapacity(1);
  44. // debug用
  45. //DebugSet();
  46. //注册状态机
  47. StateController.Register(new PlayerIdleState());
  48. StateController.Register(new PlayerMoveState());
  49. StateController.Register(new PlayerRollState());
  50. //默认状态
  51. StateController.ChangeStateInstant(PlayerStateEnum.Idle);
  52. //InitSubLine();
  53. _brushData2 = new BrushImageData(ExcelConfig.LiquidMaterial_Map["0001"]);
  54. WeaponPack.SetCapacity(10);
  55. }
  56.  
  57. private void DebugSet()
  58. {
  59. if (World is Hall)
  60. {
  61. return;
  62. }
  63. RoleState.Acceleration = 3000;
  64. RoleState.Friction = 3000;
  65. RoleState.MoveSpeed = 500;
  66. CollisionLayer = PhysicsLayer.None;
  67. CollisionMask = PhysicsLayer.None;
  68. //GameCamera.Main.Zoom = new Vector2(0.5f, 0.5f);
  69. // this.CallDelay(0.5f, () =>
  70. // {
  71. // PickUpWeapon(Create<Weapon>(Ids.Id_weapon0009));
  72. // PickUpWeapon(Create<Weapon>(Ids.Id_weapon0008));
  73. // PickUpWeapon(Create<Weapon>(Ids.Id_weapon0007));
  74. // PickUpWeapon(Create<Weapon>(Ids.Id_weapon0006));
  75. // });
  76. World.Color = new Color(1, 1, 1, 1); //关闭迷雾
  77. //显示房间小地图
  78. this.CallDelay(1, () =>
  79. {
  80. GameApplication.Instance.DungeonManager.StartRoomInfo.EachRoom(info =>
  81. {
  82. info.PreviewSprite.Visible = true;
  83. foreach (var roomDoorInfo in info.Doors)
  84. {
  85. roomDoorInfo.AislePreviewSprite.Visible = true;
  86. }
  87. });
  88. });
  89.  
  90. }
  91.  
  92. protected override RoleState OnCreateRoleState()
  93. {
  94. var roleState = new PlayerRoleState();
  95. PlayerRoleState = roleState;
  96. return roleState;
  97. }
  98.  
  99. protected override void Process(float delta)
  100. {
  101. base.Process(delta);
  102. if (IsDie)
  103. {
  104. return;
  105. }
  106.  
  107. if (_rollCoolingTimer > 0)
  108. {
  109. _rollCoolingTimer -= delta;
  110. }
  111. if (MountLookTarget) //看向目标
  112. {
  113. //脸的朝向
  114. var gPos = Position;
  115. Vector2 mousePos = InputManager.CursorPosition;
  116. if (mousePos.X > gPos.X && Face == FaceDirection.Left)
  117. {
  118. Face = FaceDirection.Right;
  119. }
  120. else if (mousePos.X < gPos.X && Face == FaceDirection.Right)
  121. {
  122. Face = FaceDirection.Left;
  123. }
  124. //枪口跟随鼠标
  125. MountPoint.SetLookAt(mousePos);
  126. }
  127.  
  128. if (InputManager.ExchangeWeapon) //切换武器
  129. {
  130. ExchangeNextWeapon();
  131. }
  132. else if (InputManager.ThrowWeapon) //扔掉武器
  133. {
  134. ThrowWeapon();
  135.  
  136. // //测试用的, 所有敌人也扔掉武器
  137. // if (Affiliation != null)
  138. // {
  139. // var enemies = Affiliation.FindIncludeItems(o =>
  140. // {
  141. // return o.CollisionWithMask(PhysicsLayer.Enemy);
  142. // });
  143. // foreach (var activityObject in enemies)
  144. // {
  145. // if (activityObject is Enemy enemy)
  146. // {
  147. // enemy.ThrowWeapon();
  148. // }
  149. // }
  150. // }
  151. }
  152. else if (InputManager.Interactive) //互动物体
  153. {
  154. TriggerInteractive();
  155. }
  156. else if (InputManager.Reload) //换弹
  157. {
  158. Reload();
  159. }
  160.  
  161. var meleeAttackFlag = false;
  162. if (InputManager.MeleeAttack) //近战攻击
  163. {
  164. if (StateController.CurrState != PlayerStateEnum.Roll) //不能是翻滚状态
  165. {
  166. if (WeaponPack.ActiveItem != null && WeaponPack.ActiveItem.Attribute.CanMeleeAttack)
  167. {
  168. meleeAttackFlag = true;
  169. MeleeAttack();
  170. }
  171. }
  172. }
  173. if (!meleeAttackFlag && InputManager.Fire) //正常开火
  174. {
  175. if (StateController.CurrState != PlayerStateEnum.Roll) //不能是翻滚状态
  176. {
  177. Attack();
  178. // //测试用,触发房间内地上的武器开火
  179. // var weaponArray = AffiliationArea.FindEnterItems(o => o is Weapon);
  180. // foreach (Weapon activityObject in weaponArray)
  181. // {
  182. // activityObject.Trigger(this);
  183. // }
  184. }
  185. }
  186.  
  187. if (InputManager.Roulette) //打开武器轮盘
  188. {
  189. //UiManager.Open_WeaponRoulette();
  190. }
  191. else if (InputManager.UseActiveProp) //使用道具
  192. {
  193. UseActiveProp();
  194. }
  195. else if (InputManager.ExchangeProp) //切换道具
  196. {
  197. ExchangeNextActiveProp();
  198. }
  199. else if (InputManager.RemoveProp) //扔掉道具
  200. {
  201. ThrowActiveProp();
  202. }
  203.  
  204. if (Input.IsKeyPressed(Key.P)) //测试用, 自杀
  205. {
  206. //Hurt(1000, 0);
  207. Hp = 0;
  208. HurtHandler(this, 1000, 0);
  209. }
  210. else if (Input.IsKeyPressed(Key.O)) //测试用, 消灭房间内所有敌人
  211. {
  212. var enemyList = AffiliationArea.FindIncludeItems(o => o is Role role && role.IsEnemyWithPlayer());
  213. foreach (var enemy in enemyList)
  214. {
  215. var hurt = ((Enemy)enemy).HurtArea;
  216. if (hurt.CanHurt(Camp))
  217. {
  218. hurt.Hurt(this, 1000, 0);
  219. }
  220. }
  221. }
  222. // //测试用
  223. // if (InputManager.Roll) //鼠标处触发互动物体
  224. // {
  225. // var now = DateTime.Now;
  226. // var mousePosition = GetGlobalMousePosition();
  227. // var freezeSprites = AffiliationArea.RoomInfo.StaticSprite.CollisionCircle(mousePosition, 25, true);
  228. // Debug.Log("检测数量: " + freezeSprites.Count + ", 用时: " + (DateTime.Now - now).TotalMilliseconds);
  229. // foreach (var freezeSprite in freezeSprites)
  230. // {
  231. // var temp = freezeSprite.Position - mousePosition;
  232. // freezeSprite.ActivityObject.MoveController.AddForce(temp.Normalized() * 300 * (25f - temp.Length()) / 25f);
  233. // }
  234. // }
  235. if (Face == FaceDirection.Right)
  236. {
  237. TipRoot.Scale = Vector2.One;
  238. }
  239. else
  240. {
  241. TipRoot.Scale = new Vector2(-1, 1);
  242. }
  243.  
  244. //测试刷地
  245. //DrawLiquid(_brushData2);
  246. }
  247.  
  248. protected override void OnAffiliationChange(AffiliationArea prevArea)
  249. {
  250. BrushPrevPosition = null;
  251. base.OnAffiliationChange(prevArea);
  252. }
  253.  
  254. protected override void OnPickUpWeapon(Weapon weapon)
  255. {
  256. EventManager.EmitEvent(EventEnum.OnPlayerPickUpWeapon, weapon);
  257. }
  258.  
  259. protected override void OnThrowWeapon(Weapon weapon)
  260. {
  261. EventManager.EmitEvent(EventEnum.OnPlayerRemoveWeapon, weapon);
  262. }
  263.  
  264. protected override int OnHandlerHurt(int damage)
  265. {
  266. //修改受到的伤害, 每次只受到1点伤害
  267. return 1;
  268. }
  269.  
  270. protected override void OnHit(ActivityObject target, int damage, float angle, bool realHarm)
  271. {
  272. //进入无敌状态
  273. if (realHarm) //真实伤害
  274. {
  275. PlayInvincibleFlashing(RoleState.WoundedInvincibleTime);
  276. }
  277. else //护盾抵消掉的
  278. {
  279. PlayInvincibleFlashing(RoleState.ShieldInvincibleTime);
  280. }
  281. }
  282.  
  283. protected override void OnChangeHp(int hp)
  284. {
  285. //GameApplication.Instance.Ui.SetHp(hp);
  286. EventManager.EmitEvent(EventEnum.OnPlayerHpChange, hp);
  287. }
  288.  
  289. protected override void OnChangeMaxHp(int maxHp)
  290. {
  291. //GameApplication.Instance.Ui.SetMaxHp(maxHp);
  292. EventManager.EmitEvent(EventEnum.OnPlayerMaxHpChange, maxHp);
  293. }
  294.  
  295. protected override void ChangeInteractiveItem(CheckInteractiveResult prev, CheckInteractiveResult result)
  296. {
  297. if (prev != null && prev.Target.ShowOutline)
  298. {
  299. prev.Target.OutlineColor = Colors.Black;
  300. }
  301. if (result != null && result.Target.ShowOutline)
  302. {
  303. result.Target.OutlineColor = Colors.White;
  304. }
  305. //派发互动对象改变事件
  306. EventManager.EmitEvent(EventEnum.OnPlayerChangeInteractiveItem, result);
  307. }
  308.  
  309. protected override void OnChangeShield(int shield)
  310. {
  311. //GameApplication.Instance.Ui.SetShield(shield);
  312. EventManager.EmitEvent(EventEnum.OnPlayerShieldChange, shield);
  313. }
  314.  
  315. protected override void OnChangeMaxShield(int maxShield)
  316. {
  317. //GameApplication.Instance.Ui.SetMaxShield(maxShield);
  318. EventManager.EmitEvent(EventEnum.OnPlayerMaxShieldChange, maxShield);
  319. }
  320.  
  321. protected override void OnDie()
  322. {
  323. StateController.Enable = false;
  324. GameCamera.Main.SetFollowTarget(null);
  325. BasisVelocity = Vector2.Zero;
  326. MoveController.ClearForce();
  327.  
  328. //暂停游戏
  329. World.Current.Pause = true;
  330. //弹出结算面板
  331. GameApplication.Instance.Cursor.SetGuiMode(true);
  332. UiManager.Open_Settlement();
  333. }
  334.  
  335. protected override void OnPickUpActiveProp(ActiveProp activeProp)
  336. {
  337. EventManager.EmitEvent(EventEnum.OnPlayerPickUpProp, activeProp);
  338. }
  339.  
  340. protected override void OnRemoveActiveProp(ActiveProp activeProp)
  341. {
  342. EventManager.EmitEvent(EventEnum.OnPlayerRemoveProp, activeProp);
  343. }
  344.  
  345. protected override void OnPickUpBuffProp(BuffProp buffProp)
  346. {
  347. EventManager.EmitEvent(EventEnum.OnPlayerPickUpProp, buffProp);
  348. }
  349.  
  350. protected override void OnRemoveBuffProp(BuffProp buffProp)
  351. {
  352. EventManager.EmitEvent(EventEnum.OnPlayerRemoveProp, buffProp);
  353. }
  354.  
  355. /// <summary>
  356. /// 处理角色移动的输入
  357. /// </summary>
  358. public void HandleMoveInput(float delta)
  359. {
  360. var dir = InputManager.MoveAxis;
  361. // 移动. 如果移动的数值接近0(是用 摇杆可能出现 方向 可能会出现浮点),就friction的值 插值 到 0
  362. // 如果 有输入 就以当前速度,用acceleration 插值到 对应方向 * 最大速度
  363. if (Mathf.IsZeroApprox(dir.X))
  364. {
  365. BasisVelocity = new Vector2(Mathf.MoveToward(BasisVelocity.X, 0, RoleState.Friction * delta), BasisVelocity.Y);
  366. }
  367. else
  368. {
  369. BasisVelocity = new Vector2(Mathf.MoveToward(BasisVelocity.X, dir.X * RoleState.MoveSpeed, RoleState.Acceleration * delta), BasisVelocity.Y);
  370. }
  371.  
  372. if (Mathf.IsZeroApprox(dir.Y))
  373. {
  374. BasisVelocity = new Vector2(BasisVelocity.X, Mathf.MoveToward(BasisVelocity.Y, 0, RoleState.Friction * delta));
  375. }
  376. else
  377. {
  378. BasisVelocity = new Vector2(BasisVelocity.X, Mathf.MoveToward(BasisVelocity.Y, dir.Y * RoleState.MoveSpeed, RoleState.Acceleration * delta));
  379. }
  380. }
  381.  
  382. /// <summary>
  383. /// 翻滚结束
  384. /// </summary>
  385. public void OverRoll()
  386. {
  387. _rollCoolingTimer = PlayerRoleState.RollCoolingTime;
  388. }
  389.  
  390. // protected override void DebugDraw()
  391. // {
  392. // base.DebugDraw();
  393. // DrawArc(GetLocalMousePosition(), 25, 0, Mathf.Pi * 2f, 20, Colors.Red, 1);
  394. // }
  395.  
  396. public override void AddGold(int goldCount)
  397. {
  398. base.AddGold(goldCount);
  399. EventManager.EmitEvent(EventEnum.OnPlayerGoldChange, RoleState.Gold);
  400. }
  401.  
  402. public override void UseGold(int goldCount)
  403. {
  404. base.UseGold(goldCount);
  405. EventManager.EmitEvent(EventEnum.OnPlayerGoldChange, RoleState.Gold);
  406. }
  407.  
  408. /// <summary>
  409. /// 玩家第一次进入房间时调用
  410. /// </summary>
  411. public virtual void OnFirstEnterRoom(RoomInfo roomInfo)
  412. {
  413. if (OnFirstEnterRoomEvent != null)
  414. {
  415. OnFirstEnterRoomEvent(roomInfo);
  416. }
  417. }
  418. }