Example #1
0
    static public CellData operator /(CellData a, int num)
    {
        int curId = MapUtility.CoordinateToId(a.Xpos, a.Zpos, a.MapDataCollection.MapSizeX);
        int id    = curId - a.MapDataCollection.MapSizeX * num;

        return(a.MapDataCollection.GetCellById(id));
    }
Example #2
0
    bool isAddNeighborList(int xIndex, int zIndex, int mapSizeX)
    {
        int neighborId = MapUtility.CoordinateToId(xIndex, zIndex, mapSizeX);

        if (buildMapId.Contains(neighborId) || totalBuildMapId.ContainsKey(neighborId))
        {
            return(true);
        }

        return(false);
    }
Example #3
0
 void checkIfNeedReBuild(List <int> newId, int xIndex, int zIndex, int mapSizeX, int mapSizeZ)
 {
     if (isCurrentMapIndex(xIndex, zIndex, mapSizeX, mapSizeZ))
     {
         int id = MapUtility.CoordinateToId(xIndex, zIndex, mapSizeX);
         if (totalBuildMapId.ContainsKey(id))
         {
             newId.Add(id);
         }
     }
 }
Example #4
0
    /// <summary>
    /// 清除cell資料
    /// </summary>
    /// <param name="x"></param>
    /// <param name="z"></param>
    public void EraseCellData(int x, int z)
    {
        int       id        = MapUtility.CoordinateToId(x, z, MapDataCollection.MapSizeX);
        MapObject mapObject = MapObjectDataCollection.GetMapObjectById(id);

        if (mapObject != null)
        {
            MapObjectDataCollection.RemoveMapObject(id);
        }

        MapDataCollection.WriteCellData(id, UnitType.NONE, Vector3.one);
    }
Example #5
0
    public void AddObjectToLayer(MapIndex mapIndex)
    {
        MapObject mo = mapController.MapObjectDataCollection.GetMapObjectById(MapUtility.CoordinateToId(mapIndex.x, mapIndex.z, mapController.MapSizeX));

        if (mo != null)
        {
            int objCount = mo.ObjectDataList.Count;
            if (objCount > 0)
            {
                mapLayers.AddObjectToLayer(selectLayers, mo.ObjectDataList[objCount - 1]);
            }
        }
    }
Example #6
0
    void writeCellData(StreamWriter sw, CellData cell, int id = -1)
    {
        string s = id >= 0 ? id.ToString() : MapUtility.CoordinateToId(cell.Xpos, cell.Zpos, MapSizeX).ToString();

        int len = cell.Height.Count;

        for (int i = 0; i < len; i++)
        {
            s += "$" + cell.Height[i].Height + "$" + cell.Height[i].Size;
        }

        s += "$" + cell.Unit;
        sw.WriteLine(s);
    }
Example #7
0
    void writeCellDataInternal(int x, int z, Vector3 collider, UnitType unitType, float height = 0)
    {
        Vector3 numberOfCell = collider.GetCellSize();

        for (int i = 0; i < numberOfCell.x; i++)
        {
            for (int j = 0; j < numberOfCell.z; j++)
            {
                int id = MapUtility.CoordinateToId(x + i, z + j, MapSizeX);
                if (id >= 0 && id < map.Length)
                {
                    WriteCellData(id, unitType, collider, height);
                }
            }
        }
    }
Example #8
0
    /// <summary>
    /// 更新cell資料
    /// </summary>
    /// <param name="prefabName"></param>
    /// <param name="x"></param>
    /// <param name="z"></param>
    /// <param name="height"></param>
    /// <param name="assetSize"></param>
    /// <param name="rotation"></param>
    /// <param name="unitType"></param>
    public void UpdateCellData(string prefabName, int x, int z, float height, Vector3 assetSize, Quaternion rotation, UnitType unitType)
    {
        if (MapDataCollection.CanBuildOnTheMap(x, z, height, assetSize))
        {
            int       id        = MapUtility.CoordinateToId(x, z, MapDataCollection.MapSizeX);
            MapObject mapObject = MapObjectDataCollection.GetMapObjectById(id);
            if (mapObject == null)
            {
                mapObject = MapObjectDataCollection.AddMapObject(id);
            }

            MapObject.ObjectData o = MapObjectDataCollection.AddObjectData(id);
            o.PrefabName = prefabName;
            o.Height     = height;
            o.Rotation   = rotation;

            MapDataCollection.WriteCellData(x, z, assetSize, unitType, height);

            GameObject obj = DrawingMap.DrawMapObjectData(mapObject, o, CenterPosition, MapSizeX, MapSizeZ, x, z, mapObjectParent.transform);
            onCreateMapObject(obj);
            MapObjectDataCollection.RaiseFinishAddObjectData(o);
        }
    }
Example #9
0
 /// <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>
 public void SavePattern(string fileName, int xMin, int xMax, int zMin, int zMax)
 {
     using (StreamWriter sw = new StreamWriter(fileName))
     {
         int patternSizeX = xMax - xMin + 1;
         int patternSizeZ = zMax - zMin + 1;
         writeMapSize(sw, patternSizeX, patternSizeZ, MapCenterPosition);
         for (int i = xMin; i <= xMax; i++)
         {
             for (int j = zMin; j <= zMax; j++)
             {
                 //Cur map id to new map id
                 int      xIndex = i - xMin;
                 CellData cell   = GetCell(i, j);
                 if (checkIfNeedSave(cell))
                 {
                     int zIndex = j - zMin;
                     writeCellData(sw, GetCell(i, j), MapUtility.CoordinateToId(xIndex, zIndex, patternSizeX));
                 }
             }
         }
     }
 }
Example #10
0
    /// <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));
                }
            }
        }
    }
Example #11
0
    bool canBuildOnTheMapInternal(int x, int z, float height, Vector3 collider)
    {
        Vector3 numberOfCell = collider.GetCellSize();

        for (int i = 0; i < numberOfCell.x; i++)
        {
            for (int j = 0; j < numberOfCell.z; j++)
            {
                int xIndex = x + i;
                int zIndex = z + j;
                if (xIndex < 0 || xIndex >= MapSizeX || zIndex < 0 || zIndex >= MapSizeZ)
                {
                    return(false);
                }
                int id = MapUtility.CoordinateToId(xIndex, zIndex, MapSizeX);
                if (map[id].Unit != UnitType.NONE && map[id].GetHeightest() > height)
                {
                    return(false);
                }
            }
        }

        return(true);
    }
Example #12
0
    //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);
            }
        }
    }
Example #13
0
    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, rayCastLayer, ref xIndex, ref zIndex);
                if (xIndex >= 0 && zIndex >= 0)
                {
                    int id = MapUtility.CoordinateToId(xIndex, zIndex, mapController.MapSizeX);
                    if (!buildMapId.Contains(id))
                    {
                        float yPos = MapUtility.CalCellHeightPosition(mapController.MapDataCollection, xIndex, zIndex, cellData);
                        if (mapController.MapDataCollection.CanBuildOnTheMap(xIndex, zIndex, yPos, cellData.Size))
                        {
                            if (!string.IsNullOrEmpty(prefabName))
                            {
                                if (!isObstacle)
                                {
                                    mapController.UpdateCellData(MapSetting.MAP_REFAB_FOLDER_NAME + prefabName.GetPathWidthoutExtension(), xIndex, zIndex, yPos,
                                                                 cellData.Size, cellGo.transform.rotation);
                                }
                                else
                                {
                                    mapController.UpdateCellData(MapSetting.MAP_REFAB_FOLDER_NAME + prefabName.GetPathWidthoutExtension(), xIndex, zIndex, yPos,
                                                                 cellData.Size, cellGo.transform.rotation, UnitType.OBSTACLE);
                                }
                            }
                            else
                            {
                                patternImporter.BuildPattern(mapController, xIndex, zIndex, yPos, cellGo.transform.rotation, isObstacle);
                            }

                            //Add id that the cell can not be builded
                            int leftX  = xIndex - ((int)Mathf.Abs(cellData.Size.x)) / 2;
                            int rightX = xIndex + ((int)Mathf.Abs(cellData.Size.x - 1)) / 2;
                            int downZ  = zIndex - ((int)Mathf.Abs(cellData.Size.z)) / 2;
                            int upZ    = zIndex + ((int)Mathf.Abs(cellData.Size.z - 1)) / 2;

                            for (int i = leftX; i <= rightX; i++)
                            {
                                for (int j = downZ; j <= upZ; j++)
                                {
                                    buildMapId.Add(MapUtility.CoordinateToId(i, j, mapController.MapSizeX));
                                }
                            }
                        }

                        AddMapIndex(xIndex, zIndex, mapIndexList);
                    }
                }
            }
            else if (dataMode == DataMode.ERASE)
            {
                GameObject hitObject = MapUtility.GetRayCastMapObjectByMousePosition(rayCastLayer);
                if (hitObject != null)
                {
                    mapController.EraseCellData(hitObject.GetComponent <AssetCellData>());
                }
            }
            else if (dataMode == DataMode.CAN_MOVE || (dataMode == DataMode.CAN_NOT_MOVE))
            {
                int xIndex = -1;
                int zIndex = -1;
                MapUtility.CalCellIndexByMousePosition(mapController.CenterPosition, mapController.MapSizeX, mapController.MapSizeZ, rayCastMapLayer, ref xIndex, ref zIndex);
                if (xIndex >= 0 && zIndex >= 0)
                {
                    if (mapController.SetCellMovable(xIndex, zIndex, dataMode == DataMode.CAN_MOVE ? true : false))
                    {
                        AddMapIndex(xIndex, zIndex, mapIndexList);
                    }
                }
            }
            else if (dataMode == DataMode.ADD_PLAYER || dataMode == DataMode.ERASE_PLAYER || dataMode == DataMode.ADD_ENEMY || dataMode == DataMode.ERASE_ENEMY)
            {
                int xIndex = -1;
                int zIndex = -1;
                MapUtility.CalCellIndexByMousePosition(mapController.CenterPosition, mapController.MapSizeX, mapController.MapSizeZ, rayCastMapLayer, ref xIndex, ref zIndex);
                if (xIndex >= 0 && zIndex >= 0)
                {
                    AddMapIndex(xIndex, zIndex, mapIndexList);
                }
            }
        }

        if (Input.GetMouseButtonUp(0))
        {
            buildMapId.Clear();
        }
    }
Example #14
0
 public CellData GetCell(int x, int z)
 {
     return(GetCellById(MapUtility.CoordinateToId(x, z, MapSizeX)));
 }
Example #15
0
 public void WriteCellData(int x, int z, UnitType unitType, int height = 0)
 {
     WriteCellData(MapUtility.CoordinateToId(x, z, MapSizeX), unitType, Vector3.one, height);
 }