void Start() { pos_w = mainTiled.WorldPosition - Vector3.right * LandTiled.halfTiledWidth; topLeft = Vector3.zero + pos_w; topRight = new Vector3(WidthInScaled(), 0) + pos_w; bottomRight = new Vector3(WidthInScaled(), -HeightInScaled()) + pos_w; bottomLeft = new Vector3(0, -HeightInScaled()) + pos_w; // To make gizmo visible, even when using depth-shader shaders, we decrease the z depth by the number of layers float depth_z = -1.0f * 3;//this.NumLayers pos_w.z += depth_z; topLeft.z += depth_z; topRight.z += depth_z; bottomRight.z += depth_z; bottomLeft.z += depth_z; if (MapDebug.IsMapPoint) { mapPoint = CoordinationConvert.TiledWorldToMapPoint(this.transform.position, rootScene.WorldPosition); cooditions = string.Format("({0},{1})", mapPoint.x, mapPoint.y); } if (MapDebug.IsWorldPoint) { worldPoint = CoordinationConvert.TiledMapToWorld3D(this.mapPoint, rootScene.MapPoint); cooditions += string.Format("/({0},{1})", worldPoint.x, worldPoint.y); } }
private void OnDrawGizmos() { pos_w = this.gameObject.transform.position; topLeft = Vector3.zero + pos_w; topRight = new Vector3(WidthInScaled(), 0) + pos_w; bottomRight = new Vector3(WidthInScaled(), -HeightInScaled()) + pos_w; bottomLeft = new Vector3(0, -HeightInScaled()) + pos_w; // To make gizmo visible, even when using depth-shader shaders, we decrease the z depth by the number of layers float depth_z = -1.0f * 3;//this.NumLayers pos_w.z += depth_z; topLeft.z += depth_z; topRight.z += depth_z; bottomRight.z += depth_z; bottomLeft.z += depth_z; Vector2 mapPos = CoordinationConvert.SceneWorldToMapPoint(this.transform.position); Vector2 worldPos = CoordinationConvert.SceneMapToWorld3D(mapPos); GUI.color = Color.red; float x = (topLeft + (topRight - topLeft) * 0.25f).x; float y = (topLeft + (bottomLeft - topLeft) * 0.25f).y; Handles.Label(new Vector3(x, y), string.Format("({0},{1})/({2},{3})", mapPos.x, mapPos.y, worldPos.x, worldPos.y)); }
/// <summary> /// 获得土地块 /// </summary> /// <param name="screenPos">屏幕坐标系的位置</param> /// <returns></returns> public LandTiled FindLandTiled(Vector3 screenPos) { //世界地图坐标 Vector2 mapPoint = CoordinationConvert.SceneCamToWorldMapPoint(screenPos); return(FindLandTiledByMapPoint(mapPoint)); }
/// <summary> /// 初始化SceneTiled模块地图 /// </summary> /// <param name="gObj">主GameObject</param> /// <param name="worldMapIndex">世界地图中的索引</param> public void Initlizate(GameObject gObj) { mainObj = gObj; mainObj.name += "_" + WorldMapIndex; #if UNITY_EDITOR if (MapDebug.DebugSceneTiled) { gObj.AddComponent <SceneTiledGizmo>(); } #endif gObj.transform.localScale = Vector3.one; gObj.transform.position = WorldPosition; mapPoint = CoordinationConvert.SceneWorldToMapPoint(WorldPosition); //构造场景内的地块信息 landTileds.Clear(); int halfWidth = LandTiled.halfTiledWidth; int halfHeight = LandTiled.halfTiledHeight; // Vector3 orginPos = WorldPosition + new Vector3(halfSceneWidth - halfWidth,0); Vector3 orginPos = WorldPosition + new Vector3(halfSceneWidth, 0); for (int i = 0; i < SceneRow; i++) { int tiledRowIndex = i * SceneRow; for (int j = 0; j < SceneColumn; j++) { int tiledIndex = tiledRowIndex + j; //计算世界坐标中的位置 float tiledX = halfWidth * j - halfWidth * i; float tiledY = -halfHeight * j - halfHeight * i; Vector3 position = new Vector3(tiledX, tiledY); position += orginPos; //构造记录映射 TiledData td = new TiledData(); //td.TiledId = ; td.SceneIndex = (byte)WorldMapIndex; td.Index = tiledIndex; //实例土地 LandTiled landTiled = new LandTiled(td); landTiled.WorldPosition = position; landTileds.Add(landTiled); MapCreater.Instance.LoadDel("Assets/Prefabs/LandSprite.prefab", obj => { landTiled.Initlizate(obj, this); }); } } }
/// <summary> /// 估算屏幕内所占格子的数量 /// </summary> private void culScreenTiledCount() { if (halfScreenWidthTiledCount != 0) { return; } Vector2 mapLeftTop = CoordinationConvert.SceneCamToWorldMapPoint(Vector2.zero); Vector2 mapRightTop = CoordinationConvert.SceneCamToWorldMapPoint(new Vector2(Screen.width, 0)); Vector2 mapLeftButtom = CoordinationConvert.SceneCamToWorldMapPoint(new Vector2(0, Screen.height)); halfScreenWidthTiledCount = (int)(mapRightTop.x - mapLeftTop.x) / 2 + 1; halfScreenHeightTiledCount = (int)(mapLeftButtom.y - mapLeftTop.y) / 2 + 1; }
/// <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]); }
private void addShowTiled(Vector2 screenPos, Dictionary <int, WorldSceneData> worldSceneDatas) { Vector2 mapPos = CoordinationConvert.SceneCamToSceneMapPoint(screenPos); addSceneTiled(mapPos, worldSceneDatas); }
/// <summary> /// 主相机移动时的操作 /// </summary> public void CameraChange() { // this.culScreenTiledCount(); // Debug.Log("halfTileW:" + halfScreenWidthTiledCount + " , halfTileH:" + halfScreenHeightTiledCount); //计算相机中心个点的世界坐标位置 // Vector2 center = new Vector2(Screen.width / 2 , Screen.height / 2); //计算TileIndex索引 Dictionary <int, WorldSceneData> worldSceneDatas = new Dictionary <int, WorldSceneData>(); // Vector2 mapPos = CoordinationConvert.SceneCamToSceneMapPoint(center); // addSceneTiled(mapPos, worldSceneDatas); // //计算横向 // for (int i = 0; i < 2; i++) // { // //横向 // float nearX = mapPos.x - (1 - (i % 2) * 2) * halfScreenWidthTiledCount; // float nearY = mapPos.y + (1 - (i % 2) * 2) * halfScreenWidthTiledCount; // Vector2 nearMapPos = new Vector2(nearX , nearY); // addSceneTiled(nearMapPos, worldSceneDatas); // // //纵向 // nearX = mapPos.x - (1 - (i % 2) * 2) * halfScreenHeightTiledCount; // nearY = mapPos.y - (1 - (i % 2) * 2) * halfScreenHeightTiledCount; // nearMapPos = new Vector2(nearX, nearY); // addSceneTiled(nearMapPos, worldSceneDatas); // } // // //计算四个边角 // addShowTiled(Vector2.zero , worldSceneDatas); // addShowTiled(new Vector2(Screen.width , 0), worldSceneDatas); // addShowTiled(new Vector2(0 , Screen.height), worldSceneDatas); // addShowTiled(new Vector2(Screen.width , Screen.height), worldSceneDatas); Vector2 mapLeftTop = CoordinationConvert.SceneCamToWorldMapPoint(Vector2.zero); Vector2 mapLeftBottom = CoordinationConvert.SceneCamToWorldMapPoint(new Vector2(0, Screen.height)); Vector2 mapRightTop = CoordinationConvert.SceneCamToWorldMapPoint(new Vector2(Screen.width, 0)); Vector2 mapRightBottom = CoordinationConvert.SceneCamToWorldMapPoint(new Vector2(Screen.width, Screen.height)); Debug.Log("Map LT: " + mapLeftTop + " , LB: " + mapLeftBottom + " , RT: " + mapRightTop + " , RB:" + mapRightBottom); int count = (int)(mapRightTop.x - mapLeftTop.x) + 2; for (int i = 0; i <= count; i++) { //上边缘 Vector2 newMapPoint = mapLeftTop + new Vector2(i + 1, -i + 1); Vector2 sceneMapPoint = CoordinationConvert.TileMapToSceneMapPoint(newMapPoint); addSceneTiled(sceneMapPoint, worldSceneDatas); //下边缘 newMapPoint = mapLeftBottom + new Vector2(i - 1, -i); sceneMapPoint = CoordinationConvert.TileMapToSceneMapPoint(newMapPoint); addSceneTiled(sceneMapPoint, worldSceneDatas); } //左边缘 count = (int)(mapLeftTop.x - mapLeftBottom.x) + 1; for (int i = 0; i <= count; i++) { Vector2 newMapPoint = mapLeftTop - new Vector2(i, i - 2); Vector2 sceneMapPoint = CoordinationConvert.TileMapToSceneMapPoint(newMapPoint); addSceneTiled(sceneMapPoint, worldSceneDatas); } //右边缘 count = (int)(mapRightTop.x - mapRightBottom.x) + 2; for (int i = 0; i <= count; i++) { Vector2 newMapPoint = mapRightTop - new Vector2(i - 2, i); Vector2 sceneMapPoint = CoordinationConvert.TileMapToSceneMapPoint(newMapPoint); addSceneTiled(sceneMapPoint, worldSceneDatas); } WorldSceneData[] wsdArr = new WorldSceneData[worldSceneDatas.Count]; worldSceneDatas.Values.CopyTo(wsdArr, 0); this.MergeMap(wsdArr); //用于测试地图的可视区域 //debugViewMap(mapLeftTop , mapLeftBottom , mapRightTop , mapRightBottom); }
public void Update() { #if UNITY_ANDROID || UNITY_IOS if (Input.touchCount <= 0) { return; } Touch touch = Input.GetTouch(0); if (touch.phase == TouchPhase.Began || touch.phase == TouchPhase.Stationary) { beginTime = Time.time; beginPos = mapCamera.ScreenToWorldPoint(touch.position); } else if (touch.phase == TouchPhase.Moved) { offset = (Vector2)mapCamera.ScreenToWorldPoint(touch.position) - beginPos; } else if (touch.phase == TouchPhase.Ended) { offset = offsetZero; lastMousePos = touch.position; endTime = Time.time; MapCamera.Instance.CallFinish(); } #else if (Input.GetMouseButtonDown(0)) { beginPos = mapCamera.ScreenToWorldPoint(Input.mousePosition + MapCamera.Instance.depath); beginTime = Time.time; } else if (Input.GetMouseButtonUp(0)) { endTime = Time.time; offset = offsetZero; beginPos = offsetZero; lastMousePos = Input.mousePosition; MapCamera.Instance.CallFinish(); } else if (beginPos != offsetZero) { Vector3 curMouseSceenPos = Input.mousePosition; float distance = Vector3.Distance(lastMousePos, curMouseSceenPos); if (distance > 5f) { lastMousePos = curMouseSceenPos; Vector3 curMousePos = mapCamera.ScreenToWorldPoint(lastMousePos + MapCamera.Instance.depath); offset = (Vector2)curMousePos - beginPos; } } #endif //移动移动 if (offset != offsetZero) { //移动相反的方向 MapCamera.Instance.Move(-offset); // cacheTrans.position += (Vector3)offset; offset = offsetZero; } //点击操作 if (Math.Abs(endTime - beginTime) < touchTime) { endTime = 0; LandTiled landTiled = MapData.Instance.FindLandTiled(lastMousePos); if (landTiled != null) { landTiled.SetTiledState(landTiled.CurrentState == ELandStatus.Select ? ELandStatus.None : ELandStatus.Select); } } if (build) { Vector2 mapPoint = CoordinationConvert.ConvertTiledIndex(Input.mousePosition); } }