Ejemplo n.º 1
0
        /// <summary>Loads the default grass sprites into the sprite pools.</summary>
        /// <param name="grassTexture">The grass texture.</param>
        private void LoadDefaultGrass(Texture2D grassTexture)
        {
            foreach (var season in new[] { "spring", "summer", "fall", "winter" })
            {
                // clear any existing default grass sprites in the sprite pool
                switch (season)
                {
                case "spring": SpringSpritePool.ClearDefaultGrass(); break;

                case "summer": SummerSpritePool.ClearDefaultGrass(); break;

                case "fall": FallSpritePool.ClearDefaultGrass(); break;

                case "winter": WinterSpritePool.ClearDefaultGrass(); break;
                }

                // calculate the default grass bounds
                var yOffset = 0;
                switch (season)
                {
                case "summer": yOffset = 21; break;

                case "fall": yOffset = 41; break;

                case "winter": yOffset = 81; break;
                }
                var grassBounds = new[] { new Rectangle(0, yOffset, 15, 20), new Rectangle(16, yOffset, 15, 20), new Rectangle(30, yOffset, 15, 20) };

                // load the individual grass sprites in the correct sprite pool using the above calculated bounds
                foreach (var grassBound in grassBounds)
                {
                    // create a new texture using the grassBound
                    var grassSprite = new Texture2D(Game1.graphics.GraphicsDevice, grassBound.Width, grassBound.Height);
                    var grassData   = new Color[grassBound.Width * grassBound.Height];
                    grassTexture.GetData(0, grassBound, grassData, 0, grassData.Length);
                    grassSprite.SetData(grassData);

                    // add sprite to correct sprite pool
                    switch (season)
                    {
                    case "spring": SpringSpritePool.AddDefaultGrass(grassSprite); break;

                    case "summer": SummerSpritePool.AddDefaultGrass(grassSprite); break;

                    case "fall": FallSpritePool.AddDefaultGrass(grassSprite); break;

                    case "winter": WinterSpritePool.AddDefaultGrass(grassSprite); break;
                    }
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>Loads all the sprites from the specified directory into the correct sprite pool.</summary>
        /// <param name="directory">The absolute directory containing the sprites.</param>
        /// <param name="contentPack">The content pack currently being loaded.</param>
        /// <param name="season">The season to load the images into.</param>
        private void LoadSpritesFromDirectory(string directory, IContentPack contentPack, ContentPackConfig contentPackConfig, string season)
        {
            foreach (var file in Directory.GetFiles(directory))
            {
                // ensure file is an image file
                if (!file.EndsWith(".png"))
                {
                    this.Monitor.Log($"Invalid file in folder: {file}");
                    continue;
                }

                // get the grass texture
                var relativePath = Path.Combine(season, Path.GetFileName(file));

                // a rare bug on Unix causes Directory.GetFiles(string) to return invalid files, it'll return a list of the expected files as well as a copy of each file but prefixed with "._"
                // these files don't actually exist and cause the below to throw an exception, I tried checking if the files started with "._" but that didn't work, in the end silently
                // catching the exception and ignoring it seemed to be the only way for it to work. silently catching shouldn't be a problem here as that shouldn't throw any other exception anyway
                Texture2D grassTexture;
                try { grassTexture = contentPack.LoadAsset <Texture2D>(relativePath); }
                catch { continue; }

                if (grassTexture == null)
                {
                    this.Monitor.Log($"Failed to get grass sprite. Path expected: {relativePath}");
                    continue;
                }

                // add the texture to the correct sprite pool
                var whiteListedLocations = contentPackConfig.WhiteListedLocations ?? new List <string>();
                whiteListedLocations.AddRange(contentPackConfig.WhiteListedGrass.FirstOrDefault(whiteListedGrass => whiteListedGrass.Key.ToLower() == new FileInfo(file).Name.ToLower()).Value ?? new List <string>());

                var blackListedLocations = contentPackConfig.BlackListedLocations ?? new List <string>();
                blackListedLocations.AddRange(contentPackConfig.BlackListedGrass.FirstOrDefault(blackListedGrass => blackListedGrass.Key.ToLower() == new FileInfo(file).Name.ToLower()).Value ?? new List <string>());

                switch (season)
                {
                case "spring": SpringSpritePool.AddCustomGrass(grassTexture, whiteListedLocations, blackListedLocations); break;

                case "summer": SummerSpritePool.AddCustomGrass(grassTexture, whiteListedLocations, blackListedLocations); break;

                case "fall": FallSpritePool.AddCustomGrass(grassTexture, whiteListedLocations, blackListedLocations); break;

                case "winter": WinterSpritePool.AddCustomGrass(grassTexture, whiteListedLocations, blackListedLocations); break;
                }
            }
        }