RoomSystem.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. namespace ET.Server
  4. {
  5. [FriendOf(typeof(Room))]
  6. public static class RoomSystem
  7. {
  8. [ObjectSystem]
  9. public class RoomAwakeSystem : AwakeSystem<Room, Player>
  10. {
  11. protected override void Awake(Room self, Player owner)
  12. {
  13. Log.Info($"创建房间实体...");
  14. self.RoomId = RandomGenerator.RandRoomId();
  15. // todo 暂时2个人先测试
  16. self.MaxNum = 2;
  17. self.Type = 1;
  18. self.OwnerId = owner.Id;
  19. self.CreateTime = TimeHelper.ServerNow();
  20. // 添加房间玩家集合
  21. self.Players.Add(owner.Id, owner);
  22. // 添加麻将主逻辑组件
  23. switch (self.Type)
  24. {
  25. case 1:
  26. // 黄冈晃晃
  27. self.AddComponent<HGHuangHuangComponent>();
  28. break;
  29. case 2:
  30. break;
  31. }
  32. // 添加本地房间数据
  33. self.DomainScene().GetComponent<GameRoomComponent>().Add(self);
  34. }
  35. }
  36. [ObjectSystem]
  37. public class RoomDestroySystem : DestroySystem<Room>
  38. {
  39. protected override void Destroy(Room self)
  40. {
  41. Log.Info($"销毁房间实体...");
  42. // 移除本地房间数据
  43. self.DomainScene().GetComponent<GameRoomComponent>()?.Remove(self.RoomId);
  44. }
  45. }
  46. /// <summary>
  47. /// 玩家是否已满
  48. /// </summary>
  49. /// <param name="self"></param>
  50. public static bool PlayerIsMax(this Room self)
  51. {
  52. if (self.Players.Count >= self.MaxNum)
  53. {
  54. return true;
  55. }
  56. return false;
  57. }
  58. /// <summary>
  59. /// 获取房间内所有玩家
  60. /// </summary>
  61. /// <param name="self"></param>
  62. /// <returns></returns>
  63. public static Dictionary<long, Player> GetAllPlayers(this Room self)
  64. {
  65. return self.Players;
  66. }
  67. /// <summary>
  68. /// 加入一个玩家
  69. /// </summary>
  70. /// <param name="self"></param>
  71. /// <param name="player"></param>
  72. public static void Add(this Room self, Player player)
  73. {
  74. self.Players.Add(player.Id, player);
  75. HGHuangHuangComponent hgHuangHuangComponent = self.GetComponent<HGHuangHuangComponent>();
  76. if (hgHuangHuangComponent == null)
  77. {
  78. return;
  79. }
  80. // 初始化玩家
  81. int index = 0;
  82. foreach (Player p in self.GetAllPlayers().Values.Where(p => p != null))
  83. {
  84. hgHuangHuangComponent.Players[index] = p;
  85. index++;
  86. }
  87. }
  88. /// <summary>
  89. /// 移除一个玩家
  90. /// </summary>
  91. /// <param name="self"></param>
  92. /// <param name="playerId"></param>
  93. public static void Remove(this Room self, long playerId)
  94. {
  95. self.Players.Remove(playerId);
  96. }
  97. /// <summary>
  98. /// 是否玩家都已准备,可开始
  99. /// </summary>
  100. /// <param name="self"></param>
  101. /// <returns></returns>
  102. public static bool IsStart(this Room self)
  103. {
  104. int cnt = 0;
  105. foreach (Player player in self.Players.Values)
  106. {
  107. if (player is { State: 1 })
  108. {
  109. cnt += 1;
  110. }
  111. }
  112. if (cnt >= self.MaxNum)
  113. {
  114. return true;
  115. }
  116. return false;
  117. }
  118. /// <summary>
  119. /// 根据当前玩家位置,获取另外三家位置顺序
  120. /// </summary>
  121. /// <param name="self"></param>
  122. /// <param name="pos"></param>
  123. /// <returns></returns>
  124. public static int[] GetSorcPos(this Room self, int pos)
  125. {
  126. int[] nextPos = pos switch
  127. {
  128. 3 => new [] { 0, 1, 2 },
  129. 2 => new [] { 3, 0, 1 },
  130. 1 => new [] { 2, 3, 0 },
  131. 0 => new [] { 1, 2, 3 },
  132. _ => null
  133. };
  134. return nextPos;
  135. }
  136. }
  137. }