1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- using System.Collections.Generic;
- using System.Linq;
- namespace ET.Server
- {
- [FriendOf(typeof (SessionPlayerComponent))]
- public static class SessionPlayerComponentSystem
- {
- public class SessionPlayerComponentAwakeSystem: AwakeSystem<SessionPlayerComponent>
- {
- protected override void Awake(SessionPlayerComponent self)
- {
- self.PlayerList = new List<PlayerInfo>();
- self.PlayerId = 0;
- }
- }
- public class SessionPlayerComponentDestroySystem: DestroySystem<SessionPlayerComponent>
- {
- protected override void Destroy(SessionPlayerComponent self)
- {
- WNPlayer player = self.GetMyPlayer();
- if (player == null)
- {
- return;
- }
- Log.Info($"玩家断线了, playerId={self.PlayerId}, name={player.GetName()}");
- // 玩家离开
- player.GetComponent<PlayerTempDataComponent>().MapData.ready = false;
- Map map = player.Map;
- if (map != null)
- {
- // 记录玩家历史
- map.SyncPlayerHistoryData(player);
- // 场景移除玩家
- map.RemovePlayer(player, false);
- map.PlayerLeaveRequest(player, false);
- }
- self.DomainScene().GetComponent<GamePlayerComponent>().Remove(player.GetId());
- player.Dispose();
- }
- }
- /// <summary>
- /// 是否存在
- /// </summary>
- /// <param name="self"></param>
- /// <param name="playerId"></param>
- /// <returns></returns>
- public static bool IsExist(this SessionPlayerComponent self, long playerId)
- {
- return self.PlayerList.Any(info => info != null && info.PlayerId == playerId);
- }
- /// <summary>
- /// 获取
- /// </summary>
- /// <param name="self"></param>
- /// <param name="playerId"></param>
- /// <returns></returns>
- public static PlayerInfo Get(this SessionPlayerComponent self, long playerId)
- {
- return self.PlayerList.FirstOrDefault(info => info != null && info.PlayerId == playerId);
- }
- /// <summary>
- /// 添加
- /// </summary>
- /// <param name="self"></param>
- /// <param name="info"></param>
- public static void Add(this SessionPlayerComponent self, PlayerInfo info)
- {
- if (info != null)
- {
- self.PlayerList.Add(info);
- }
- }
- /// <summary>
- /// 获取玩家实体
- /// </summary>
- /// <param name="self"></param>
- /// <returns></returns>
- public static WNPlayer GetMyPlayer(this SessionPlayerComponent self)
- {
- return self.DomainScene().GetComponent<GamePlayerComponent>().Get(self.PlayerId);
- }
- }
- }
|