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