/// <summary> /// mindId and maxId are the bounding volumes of the map /// </summary> /// <param name="fileName"></param> /// <param name="minId"></param> /// <param name="maxId"></param> public void SavePattern(string fileName, int minId, int maxId) { int xMin = -1, xMax = -1; int zMin = -1, zMax = -1; MapUtility.IdToCoordinate(minId, MapSizeX, ref xMin, ref zMin); MapUtility.IdToCoordinate(maxId, MapSizeX, ref xMax, ref zMax); SavePattern(fileName, xMin, xMax, zMin, zMax); }
public void BuildPattern(MapController mapController, int targetX, int targetZ, float height, Quaternion rotation, bool isObstacle) { if (mapData == null || mapObjectData == null) { return; } if (CanBuild(mapController.MapDataCollection, targetX, targetZ, height, rotation)) { int oriXSize = (int)assetSize.x; int oriZSize = (int)assetSize.z; //旋轉 int xSize = oriXSize; int zSize = oriZSize; Rotate(rotation.eulerAngles.y, ref xSize, ref zSize); int offsetX = targetX - (int)(xSize * 0.5f); int offsetZ = targetZ - (int)(zSize * 0.5f); IEnumerator mapObjItr = mapObjectData.GetObjectEnumerator(); while (mapObjItr.MoveNext()) { MapObject mo = mapObjItr.Current as MapObject; //計算x,z的值,要用還未旋轉的值去計算 int xIndex = -1; int zIndex = -1; MapUtility.IdToCoordinate(mo.Id, oriXSize, ref xIndex, ref zIndex); //get xIndex, zIndex from pattern coordinate int len = mo.ObjectDataList.Count; for (int i = 0; i < len; i++) { MapObject.ObjectData objData = mo.ObjectDataList[i]; GameObject obj = Resources.Load(objData.PrefabName) as GameObject; AssetCellData cellData = obj.GetComponent <AssetCellData>(); Rotate(rotation.eulerAngles.y, oriXSize, oriZSize, cellData.Size.z, ref xIndex, ref zIndex); xIndex += offsetX; //translate to map coordinate zIndex += offsetZ; if (!isObstacle) { mapController.UpdateCellData(objData.PrefabName, xIndex, zIndex, height + objData.Height, cellData.Size, objData.Rotation * rotation); } else { mapController.UpdateCellData(objData.PrefabName, xIndex, zIndex, height + objData.Height, cellData.Size, objData.Rotation * rotation, UnitType.OBSTACLE); } } } } }
/// <summary> /// 清除cell資料 /// </summary> /// <param name="assetCellData"></param> public void EraseCellData(AssetCellData assetCellData) { MapObject mapObject = assetCellData.MapObj; if (mapObject != null) { int xIndex = -1; int zIndex = -1; MapUtility.IdToCoordinate(mapObject.Id, MapSizeX, ref xIndex, ref zIndex); MapDataCollection.WriteCellData(xIndex, zIndex, assetCellData.Size, UnitType.NONE, assetCellData.ObjData.Height); MapObjectDataCollection.RemoveObjectData(mapObject.Id, assetCellData.ObjData); } }
void initMap(Vector3 centerPosition) { MapCenterPosition = centerPosition; int len = map.Length; for (int i = 0; i < len; i++) { int xIndex = -1; int zIndex = -1; MapUtility.IdToCoordinate(i, MapSizeX, ref xIndex, ref zIndex); map[i] = new CellData(xIndex, zIndex, this); } }
static void DrawCellByTopLeftCellPosition(MapObject mapObject, Vector3 topLeftCellPos, int MapSizeX, int MapSizeZ, Transform parent = null) { int xIndex = 0; int zIndex = 0; MapUtility.IdToCoordinate(mapObject.Id, MapSizeX, ref xIndex, ref zIndex); int len = mapObject.ObjectDataList.Count; for (int i = 0; i < len; i++) { DrawMapObjectDataTopLeftCellPosition(mapObject, mapObject.ObjectDataList[i], topLeftCellPos, xIndex, zIndex, parent); } }
void checkIfNeedReBuild(MapController mapController) { int mapSizeX = mapController.MapSizeX; int mapSizeZ = mapController.MapSizeZ; List <int> newId = new List <int>(); foreach (var item in buildMapId) { int curXIndex = -1; int curZIndex = -1; MapUtility.IdToCoordinate(item, mapSizeX, ref curXIndex, ref curZIndex); checkIfNeedReBuild(newId, curXIndex, curZIndex, mapSizeX, mapSizeZ); checkIfNeedReBuild(newId, curXIndex, curZIndex + 1, mapSizeX, mapSizeZ); checkIfNeedReBuild(newId, curXIndex, curZIndex - 1, mapSizeX, mapSizeZ); checkIfNeedReBuild(newId, curXIndex - 1, curZIndex, mapSizeX, mapSizeZ); checkIfNeedReBuild(newId, curXIndex + 1, curZIndex, mapSizeX, mapSizeZ); } int len = newId.Count; for (int i = 0; i < len; i++) { int id = newId[i]; int curXIndex = -1; int curZIndex = -1; MapUtility.IdToCoordinate(newId[i], mapSizeX, ref curXIndex, ref curZIndex); //mapController.EraseCellData(curXIndex, curZIndex); if (totalBuildMapId.ContainsKey(id)) { float h = totalBuildMapId[id]; MapObject mo = mapController.MapObjectDataCollection.GetMapObjectById(id); int objLen = mo.ObjectDataList.Count; for (int j = 0; j < objLen; j++) { if (mo.ObjectDataList[j].Height == h) { mapController.EraseCellData(mo.ObjectDataList[j].Go.GetComponent <AssetCellData>()); break; } } } buildMapId.Add(id); } }
/// <summary> /// xMin、zMin、xMax、zMax are the bounding volumes of the map /// </summary> /// <param name="fileName"></param> /// <param name="xMin"></param> /// <param name="xMax"></param> /// <param name="zMin"></param> /// <param name="zMax"></param> /// <param name="MapSizeX"></param> public void SavePattern(string fileName, int xMin, int xMax, int zMin, int zMax, int MapSizeX) { if (ObjectList.Count > 0) { using (StreamWriter sw = new StreamWriter(fileName)) { int patternSizeX = xMax - xMin + 1; foreach (var kvp in ObjectList) { //Cur map id to new map id int curId = kvp.Key; int xIndex = -1; int zIndex = -1; MapUtility.IdToCoordinate(curId, MapSizeX, ref xIndex, ref zIndex); //get old map xIndex, zIndex xIndex -= xMin; //translate to new map xIndex, zIndex zIndex -= zMin; saveMapObject(kvp.Value, sw, MapUtility.CoordinateToId(xIndex, zIndex, patternSizeX)); } } } }
//cellGo -> default gameObject //prefabName -> Map Connection prefab public override void Action(string prefabName, GameObject cellGo, AssetCellData cellData, DataMode dataMode, bool isObstacle, List <MapIndex> mapIndexList) { mapIndexList.Clear(); base.Action(prefabName, cellGo, cellData, dataMode, isObstacle, mapIndexList); if (Input.GetMouseButton(0)) { if (dataMode == DataMode.ADD) { int xIndex = -1; int zIndex = -1; MapUtility.CalCellIndexByMousePosition(mapController.CenterPosition, mapController.MapSizeX, mapController.MapSizeZ, rayCastMapLayer, ref xIndex, ref zIndex); if (xIndex >= 0 && zIndex >= 0) { int id = MapUtility.CoordinateToId(xIndex, zIndex, mapController.MapSizeX); if (!buildMapId.Contains(id)) { if (!isPress) { if (totalBuildMapId.ContainsKey(id)) { buildHeight = totalBuildMapId[id]; } else { buildHeight = MapUtility.CalCellHeightPosition(mapController.MapDataCollection, xIndex, zIndex, cellData); } } if (mapController.MapDataCollection.CanBuildOnTheMap(xIndex, zIndex, buildHeight, cellData.Size) || totalBuildMapId.ContainsKey(id)) { buildMapId.Add(id); //Draw dummy object GameObject dummy = Object.Instantiate(dummyObject) as GameObject; dummy.transform.parent = dummyParent.transform; Vector3 pos = mapController.GetCellPosition(xIndex, zIndex); dummy.transform.position = new Vector3(pos.x, buildHeight, pos.z); isPress = true; } } } } else if (dataMode == DataMode.ERASE) { GameObject hitObject = MapUtility.GetRayCastMapObjectByMousePosition(rayCastLayer); if (hitObject != null) { AssetCellData cd = hitObject.GetComponent <AssetCellData>(); mapController.EraseCellData(cd); totalBuildMapId.Remove(cd.MapObj.Id); } } } else if (Input.GetMouseButtonUp(0) && isPress) { if (buildMapId.Count == 0) { return; } MapConnection mapConn = (Resources.Load(MapSetting.MAP_CONNECT_TILE_FOLDER_NAME + prefabName.GetPathWidthoutExtension()) as GameObject).GetComponent <MapConnection>(); checkIfNeedReBuild(mapController); List <MapDir> neighBorList = new List <MapDir>(); foreach (var item in buildMapId) { int curXIndex = -1; int curZIndex = -1; MapUtility.IdToCoordinate(item, mapController.MapSizeX, ref curXIndex, ref curZIndex); getNeighborDir(curXIndex, curZIndex, mapController, neighBorList); Quaternion rot = Quaternion.identity; string p = getPrefabName(neighBorList, cellGo, mapConn, ref rot); if (!isObstacle) { mapController.UpdateCellData(p, curXIndex, curZIndex, buildHeight, cellData.Size, rot); } else { mapController.UpdateCellData(p, curXIndex, curZIndex, buildHeight, cellData.Size, rot, UnitType.OBSTACLE); } AddMapIndex(curXIndex, curZIndex, mapIndexList); totalBuildMapId[item] = buildHeight; } buildMapId.Clear(); isPress = false; for (int i = dummyParent.transform.childCount - 1; i >= 0; i--) { Object.Destroy(dummyParent.transform.GetChild(i).gameObject); } } }