示例#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);
        }
示例#2
0
        /// <summary>
        /// Loads an animation based on the name of the name of the texture 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"></param>
        public void LoadAnimation(string textureName)
        {
            IAnimationState animation = ASMachine.LoadState <SpriteAnimation>(textureName);
            Texture2D       frame     = _owner.GetResources().GetTexture(textureName);

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

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

            while (frame != null)
            {
                frames.Add(frame);
                frame = _owner.GetResources().GetTexture(textureName + "_" + i);
                i++;
            }

            if (frames.Count == 0)
            {
                Console.WriteLine("Not frames were found with name: " + textureName);
            }
            animation.LoadFrames(frames.ToArray());
        }
示例#3
0
        /// <summary>
        /// Loads an animation based on the name of the texture previouly from a spritesheet. Texture should have been
        /// previously loaded in the resource manager. Set the number of rows and columns
        /// and choose the row that contains the animation
        /// Animation are loaded from a spritesheet from a row
        /// </summary>
        /// <param name="animationName">Name to be given to the state and the animation</param>
        /// <param name="textureName">Name of the texture file/ spritesheet</param>
        /// <param name="numRows">Number of rows that the spritesheet has</param>
        /// <param name="numCols">Number of cols that the spritesheet has</param>
        /// <param name="row">Row of the animation </param>
        /// <returns>Returns the state created that hold the animation</returns>
        public IAnimationState LoadAnimation(string animationName, string textureName, int numRows, int numCols, int row)
        {
            //Creates a new animation
            IAnimationState animation = ASMachine.LoadState <SpritsheetAnimation>(animationName);

            //Get the texture/ spritesheet fro mthe resources
            texture = resourceManager.GetTexture(textureName);

            //Calculates the width and height of each frame
            int width  = texture.Width / numCols;
            int height = texture.Height / numRows;
            //Calculates the pixels of the specific row to get the various frames
            int y = (height) * (row - 1);

            List <Rectangle> drawAreas = new List <Rectangle>();
            int x = 0;

            //Gets all the textures of a specific row
            while (true)
            {
                drawAreas.Add(new Rectangle(x, y, width, height));
                x += width;
                if (x + width >= texture.Width)
                {
                    break;
                }
            }
            //Loads all the animation frames found into the animation
            SpritesheetAnimationFrames animationFrames = new SpritesheetAnimationFrames();

            animationFrames.drawAreas   = drawAreas.ToArray();
            animationFrames.spriteSheet = texture;
            animationFrames.origin      = new Vector2(drawAreas[0].Width / 2, drawAreas[0].Height / 2);
            animation.LoadFrames(animationFrames);
            return(animation);
        }