Ejemplo n.º 1
0
    //GENERATE MAP
    static public bool Generator(byte[] state, out Tile[,] lowarray, out Tile[,] midarray, out Tile[,] higharray, Transform lowplane, Transform midplane, Transform highplane)
    {
        // PARSE HEADER

        // byte array converted to string
        string statestring = System.Text.Encoding.ASCII.GetString(state);

        // header variables
        int    w        = 0;
        int    h        = 0;
        string woodstr  = "";
        string gemstr   = "";
        string stonestr = "";
        int    players  = 0;
        string datatype = "";

        // get the variables in the header
        foreach (string s in statestring.Split('\n'))
        {
            string[] spl = s.Split(' ');
            switch (spl[0])
            {
            case "WIDTH":
                w = int.Parse(spl[1]);
                break;

            case "HEIGHT":
                h = int.Parse(spl[1]);
                break;

            case "PLAYERS":
                players = int.Parse(spl[1]);
                break;

            case "FRAGS":
                GenerateLevel.ParsePlayersFragments(s);
                print(s);
                break;

            case "WOOD":
                woodstr = s;
                break;

            case "STONE":
                stonestr = s;
                break;

            case "GEMS":
                gemstr = s;
                break;

            case "CURRENTTURN":
                PlayerManager.PM.CurrTurn = int.Parse(spl[1]);
                break;

            case "DATA":
                datatype = (spl.Length > 1)? spl[1] : "";
                break;
            }

            if (spl[0] == "DATA")
            {
                break;
            }
        }

        // Set wood
        string[] tempspl = woodstr.Split(' ');
        for (int i = 1; i < tempspl.Length && i < players + 1; i++)
        {
            FragmentManager.FM.SetWood(int.Parse(tempspl[i]), i);
        }
        // Set stone
        tempspl = stonestr.Split(' ');
        for (int i = 1; i < tempspl.Length && i < players + 1; i++)
        {
            FragmentManager.FM.SetStone(int.Parse(tempspl[i]), i);
        }
        // Set stone
        tempspl = gemstr.Split(' ');
        for (int i = 1; i < tempspl.Length && i < players + 1; i++)
        {
            FragmentManager.FM.SetGems(int.Parse(tempspl[i]), i);
        }

        //PARSE BODY DATA

        //make the arrays more easily accesible via layers
        lowarray             = new Tile[w, h];
        midarray             = new Tile[w, h];
        higharray            = new Tile[w, h];
        Tile[][,] tilearrays = { lowarray, midarray, higharray };
        Transform[] transarray = { lowplane, midplane, highplane };

        // point at which the binary data starts
        int dataStart = statestring.IndexOf("\n", statestring.IndexOf("DATA")) + 1;

        //copy data out to decompress it
        if (datatype == "binary_compressed")
        {
            byte[] boarddata = new byte[state.Length - dataStart];
            System.Buffer.BlockCopy(state, dataStart, boarddata, 0, state.Length - dataStart);

            boarddata = CompressionHelper0_1.Decompress(boarddata);

            state     = boarddata;
            dataStart = 0;
        }

        //generate the level
        for (int i = dataStart; i < state.Length; i++)
        {
            //get the x and y and layer for the tile
            int y     = (i - dataStart) / (6 * w);
            int x     = (((i - dataStart) / 6) - y * w) % w;
            int layer = y / h;
            y %= h;

            // if the layer is too big, run
            if (layer > 2)
            {
                break;
            }

            //store the recent tile
            Tile t = null;

            if (state[i] != 0)
            {
                const float rad = 4.31f;

                //create the tile
                t = Instantiate(Resources.Load("prefabs/Tile", typeof(Tile))) as Tile;

                //place and store the layer
                t.transform.parent      = transarray[layer];
                tilearrays[layer][x, y] = t;

                t.transform.localPosition = new Vector3((x - w / 2) * rad * 3.45f + rad * (y % 2) * 1.725f, 0, (y - h / 2) * rad * 1);

                // BYTE 1
                t.ORIG_HEIGHT = state[i] - 1 - 10;                        // original height
                // BYTE 2
                t.setHeight((state[i + 1] & HEIGHT_MASK) - 1 - 10);       // current height
                t.setRichness(((state[i + 1] & RICHNESS_MASK) >> 5) - 1); // current richness

                // BYTE 3 & 4
                ByteToUnit(state[i + 2], state[i + 3], t);                        //create unit on the tile

                // BYTE 5
                ByteToSurfFrag(state[i + 4], t);

                // BYTE 6
                t.setFragment((FragType)(state[i + 5] & FRAGMENT_MASK));                    //sets the internal fragment of the tile
            }


            i += 5;
        }

        return(false);
    }