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