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; }
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) { 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); } } }