/// <summary> /// 获得一个缓存的SceneTiled对应 /// </summary> /// <param name="sceneData"></param> /// <returns></returns> public SceneTiled SpwanSceneTiled(WorldSceneData sceneData) { if (!SceneMap.Contains(sceneData.WorldIndex)) { SceneTiled sceneTile = new SceneTiled(sceneData); SceneTilePool.Add(sceneTile); if (SceneTilePool.Count > MaxSceneTiledCount) { DestroyFirst(); } SceneMap.Add(sceneData.WorldIndex); } else { //重新排序 SceneTiled sceneTiled = null; for (int i = SceneTilePool.Count - 1; i >= 0; i--) { if (SceneTilePool[i].WorldMapIndex == sceneData.WorldIndex) { sceneTiled = SceneTilePool[i]; SceneTilePool.RemoveAt(i); break; } } SceneTilePool.Add(sceneTiled); } return(SceneTilePool[SceneTilePool.Count - 1]); }
public void DestroyFirst() { SceneTiled sceneTiled = SceneTilePool[0]; SceneMap.Remove(sceneTiled.WorldMapIndex); sceneTiled.Destroy(); SceneTilePool.RemoveAt(0); }
/// <summary> /// 根据土地的地图坐标获取LandTiled对象 /// </summary> /// <param name="mapPoint">地图坐标系位置</param> /// <returns></returns> public LandTiled FindLandTiledByMapPoint(Vector2 mapPoint) { //父层Scene坐标 Vector2 sceneMapPoint = CoordinationConvert.TileMapToSceneMapPoint(mapPoint); SceneTiled sceneTiled = GetSceneTiled(sceneMapPoint); if (sceneTiled == null) { return(null); } //相对Scene的坐标 Vector2 relativeMapPoint = mapPoint - new Vector2((int)sceneMapPoint.x * SceneTiled.SceneRow, (int)sceneMapPoint.y * SceneTiled.SceneColumn); int landIndex = (int)(relativeMapPoint.x + relativeMapPoint.y * SceneTiled.SceneColumn); return(sceneTiled[landIndex]); }
/// <summary> /// 初始化地表 /// </summary> public void Initlizate(GameObject mainGO, SceneTiled parent) { mainObj = mainGO; mainObj.name += "_" + TiledIndex; mainObj.transform.localScale = Vector3.one; mainObj.transform.position = WorldPosition - Vector3.up * halfTiledHeight; mainObj.transform.SetParent(parent.MainObj.transform); imgStatus = mainObj.GetComponent <Image>(); this.SetTiledState(ELandStatus.None); #if UNITY_EDITOR //debug if (MapDebug.DebugLandTiled) { LandTiledGizmo ltg = mainObj.AddComponent <LandTiledGizmo>(); ltg.mainTiled = this; ltg.TiledWidth = (int)TileRange.x; ltg.TiledHeight = (int)TileRange.y; ltg.rootScene = parent; } #endif }
/// <summary> /// 合并地图 /// ps:由于每块SceneTiled的位置都是根据下标计算的 /// </summary> /// <param name="tileds">tiled列表</param> public void MergeMap(WorldSceneData[] tileds) { int halfWidth = SceneTiled.MaxSceneWidth / 2; //每块SceneTiled的宽度的一半 int halfHeight = SceneTiled.MaxSceneHeight / 2; //每块SceneTiled的高度的一半 MapData dataPool = MapData.Instance; for (int i = 0; i < tileds.Length; i++) { if (dataPool.HasCacheTiled(tileds[i])) { SceneTiled cacheTiled = dataPool.SpwanSceneTiled(tileds[i]); cacheTiled.Active = true; continue; } SceneTiled tiled = dataPool.SpwanSceneTiled(tileds[i]); int tiledIndex = tileds[i].WorldIndex; //地图世界坐标中的索引 //计算世界坐标中的位置 int column = tiledIndex / MapRow; float tiledX = halfWidth * column - halfWidth * (tiledIndex % MapRow); float tiledY = -halfHeight * column - halfHeight * (tiledIndex % MapRow); tiled.WorldPosition = new Vector3(CoordinationConvert.OrginMapPoint.x + tiledX, CoordinationConvert.OrginMapPoint.y + tiledY); //异步加载资源 string sceneAssetName = getSceneTileAssetName(tileds[i].SceneId); LoadDel(sceneAssetName, gObj => { if (gObj == null) { Debug.LogWarning("<<MergeMap>> Can find Scene tiled ! Name is " + sceneAssetName); return; } gObj.transform.SetParent(MapRoot.transform); SceneTiled sceneTiled = dataPool.GetSceneTiled(tileds[i].WorldIndex); sceneTiled.Initlizate(gObj); }); } //删除被隐藏的 if (lastTileds != null) { for (int i = lastTileds.Length - 1; i >= 0; i--) { bool isTrue = false; for (int j = 0; j < tileds.Length; j++) { if (lastTileds[i].WorldIndex == tileds[j].WorldIndex) { isTrue = true; break; } } //隐藏超出屏幕的 if (!isTrue) { SceneTiled tiled = dataPool.GetSceneTiled(lastTileds[i].WorldIndex); tiled.Active = false; } } } lastTileds = tileds; }