Newer
Older
DungeonShooting / DungeonShooting_Godot / src / game / activity / role / Player.cs
@lijincheng lijincheng on 25 Jul 2023 7 KB 调整部分 Ui 函数名称
  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.  
  15. /// <summary>
  16. /// 设置当前操作的玩家对象
  17. /// </summary>
  18. public static void SetCurrentPlayer(Player player)
  19. {
  20. Current = player;
  21. //设置相机和鼠标跟随玩家
  22. GameCamera.Main.SetFollowTarget(player);
  23. GameApplication.Instance.Cursor.SetMountRole(player);
  24. }
  25. public override void OnInit()
  26. {
  27. base.OnInit();
  28. AttackLayer = PhysicsLayer.Wall | PhysicsLayer.Prop | PhysicsLayer.Enemy;
  29. Camp = CampEnum.Camp1;
  30.  
  31. MaxHp = 6;
  32. Hp = 6;
  33. MaxShield = 0;
  34. Shield = 0;
  35.  
  36. // debug用
  37. // Acceleration = 3000;
  38. // Friction = 3000;
  39. // MoveSpeed = 500;
  40. // CollisionLayer = 0;
  41. // CollisionMask = 0;
  42. // GameCamera.Main.Zoom = new Vector2(0.5f, 0.5f);
  43. }
  44.  
  45. protected override void Process(float delta)
  46. {
  47. if (IsDie)
  48. {
  49. return;
  50. }
  51. base.Process(delta);
  52. //脸的朝向
  53. if (LookTarget == null)
  54. {
  55. var gPos = GlobalPosition;
  56. Vector2 mousePos = InputManager.CursorPosition;
  57. if (mousePos.X > gPos.X && Face == FaceDirection.Left)
  58. {
  59. Face = FaceDirection.Right;
  60. }
  61. else if (mousePos.X < gPos.X && Face == FaceDirection.Right)
  62. {
  63. Face = FaceDirection.Left;
  64. }
  65. //枪口跟随鼠标
  66. MountPoint.SetLookAt(mousePos);
  67. }
  68.  
  69. if (InputManager.ExchangeWeapon) //切换武器
  70. {
  71. ExchangeNextWeapon();
  72. }
  73. else if (InputManager.ThrowWeapon) //扔掉武器
  74. {
  75. ThrowWeapon();
  76.  
  77. // //测试用的, 所有敌人也扔掉武器
  78. // if (Affiliation != null)
  79. // {
  80. // var enemies = Affiliation.FindIncludeItems(o =>
  81. // {
  82. // return o.CollisionWithMask(PhysicsLayer.Enemy);
  83. // });
  84. // foreach (var activityObject in enemies)
  85. // {
  86. // if (activityObject is Enemy enemy)
  87. // {
  88. // enemy.ThrowWeapon();
  89. // }
  90. // }
  91. // }
  92. }
  93. else if (InputManager.Interactive) //互动物体
  94. {
  95. TriggerInteractive();
  96. }
  97. else if (InputManager.Reload) //换弹
  98. {
  99. Reload();
  100. }
  101.  
  102. if (InputManager.Fire) //开火
  103. {
  104. Attack();
  105. }
  106.  
  107. if (InputManager.UseActiveProp) //使用道具
  108. {
  109. UseActiveProp();
  110. }
  111. else if (InputManager.RemoveProp) //扔掉道具
  112. {
  113. ThrowActiveProp();
  114. }
  115.  
  116. if (Input.IsKeyPressed(Key.P))
  117. {
  118. Hurt(1000, 0);
  119. }
  120. }
  121.  
  122. protected override void PhysicsProcess(float delta)
  123. {
  124. if (IsDie)
  125. {
  126. return;
  127. }
  128.  
  129. base.PhysicsProcess(delta);
  130. HandleMoveInput(delta);
  131. //播放动画
  132. PlayAnim();
  133. }
  134.  
  135. protected override void OnPickUpWeapon(Weapon weapon)
  136. {
  137. EventManager.EmitEvent(EventEnum.OnPlayerPickUpWeapon, weapon);
  138. }
  139.  
  140. protected override void OnThrowWeapon(Weapon weapon)
  141. {
  142. EventManager.EmitEvent(EventEnum.OnPlayerRemoveWeapon, weapon);
  143. }
  144.  
  145. protected override int OnHandlerHurt(int damage)
  146. {
  147. //修改受到的伤害, 每次只受到1点伤害
  148. return 1;
  149. }
  150.  
  151. protected override void OnHit(int damage, bool realHarm)
  152. {
  153. //进入无敌状态
  154. if (realHarm) //真实伤害
  155. {
  156. PlayInvincibleFlashing(RoleState.WoundedInvincibleTime);
  157. }
  158. else //护盾抵消掉的
  159. {
  160. PlayInvincibleFlashing(RoleState.ShieldInvincibleTime);
  161. }
  162. }
  163.  
  164. protected override void OnChangeHp(int hp)
  165. {
  166. //GameApplication.Instance.Ui.SetHp(hp);
  167. EventManager.EmitEvent(EventEnum.OnPlayerHpChange, hp);
  168. }
  169.  
  170. protected override void OnChangeMaxHp(int maxHp)
  171. {
  172. //GameApplication.Instance.Ui.SetMaxHp(maxHp);
  173. EventManager.EmitEvent(EventEnum.OnPlayerMaxHpChange, maxHp);
  174. }
  175.  
  176. protected override void ChangeInteractiveItem(CheckInteractiveResult prev, CheckInteractiveResult result)
  177. {
  178. if (prev != null && prev.Target.ShowOutline)
  179. {
  180. prev.Target.OutlineColor = Colors.Black;
  181. }
  182. if (result != null && result.Target.ShowOutline)
  183. {
  184. result.Target.OutlineColor = Colors.White;
  185. }
  186. //派发互动对象改变事件
  187. EventManager.EmitEvent(EventEnum.OnPlayerChangeInteractiveItem, result);
  188. }
  189.  
  190. protected override void OnChangeShield(int shield)
  191. {
  192. //GameApplication.Instance.Ui.SetShield(shield);
  193. EventManager.EmitEvent(EventEnum.OnPlayerShieldChange, shield);
  194. }
  195.  
  196. protected override void OnChangeMaxShield(int maxShield)
  197. {
  198. //GameApplication.Instance.Ui.SetMaxShield(maxShield);
  199. EventManager.EmitEvent(EventEnum.OnPlayerMaxShieldChange, maxShield);
  200. }
  201.  
  202. protected override void OnDie()
  203. {
  204. GameCamera.Main.SetFollowTarget(null);
  205. BasisVelocity = Vector2.Zero;
  206. MoveController.ClearForce();
  207. UiManager.Open_Settlement();
  208. }
  209.  
  210. protected override void OnPickUpActiveProp(ActiveProp activeProp)
  211. {
  212. EventManager.EmitEvent(EventEnum.OnPlayerPickUpProp, activeProp);
  213. }
  214.  
  215. protected override void OnRemoveActiveProp(ActiveProp activeProp)
  216. {
  217. EventManager.EmitEvent(EventEnum.OnPlayerRemoveProp, activeProp);
  218. }
  219.  
  220. protected override void OnPickUpBuffProp(BuffProp buffProp)
  221. {
  222. EventManager.EmitEvent(EventEnum.OnPlayerPickUpProp, buffProp);
  223. }
  224.  
  225. protected override void OnRemoveBuffProp(BuffProp buffProp)
  226. {
  227. EventManager.EmitEvent(EventEnum.OnPlayerRemoveProp, buffProp);
  228. }
  229.  
  230. //处理角色移动的输入
  231. private void HandleMoveInput(float delta)
  232. {
  233. //角色移动
  234. Vector2 dir = InputManager.MoveAxis;
  235. // 移动. 如果移动的数值接近0(是用 摇杆可能出现 方向 可能会出现浮点),就friction的值 插值 到 0
  236. // 如果 有输入 就以当前速度,用acceleration 插值到 对应方向 * 最大速度
  237. if (Mathf.IsZeroApprox(dir.X))
  238. {
  239. BasisVelocity = new Vector2(Mathf.MoveToward(BasisVelocity.X, 0, RoleState.Friction * delta), BasisVelocity.Y);
  240. }
  241. else
  242. {
  243. BasisVelocity = new Vector2(Mathf.MoveToward(BasisVelocity.X, dir.X * RoleState.MoveSpeed, RoleState.Acceleration * delta),
  244. BasisVelocity.Y);
  245. }
  246.  
  247. if (Mathf.IsZeroApprox(dir.Y))
  248. {
  249. BasisVelocity = new Vector2(BasisVelocity.X, Mathf.MoveToward(BasisVelocity.Y, 0, RoleState.Friction * delta));
  250. }
  251. else
  252. {
  253. BasisVelocity = new Vector2(BasisVelocity.X,
  254. Mathf.MoveToward(BasisVelocity.Y, dir.Y * RoleState.MoveSpeed, RoleState.Acceleration * delta));
  255. }
  256. }
  257.  
  258. // 播放动画
  259. private void PlayAnim()
  260. {
  261. if (BasisVelocity != Vector2.Zero)
  262. {
  263. if ((Face == FaceDirection.Right && BasisVelocity.X >= 0) || Face == FaceDirection.Left && BasisVelocity.X <= 0) //向前走
  264. {
  265. AnimatedSprite.Play(AnimatorNames.Run);
  266. }
  267. else if ((Face == FaceDirection.Right && BasisVelocity.X < 0) || Face == FaceDirection.Left && BasisVelocity.X > 0) //向后走
  268. {
  269. AnimatedSprite.Play(AnimatorNames.ReverseRun);
  270. }
  271. }
  272. else
  273. {
  274. AnimatedSprite.Play(AnimatorNames.Idle);
  275. }
  276. }
  277. }