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