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