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. //血量为0, 扔掉所有武器
  291. if (Hp <= 0) //死亡
  292. {
  293. BasisVelocity = Vector2.Zero;
  294. Velocity = Vector2.Zero;
  295. ThrowAllWeapon();
  296. GameCamera.Main.CreateShake(new Vector2(3, 3), 1f, true);
  297. GameCamera.Main.FollowsMouseAmount = 0;
  298. GameCamera.Main.PlayZoomAnimation(new Vector2(2, 2), 5);
  299.  
  300. SoundManager.PlaySoundByConfig("role_die", Position);
  301. }
  302. else //受伤
  303. {
  304. SoundManager.PlaySoundByConfig("role_hurt", Position);
  305. }
  306. }
  307.  
  308. protected override void OnChangeHp(int hp)
  309. {
  310. //GameApplication.Instance.Ui.SetHp(hp);
  311. EventManager.EmitEvent(EventEnum.OnPlayerHpChange, hp);
  312. }
  313.  
  314. protected override void OnChangeMaxHp(int maxHp)
  315. {
  316. //GameApplication.Instance.Ui.SetMaxHp(maxHp);
  317. EventManager.EmitEvent(EventEnum.OnPlayerMaxHpChange, maxHp);
  318. }
  319.  
  320. protected override void ChangeInteractiveItem(CheckInteractiveResult prev, CheckInteractiveResult result)
  321. {
  322. //派发互动对象改变事件
  323. EventManager.EmitEvent(EventEnum.OnPlayerChangeInteractiveItem, result);
  324. }
  325.  
  326. protected override void OnChangeShield(int shield)
  327. {
  328. //GameApplication.Instance.Ui.SetShield(shield);
  329. EventManager.EmitEvent(EventEnum.OnPlayerShieldChange, shield);
  330. }
  331.  
  332. protected override void OnChangeMaxShield(int maxShield)
  333. {
  334. //GameApplication.Instance.Ui.SetMaxShield(maxShield);
  335. EventManager.EmitEvent(EventEnum.OnPlayerMaxShieldChange, maxShield);
  336. }
  337.  
  338. protected override void OnDie()
  339. {
  340. StateController.Enable = false;
  341. GameCamera.Main.SetFollowTarget(null);
  342. BasisVelocity = Vector2.Zero;
  343. MoveController.ClearForce();
  344. Visible = false;
  345.  
  346. World.CallDelay(0.5f, () =>
  347. {
  348. //暂停游戏
  349. World.Current.Pause = true;
  350. //弹出结算面板
  351. GameApplication.Instance.Cursor.SetGuiMode(true);
  352. UiManager.Open_Settlement();
  353. });
  354. }
  355.  
  356. protected override void OnPickUpActiveProp(ActiveProp activeProp)
  357. {
  358. EventManager.EmitEvent(EventEnum.OnPlayerPickUpProp, activeProp);
  359. }
  360.  
  361. protected override void OnRemoveActiveProp(ActiveProp activeProp)
  362. {
  363. EventManager.EmitEvent(EventEnum.OnPlayerRemoveProp, activeProp);
  364. }
  365.  
  366. protected override void OnPickUpBuffProp(BuffProp buffProp)
  367. {
  368. EventManager.EmitEvent(EventEnum.OnPlayerPickUpProp, buffProp);
  369. }
  370.  
  371. protected override void OnRemoveBuffProp(BuffProp buffProp)
  372. {
  373. EventManager.EmitEvent(EventEnum.OnPlayerRemoveProp, buffProp);
  374. }
  375.  
  376. /// <summary>
  377. /// 处理角色移动的输入
  378. /// </summary>
  379. public void HandleMoveInput(float delta)
  380. {
  381. var dir = InputManager.MoveAxis;
  382. // 移动. 如果移动的数值接近0(是用 摇杆可能出现 方向 可能会出现浮点),就friction的值 插值 到 0
  383. // 如果 有输入 就以当前速度,用acceleration 插值到 对应方向 * 最大速度
  384. if (Mathf.IsZeroApprox(dir.X))
  385. {
  386. BasisVelocity = new Vector2(Mathf.MoveToward(BasisVelocity.X, 0, RoleState.Friction * delta), BasisVelocity.Y);
  387. }
  388. else
  389. {
  390. BasisVelocity = new Vector2(Mathf.MoveToward(BasisVelocity.X, dir.X * RoleState.MoveSpeed, RoleState.Acceleration * delta), BasisVelocity.Y);
  391. }
  392.  
  393. if (Mathf.IsZeroApprox(dir.Y))
  394. {
  395. BasisVelocity = new Vector2(BasisVelocity.X, Mathf.MoveToward(BasisVelocity.Y, 0, RoleState.Friction * delta));
  396. }
  397. else
  398. {
  399. BasisVelocity = new Vector2(BasisVelocity.X, Mathf.MoveToward(BasisVelocity.Y, dir.Y * RoleState.MoveSpeed, RoleState.Acceleration * delta));
  400. }
  401. }
  402.  
  403. /// <summary>
  404. /// 翻滚结束
  405. /// </summary>
  406. public void OverRoll()
  407. {
  408. _rollCoolingTimer = PlayerRoleState.RollCoolingTime;
  409. }
  410.  
  411. // protected override void DebugDraw()
  412. // {
  413. // base.DebugDraw();
  414. // DrawArc(GetLocalMousePosition(), 25, 0, Mathf.Pi * 2f, 20, Colors.Red, 1);
  415. // }
  416.  
  417. public override void AddGold(int goldCount)
  418. {
  419. base.AddGold(goldCount);
  420. EventManager.EmitEvent(EventEnum.OnPlayerGoldChange, RoleState.Gold);
  421. }
  422.  
  423. public override void UseGold(int goldCount)
  424. {
  425. base.UseGold(goldCount);
  426. EventManager.EmitEvent(EventEnum.OnPlayerGoldChange, RoleState.Gold);
  427. }
  428.  
  429. /// <summary>
  430. /// 玩家第一次进入房间时调用
  431. /// </summary>
  432. public virtual void OnFirstEnterRoom(RoomInfo roomInfo)
  433. {
  434. if (OnFirstEnterRoomEvent != null)
  435. {
  436. OnFirstEnterRoomEvent(roomInfo);
  437. }
  438. }
  439. }