public void UnregisterParser(Type type) => ParserMap.Remove(type);
Exemple #2
0
    private void Awake()
    {
        Instance = this;
        ghostPos = new List <Vector2Int>();
        _cam     = Camera.main;
        TextAsset textFile = Resources.Load <TextAsset>(nameMap);

        _mapDeserialize = textFile.text.Split(Environment.NewLine.ToCharArray(), 200, StringSplitOptions.RemoveEmptyEntries);
        for (int i = 0; i < _mapDeserialize.Length; i++)
        {
            _mapDeserialize[i] = _mapDeserialize[i].Replace("\n", "").Replace("\r", "");
        }
        // _mapDeserialize = File.ReadAllLines(Application.streamingAssetsPath + "/" + nameMap;
        rows    = _mapDeserialize.Length;
        columns = _mapDeserialize[0].Length;
        Debug.Log("Columns = " + columns);
        Debug.Log("Rows = " + rows);
        map         = new int[columns, rows];
        mapConcrete = new GameObject[columns, rows];
        for (int i = 0; i < rows; i++)
        {
            for (int j = 0; j < columns; j++)
            {
                if (!int.TryParse(_mapDeserialize[i][j].ToString(), out map[j, i]))
                {
                    Debug.LogError("Invalid Map!!!");
                }
            }
        }
        for (int i = 0; i < columns; i++)
        {
            for (int j = 0; j < rows; j++)
            {
                switch (map[i, j])
                {
                case 0:
                    mapConcrete[i, j] = Instantiate(terrain,
                                                    new Vector3(i * terrain.gameObject.transform.right.x + 0.5f,
                                                                j * -terrain.gameObject.transform.up.y - 0.5f, -5), Quaternion.identity);
                    break;

                case 1:
                    mapConcrete[i, j] = Instantiate(wall,
                                                    new Vector3(i * terrain.gameObject.transform.right.x + 0.5f,
                                                                j * -terrain.gameObject.transform.up.y - 0.5f, -5), Quaternion.identity);
                    break;

                case 2:
                    mapConcrete[i, j] = Instantiate(empty,
                                                    new Vector3(i * terrain.gameObject.transform.right.x + 0.5f,
                                                                j * -terrain.gameObject.transform.up.y - 0.5f, -5), Quaternion.identity);
                    break;

                case 3:
                    ghostPos.Add(new Vector2Int(i, j));
                    map[i, j]         = 0;
                    mapConcrete[i, j] = Instantiate(terrain,
                                                    new Vector3(i * terrain.gameObject.transform.right.x + 0.5f,
                                                                j * -terrain.gameObject.transform.up.y - 0.5f, -5), Quaternion.identity);
                    break;

                case 4:
                    playerPos         = new Vector2Int(i, j);
                    map[i, j]         = 0;
                    mapConcrete[i, j] = Instantiate(terrain,
                                                    new Vector3(i * terrain.gameObject.transform.right.x + 0.5f,
                                                                j * -terrain.gameObject.transform.up.y - 0.5f, -5), Quaternion.identity);
                    break;

                case 5:
                    map[i, j]         = 0;
                    mapConcrete[i, j] = Instantiate(terrain,
                                                    new Vector3(i * terrain.gameObject.transform.right.x + 0.5f,
                                                                j * -terrain.gameObject.transform.up.y - 0.5f, -5), Quaternion.identity);
                    Instantiate(pills,
                                new Vector3(i * terrain.gameObject.transform.right.x + 0.5f,
                                            j * -terrain.gameObject.transform.up.y - 0.5f, -5), Quaternion.identity);
                    break;

                case 6:
                    mapConcrete[i, j] = Instantiate(teleport,
                                                    new Vector3(i * terrain.gameObject.transform.right.x + 0.5f,
                                                                j * -terrain.gameObject.transform.up.y - 0.5f, -5), Quaternion.identity);
                    break;
                }
            }
        }

        int x = Random.Range(0, columns);
        int y = Random.Range(0, rows);

        while (map[x, y] != 0 && new Vector2Int(x, y) != playerPos)
        {
            x = Random.Range(0, columns);
            y = Random.Range(0, rows);
        }

        finish = Instantiate(exit,
                             new Vector3(x * terrain.gameObject.transform.right.x + 0.5f, y * -terrain.gameObject.transform.up.y - 0.5f, -5),
                             Quaternion.identity);
        if (rows > columns)
        {
            _cam.orthographicSize = rows * terrain.transform.up.y / 2;
        }
        else
        {
            _cam.orthographicSize = columns * terrain.transform.right.x / 2 - 3;
        }
        _cam.gameObject.transform.position = new Vector3(terrain.transform.right.x * columns / 2, -terrain.transform.up.y * rows / 2, -10);
    }