示例#1
0
文件: map.cs 项目: Wirless/cyclops
        /// <summary>
        /// Get the tile at the specified position or null if no
        /// such maptile exists.
        /// </summary>
        /// <param name="x">The x coordinate of the position.</param>
        /// <param name="y">The y coordinate of the position.</param>
        /// <param name="z">The z coordinate of the position.</param>
        /// <returns>The tile at the specified position.</returns>
        public Tile GetTile(int x, int y, byte z)
        {
            int  hash = Position.HashCode((ushort)x, (ushort)y);
            Tile tile;
            //Check if tile is cached
            bool exists = cachedTiles[z].TryGetValue(hash, out tile);

            if (exists)
            {
                return(tile);
            }

            ushort[] vals;
            //Tile isn't cached, try main
            exists = mainTiles[z].TryGetValue(hash, out vals);
            if (exists)
            {
                //Create tile and add to cached
                Item ground = Item.CreateItem(vals[0]);
                ground.CurrentPosition = new Position((ushort)x, (ushort)y, z);
                tile = new Tile(ground);
                for (int i = 1; i < vals.Length; i++)
                {
                    if (vals[i] == 0)
                    {
                        Log.Write("Invalid ID at position x: " + x + " y: " + y + " z: " + z);
                    }
                    else
                    {
                        Item item = Item.CreateItem(vals[i]);
                        item.CurrentPosition = new Position((ushort)x, (ushort)y, z);
                        tile.AddThing(item);
                    }
                }

                //Cache it
                cachedTiles[z].Add(hash, tile);
                return(tile);
            }

            return(null);
        }
示例#2
0
文件: map.cs 项目: Wirless/cyclops
        /// <summary>
        /// Load the map.
        /// </summary>
        public static Map Load()
        {
            FileHandler handler = new FileHandler();
            //string path = Config.GetDataPath() + "world/" + Config.GetMapName();  TODO: REMOVE
            string       path    = Config.GetDataPath() + Config.GetWorldDirectory() + Config.GetMapName();
            BinaryReader bReader = new BinaryReader(File.Open(path, FileMode.Open));
            int          sizex   = bReader.ReadUInt16(); //Height
            int          sizey   = bReader.ReadUInt16(); //Width
            Map          map     = new Map(sizex, sizey);

            uint tileCount = bReader.ReadUInt32(); //Tile Count

            for (int i = 0; i < tileCount; i++)
            {
                ushort x         = bReader.ReadUInt16(); //X position
                ushort y         = bReader.ReadUInt16(); //Y position
                byte   z         = bReader.ReadByte();   //Z position
                ushort id        = bReader.ReadUInt16(); //Tile ID
                byte   itemCount = bReader.ReadByte();

                ushort[] mainTile = new ushort[itemCount + 1]; //Item count + ground
                mainTile[0] = id;
                //map.SetTile(x, y, z, new Tile(Item.CreateItem(id)));

                for (int j = 1; j <= itemCount; j++)
                {
                    ushort itemID = bReader.ReadUInt16();
                    mainTile[j] = itemID;
                }
                int hashCode = Position.HashCode(x, y);
                map.mainTiles[z].Add(hashCode, mainTile);
            }

            bReader.Close();
            return(map);
        }
示例#3
0
文件: map.cs 项目: Wirless/cyclops
        /// <summary>
        /// Set the tile at the specified position.
        /// </summary>
        /// <param name="x">The x coordinate of the position.</param>
        /// <param name="y">The y coordinate of the position.</param>
        /// <param name="z">The z coordinate of the position.</param>
        /// <param name="tile">The tile to set.</param>
        public void SetTile(ushort x, ushort y, byte z, Tile tile)
        {
            int hash = Position.HashCode(x, y);

            cachedTiles[z].Add(hash, tile);
        }