示例#1
0
        /// <summary>
        ///     Attaches new region to <paramref name="view"/> and registers it under <paramref name="regionName"/> name.
        /// </summary>
        /// <remarks>
        ///     Exact type of <see cref="IRegion"/> created and attached will be determined by <see cref="IRegionFactory"/>
        ///     used by this <see cref="RegionManager"/>. Decision will be made based on <paramref name="view"/> type,
        ///     attributes or properties.
        /// </remarks>
        /// <param name="regionName">Name that new region will be registered under</param>
        /// <param name="view">View to attach region to</param>
        /// <returns>Attached region</returns>
        /// <exception cref="ArgumentException">
        ///     When another region has already been registered under <paramref name="regionName"/> name
        /// </exception>
        public IRegion AttachRegion(string regionName, DependencyObject view)
        {
            if (view == null)
            {
                throw new ArgumentNullException("view");
            }

            ValidateRegionName(regionName);
            if (ContainsRegion(regionName))
            {
                string message = string.Format("Region \"{0}\" already exists", regionName);
                throw new ArgumentException(message);
            }

            IRegion region = _regionFactory.CreateRegion(view);

            _regions.Add(regionName, region);
            return(region);
        }
示例#2
0
        public void Load(Stream mapStream,
                         Stream regionChangesStream,
                         bool createRegionChanges,
                         uint inWorldWidth,
                         uint inWorldHeight,
                         uint regionWidth,
                         uint regionHeight,
                         uint miniMapRegionWidth,
                         uint miniMapRegionHeight)
        {
            logger.Info("Loading map...");

            if (mapStream == null)
            {
                throw new Exception("Missing map");
            }

            if (regionChangesStream == null)
            {
                throw new Exception("Missing region changes");
            }

            if (regionWidth == 0 || regionHeight == 0 || inWorldWidth % regionWidth != 0 ||
                inWorldHeight % regionHeight != 0)
            {
                throw new Exception("Invalid region size configured");
            }

            if (miniMapRegionWidth == 0 || miniMapRegionHeight == 0 || inWorldWidth % miniMapRegionWidth != 0 ||
                inWorldHeight % miniMapRegionHeight != 0)
            {
                throw new Exception("Invalid city region size configured");
            }

            WorldWidth  = inWorldWidth;
            WorldHeight = inWorldHeight;

            // creating regions;
            RegionSize = regionWidth * regionHeight;

            var column = (int)(inWorldWidth / regionWidth);
            var row    = (int)(inWorldHeight / regionHeight);

            RegionsCount = column * row;
            MapData      = new byte[RegionSize * Region.TILE_SIZE * RegionsCount];

            RegionChanges = regionChangesStream;

            regions = new List <IRegion>(RegionsCount);

            for (int regionId = 0; regionId < RegionsCount; ++regionId)
            {
                var data = new byte[RegionSize * Region.TILE_SIZE]; // 1 tile is 2 bytes

                var mapDataOffset = (int)(RegionSize * Region.TILE_SIZE * regionId);

                if (mapStream.Read(MapData, mapDataOffset, (int)(RegionSize * Region.TILE_SIZE)) !=
                    RegionSize * Region.TILE_SIZE)
                {
                    throw new Exception("Not enough map data");
                }

                if (createRegionChanges)
                {
                    Buffer.BlockCopy(MapData, mapDataOffset, data, 0, data.Length);
                    regionChangesStream.Write(data, 0, data.Length);
                }
                else
                {
                    if (RegionChanges.Read(data, 0, (int)(RegionSize * Region.TILE_SIZE)) !=
                        RegionSize * Region.TILE_SIZE)
                    {
                        throw new Exception("Not enough region change map data");
                    }
                }

                AddRegion(regionFactory.CreateRegion(data));
            }

            logger.Info(String.Format("Loaded map file regions[{0}]", regions.Count));

            // creating city regions;
            column = (int)(inWorldWidth / miniMapRegionWidth);
            row    = (int)(inWorldHeight / miniMapRegionHeight);
            int miniMapRegionsCount = column * row;

            MiniMapRegions = miniMapRegionManagerFactory.CreateMiniMapRegionManager(miniMapRegionsCount);
        }