Newer
Older
DungeonShooting / DungeonShooting_Godot / src / game / activity / role / player / Player.cs
  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. foreach (var role in World.Role_InstanceList)
  195. {
  196. if (IsEnemy(role))
  197. {
  198. role.Camp = Camp;
  199. break;
  200. }
  201. }
  202. }
  203. else if (InputManager.ExchangeProp) //切换道具
  204. {
  205. ExchangeNextActiveProp();
  206. }
  207. else if (InputManager.RemoveProp) //扔掉道具
  208. {
  209. ThrowActiveProp();
  210. }
  211.  
  212. if (Input.IsKeyPressed(Key.P)) //测试用, 自杀
  213. {
  214. //Hurt(1000, 0);
  215. Hp = 0;
  216. HurtHandler(this, 1000, 0);
  217. }
  218. else if (Input.IsKeyPressed(Key.O)) //测试用, 消灭房间内所有敌人
  219. {
  220. var enemyList = AffiliationArea.FindIncludeItems(o => o is Role role && role.IsEnemyWithPlayer());
  221. foreach (var enemy in enemyList)
  222. {
  223. var hurt = ((Enemy)enemy).HurtArea;
  224. if (hurt.CanHurt(Camp))
  225. {
  226. hurt.Hurt(this, 1000, 0);
  227. }
  228. }
  229. }
  230. // //测试用
  231. // if (InputManager.Roll) //鼠标处触发互动物体
  232. // {
  233. // var now = DateTime.Now;
  234. // var mousePosition = GetGlobalMousePosition();
  235. // var freezeSprites = AffiliationArea.RoomInfo.StaticSprite.CollisionCircle(mousePosition, 25, true);
  236. // Debug.Log("检测数量: " + freezeSprites.Count + ", 用时: " + (DateTime.Now - now).TotalMilliseconds);
  237. // foreach (var freezeSprite in freezeSprites)
  238. // {
  239. // var temp = freezeSprite.Position - mousePosition;
  240. // freezeSprite.ActivityObject.MoveController.AddForce(temp.Normalized() * 300 * (25f - temp.Length()) / 25f);
  241. // }
  242. // }
  243. if (Face == FaceDirection.Right)
  244. {
  245. TipRoot.Scale = Vector2.One;
  246. }
  247. else
  248. {
  249. TipRoot.Scale = new Vector2(-1, 1);
  250. }
  251.  
  252. //测试刷地
  253. DrawLiquid(_brushData2);
  254. }
  255.  
  256. protected override void OnAffiliationChange(AffiliationArea prevArea)
  257. {
  258. BrushPrevPosition = null;
  259. base.OnAffiliationChange(prevArea);
  260. }
  261.  
  262. protected override void OnPickUpWeapon(Weapon weapon)
  263. {
  264. EventManager.EmitEvent(EventEnum.OnPlayerPickUpWeapon, weapon);
  265. }
  266.  
  267. protected override void OnThrowWeapon(Weapon weapon)
  268. {
  269. EventManager.EmitEvent(EventEnum.OnPlayerRemoveWeapon, weapon);
  270. }
  271.  
  272. protected override int OnHandlerHurt(int damage)
  273. {
  274. //修改受到的伤害, 每次只受到1点伤害
  275. return 1;
  276. }
  277.  
  278. protected override void OnHit(ActivityObject target, int damage, float angle, bool realHarm)
  279. {
  280. //进入无敌状态
  281. if (realHarm) //真实伤害
  282. {
  283. PlayInvincibleFlashing(RoleState.WoundedInvincibleTime);
  284. }
  285. else //护盾抵消掉的
  286. {
  287. PlayInvincibleFlashing(RoleState.ShieldInvincibleTime);
  288. }
  289. }
  290.  
  291. protected override void OnChangeHp(int hp)
  292. {
  293. //GameApplication.Instance.Ui.SetHp(hp);
  294. EventManager.EmitEvent(EventEnum.OnPlayerHpChange, hp);
  295. }
  296.  
  297. protected override void OnChangeMaxHp(int maxHp)
  298. {
  299. //GameApplication.Instance.Ui.SetMaxHp(maxHp);
  300. EventManager.EmitEvent(EventEnum.OnPlayerMaxHpChange, maxHp);
  301. }
  302.  
  303. protected override void ChangeInteractiveItem(CheckInteractiveResult prev, CheckInteractiveResult result)
  304. {
  305. if (prev != null && prev.Target.ShowOutline)
  306. {
  307. prev.Target.OutlineColor = Colors.Black;
  308. }
  309. if (result != null && result.Target.ShowOutline)
  310. {
  311. result.Target.OutlineColor = Colors.White;
  312. }
  313. //派发互动对象改变事件
  314. EventManager.EmitEvent(EventEnum.OnPlayerChangeInteractiveItem, result);
  315. }
  316.  
  317. protected override void OnChangeShield(int shield)
  318. {
  319. //GameApplication.Instance.Ui.SetShield(shield);
  320. EventManager.EmitEvent(EventEnum.OnPlayerShieldChange, shield);
  321. }
  322.  
  323. protected override void OnChangeMaxShield(int maxShield)
  324. {
  325. //GameApplication.Instance.Ui.SetMaxShield(maxShield);
  326. EventManager.EmitEvent(EventEnum.OnPlayerMaxShieldChange, maxShield);
  327. }
  328.  
  329. protected override void OnDie()
  330. {
  331. StateController.Enable = false;
  332. GameCamera.Main.SetFollowTarget(null);
  333. BasisVelocity = Vector2.Zero;
  334. MoveController.ClearForce();
  335.  
  336. //暂停游戏
  337. World.Current.Pause = true;
  338. //弹出结算面板
  339. GameApplication.Instance.Cursor.SetGuiMode(true);
  340. UiManager.Open_Settlement();
  341. }
  342.  
  343. protected override void OnPickUpActiveProp(ActiveProp activeProp)
  344. {
  345. EventManager.EmitEvent(EventEnum.OnPlayerPickUpProp, activeProp);
  346. }
  347.  
  348. protected override void OnRemoveActiveProp(ActiveProp activeProp)
  349. {
  350. EventManager.EmitEvent(EventEnum.OnPlayerRemoveProp, activeProp);
  351. }
  352.  
  353. protected override void OnPickUpBuffProp(BuffProp buffProp)
  354. {
  355. EventManager.EmitEvent(EventEnum.OnPlayerPickUpProp, buffProp);
  356. }
  357.  
  358. protected override void OnRemoveBuffProp(BuffProp buffProp)
  359. {
  360. EventManager.EmitEvent(EventEnum.OnPlayerRemoveProp, buffProp);
  361. }
  362.  
  363. /// <summary>
  364. /// 处理角色移动的输入
  365. /// </summary>
  366. public void HandleMoveInput(float delta)
  367. {
  368. var dir = InputManager.MoveAxis;
  369. // 移动. 如果移动的数值接近0(是用 摇杆可能出现 方向 可能会出现浮点),就friction的值 插值 到 0
  370. // 如果 有输入 就以当前速度,用acceleration 插值到 对应方向 * 最大速度
  371. if (Mathf.IsZeroApprox(dir.X))
  372. {
  373. BasisVelocity = new Vector2(Mathf.MoveToward(BasisVelocity.X, 0, RoleState.Friction * delta), BasisVelocity.Y);
  374. }
  375. else
  376. {
  377. BasisVelocity = new Vector2(Mathf.MoveToward(BasisVelocity.X, dir.X * RoleState.MoveSpeed, RoleState.Acceleration * delta), BasisVelocity.Y);
  378. }
  379.  
  380. if (Mathf.IsZeroApprox(dir.Y))
  381. {
  382. BasisVelocity = new Vector2(BasisVelocity.X, Mathf.MoveToward(BasisVelocity.Y, 0, RoleState.Friction * delta));
  383. }
  384. else
  385. {
  386. BasisVelocity = new Vector2(BasisVelocity.X, Mathf.MoveToward(BasisVelocity.Y, dir.Y * RoleState.MoveSpeed, RoleState.Acceleration * delta));
  387. }
  388. }
  389.  
  390. /// <summary>
  391. /// 翻滚结束
  392. /// </summary>
  393. public void OverRoll()
  394. {
  395. _rollCoolingTimer = PlayerRoleState.RollCoolingTime;
  396. }
  397.  
  398. // protected override void DebugDraw()
  399. // {
  400. // base.DebugDraw();
  401. // DrawArc(GetLocalMousePosition(), 25, 0, Mathf.Pi * 2f, 20, Colors.Red, 1);
  402. // }
  403.  
  404. public override void AddGold(int goldCount)
  405. {
  406. base.AddGold(goldCount);
  407. EventManager.EmitEvent(EventEnum.OnPlayerGoldChange, RoleState.Gold);
  408. }
  409.  
  410. public override void UseGold(int goldCount)
  411. {
  412. base.UseGold(goldCount);
  413. EventManager.EmitEvent(EventEnum.OnPlayerGoldChange, RoleState.Gold);
  414. }
  415.  
  416. /// <summary>
  417. /// 玩家第一次进入房间时调用
  418. /// </summary>
  419. public virtual void OnFirstEnterRoom(RoomInfo roomInfo)
  420. {
  421. if (OnFirstEnterRoomEvent != null)
  422. {
  423. OnFirstEnterRoomEvent(roomInfo);
  424. }
  425. }
  426. }