Example #1
0
        /// <summary>
        /// Loads a sprite animation based on the name of the files
        /// The texture files need to be loaded previously
        /// The names of the animation frame files should begin with the same string
        /// and add the number of the frame separated with an underscore
        /// </summary>
        /// <param name="textureName">Identifier of the texture files of each frame</param>
        /// <returns>Returns the created Animation State</returns>
        public IAnimationState LoadAnimation(string animationName, string textureName)
        {
            //Create the animation state
            IAnimationState animation = ASMachine.LoadState <SpriteAnimation>(textureName);
            //Get the initial texture for the first frame
            Texture2D frame = resourceManager.GetTexture(textureName);

            if (frame == null)
            {
                frame = resourceManager.GetTexture(textureName + "_0");
            }

            List <Texture2D> frames = new List <Texture2D>();
            int i = 1;

            //Adds the various frames until there are no more frames to add
            while (frame != null)
            {
                frames.Add(frame);
                frame = resourceManager.GetTexture(textureName + "_" + i);
                i++;
            }

            if (frames.Count == 0)
            {
                Console.WriteLine("No frames were found with name: " + textureName);
            }
            //Loads the franes into the animation
            SpriteAnimationFrames animationFrames = new SpriteAnimationFrames();

            animationFrames.frames = frames.ToArray();
            animation.LoadFrames(animationFrames);
            return(animation);
        }
        /// <summary>
        /// Loads the frames of the animation
        /// </summary>
        /// <param name="framesStruct">Struct containing the array with the textures for each frame</param>
        public void LoadFrames(IAnimationFrames framesStruct)
        {
            SpriteAnimationFrames animationFrames = (SpriteAnimationFrames)framesStruct;

            frames = animationFrames.frames;
            currentFrame.Texture  = frames[0];
            currentFrame.DrawArea = new Rectangle(0, 0, currentFrame.Texture.Width, currentFrame.Texture.Height);
            frameIndex            = 0;
        }