示例#1
0
    void Awake()
    {
        bluePos   = new List <Vector2Int>();
        orangePos = new List <Vector2Int>();
        size      = new Vector2Int();
        List <string> rows = new List <string>();
        StreamReader  sr   = new StreamReader(Application.dataPath + "/" + mapFile);
        string        line = sr.ReadLine();

        while (line != null)
        {
            rows.Add(line);
            if (line.Length > size.x)
            {
                size.x = line.Length;
            }
            line = sr.ReadLine();
        }

        rows.Reverse();

        size.y = rows.Count;

        offset = new Vector2Int(-size.x / 2, -size.y / 2);

        grid    = new byte[size.y][];
        pellets = new GameObject[size.y][];
        waller  = GetComponent <WallGenerator>();
        waller.SetUp(size);

        for (int i = 0; i < size.y; ++i)
        {
            grid[i]    = new byte[size.x];
            pellets[i] = new GameObject[size.x];
            for (int j = 0; j < rows[i].Length; ++j)
            {
                byte val = translateChar(rows[i][j]);
                grid[i][j] = val;

                Vector3    ip  = new Vector3(j + offset.x, i + offset.y, 0);
                GameObject obj = (GameObject)Instantiate(lookup[val], ip, Quaternion.identity, transform);

                switch (val)
                {
                case 1:
                    pellets[i][j] = obj;
                    break;

                case 2:
                    pellets[i][j] = obj;
                    break;

                case 3:
                    bluePos.Add(new Vector2Int(j, i));
                    break;

                case 4:
                    orangePos.Add(new Vector2Int(j, i));
                    break;

                case 5:
                    waller.AddWall(i, j, obj.GetComponent <Wall>());
                    break;
                }
            }
            for (int j = rows[i].Length; j < size.x; ++j)
            {
                grid[i][j] = 0;
            }
        }
        waller.Build();
    }