示例#1
0
    public static MapGenetic read(string content)
    {
        /*
         * File format :
         * Height Width 0|1(neighborhood4)
         * Height *
         *  [Width * MapTileType (index)]
         */

        // Reading Information
        try{
            string[]        linesWithComments = content.Split('\n');
            List <string[]> lines             = new List <string[]>();
            // filtering comments and empty lines
            for (int i = 0; i < linesWithComments.Length; i++)
            {
                string[] lineSplitSpace = linesWithComments[i].Trim().Split(' ');
                if (lineSplitSpace.Length > 0 && lineSplitSpace[0].Length > 0 && lineSplitSpace[0][0] != '#')
                {
                    lines.Add(lineSplitSpace);
                }
            }

            int lineIt = 0;

            // initial info
            string[] infoline = lines[lineIt];
            lineIt++;
            int  height        = int.Parse(infoline [0]);
            int  width         = int.Parse(infoline [1]);
            bool neighborhood4 = int.Parse(infoline [2]) == 1;
            //bool bidirectional = int.Parse(infoline [3]) == 1;

            MapGenetic m = new MapGenetic(height, width, neighborhood4, true);

            // Vertices Costs
            for (int i = 0; i < 4; i++)
            {
                // line for the cost
                string[] costLine = lines[lineIt];
                lineIt++;

                m.setMapTileTypeCost(MapGenetic.typeIndexToType(int.Parse(costLine[0])), float.Parse(costLine[1]));
            }

            // Vertices
            for (int y = 0; y < height; y++)
            {
                string[] mapLine = lines[lineIt];
                lineIt++;
                for (int x = 0; x < width; x++)
                {
                    m.addTile(x, height - 1 - y, MapGenetic.typeIndexToType(int.Parse(mapLine[x])));
                }
            }

            // Targets
            int nTargets = int.Parse(lines[lineIt][0]);
            lineIt++;
            for (int i = 0; i < nTargets; i++)
            {
                GeneticTarget t = new GeneticTarget(new Vector2(int.Parse(lines[lineIt][0]), int.Parse(lines[lineIt][1])), m);
                if (m.checkCharacterPosition(t))
                {
                    m.addTarget(t);
                }
                else
                {
                    Debug.LogError("Map : the position of the enemy " + (i + 1) + " is incorrect.");
                    throw new UnityEngine.UnityException("Map : the position of the enemy " + (i + 1) + " is incorrect.");
                }
                lineIt++;
            }

            return(m);
        }catch (System.Exception ex) {
            Debug.LogError("Error while opening the Map file : wrong format");
            Debug.LogError(ex.Message);
            return(null);
        }
    }
示例#2
0
 public void addTarget(GeneticTarget target)
 {
     this.targets.Add(target);
 }