Exemplo n.º 1
0
    void LoadLevel(int world, int level)
    {
        EmptyLevel();
        int index = worldLoader.GetIndex(world, level);

        // Read the data from the file in assets
        string    levelName = (string)worldLoader.levels[index];
        TextAsset textFile  = (TextAsset)worldLoader.levelAssets[levelName];
        string    text      = textFile.text;

        Regex replaceR       = new Regex("\r");
        Regex replaceComment = new Regex("[ ]*;.*\n");
        Regex replaceLine    = new Regex("[\n]*\n");
        Regex replaceBegin   = new Regex("^\n");

        text = replaceR.Replace(text, "");
        text = replaceComment.Replace(text, "\n");
        text = replaceLine.Replace(text, "\n");
        text = replaceBegin.Replace(text, "");
        string[] lines = Regex.Split(text, "\n");

        int    width  = Convert.ToInt32(lines[0]);
        int    height = Convert.ToInt32(lines[1]);
        Camera cam    = GameObject.Find("Main Camera").GetComponent <Camera>();

        cam.transform.position = new Vector3((float)Convert.ToDouble(lines[2]), (float)Convert.ToDouble(lines[3]), -WORLD_HEIGHT);

        AndAllEndLevelCondition endLevelCondition = new AndAllEndLevelCondition();
        Level loaded = new Level(levelName, lines[4].Trim(), endLevelCondition, world, level);

        endLevelCondition.Add(new CollectEndLevelCondition(loaded, lines[5].Trim()));
        int       offset       = 6;
        ArrayList blockedCells = new ArrayList();

        for (int i = 0; i < height; i++)
        {
            float        y                 = height - (i) - 1 + 0.5f;
            string       s                 = lines[i + offset];
            int          lastCell          = -2; // prevent creating right away
            CellBehavior lastCellBehaviour = null;
            for (int j = 0; j < s.Length && j < width; j++)
            {
                float      x   = j + 0.5f;
                GameObject go  = null;
                char       c   = s[j];
                Vector3    pos = new Vector3(x, y, 0);

                if (c >= 'A' && c <= 'Z')
                {
                    go = (GameObject)Instantiate(cell, pos, Quaternion.identity);
                    go.transform.SetParent(this.transform);
                    CellController cc = go.GetComponent <CellController>();
                    if (y < MIDDLE_BAR_Y)
                    {
                        cc.immutable = true;
                    }
                    cc.Loot = c.ToString();
                }
                else if (c >= 'a' && c <= 'z')
                {
                    go = (GameObject)Instantiate(cell, pos, Quaternion.identity);
                    go.transform.SetParent(this.transform);

                    CellController cc = go.GetComponent <CellController>();
                    cc.Loot = "";
                }
                else if (c == '▔')
                {
                    blockedCells.Add(new int[] { (int)x, (int)y + 1, CellController.DOWN });
                }
                else if (c == '▁')
                {
                    blockedCells.Add(new int[] { (int)x, (int)y - 1, CellController.UP });
                }
                else if (c == '-')
                {
                    go = (GameObject)Instantiate(cabinet, pos, Quaternion.identity);
                    go.transform.SetParent(this.transform);
                }
                else if (c == '+')
                {
                    go = (GameObject)Instantiate(counter, pos, Quaternion.identity);
                    go.transform.SetParent(this.transform);
                }
                else if (c == '.')
                {
                    go = (GameObject)Instantiate(pointerCell, pos, Quaternion.identity);
                    go.transform.SetParent(this.transform);
                }
                else if (c == '@')
                {
                    go = (GameObject)Instantiate(linkedCell, pos, Quaternion.identity);
                    go.transform.SetParent(this.transform);
                    j++;
                }
                else if (c == '#')
                {
                    go = (GameObject)Instantiate(cell, pos, Quaternion.identity);
                    go.transform.SetParent(this.transform);
                }
                else if (c == '&')
                {
                    ampersand.transform.position = new Vector3(pos.x, pos.y, -5);
                }
                else if (c == '*')
                {
                    star.transform.position = new Vector3(pos.x, pos.y, -5);
                }

                if (go != null && (go.tag == "cell" || go.tag == "pointer"))
                {
                    CellBehavior cb = go.GetComponent <CellBehavior>();
                    if (lastCell == j - 1)
                    {
                        if (lastCellBehaviour.owningArray == null)
                        {
                            // must create
                            lastCellBehaviour.owningArray = new CellArray();
                            lastCellBehaviour.owningArray.Add(lastCellBehaviour.gameObject);
                            lastCellBehaviour.arrayIndex = 0;
                        }
                        cb.owningArray = lastCellBehaviour.owningArray;
                        cb.owningArray.Add(go);
                        cb.arrayIndex = lastCellBehaviour.arrayIndex + 1;
                    }
                    lastCell          = j;
                    lastCellBehaviour = cb;
                }
            }
        }

        loadObjects();

        offset += height;
        int links = Convert.ToInt32(lines[offset++]);

        for (int i = 0; i < links; i++)
        {
            string[]          lineLinks = Regex.Split(lines[offset + i], "[\\s,]+");
            int               x1        = Convert.ToInt32(lineLinks[0]);
            int               y1        = Convert.ToInt32(lineLinks[1]);
            int               x2        = Convert.ToInt32(lineLinks[2]);
            int               y2        = Convert.ToInt32(lineLinks[3]);
            GameObject        pointer   = findAt(x1, y1);
            GameObject        target    = findAt(x2, y2);
            PointerController pc        = pointer.GetComponent <PointerController>();
            CellBehavior      cc        = target.GetComponent <CellBehavior>();
            pc.Target = cc;
        }

        offset += links;
        for (int i = 0; i < height; i++)
        {
            int    y = height - (i) - 1;
            string s = lines[i + offset];
            for (int j = 0; j < s.Length && j < width; j++)
            {
                int x = j;

                char c = s[j];

                if (c >= 'A' && c <= 'Z')
                {
                    endLevelCondition.Add(new CellValueEndLevelCondition(levelController, x, y, c.ToString()));
                }
                else if (c == '/')
                {
                    GameObject go = findAt(x, y);
                    endLevelCondition.Add(new CellFreedEndLevelCondition(go.GetComponent <CellBehavior>()));
                }
            }
        }

        offset += height;
        int targetLinks = Convert.ToInt32(lines[offset++]);

        for (int i = 0; i < targetLinks; i++)
        {
            string[]          lineLinks = Regex.Split(lines[offset + i], "[\\s,]+");
            int               x1        = Convert.ToInt32(lineLinks[0]);
            int               y1        = Convert.ToInt32(lineLinks[1]);
            int               x2        = Convert.ToInt32(lineLinks[2]);
            int               y2        = Convert.ToInt32(lineLinks[3]);
            GameObject        pointer   = findAt(x1, y1);
            GameObject        target    = findAt(x2, y2);
            PointerController pc        = pointer.GetComponent <PointerController>();
            CellBehavior      cc        = target.GetComponent <CellBehavior>();
            endLevelCondition.Add(new LinkTargetEndLevelCondition(pc, cc));
        }
        offset += targetLinks;

        foreach (int[] blockedCell in blockedCells)
        {
            GameObject go = findAt(blockedCell[0], blockedCell[1]);
            if (go.tag == "cell" || go.tag == "pointer")
            {
                CellBehavior cc = go.GetComponent <CellBehavior>();
                cc.Blocked |= blockedCell[2];
            }
        }

        setDisplayTypeOnObjects();
        int  remaining = lines.Length - offset;
        int  tools     = 0;
        bool code      = false;

        for (int i = 0; i < remaining; i++)
        {
            string[] line    = Regex.Split(lines[offset + i], "[\\s,]+");
            string   keyword = line[0];
            if (keyword == "end")
            {
                code = false;
            }
            else if (code)
            {
                loaded.solutionCode.Add(lines[offset + i] + ";");
            }
            else if (keyword == "begin")
            {
                code = true;
            }
            else if (keyword == "heapArea")
            {
                loaded.heapArea = new int[] { Int32.Parse(line[1]), Int32.Parse(line[2]), Int32.Parse(line[3]), Int32.Parse(line[4]) };
            }
            else if (keyword == "addy")
            {
                ampersand.ActiveTool = MakeTool(GetToolProto(line[1]));
                if (line.Length >= 3)
                {
                    ampersand.InActiveTool = MakeTool(GetToolProto(line[2]));
                }
                tools += 1;
            }
            else if (keyword == "val")
            {
                star.ActiveTool = MakeTool(GetToolProto(line[1]));
                if (line.Length >= 3)
                {
                    star.InActiveTool = MakeTool(GetToolProto(line[2]));
                }
                tools += 2;
            }
            else if (keyword == "par")
            {
                loaded.par = Int32.Parse(line[1]);
            }
        }

        if ((tools & 1) == 0)
        {
            if (world == 0)
            {
                ampersand.ActiveTool = MakeTool(valueTool);
            }
            else if (world >= 3)
            {
                ampersand.ActiveTool = MakeTool(pointerTool);
            }
            else
            {
                ampersand.ActiveTool = MakeTool(pointerTool);
            }
        }
        if ((tools & 2) == 0)
        {
            if (world == 0)
            {
                star.ActiveTool = MakeTool(valueTool);
            }
            else if (world >= 3)
            {
                star.ActiveTool   = MakeTool(valueTool);
                star.InActiveTool = MakeTool(incrementTool);
            }
            else
            {
                star.ActiveTool = MakeTool(valueTool);
            }
        }

        levelController.Current = loaded;
    }