示例#1
0
        //Serialize a set of rectangles(RectList) to SerializedRects
        public static SerializedRects RectsToSerializedRects(RectList rectSet)
        {
            GridConverter.BuildCircuit(rectSet, false);
            var sb = new StringBuilder();

            bool mode2D = false;// RectsAre2D(rectSet);

            //Sort the rectlist into sets of rectlists where rects have same properties
            IEnumerable <List <Rect> > superRects = SuperSortByProperties(rectSet);

            //Iterate through entire set of rectlists
            foreach (List <Rect> rectList in superRects)
            {
                string rectPropertiesSerialized = SerializeRectProperties(rectList[0].Properties, mode2D);
                sb.Append(rectPropertiesSerialized);

                //Iterate through each rect in the set (will all have same properties)
                //Each will inherit properties values when read later from last properties write
                foreach (Rect rect in rectList)
                {
                    string rectSerialized = Transcode64.SerializeRectPointsToString(rect, 1, mode2D);
                    rectSerialized += SerializeCircuitProperties(rectList[0].Properties);
                    sb.Append(rectSerialized);
                }
            }
            //Then return new serialized rect with that string
            return(new SerializedRects(sb.ToString()));
        }
示例#2
0
 //Circuit
 public static void BuildCircuit(RectList rects, bool verbose)
 {
     GridConverter.BuildCircuit(rects, verbose);
 }
示例#3
0
        //Deserialized SerializedRects back to set of rectangles(RectList)
        public static RectList SerializedRectsToRects(SerializedRects serializedRects)
        {
            RectList rects     = new RectList();
            char     state     = ' ';
            byte     shapeId   = 0;
            byte     textureId = 0;
            ulong    rgba      = 0;
            byte     groupId   = 0;
            ulong    worldid   = 0;
            string   serial    = serializedRects.SerializedData;
            char     mode      = CharRects3D;


            for (int i = 0; i < serial.Length; i++)
            {
                if (serial[i] == CharRects2D)
                {
                    mode = CharRects2D; state = CharRgba;
                }
                if (serial[i] == CharRgba)
                {
                    mode = CharRects3D; state = CharRgba;
                }
                if (serial[i] == CharGroup)
                {
                    state = CharGroup;
                }
                if (serial[i] == CharLimit255)
                {
                    state = CharLimit255;
                }
                if (serial[i] == CharCircuit)
                {
                    state = CharCircuit;
                }
                if (serial[i] == CharWorld)
                {
                    state = CharWorld;
                }
                switch (state)
                {
                case CharGroup:
                {
                    groupId = Transcode64.From64(serial[++i]);
                    state   = mode;
                    break;
                }

                case CharRgba:    //Reading RGBA palette
                {
                    i++;
                    int r = Transcode64.From64(serial[i++]);
                    int g = Transcode64.From64(serial[i++]);
                    int b = Transcode64.From64(serial[i++]);
                    int a = Transcode64.From64(serial[i++]);

                    rgba      = Transcode64.RecodeRgbatoUlong((byte)r, (byte)g, (byte)b, (byte)a);
                    shapeId   = Transcode64.From64(serial[i++]);
                    textureId = Transcode64.From64(serial[i]);
                    state     = mode;

                    break;
                }

                case CharRects2D:    //Reading emergents
                {
                    Rect rect = new Rect();
                    rect.Pt1[0] = Transcode64.From64(serial[i++]);
                    rect.Pt1[2] = Transcode64.From64(serial[i++]);

                    rect.Pt2[0] = Transcode64.From64(serial[i++]);
                    rect.Pt2[2] = Transcode64.From64(serial[i]);

                    //Convert from inclusve
                    rect.Pt2[0]++;
                    rect.Pt2[2]++;

                    rect.Properties.Rgba      = rgba;
                    rect.Properties.ShapeId   = shapeId;
                    rect.Properties.TextureId = textureId;
                    rect.Properties.GroupId   = groupId;
                    rect.Properties.WorldId   = worldid;
                    rects.AddRect(rect);
                    break;
                }

                case CharRects3D:    //Reading emergents
                {
                    Rect rect = new Rect();
                    rect.Pt1[0] = Transcode64.From64(serial[i++]);
                    rect.Pt1[1] = Transcode64.From64(serial[i++]);
                    rect.Pt1[2] = Transcode64.From64(serial[i++]);

                    rect.Pt2[0] = Transcode64.From64(serial[i++]);
                    rect.Pt2[1] = Transcode64.From64(serial[i++]);
                    rect.Pt2[2] = Transcode64.From64(serial[i]);

                    //Convert from inclusve
                    rect.Pt2[0]++;
                    rect.Pt2[1]++;
                    rect.Pt2[2]++;

                    rect.Properties.Rgba      = rgba;
                    rect.Properties.ShapeId   = shapeId;
                    rect.Properties.TextureId = textureId;
                    rect.Properties.GroupId   = groupId;
                    rect.Properties.WorldId   = worldid;
                    rects.AddRect(rect);
                    break;
                }

                case CharCircuit:
                {
                    i++;
                    byte pid = Transcode64.From64(serial[i++]);      //physics id
                    byte cid = Transcode64.From64(serial[i]);        //circuit id
                    if (rects.GetRect(rects.Count - 1).Properties.CircuitIds == null)
                    {
                        rects.GetRect(rects.Count - 1).Properties.CircuitIds = new List <int>();
                    }

                    rects.GetRect(rects.Count - 1).Properties.PhysicsId = pid;
                    rects.GetRect(rects.Count - 1).Properties.CircuitIds.Add(cid);
                    state = mode;
                    break;
                }

                case CharWorld:    //Reading World ID
                {
                    i++;
                    int v4 = Transcode64.From64(serial[i++]);
                    int v3 = Transcode64.From64(serial[i++]);
                    int v2 = Transcode64.From64(serial[i++]);
                    int v1 = Transcode64.From64(serial[i++]);

                    worldid = (ulong)((v4 << 24) | (v3 << 16) | (v2 << 8) | v1);
                    state   = mode;

                    break;
                }
                }
            }
            GridConverter.BuildCircuit(rects, false);
            return(rects);
        }