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