/// <summary> /// 计算玩家位于坐标时应该加载的Area /// </summary> public int CaculateAreaIndex(Vector3 position) { int voxelIndex = VoxelGridInfo.CaculateVoxelIndex(position); VoxelInfo voxel = VoxelGridInfo.VoxelInfos[voxelIndex]; if (voxel.Indexs == null) { return(Constants.NOTSET_AREA_INDEX); } if (voxel.Indexs.Length == 0) { return(Constants.NOTSET_AREA_INDEX); } else { int nearestAreaIndex = Constants.NOTSET_AREA_INDEX; float nearestAreaDistance = float.MaxValue; for (int iArea = 0; iArea < voxel.Indexs.Length; iArea++) { int areaIndex = voxel.Indexs[iArea]; AreaInfo iterAreaInfo = AreaInfos[areaIndex]; float distance = (position - iterAreaInfo.AABB.center).sqrMagnitude / iterAreaInfo.Diameter / iterAreaInfo.Diameter; if (distance < nearestAreaDistance) { nearestAreaDistance = distance; nearestAreaIndex = areaIndex; } } return(nearestAreaIndex); } }
protected void OnDrawGizmosSelected() { if (m_MapInfo == null) { return; } if (m_AreaTransform == null) { m_AreaTransform = GameObject.CreatePrimitive(PrimitiveType.Sphere).transform; m_AreaTransform.SetParent(transform, false); } int areaIndex = m_MapInfo.CaculateAreaIndex(transform.localPosition); if (areaIndex == Constants.NOTSET_AREA_INDEX) { m_AreaTransform.gameObject.SetActive(false); } else { m_AreaTransform.gameObject.SetActive(true); AreaInfo areaInfo = m_MapInfo.AreaInfos[areaIndex]; m_AreaTransform.localScale = Vector3.one * areaInfo.Diameter; m_AreaTransform.position = areaInfo.Position; } }
/// <summary> /// 反序列化 /// </summary> /// <param name="bytes"></param> public void Deserialize(byte[] bytes) { if (bytes == null) { return; } BinaryFile file = new BinaryFile(System.Text.Encoding.Unicode); if (!file.OpenRead(bytes)) { file.Close(); return; } BinaryReader reader = file.m_Reader; this.Uid = reader.ReadUInt32(); this.SceneAddressableKey = reader.ReadString(); int areaInfosLength = reader.ReadInt32(); if (areaInfosLength > 0) { this.AreaInfos = new AreaInfo[areaInfosLength]; for (int iArea = 0; iArea < areaInfosLength; iArea++) { AreaInfo areaInfo = new AreaInfo(); areaInfo.Deserialize(reader); this.AreaInfos[iArea] = areaInfo; } } this.VoxelGridInfo = new VoxelGridInfo(); this.VoxelGridInfo.Deserialize(reader); file.Close(); }
public Vector3 WorldPositionToServerAreaOffsetPosition(Vector3 worldPosition, ulong areaId) { Map.AreaInfo areaInfo = Map.MapManager.GetInstance().FindAreaInfoByUidFromCurrentMap(areaId); if (areaInfo != null) { return(worldPosition - areaInfo.Position); } return(Vector3.zero); }
public void Initialie(MapPreview owner, AreaInfo area) { m_Owner = owner; AreaInfo = area; gameObject.name = Constants.AREA_PREVIEW_GAMEOBJECT_NAME_STARTWITHS + AreaInfo.Uid; transform.SetParent(owner.transform, false); m_UnitsRoot = new GameObject("Units").transform; m_UnitsRoot.SetParent(transform); m_VoxelRoot = new GameObject(Constants.VOXEL_PREVIEW_GAMEOBJECT_NAME_FORMAT).transform; m_VoxelRoot.SetParent(transform); }
public bool ServerAreaOffsetPositionToWorldPosition(out Vector3 worldPosition, Vector3 areaOffsetPosition, ulong areaId) { worldPosition = Vector3.zero; Map.AreaInfo areaInfo = Map.MapManager.GetInstance().FindAreaInfoByUidFromCurrentMap(areaId); if (areaInfo != null) { worldPosition = areaInfo.Position + areaOffsetPosition; return(true); } return(false); }
public void RequestInitialize(MapController owner, AreaInfo areaInfo) { m_AreaRoot = new GameObject(Constants.AREA_GAMEOBJECT_NAME_STARTWITHS + areaInfo.Uid).transform; m_AreaRoot.SetPositionAndRotation(Vector3.zero, areaInfo.Rotation); m_IsReleasing = false; m_Owner = owner; m_AreaInfo = areaInfo; DoUpdate_AreaRoot(true); string areaDetailInfoAddressableKey = string.Format(Constants.AREA_DETAIL_INFO_FILENAME_FORMAT , owner.GetMapUid() , areaInfo.Uid); m_AreaDetailLoaderHandle = AssetUtil.LoadAssetAsync(areaDetailInfoAddressableKey, OnLoadAreaDetailInfoCompleted); }
/// <summary> /// 切换Area /// </summary> private void TryChangeArea(int areaIndex) { if (m_CurrentAreaController != null) { ulong lastAreaUid = GetCurrentAreaUid(); ulong newAreaUid = areaIndex != Constants.NOTSET_AREA_INDEX ? m_MapInfo.AreaInfos[areaIndex].Uid : Constants.NOTSET_AREA_UID; if (lastAreaUid != newAreaUid) { DebugUtility.Log(Constants.LOG_TAG, $"Begin change area from {lastAreaUid} to {newAreaUid}, {MapManager.GetInstance()._PlayerInfo}"); } } if (m_CurrentAreaController != null) { m_CurrentAreaController.RequestRelease(); m_ExpiredAreaControllers.Add(m_CurrentAreaController); m_CurrentAreaController = null; } if (areaIndex != Constants.NOTSET_AREA_INDEX) { AreaInfo targetAraeInfo = m_MapInfo.AreaInfos[areaIndex]; if (m_LimitChangeToAreaUid == Constants.NOT_LIMIT_AREA_UID || m_LimitChangeToAreaUid == targetAraeInfo.Uid) { m_State = State.ChangingArea; m_CurrentAreaController = new AreaController(); m_CurrentAreaController.RequestInitialize(this, m_MapInfo.AreaInfos[areaIndex]); DebugUtility.Log(Constants.LOG_TAG, "Begin invoke on changed area"); try { MapManager.GetInstance()._OnChangedArea?.Invoke(m_MapInfo.AreaInfos[areaIndex].Uid); DebugUtility.Log(Constants.LOG_TAG, "End invoke on changed area"); } catch (Exception e) { DebugUtility.LogError(Constants.LOG_TAG, string.Format("Invoke on changed area failed, Exception:\n{0}", e.ToString())); } } else { DebugUtility.LogVerbose(Constants.LOG_TAG, $"Cant change to area({targetAraeInfo.Uid}), because limit change to uid({m_LimitChangeToAreaUid})"); } } }
/// <summary> /// 通过AreaId获取AreaInfo /// </summary> /// <returns></returns> public AreaInfo GetAreaInfoByAreaId(ulong areaId) { if (AreaInfos != null && AreaInfos.Length > 0) { for (int iArea = 0; iArea < AreaInfos.Length; iArea++) { AreaInfo areaInfo = AreaInfos[iArea]; if (areaInfo != null && areaInfo.Uid == areaId) { return(areaInfo); } } } return(null); }
private void DoInstantiateAllArea() { if (m_AreaPreviews != null) { DoDestroyAllArea(); } m_AreaPreviews = new AreaPreview[MapInfo.AreaInfos.Length]; for (int iArea = 0; iArea < MapInfo.AreaInfos.Length; iArea++) { AreaInfo iterAreaInfo = MapInfo.AreaInfos[iArea]; GameObject iterArea = new GameObject(); AreaPreview iterAreaPreview = iterArea.AddComponent <AreaPreview>(); m_AreaPreviews[iArea] = iterAreaPreview; iterAreaPreview.Initialie(this, iterAreaInfo); } }
private void WriteMapInfoBinary(BinaryFile file) { BinaryWriter writer = file.m_Writer; writer.Write(this.Uid); writer.Write(this.SceneAddressableKey); if (this.AreaInfos == null || this.AreaInfos.Length <= 0) { writer.Write(0); } else { writer.Write(this.AreaInfos.Length); for (int iArea = 0; iArea < this.AreaInfos.Length; iArea++) { AreaInfo areaInfo = this.AreaInfos[iArea]; areaInfo.Serialize(writer); } } VoxelGridInfo voxelGridInfo = this.VoxelGridInfo; voxelGridInfo.Serialize(writer); }