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 Dispose() { if (player != null) { player.Dispose(); //移除当前玩家 NetManager.Instance.server.playerManager.RemovePlayer(player); //向其他玩家发送玩家退出消息 LeaveGamePackage package = PackageFactory.GetPackage(PackageType.LeaveGame) as LeaveGamePackage; package.aoId = player.aoId; NetManager.Instance.server.playerManager.BroadcastPackage(package); } base.Dispose(); }
private IEnumerator ResponseNetChunk() { while (true) { NetChunkData netChunkData = world.WorldGenerator.DataProcessorManager.DeResponseNetChunkData(); if (netChunkData != null) { ResponseChunkPackage package = PackageFactory.GetPackage(PackageType.ResponseChunk) as ResponseChunkPackage; package.roleId = netChunkData.roleId; package.pos = netChunkData.worldPos; package.isExit = netChunkData.isExist; package.compressType = (byte)netChunkData.data.compressType; package.chunkByteData = netChunkData.data.data; NetManager.Instance.client.SendPackage(package); } yield return(null); } }
private void UpdateViewPlayer() { List <int> curViewPlayers = NetManager.Instance.server.playerManager.GetAroundPlayer(inChunkPos, ClientPlayer.VIEW_WIDTH); for (int i = 0; i < curViewPlayers.Count; i++) { if (viewPlayers.Contains(curViewPlayers[i])) { RemoveViewPlayer(curViewPlayers[i]); } else { ClientPlayer otherPlayer = NetManager.Instance.server.playerManager.GetPlayer(curViewPlayers[i]); otherPlayer.AddViewEntity(aoId); //通知玩家,有entity进入视野 EntityJoinViewPackage entityJoinViewPackage = PackageFactory.GetPackage(PackageType.EntityJoinView) as EntityJoinViewPackage; ClientEntityInfo info = new ClientEntityInfo(); info.aoId = aoId; info.type = type; info.entityId = entityId; info.position = position; info.extData = extData; info.roleId = hostPlayer == null ? -1 : hostPlayer.id; entityJoinViewPackage.info = info; otherPlayer.worker.SendPackage(entityJoinViewPackage); } } for (int i = 0; i < viewPlayers.Count; i++) { ClientPlayer otherPlayer = NetManager.Instance.server.playerManager.GetPlayer(viewPlayers[i]); otherPlayer.RemoveViewEntity(aoId); //通知玩家,有entity退出视野 EntityLeaveViewPackage entityLeaveViewPackage = PackageFactory.GetPackage(PackageType.EntityLeaveView) as EntityLeaveViewPackage; entityLeaveViewPackage.aoId = aoId; entityLeaveViewPackage.type = type; otherPlayer.worker.SendPackage(entityLeaveViewPackage); } viewPlayers = curViewPlayers; //检测当前entity是否退出玩家视野,如果是的话,更改entity所属 CheckViewHold(); }
public override void ServerDo(ClientConnectionWorker connectionWork) { ResponseRemoveChunkExtDataPackage package = PackageFactory.GetPackage(PackageType.ResponseRemoveChunkExtData) as ResponseRemoveChunkExtDataPackage; package.pos = pos; package.needSave = NetManager.Instance.server.sceneManager.CanchangedChunkSaved(pos); package.entities = new List <ClientEntityInfo>(); List <ClientEntity> list = NetManager.Instance.server.entityManager.GetEntitiesInChunk(pos); if (list != null) { for (int i = 0; i < list.Count; i++) { package.entities.Add(list[i].GetClientEntityInfo()); } } connectionWork.SendPackage(package); }
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); } }
public override void ServerDo(ClientConnectionWorker connectionWork) { for (int i = 0; i < entityInfos.Count; i++) { NetManager.Instance.server.entityManager.InitEntity(entityInfos[i]); } //可是entity全部交由服务器来管理,因此,客户端的某些怪物可能需要移掉(不在视野范围之内) for (int i = 0; i < entityInfos.Count; i++) { ClientEntity clientEntity = NetManager.Instance.server.entityManager.GetEntity(entityInfos[i].aoId); //检测归属变更 clientEntity.CheckViewHold(); if (clientEntity.viewPlayers.Count <= 0) { EntityLeaveViewPackage leaveViewPackage = PackageFactory.GetPackage(PackageType.EntityLeaveView) as EntityLeaveViewPackage; leaveViewPackage.aoId = clientEntity.aoId; leaveViewPackage.type = clientEntity.type; connectionWork.SendPackage(leaveViewPackage); } } }
//(联机使用) public void GenerateFromServer(int x, int y, int z) { WorldPos pos = new WorldPos(x, y, z); if (_world.chunks.ContainsKey(pos)) { return; } Chunk newChunk = _world.CreateNewChunk(pos); if (newChunk == null) { return; } _world.chunks.Add(newChunk.worldPos, newChunk); RequestChunkPackage package = PackageFactory.GetPackage(PackageType.ResquestChunk) as RequestChunkPackage; package.roleId = NetManager.Instance.client.roleId; package.pos = pos; NetManager.Instance.client.SendPackage(package); }
public override void ServerDo(ClientConnectionWorker connectionWork) { if (NetManager.Instance.server.sceneManager.RefreshEntity(pos)) { List <int> listRefershEntity = new List <int>(); for (int i = 0; i < entities.Count; i++) { int aoId = AoIdManager.instance.getAoId(); ClientEntityInfo info = new ClientEntityInfo(); info.aoId = aoId; info.entityId = entities[i].id; info.type = entities[i].type; info.extData = entities[i].exData; info.position = entities[i].pos; info.roleId = connectionWork.player.id; NetManager.Instance.server.entityManager.InitEntity(info); listRefershEntity.Add(aoId); } //全部刷新完成后,通知持有该块的玩家更改刷新标志,并通知其刷新怪物 ChunkSignChangedPackage signChangedPackage = PackageFactory.GetPackage(PackageType.ChunkSignChanged) as ChunkSignChangedPackage; signChangedPackage.pos = pos; signChangedPackage.sign = NetManager.Instance.server.sceneManager.GetSign(pos); NetManager.Instance.server.sceneManager.BroadcastPlayerHasChunkPackage(connectionWork.player, pos, signChangedPackage, true); for (int i = 0; i < listRefershEntity.Count; i++) { ClientEntity clientEntity = NetManager.Instance.server.entityManager.GetEntity(listRefershEntity[i]); ClientEntityInfo clientEntityInfo = clientEntity.GetClientEntityInfo(); EntityJoinViewPackage entityJoinViewPackage = PackageFactory.GetPackage(PackageType.EntityJoinView) as EntityJoinViewPackage; entityJoinViewPackage.info = clientEntityInfo; for (int j = 0; j < clientEntity.viewPlayers.Count; j++) { ClientPlayer clientPlayer = NetManager.Instance.server.playerManager.GetPlayer(clientEntity.viewPlayers[j]); clientPlayer.worker.SendPackage(entityJoinViewPackage); } } // Debug.Log("请求刷新区块pos:" + pos.ToString() + "的entity成功,刷新数量:" + listRefershEntity.Count + " changedSign:" +signChangedPackage.sign); } }
public void RemoveChunkInLocalAndRequestExtData(Chunk chunk, bool isSave = false) { if (isSave) { if (!_savedChunks.ContainsKey(chunk.worldPos)) { _savedChunks.Add(chunk.worldPos, chunk); } } else { world.chunks.Remove(chunk.worldPos); if (!_removedChunks.ContainsKey(chunk.worldPos)) { _removedChunks.Add(chunk.worldPos, chunk); } } RequestRemoveChunkExtDataPackage package = PackageFactory.GetPackage(PackageType.RequestRemoveChunkExtData) as RequestRemoveChunkExtDataPackage; package.pos = chunk.worldPos; NetManager.Instance.client.SendPackage(package); }
public override void ServerDo(ClientConnectionWorker connectionWork) { ClientPlayer mainPlayer = NetManager.Instance.server.playerManager.mainPlayer; if (mainPlayer.id == roleId) { ResponseChunkPackage package = PackageFactory.GetPackage(PackageType.ResponseChunk) as ResponseChunkPackage; package.roleId = roleId; package.pos = pos; package.isExit = false; ClientChangedChunkData data = NetManager.Instance.server.sceneManager.GetChangedChunkData(pos); if (data != null) { package.hasChangedData = true; package.changedData = data; } mainPlayer.worker.SendPackage(package); } else { mainPlayer.worker.SendPackage(this); } }
//检测当前entity的持有者是否正确,如不正确,会变更 public void CheckViewHold() { if (hostPlayer != null) { if (!viewPlayers.Contains(hostPlayer.id)) { this.hostPlayer = null; } } if (hostPlayer == null) { ClientPlayer player = null; int minDis = int.MaxValue; //找到距离当前怪物最近的玩家,将怪物的主动权交个他 for (int i = 0; i < viewPlayers.Count; i++) { ClientPlayer tempPlayer = NetManager.Instance.server.playerManager.GetPlayer(viewPlayers[i]); int dis = Mathf.Max(Mathf.Abs(tempPlayer.inChunkPos.x - inChunkPos.x), Mathf.Abs(tempPlayer.inChunkPos.z - inChunkPos.z)); if (dis < minDis) { player = tempPlayer; minDis = dis; } } this.hostPlayer = player; if (this.hostPlayer != null) { //通知当前玩家的entity变为本地entity,同步的时候就一当前玩家的entity为主 EntityNetObjChangedPackage netObjChangedPackage = PackageFactory.GetPackage(PackageType.EntityNetObjChanged) as EntityNetObjChangedPackage; netObjChangedPackage.aoId = aoId; netObjChangedPackage.type = type; this.hostPlayer.worker.SendPackage(netObjChangedPackage); } } }
public override void ServerDo(ClientConnectionWorker connectionWork) { UnityEngine.Debug.Log("收到客户端:" + this.ip + ":" + this.port + "的连接请求!"); connectionWork.SendPackage(PackageFactory.GetPackage(this.id)); }