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