123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168 |
- using System.Collections.Generic;
- using System.Linq;
- namespace ET.Server
- {
- [FriendOf(typeof(Room))]
- public static class RoomSystem
- {
- [ObjectSystem]
- public class RoomAwakeSystem : AwakeSystem<Room, Player>
- {
- protected override void Awake(Room self, Player owner)
- {
- Log.Info($"创建房间实体...");
- self.RoomId = RandomGenerator.RandRoomId();
- // todo 暂时2个人先测试
- self.MaxNum = 2;
- self.Type = 1;
- self.OwnerId = owner.Id;
- // todo 暂时8局先测试
- self.MaxRound = 8;
- self.CreateTime = TimeHelper.ServerNow();
- // 添加房间玩家集合
- self.Players.Add(owner.Id, owner);
- // 添加麻将主逻辑组件
- switch (self.Type)
- {
- case 1:
- // 黄冈晃晃
- self.AddComponent<HGHHComponent>();
- break;
- case 2:
- break;
- }
-
- // 添加本地房间数据
- self.DomainScene().GetComponent<GameRoomComponent>().Add(self);
- }
- }
-
- [ObjectSystem]
- public class RoomDestroySystem : DestroySystem<Room>
- {
- protected override void Destroy(Room self)
- {
- Log.Info($"销毁房间实体...");
- // 移除本地房间数据
- self.DomainScene().GetComponent<GameRoomComponent>()?.Remove(self.RoomId);
- }
- }
-
- /// <summary>
- /// 玩家是否已满
- /// </summary>
- /// <param name="self"></param>
- public static bool PlayerIsMax(this Room self)
- {
- if (self.Players.Count >= self.MaxNum)
- {
- return true;
- }
- return false;
- }
-
- /// <summary>
- /// 获取房间内所有玩家
- /// </summary>
- /// <param name="self"></param>
- /// <returns></returns>
- public static Dictionary<long, Player> GetAllPlayers(this Room self)
- {
- return self.Players;
- }
- /// <summary>
- /// 加入一个玩家
- /// </summary>
- /// <param name="self"></param>
- /// <param name="player"></param>
- public static void Add(this Room self, Player player)
- {
- self.Players.Add(player.Id, player);
- HGHHComponent hgHuangHuangComponent = self.GetComponent<HGHHComponent>();
- if (hgHuangHuangComponent == null)
- {
- return;
- }
- // 初始化玩家座位
- int index = 0;
- foreach (Player p in self.GetAllPlayers().Values.Where(p => p != null))
- {
- p.Pos = index;
- hgHuangHuangComponent.Players[index] = p;
- index += 1;
- }
- }
-
- /// <summary>
- /// 移除一个玩家
- /// </summary>
- /// <param name="self"></param>
- /// <param name="playerId"></param>
- public static void Remove(this Room self, long playerId)
- {
- self.Players.Remove(playerId);
- }
- /// <summary>
- /// 获取房间内玩家
- /// </summary>
- /// <param name="self"></param>
- /// <param name="playerId"></param>
- /// <returns></returns>
- public static Player GetPlayer(this Room self, long playerId)
- {
- return self.Players.GetValueOrDefault(playerId);
- }
- /// <summary>
- /// 是否玩家都已准备,可开始
- /// </summary>
- /// <param name="self"></param>
- /// <returns></returns>
- public static bool IsStart(this Room self)
- {
- int cnt = 0;
-
- foreach (Player player in self.Players.Values)
- {
- if (player is { State: 1 })
- {
- cnt += 1;
- }
- }
- if (cnt >= self.MaxNum)
- {
- return true;
- }
-
- return false;
- }
- /// <summary>
- /// 根据当前玩家位置,获取另外三家位置顺序
- /// </summary>
- /// <param name="self"></param>
- /// <param name="pos"></param>
- /// <returns></returns>
- public static int[] GetSorcPos(this Room self, int pos)
- {
- int[] nextPos = pos switch
- {
- // 3 => new [] { 0, 1, 2 },
- // 2 => new [] { 3, 0, 1 },
- // 1 => new [] { 2, 3, 0 },
- // 0 => new [] { 1, 2, 3 },
- // todo 正式版此处改为4人
- 1 => new [] { 0 },
- 0 => new [] { 1 },
- _ => null
- };
- return nextPos;
- }
- }
- }
|