Newer
Older
DungeonShooting / DungeonShooting_Godot / src / game / activity / role / player / Player.cs
@小李xl 小李xl on 10 Nov 2023 10 KB 更改角色模板继承关系
  1. using System;
  2. using Godot;
  3.  
  4.  
  5. /// <summary>
  6. /// 玩家角色基类, 所有角色都必须继承该类
  7. /// </summary>
  8. [Tool]
  9. public partial class Player : AdvancedRole
  10. {
  11. /// <summary>
  12. /// 获取当前操作的角色
  13. /// </summary>
  14. public static Player Current { get; private set; }
  15. /// <summary>
  16. /// 玩家身上的状态机控制器
  17. /// </summary>
  18. public StateController<Player, PlayerStateEnum> StateController { get; private set; }
  19.  
  20. /// <summary>
  21. /// 是否可以翻滚
  22. /// </summary>
  23. public bool CanRoll => _rollCoolingTimer <= 0;
  24. //翻滚冷却计时器
  25. private float _rollCoolingTimer = 0;
  26. /// <summary>
  27. /// 设置当前操作的玩家对象
  28. /// </summary>
  29. public static void SetCurrentPlayer(Player player)
  30. {
  31. Current = player;
  32. //设置相机和鼠标跟随玩家
  33. GameCamera.Main.SetFollowTarget(player);
  34. GameApplication.Instance.Cursor.SetMountRole(player);
  35. }
  36. public override void OnInit()
  37. {
  38. base.OnInit();
  39.  
  40. IsAi = false;
  41. StateController = AddComponent<StateController<Player, PlayerStateEnum>>();
  42. AttackLayer = PhysicsLayer.Wall | PhysicsLayer.Enemy;
  43. EnemyLayer = EnemyLayer = PhysicsLayer.Enemy;
  44. Camp = CampEnum.Camp1;
  45.  
  46. MaxHp = 6;
  47. Hp = 6;
  48. MaxShield = 0;
  49. Shield = 0;
  50.  
  51. // debug用
  52. // RoleState.Acceleration = 3000;
  53. // RoleState.Friction = 3000;
  54. // RoleState.MoveSpeed = 500;
  55. // CollisionLayer = 0;
  56. // CollisionMask = 0;
  57. //GameCamera.Main.Zoom = new Vector2(0.2f, 0.2f);
  58. //GameCamera.Main.Zoom = new Vector2(0.5f, 0.5f);
  59. // this.CallDelay(0.5f, () =>
  60. // {
  61. // var weapon = Create<Weapon>(Ids.Id_weapon0009);
  62. // PickUpWeapon(weapon);
  63. // });
  64. //注册状态机
  65. StateController.Register(new PlayerIdleState());
  66. StateController.Register(new PlayerMoveState());
  67. StateController.Register(new PlayerRollState());
  68. //默认状态
  69. StateController.ChangeStateInstant(PlayerStateEnum.Idle);
  70.  
  71. //InitSubLine();
  72. }
  73.  
  74. protected override void Process(float delta)
  75. {
  76. base.Process(delta);
  77. if (IsDie)
  78. {
  79. return;
  80. }
  81.  
  82. if (_rollCoolingTimer > 0)
  83. {
  84. _rollCoolingTimer -= delta;
  85. }
  86. //脸的朝向
  87. if (LookTarget == null)
  88. {
  89. var gPos = GlobalPosition;
  90. Vector2 mousePos = InputManager.CursorPosition;
  91. if (mousePos.X > gPos.X && Face == FaceDirection.Left)
  92. {
  93. Face = FaceDirection.Right;
  94. }
  95. else if (mousePos.X < gPos.X && Face == FaceDirection.Right)
  96. {
  97. Face = FaceDirection.Left;
  98. }
  99.  
  100. if (MountLookTarget)
  101. {
  102. //枪口跟随鼠标
  103. MountPoint.SetLookAt(mousePos);
  104. }
  105. }
  106.  
  107. if (InputManager.ExchangeWeapon) //切换武器
  108. {
  109. ExchangeNextWeapon();
  110. }
  111. else if (InputManager.ThrowWeapon) //扔掉武器
  112. {
  113. ThrowWeapon();
  114.  
  115. // //测试用的, 所有敌人也扔掉武器
  116. // if (Affiliation != null)
  117. // {
  118. // var enemies = Affiliation.FindIncludeItems(o =>
  119. // {
  120. // return o.CollisionWithMask(PhysicsLayer.Enemy);
  121. // });
  122. // foreach (var activityObject in enemies)
  123. // {
  124. // if (activityObject is Enemy enemy)
  125. // {
  126. // enemy.ThrowWeapon();
  127. // }
  128. // }
  129. // }
  130. }
  131. else if (InputManager.Interactive) //互动物体
  132. {
  133. TriggerInteractive();
  134. }
  135. else if (InputManager.Reload) //换弹
  136. {
  137. Reload();
  138. }
  139.  
  140. var meleeAttackFlag = false;
  141. if (InputManager.MeleeAttack) //近战攻击
  142. {
  143. if (StateController.CurrState != PlayerStateEnum.Roll) //不能是翻滚状态
  144. {
  145. if (WeaponPack.ActiveItem != null && WeaponPack.ActiveItem.Attribute.CanMeleeAttack)
  146. {
  147. meleeAttackFlag = true;
  148. MeleeAttack();
  149. }
  150. }
  151. }
  152. if (!meleeAttackFlag && InputManager.Fire) //正常开火
  153. {
  154. if (StateController.CurrState != PlayerStateEnum.Roll) //不能是翻滚状态
  155. {
  156. Attack();
  157. // //测试用,触发房间内地上的武器开火
  158. // var weaponArray = AffiliationArea.FindEnterItems(o => o is Weapon);
  159. // foreach (Weapon activityObject in weaponArray)
  160. // {
  161. // activityObject.Trigger(this);
  162. // }
  163. }
  164. }
  165.  
  166. if (InputManager.UseActiveProp) //使用道具
  167. {
  168. UseActiveProp();
  169. }
  170. else if (InputManager.ExchangeProp) //切换道具
  171. {
  172. ExchangeNextActiveProp();
  173. }
  174. else if (InputManager.RemoveProp) //扔掉道具
  175. {
  176. ThrowActiveProp();
  177. }
  178.  
  179. if (Input.IsKeyPressed(Key.P)) //测试用, 自杀
  180. {
  181. //Hurt(1000, 0);
  182. Hp = 0;
  183. Hurt(1000, 0);
  184. }
  185. else if (Input.IsKeyPressed(Key.O)) //测试用, 消灭房间内所有敌人
  186. {
  187. var enemyList = AffiliationArea.FindIncludeItems(o => o.CollisionWithMask(PhysicsLayer.Enemy));
  188. foreach (var enemy in enemyList)
  189. {
  190. ((AdvancedEnemy)enemy).Hurt(1000, 0);
  191. }
  192. }
  193. // //测试用
  194. // if (InputManager.Roll) //鼠标处触发互动物体
  195. // {
  196. // var now = DateTime.Now;
  197. // var mousePosition = GetGlobalMousePosition();
  198. // var freezeSprites = AffiliationArea.RoomInfo.StaticSprite.CollisionCircle(mousePosition, 25, true);
  199. // Debug.Log("检测数量: " + freezeSprites.Count + ", 用时: " + (DateTime.Now - now).TotalMilliseconds);
  200. // foreach (var freezeSprite in freezeSprites)
  201. // {
  202. // var temp = freezeSprite.Position - mousePosition;
  203. // freezeSprite.ActivityObject.MoveController.AddForce(temp.Normalized() * 300 * (25f - temp.Length()) / 25f);
  204. // }
  205. // }
  206. }
  207.  
  208. protected override void OnPickUpWeapon(Weapon weapon)
  209. {
  210. EventManager.EmitEvent(EventEnum.OnPlayerPickUpWeapon, weapon);
  211. }
  212.  
  213. protected override void OnThrowWeapon(Weapon weapon)
  214. {
  215. EventManager.EmitEvent(EventEnum.OnPlayerRemoveWeapon, weapon);
  216. }
  217.  
  218. protected override int OnHandlerHurt(int damage)
  219. {
  220. //修改受到的伤害, 每次只受到1点伤害
  221. return 1;
  222. }
  223.  
  224. protected override void OnHit(int damage, bool realHarm)
  225. {
  226. //进入无敌状态
  227. if (realHarm) //真实伤害
  228. {
  229. PlayInvincibleFlashing(RoleState.WoundedInvincibleTime);
  230. }
  231. else //护盾抵消掉的
  232. {
  233. PlayInvincibleFlashing(RoleState.ShieldInvincibleTime);
  234. }
  235. }
  236.  
  237. protected override void OnChangeHp(int hp)
  238. {
  239. //GameApplication.Instance.Ui.SetHp(hp);
  240. EventManager.EmitEvent(EventEnum.OnPlayerHpChange, hp);
  241. }
  242.  
  243. protected override void OnChangeMaxHp(int maxHp)
  244. {
  245. //GameApplication.Instance.Ui.SetMaxHp(maxHp);
  246. EventManager.EmitEvent(EventEnum.OnPlayerMaxHpChange, maxHp);
  247. }
  248.  
  249. protected override void ChangeInteractiveItem(CheckInteractiveResult prev, CheckInteractiveResult result)
  250. {
  251. if (prev != null && prev.Target.ShowOutline)
  252. {
  253. prev.Target.OutlineColor = Colors.Black;
  254. }
  255. if (result != null && result.Target.ShowOutline)
  256. {
  257. result.Target.OutlineColor = Colors.White;
  258. }
  259. //派发互动对象改变事件
  260. EventManager.EmitEvent(EventEnum.OnPlayerChangeInteractiveItem, result);
  261. }
  262.  
  263. protected override void OnChangeShield(int shield)
  264. {
  265. //GameApplication.Instance.Ui.SetShield(shield);
  266. EventManager.EmitEvent(EventEnum.OnPlayerShieldChange, shield);
  267. }
  268.  
  269. protected override void OnChangeMaxShield(int maxShield)
  270. {
  271. //GameApplication.Instance.Ui.SetMaxShield(maxShield);
  272. EventManager.EmitEvent(EventEnum.OnPlayerMaxShieldChange, maxShield);
  273. }
  274.  
  275. protected override void OnDie()
  276. {
  277. StateController.Enable = false;
  278. GameCamera.Main.SetFollowTarget(null);
  279. BasisVelocity = Vector2.Zero;
  280. MoveController.ClearForce();
  281.  
  282. //暂停游戏
  283. GameApplication.Instance.World.Pause = true;
  284. //弹出结算面板
  285. GameApplication.Instance.Cursor.SetGuiMode(true);
  286. UiManager.Open_Settlement();
  287. }
  288.  
  289. protected override void OnPickUpActiveProp(ActiveProp activeProp)
  290. {
  291. EventManager.EmitEvent(EventEnum.OnPlayerPickUpProp, activeProp);
  292. }
  293.  
  294. protected override void OnRemoveActiveProp(ActiveProp activeProp)
  295. {
  296. EventManager.EmitEvent(EventEnum.OnPlayerRemoveProp, activeProp);
  297. }
  298.  
  299. protected override void OnPickUpBuffProp(BuffProp buffProp)
  300. {
  301. EventManager.EmitEvent(EventEnum.OnPlayerPickUpProp, buffProp);
  302. }
  303.  
  304. protected override void OnRemoveBuffProp(BuffProp buffProp)
  305. {
  306. EventManager.EmitEvent(EventEnum.OnPlayerRemoveProp, buffProp);
  307. }
  308.  
  309. /// <summary>
  310. /// 处理角色移动的输入
  311. /// </summary>
  312. public void HandleMoveInput(float delta)
  313. {
  314. var dir = InputManager.MoveAxis;
  315. // 移动. 如果移动的数值接近0(是用 摇杆可能出现 方向 可能会出现浮点),就friction的值 插值 到 0
  316. // 如果 有输入 就以当前速度,用acceleration 插值到 对应方向 * 最大速度
  317. if (Mathf.IsZeroApprox(dir.X))
  318. {
  319. BasisVelocity = new Vector2(Mathf.MoveToward(BasisVelocity.X, 0, RoleState.Friction * delta), BasisVelocity.Y);
  320. }
  321. else
  322. {
  323. BasisVelocity = new Vector2(Mathf.MoveToward(BasisVelocity.X, dir.X * RoleState.MoveSpeed, RoleState.Acceleration * delta), BasisVelocity.Y);
  324. }
  325.  
  326. if (Mathf.IsZeroApprox(dir.Y))
  327. {
  328. BasisVelocity = new Vector2(BasisVelocity.X, Mathf.MoveToward(BasisVelocity.Y, 0, RoleState.Friction * delta));
  329. }
  330. else
  331. {
  332. BasisVelocity = new Vector2(BasisVelocity.X, Mathf.MoveToward(BasisVelocity.Y, dir.Y * RoleState.MoveSpeed, RoleState.Acceleration * delta));
  333. }
  334. }
  335.  
  336. /// <summary>
  337. /// 翻滚结束
  338. /// </summary>
  339. public void OverRoll()
  340. {
  341. _rollCoolingTimer = RoleState.RollTime;
  342. }
  343. // protected override void DebugDraw()
  344. // {
  345. // base.DebugDraw();
  346. // DrawArc(GetLocalMousePosition(), 25, 0, Mathf.Pi * 2f, 20, Colors.Red, 1);
  347. // }
  348. }