예제 #1
0
        //Untested
        /// <summary>
        /// Converts all needed information for saving a tile into a condensed form (16 bits).
        /// </summary>
        /// <param name="readFrom">A Single FloorTile to be serialized</param>
        /// <returns>A 16-bit representation of the FloorTile. If Negative, the tile is disabled.</returns>
        public static Int16 Serialize(ref FloorTile readFrom)
        {
            //Need to list order and usage of bits here.

            // [10000000 00000000] - Enabled                                                    0x80 = 10000000
            // [01100000 00000000] - Tile Type (Floor, Hole, Fake Wall, Wall)                   0x40 = 01000000
            // [00010000 00000000] - Item Object (Vase, Chest, Dropped Items, Corpse, Etc)
            // [00001000 00000000] - Monster Spawn
            // [00000100 00000000] - Constant Floor Effect (Fire, etc.)
            // [00000010 00000000] - Floor Decoration (Rocks, Debris, Bones, Etc)
            // [00000001 00000000] - Yellow Wire
            // [00000000 10000000] - Green Wire
            // [00000000 01000000] - Blue Wire
            // [00000000 00100000] - Red Wire
            // [00000000 00011111] - Tile Theme (Placed at end for simple addition)

            UInt16 temp = 0;

            if (readFrom.Enabled)
            {
                temp = (UInt16)(temp | 0x8000);     // 1000 0000
            }

            switch (readFrom.Type)
            {
                case TileType.FLOOR:
                    // Keep Default Value of 0      // 0000 0000
                    break;
                case TileType.HOLE:
                    temp = (UInt16)(temp | 0x2000); // 0010 0000
                    break;
                case TileType.ILLUSION_WALL:
                    temp = (UInt16)(temp | 0x4000); // 0100 0000
                    break;
                case TileType.WALL:
                    temp = (UInt16)(temp | 0x6000); // 0110 0000
                    break;
                default:
                    //Default to WALL
                    temp = (UInt16)(temp | 0x6000); // 0110 0000
                    break;
            }

            if (readFrom.ItemExists)
            {
                temp = (UInt16)(temp | 0x1000);     // 0001 0000
            }

            if (readFrom.MonsterExists)
            {
                temp = (UInt16)(temp | 0x0800);     // 0000 1000
            }

            if (readFrom.ConstantEffectExists)
            {
                temp = (UInt16)(temp | 0x0400);     // 0000 0100
            }

            if (readFrom.DecorationExists)
            {
                temp = (UInt16)(temp | 0x0200);     // 0000 0010
            }

            if (readFrom.YellowWire)
            {
                temp = (UInt16)(temp | 0x0100);
            }

            if (readFrom.GreenWire)
            {
                temp = (UInt16)(temp | 0x0080);
            }

            if (readFrom.BlueWire)
            {
                temp = (UInt16)(temp | 0x0040);
            }

            if (readFrom.RedWire)
            {
                temp = (UInt16)(temp | 0x0020);
            }

            // Should just add the Theme value to the variable, no need for a long switch statement or bit shifting
            temp = (UInt16)(temp + (UInt16)readFrom.Theme);

            return (Int16)temp; //Using a signed variable to assist determining if the tile is enabled or not
        }
예제 #2
0
        //Untested
        public static FloorTile Deserialize(Int16 readFrom)
        {
            // [10000000 00000000] - Enabled                                                    0x80 = 10000000
            // [01100000 00000000] - Tile Type (Floor, Hole, Fake Wall, Wall)                   0x40 = 01000000
            // [00010000 00000000] - Item Object (Vase, Chest, Dropped Items, Corpse, Etc)
            // [00001000 00000000] - Monster Spawn
            // [00000100 00000000] - Constant Floor Effect (Fire, etc.)
            // [00000010 00000000] - Floor Decoration (Rocks, Debris, Bones, Etc)
            // [00000001 00000000] - Yellow Wire
            // [00000000 10000000] - Green Wire
            // [00000000 01000000] - Blue Wire
            // [00000000 00100000] - Red Wire
            // [00000000 00011111] - Tile Theme (Placed at end for simple addition)

            FloorTile temp = new FloorTile();
            Int16 tempRead;

            // If Negative - thankfully casting doesn't change the sign bit
            if (readFrom < 0)
            {
                temp.Enabled = true;
            }

            tempRead = (Int16)(readFrom & 0x6000);
            switch (tempRead)
            {
                case 0x0000:
                    temp.Type = TileType.FLOOR;
                    break;
                case 0x2000:
                    temp.Type = TileType.HOLE;
                    break;
                case 0x4000:
                    temp.Type = TileType.ILLUSION_WALL;
                    break;
                case 0x6000:
                    temp.Type = TileType.WALL;
                    break;
                default:
                    temp.Type = TileType.WALL;
                    // Throw a console error
                    Console.Out.WriteLine("FloorTile.cs::Deserialize(Int16 readFrom) -- Impossible Error, TileType doesn't exist");
                    break;
            }

            // From this point on, I'm trying to decide if just bitshifting would be quicker or not.
            // Possible performance gain in this function.
            temp.ItemExists = (readFrom & 0x1000) != 0 ? true : false;
            temp.MonsterExists = (readFrom & 0x0800) != 0 ? true : false;
            temp.ConstantEffectExists = (readFrom & 0x0400) != 0 ? true : false;
            temp.DecorationExists = (readFrom & 0x0200) != 0 ? true : false;

            temp.YellowWire = (readFrom & 0x0100) != 0 ? true : false;
            temp.GreenWire = (readFrom & 0x0080) != 0 ? true : false;
            temp.BlueWire = (readFrom & 0x0040) != 0 ? true : false;
            temp.RedWire = (readFrom & 0x0020) != 0 ? true : false;

            tempRead = (Int16)(readFrom & 0x001F);
            temp.Theme = (TileTheme)tempRead;

            return temp;
        }