Пример #1
0
    private bool loadFile()
    {
        int       numRow = 0, numCol = 0;
        ArrayList sMap = new ArrayList();         // Write data into

        if (!File.Exists(MAP_DIRECTORY + Name + MAP_EXTENSION))
        {
            return(false);
        }
        StreamReader file = new StreamReader(File.OpenRead(MAP_DIRECTORY + Name + MAP_EXTENSION)); // Open file

        while (!file.EndOfStream)
        {
            string line = file.ReadLine();
            if (line.StartsWith("//"))
            {
                continue;                 // Ignores commented line
            }
            else if (line.StartsWith(TILE_OBJECTILE_IDENTIFIER.ToString()))
            {
                // Objective
                var fullStr = line.Substring(TILE_OBJECTILE_IDENTIFIER.ToString().Length);

                // Tokenize
                var objectiveStrs = fullStr.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);

                if (objectiveStrs.Length > 0)
                {
                    // Get the type
                    objectiveType = (Objectives.Type)Convert.ToInt32(objectiveStrs[0]);

                    if (objectiveStrs.Length > 1)
                    {
                        // Get the params and store them for another class to handle
                        objectiveParams = fullStr.Substring(objectiveStrs[0].Length);
                    }
                }
                continue;
            }

            string[] tokens    = line.Split(',');
            int      newLength = tokens.Length;
            if (newLength > numCol)
            {
                numCol = newLength;
            }
            sMap.Add(line);
        }
        numRow = sMap.Count;
        file.Close();
        if (generateMap(sMap, numRow, numCol))
        {
            active = true;
            return(true);
        }
        return(false);
    }
Пример #2
0
    public bool Save(Objectives.Type objective, string objectiveParam)
    {
        if (Name == "")
        {
            return(false);
        }
        // Replace all space with underscore as space will give some problems
        Name = Name.Replace(' ', '_');
        string       path = MAP_DIRECTORY + Name + MAP_EXTENSION;
        StreamWriter file = new StreamWriter(File.Create(path));

        // Save objective
        file.WriteLine(TILE_OBJECTILE_IDENTIFIER.ToString() + ((int)objective) + ", " + objectiveParam);

        foreach (Row row in map)
        {
            string line = "";
            foreach (MultiLayerTile multiTile in row.column)
            {
                if (multiTile.multiLayerTile.Count > 0)
                {
                    foreach (GameObject goTile in multiTile.multiLayerTile)
                    {
                        Tile tile = goTile.GetComponent <Tile>();
                        line += (int)(tile.Type) + TILE_MULTIPLE_LAYER_SPLIT.ToString();
                    }
                    // Remove the extra multiple layer split
                    line = line.Remove(line.Length - 1);
                }
                else
                {
                    line += (int)Tile.TILE_TYPE.TILE_EMPTY;
                }
                line += TILE_SPLIT.ToString();
            }
            line = line.Remove(line.Length - 1);
            file.WriteLine(line);
        }
        file.Close();
        return(true);
    }
Пример #3
0
    private bool loadFile()
    {
        int numRow = 0, numCol= 0;
        ArrayList sMap = new ArrayList(); // Write data into
        if (!File.Exists(MAP_DIRECTORY + Name + MAP_EXTENSION))
        {
            return false;
        }
        StreamReader file = new StreamReader(File.OpenRead(MAP_DIRECTORY + Name + MAP_EXTENSION)); // Open file
        while (!file.EndOfStream)
        {
            string line = file.ReadLine();
            if (line.StartsWith("//"))
            {
                continue; // Ignores commented line
            }
            else if (line.StartsWith(TILE_OBJECTILE_IDENTIFIER.ToString()))
            {
                // Objective
                var fullStr = line.Substring(TILE_OBJECTILE_IDENTIFIER.ToString().Length);

                // Tokenize
                var objectiveStrs = fullStr.Split(new string[] {","}, StringSplitOptions.RemoveEmptyEntries);

                if (objectiveStrs.Length > 0)
                {
                    // Get the type
                    objectiveType = (Objectives.Type) Convert.ToInt32(objectiveStrs[0]);

                    if (objectiveStrs.Length > 1)
                    {
                        // Get the params and store them for another class to handle
                        objectiveParams = fullStr.Substring(objectiveStrs[0].Length);
                    }
                }
                continue;
            }

            string[] tokens = line.Split(',');
            int newLength = tokens.Length;
            if (newLength > numCol)
            {
                numCol = newLength;
            }
            sMap.Add(line);
        }
        numRow = sMap.Count;
        file.Close();
        if (generateMap(sMap, numRow, numCol))
        {
            active = true;
            return true;
        }
        return false;
    }