Newer
Older
DungeonShooting / DungeonShooting_Godot / src / game / activity / role / Player.cs
  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.  
  132. public override bool PickUpWeapon(Weapon weapon, bool exchange = true)
  133. {
  134. //拾起武器
  135. var result = base.PickUpWeapon(weapon, exchange);
  136. if (result)
  137. {
  138. EventManager.EmitEvent(EventEnum.OnPlayerPickUpWeapon, weapon);
  139. }
  140. return result;
  141. }
  142.  
  143. public override void ThrowWeapon()
  144. {
  145. //扔掉武器
  146. var weapon = Holster.ActiveWeapon;
  147. base.ThrowWeapon();
  148. EventManager.EmitEvent(EventEnum.OnPlayerRemoveWeapon, weapon);
  149. }
  150.  
  151. public override void ThrowWeapon(int index)
  152. {
  153. //扔掉武器
  154. var weapon = Holster.GetWeapon(index);
  155. base.ThrowWeapon(index);
  156. EventManager.EmitEvent(EventEnum.OnPlayerRemoveWeapon, weapon);
  157. }
  158.  
  159. protected override int OnHandlerHurt(int damage)
  160. {
  161. //修改受到的伤害, 每次只受到1点伤害
  162. return 1;
  163. }
  164.  
  165. protected override void OnHit(int damage, bool realHarm)
  166. {
  167. //进入无敌状态
  168. if (realHarm) //真实伤害
  169. {
  170. PlayInvincibleFlashing(RoleState.WoundedInvincibleTime);
  171. }
  172. else //护盾抵消掉的
  173. {
  174. PlayInvincibleFlashing(RoleState.ShieldInvincibleTime);
  175. }
  176. }
  177.  
  178. protected override void OnChangeHp(int hp)
  179. {
  180. //GameApplication.Instance.Ui.SetHp(hp);
  181. EventManager.EmitEvent(EventEnum.OnPlayerHpChange, hp);
  182. }
  183.  
  184. protected override void OnChangeMaxHp(int maxHp)
  185. {
  186. //GameApplication.Instance.Ui.SetMaxHp(maxHp);
  187. EventManager.EmitEvent(EventEnum.OnPlayerMaxHpChange, maxHp);
  188. }
  189.  
  190. protected override void ChangeInteractiveItem(CheckInteractiveResult prev, CheckInteractiveResult result)
  191. {
  192. if (prev != null && prev.Target.ShowOutline)
  193. {
  194. prev.Target.OutlineColor = Colors.Black;
  195. }
  196. if (result != null && result.Target.ShowOutline)
  197. {
  198. result.Target.OutlineColor = Colors.White;
  199. }
  200. //派发互动对象改变事件
  201. EventManager.EmitEvent(EventEnum.OnPlayerChangeInteractiveItem, result);
  202. }
  203.  
  204. protected override void OnChangeShield(int shield)
  205. {
  206. //GameApplication.Instance.Ui.SetShield(shield);
  207. EventManager.EmitEvent(EventEnum.OnPlayerShieldChange, shield);
  208. }
  209.  
  210. protected override void OnChangeMaxShield(int maxShield)
  211. {
  212. //GameApplication.Instance.Ui.SetMaxShield(maxShield);
  213. EventManager.EmitEvent(EventEnum.OnPlayerMaxShieldChange, maxShield);
  214. }
  215.  
  216. protected override void OnDie()
  217. {
  218. GameCamera.Main.SetFollowTarget(null);
  219. BasisVelocity = Vector2.Zero;
  220. MoveController.ClearForce();
  221. UiManager.Open_Settlement();
  222. //GameApplication.Instance.World.ProcessMode = ProcessModeEnum.WhenPaused;
  223. }
  224.  
  225. //处理角色移动的输入
  226. private void HandleMoveInput(float delta)
  227. {
  228. //角色移动
  229. Vector2 dir = InputManager.MoveAxis;
  230. // 移动. 如果移动的数值接近0(是用 摇杆可能出现 方向 可能会出现浮点),就friction的值 插值 到 0
  231. // 如果 有输入 就以当前速度,用acceleration 插值到 对应方向 * 最大速度
  232. if (Mathf.IsZeroApprox(dir.X))
  233. {
  234. BasisVelocity = new Vector2(Mathf.MoveToward(BasisVelocity.X, 0, RoleState.Friction * delta), BasisVelocity.Y);
  235. }
  236. else
  237. {
  238. BasisVelocity = new Vector2(Mathf.MoveToward(BasisVelocity.X, dir.X * RoleState.MoveSpeed, RoleState.Acceleration * delta),
  239. BasisVelocity.Y);
  240. }
  241.  
  242. if (Mathf.IsZeroApprox(dir.Y))
  243. {
  244. BasisVelocity = new Vector2(BasisVelocity.X, Mathf.MoveToward(BasisVelocity.Y, 0, RoleState.Friction * delta));
  245. }
  246. else
  247. {
  248. BasisVelocity = new Vector2(BasisVelocity.X,
  249. Mathf.MoveToward(BasisVelocity.Y, dir.Y * RoleState.MoveSpeed, RoleState.Acceleration * delta));
  250. }
  251. }
  252.  
  253. // 播放动画
  254. private void PlayAnim()
  255. {
  256. if (BasisVelocity != Vector2.Zero)
  257. {
  258. if ((Face == FaceDirection.Right && BasisVelocity.X >= 0) || Face == FaceDirection.Left && BasisVelocity.X <= 0) //向前走
  259. {
  260. AnimatedSprite.Play(AnimatorNames.Run);
  261. }
  262. else if ((Face == FaceDirection.Right && BasisVelocity.X < 0) || Face == FaceDirection.Left && BasisVelocity.X > 0) //向后走
  263. {
  264. AnimatedSprite.Play(AnimatorNames.ReverseRun);
  265. }
  266. }
  267. else
  268. {
  269. AnimatedSprite.Play(AnimatorNames.Idle);
  270. }
  271. }
  272. }