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