Ejemplo n.º 1
0
        public void UpdateViewEntity()
        {
            List <int> curViewEntities = NetManager.Instance.server.entityManager.GetAroundEntity(inChunkPos, viewWidth);

            for (int i = 0; i < curViewEntities.Count; i++)
            {
                //如果原先已经包含了该玩家,那么跳过
                if (viewEntities.Contains(curViewEntities[i]))
                {
                    RemoveViewEntity(curViewEntities[i]);
                    continue;
                }
                else
                {
                    //如果看见其他怪物了,其他怪物的应用需要更新
                    ClientEntity otherEntity = NetManager.Instance.server.entityManager.GetEntity(curViewEntities[i]);
                    otherEntity.AddViewPlayer(id);

                    //通知当前玩家,有entity进入视野
                    EntityJoinViewPackage entityJoinViewPackage = PackageFactory.GetPackage(PackageType.EntityJoinView)
                                                                  as EntityJoinViewPackage;
                    ClientEntityInfo info = new ClientEntityInfo();
                    info.aoId     = otherEntity.aoId;
                    info.type     = otherEntity.type;
                    info.entityId = otherEntity.entityId;
                    info.position = otherEntity.position;
                    info.extData  = otherEntity.extData;
                    info.roleId   = otherEntity.hostPlayer == null ? -1 : otherEntity.hostPlayer.id;
                    entityJoinViewPackage.info = info;
                    this.worker.SendPackage(entityJoinViewPackage);

                    //更新当前entity所属
                    otherEntity.CheckViewHold();
                }
            }

            //剩下的怪物都是看不见的了
            for (int i = 0; i < viewEntities.Count; i++)
            {
                ClientEntity otherEntity = NetManager.Instance.server.entityManager.GetEntity(viewEntities[i]);
                otherEntity.RemoveViewPlayer(id);

                //通知玩家,有entity退出视野
                EntityLeaveViewPackage entityLeaveViewPackage = PackageFactory.GetPackage(PackageType.EntityLeaveView)
                                                                as EntityLeaveViewPackage;
                entityLeaveViewPackage.aoId = otherEntity.aoId;
                entityLeaveViewPackage.type = otherEntity.type;
                this.worker.SendPackage(entityLeaveViewPackage);

                //更新当前entity所属
                otherEntity.CheckViewHold();
            }
            //最后更新自己能看见的人
            viewEntities = curViewEntities;
        }
Ejemplo n.º 2
0
        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)
        {
            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);
            }
        }