// Copyright (c) Craftwork Games. All rights reserved. // Licensed under the MIT license. // See LICENSE file in the project root for full license information. using System; using System.Collections.Generic; namespace MonoGame.Extended.Graphics; /// /// Represents a sprite sheet containing textures and animations. /// public class SpriteSheet { private readonly Dictionary _animations = new Dictionary(); /// /// Gets the number of animations defined in the sprite sheet. /// public int AnimationCount => _animations.Count; /// /// Gets the name of the sprite sheet. /// public string Name { get; } /// /// Gets the texture atlas associated with the sprite sheet. /// public Texture2DAtlas TextureAtlas { get; } /// /// Initializes a new instance of the class. /// /// The name of the sprite sheet. /// The texture atlas to use for the sprite sheet. /// Thrown when the is null. public SpriteSheet(string name, Texture2DAtlas textureAtlas) { ArgumentNullException.ThrowIfNull(textureAtlas); TextureAtlas = textureAtlas; Name = name; } /// /// Creates a sprite from the specified region index. /// /// The index of the region in the texture atlas. /// A new instance. public Sprite CreateSprite(int regionIndex) => TextureAtlas.CreateSprite(regionIndex); /// /// Creates a sprite from the specified region name. /// /// The name of the region in the texture atlas. /// A new instance. public Sprite CreateSprite(string regionName) => TextureAtlas.CreateSprite(regionName); /// /// Defines a new animation for the sprite sheet. /// /// The name of the animation. /// The action to build the animation definition. public void DefineAnimation(string name, Action buildAction) { SpriteSheetAnimationBuilder builder = new SpriteSheetAnimationBuilder(name, this); buildAction(builder); SpriteSheetAnimation definition = builder.Build(); _animations.Add(name, definition); } public SpriteSheetAnimation GetAnimation(string name) => _animations[name]; /// /// Removes the animation definition with the specified name. /// /// The name of the animation to remove. /// true if the animation was successfully removed; otherwise, false. public bool RemoveAnimationDefinition(string name) => _animations.Remove(name); }