Пример #1
0
        /*********
        ** Public methods
        *********/

        /// <summary>Construct an instance.</summary>
        /// <param name="position">The position the door is at.</param>
        /// <param name="property">Parsed door info.</param>
        /// <param name="map">The map the door will modify.</param>
        /// <param name="extras">Parsed door extras.</param>
        /// <param name="callbackTimer">Timer for animations.</param>
        public PendingDoor(Point position, MapDoor property, Map map, MapDoorExtras extras, CallbackTimer callbackTimer)
        {
            this.map           = map;
            this.Position      = position;
            this.Property      = property;
            this.extras        = extras;
            this.callbackTimer = callbackTimer;
        }
        /// <summary>Finds every door in a map.</summary>
        /// <param name="map">The map to look in.</param>
        /// <param name="pendingDoors">The doors that were found.</param>
        /// <returns>Whether any doors were found.</returns>
        public bool FindDoorsInLocation(Map map, out IList <PendingDoor> pendingDoors)
        {
            pendingDoors = new List <PendingDoor>();

            Layer backLayer = map?.GetLayer("Back");

            if (backLayer == null)
            {
                return(false);
            }

            // Search for doors in the provided location.
            bool foundDoors = false;

            for (int x = 0; x < backLayer.LayerWidth; x++)
            {
                for (int y = 0; y < backLayer.LayerHeight; y++)
                {
                    Tile tile = backLayer.Tiles[x, y];

                    if (tile == null)
                    {
                        continue;
                    }

                    // If there's no version property log an error if other properties are present.
                    if (!tile.Properties.TryGetValue(MapDoorVersion.PropertyKey, out PropertyValue doorVersionValue))
                    {
                        if (tile.Properties.ContainsKey(MapDoor.PropertyKey) || tile.Properties.ContainsKey(MapDoorExtras.PropertyKey))
                        {
                            this.errorQueue.AddError($"The door at ({x},{y}) is malformed. Info: Missing a {MapDoorVersion.PropertyKey} property.");
                        }

                        continue;
                    }

                    // Log an error if the version is invalid.
                    if (!MapDoorVersion.TryParseString(doorVersionValue.ToString(), out string error, out MapDoorVersion version))
                    {
                        this.errorQueue.AddError($"The {MapDoorVersion.PropertyKey} property at ({x},{y}) is malformed. Info: {error}.");
                        continue;
                    }

                    if (version.PropertyVersion.IsNewerThan(this.modVersion))
                    {
                        this.errorQueue.AddError($"The door at ({x},{y}) is too new to be loaded ({version.PropertyVersion}). Please update Better Doors! ");
                        continue;
                    }

                    // Log an error if the door property is missing.
                    if (!tile.Properties.TryGetValue(MapDoor.PropertyKey, out PropertyValue doorValue))
                    {
                        this.errorQueue.AddError($"The door at ({x},{y}) is malformed. Info: No {MapDoor.PropertyKey} property was found.");
                        continue;
                    }

                    // Log an error if the door property is invalid.
                    if (!MapDoor.TryParseString(doorValue.ToString(), version.PropertyVersion, out error, out MapDoor property))
                    {
                        this.errorQueue.AddError($"The {MapDoor.PropertyKey} property at ({x},{y}) is malformed. Info: {error}.");
                        continue;
                    }

                    // Log an error if the door extras property is present but invalid.
                    MapDoorExtras extras = new MapDoorExtras();
                    if (tile.Properties.TryGetValue(MapDoorExtras.PropertyKey, out PropertyValue doorExtraValue) && !MapDoorExtras.TryParseString(doorExtraValue.ToString(), version.PropertyVersion, out error, out extras))
                    {
                        this.errorQueue.AddError($"The {MapDoorExtras.PropertyKey} property at ({x},{y}) is malformed. Info: {error}.");
                        continue;
                    }

                    // Log an error for invalid door and extras property combinations.
                    if (property.Orientation == Orientation.Horizontal && extras.IsDoubleDoor)
                    {
                        this.errorQueue.AddError($"The door at ({x},{y}) is invalid. Info: Horizontal doors can't be double doors.");
                        continue;
                    }

                    foundDoors = true;

                    // Record sprite request.
                    this.doorTileInfoManager.RegisterDoorRequest(property.ModId, property.DoorName, property.Orientation, property.OpeningDirection);

                    // Mark door as pending.
                    pendingDoors.Add(new PendingDoor(new Point(x, y), property, map, extras, this.timer));
                }
            }

            this.errorQueue.PrintErrors("Found some errors when parsing doors from maps:");

            return(foundDoors);
        }