private void UpdateViewPlayer() { //更新当前人物的视野与其他人物的视野 List <int> curViewPlayers = NetManager.Instance.server.playerManager.GetAroundPlayer(this, viewWidth, false); for (int i = 0; i < curViewPlayers.Count; i++) { //如果原先已经包含了该玩家,那么跳过 if (viewPlayers.Contains(curViewPlayers[i])) { RemoveViewPlayer(curViewPlayers[i]); continue; } else { //如果看见其他人了,其他人也能看到自己 ClientPlayer otherPlayer = NetManager.Instance.server.playerManager.GetPlayer(curViewPlayers[i]); otherPlayer.AddViewPlayer(id); //通知其他人我进入他的视野 PlayerJoinViewPackage joinOtherPlayerViewPackage = PackageFactory.GetPackage(PackageType.PlayerJoinView) as PlayerJoinViewPackage; joinOtherPlayerViewPackage.playerId = playerId; joinOtherPlayerViewPackage.aoId = aoId; joinOtherPlayerViewPackage.birthPlace = position; otherPlayer.worker.SendPackage(joinOtherPlayerViewPackage); //通知我其他人进入我的视野 PlayerJoinViewPackage otherPlayerJoinViewPackage = PackageFactory.GetPackage(PackageType.PlayerJoinView) as PlayerJoinViewPackage; otherPlayerJoinViewPackage.playerId = otherPlayer.playerId; otherPlayerJoinViewPackage.aoId = otherPlayer.aoId; otherPlayerJoinViewPackage.birthPlace = otherPlayer.position; this.worker.SendPackage(otherPlayerJoinViewPackage); } } //剩下的人都是看不见的了 for (int i = 0; i < viewPlayers.Count; i++) { ClientPlayer otherPlayer = NetManager.Instance.server.playerManager.GetPlayer(viewPlayers[i]); otherPlayer.RemoveViewPlayer(id); //通知其他人我离开了他的视野 PlayerLeaveViewPackage leaveOtherPlayerPackage = PackageFactory.GetPackage(PackageType.PlayerLeaveView) as PlayerLeaveViewPackage; leaveOtherPlayerPackage.aoId = aoId; otherPlayer.worker.SendPackage(leaveOtherPlayerPackage); //通知我其他人离开了我的视野 PlayerLeaveViewPackage otherPlayerLeavePackage = PackageFactory.GetPackage(PackageType.PlayerLeaveView) as PlayerLeaveViewPackage; otherPlayerLeavePackage.aoId = otherPlayer.aoId; this.worker.SendPackage(otherPlayerLeavePackage); } //最后更新自己能看见的人 viewPlayers = curViewPlayers; }
public override void ServerDo(ClientConnectionWorker connectionWork) { //在服务器中初始化当前加入玩家的信息 int aoId = AoIdManager.instance.getAoId(); Vector3 birthPlace = NetManager.Instance.server.playerManager.mainPlayer.position; ClientPlayerInfo playerInfo = new ClientPlayerInfo(); playerInfo.id = roleId; playerInfo.aoId = aoId; playerInfo.position = birthPlace; playerInfo.playerId = playerId; playerInfo.configName = NetManager.Instance.server.playerManager.mainPlayer.configName; playerInfo.seed = NetManager.Instance.server.playerManager.mainPlayer.seed; connectionWork.InitPlayer(playerInfo); NetManager.Instance.server.playerManager.AddPlayer(connectionWork.player); //通知自己进入游戏,并返回一些配置 ClientJoinGamePackage package = PackageFactory.GetPackage(PackageType.ClientJoinGame) as ClientJoinGamePackage; package.playerId = playerId; package.aoId = aoId; package.birthPlace = birthPlace; package.worldConfigName = NetManager.Instance.server.playerManager.mainPlayer.configName; package.seed = NetManager.Instance.server.playerManager.mainPlayer.seed; package.time = NetManager.Instance.server.gameTime; connectionWork.SendPackage(package); //通知其他人我已进入游戏,并出现在他们视野内 PlayerJoinViewPackage joinViewPackage = PackageFactory.GetPackage(PackageType.PlayerJoinView) as PlayerJoinViewPackage; joinViewPackage.playerId = playerId; joinViewPackage.aoId = aoId; joinViewPackage.birthPlace = birthPlace; List <int> viewPlayers = connectionWork.player.viewPlayers; for (int i = 0; i < viewPlayers.Count; i++) { ClientPlayer otherPlayer = NetManager.Instance.server.playerManager.GetPlayer(viewPlayers[i]); otherPlayer.worker.SendPackage(joinViewPackage); } }