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