Exemple #1
0
        /// <summary>
        ///     Get the Rectangle definition of the slice at the current frame of
        ///     animation, if there is a slice key defined for the frame
        /// </summary>
        /// <param name="sliceName">The name of the slice</param>
        /// <returns>
        ///     A Rectangle definition of the frame slice, at the xy-coordinate of
        ///     this sprite.  If no slice key exists for the current frame,
        ///     null is returned.
        /// </returns>
        /// <exception cref="ArgumentException">
        ///     Thrown if the slice name provided does not exist in the animation definitions slice dictionary
        /// </exception>
        public Rectangle?GetCurrentFrameSlice(string sliceName)
        {
            //  Ensure that we have a slice defined with the given name
            if (_animationDefinition.Slices.ContainsKey(sliceName))
            {
                //  Get the slice
                Slice slice = _animationDefinition.Slices[sliceName];

                //  Ensure we have a slice key at the current animation frame index
                if (slice.keys.ContainsKey(CurrentFrameIndex))
                {
                    //  Get the slice key
                    SliceKey sliceKey = slice.keys[CurrentFrameIndex];

                    //  Get the rectangle that represents the bounds of the slicekey
                    Rectangle rect = sliceKey.bounds;

                    //  Update the xy-coordinate of the rect to match the positional data of this sprite
                    rect.X += (int)Position.X;
                    rect.Y += (int)Position.Y;

                    //  return the rectangle
                    return(rect);
                }
                else
                {
                    //  There is no slicekey for the current frame index, so we return null
                    return(null);
                }
            }
            else
            {
                //  No slice exists with the given name, throw error
                throw new ArgumentException($"The animation definition does not contain a slice definition with the name {sliceName}");
            }
        }
Exemple #2
0
        /// <summary>
        ///     Reads the AnimationDefinition for the content manager
        /// </summary>
        /// <param name="input">Provided by the content manager when using load</param>
        /// <param name="existingInstance">Provided by the content manter when using load</param>
        /// <returns></returns>
        protected override AnimationDefinition Read(ContentReader input, AnimationDefinition existingInstance)
        {
            //  Read how many frames there are in total
            int frameCount = input.ReadInt32();

            //  Initialize a new list of frames
            List <Frame> frames = new List <Frame>();

            //  Read all the data about the frames
            for (int i = 0; i < frameCount; i++)
            {
                //  Read the x-coordinate value
                int x = input.ReadInt32();

                //  Read the y-coordinate value
                int y = input.ReadInt32();

                //  Read the (w)idth value
                int w = input.ReadInt32();

                //  Read the (h)eight value
                int h = input.ReadInt32();

                //  Read the duration value
                //  Aseprite exports the millisecond value, but we need
                //  to know the total seconds so we can compare with
                //  GameTime.TotalSeconds as is standard when getting delta
                //  time in MonoGame
                float duration = input.ReadInt32() / 1000.0f;

                //  Create a frame
                Frame frame = new Frame(x, y, w, h, duration);

                //  Store the frame
                frames.Add(frame);
            }

            //  Read how many animation definitions there are in total
            int animationCount = input.ReadInt32();

            //  Initilize a new dictionary for the animations
            Dictionary <string, Animation> animations = new Dictionary <string, Animation>();

            //  Read all the animation definition data
            for (int i = 0; i < animationCount; i++)
            {
                //  Read the animation name
                string name = input.ReadString();

                //  Read the starting (from) frame
                int from = input.ReadInt32();

                //  Read the ending (to) frame
                int to = input.ReadInt32();

                //  Create a new animation definition
                Animation animation = new Animation(name, from, to);

                //  Store the animation
                animations.Add(animation.name, animation);
            }

            //  Read how many slice definitons there are in total
            int sliceCount = input.ReadInt32();

            //  Initilize a new dictionary for the slices
            Dictionary <string, Slice> slices = new Dictionary <string, Slice>();

            //  Read all the slice definition data
            for (int i = 0; i < sliceCount; i++)
            {
                //  Read the slice name
                string name = input.ReadString();

                //  Read the slice color
                string color = input.ReadString();

                //  the color is a hex string, so it has to be converted to a Microsoft.Xna.Framework.Color type
                Color colorActual = HextToColor(color.Replace("#", ""));


                //  Read how many keys there are in this slice
                int keyCount = input.ReadInt32();

                //  Initilize a new dictionary for the keys
                Dictionary <int, SliceKey> keys = new Dictionary <int, SliceKey>();

                //  Read all the slice key definition data
                for (int j = 0; j < keyCount; j++)
                {
                    //  read the frame number
                    int frame = input.ReadInt32();

                    //  read the x-coordinate
                    int x = input.ReadInt32();

                    //  read the y-coordinate
                    int y = input.ReadInt32();

                    //  read the width
                    int w = input.ReadInt32();

                    //  read the height
                    int h = input.ReadInt32();

                    //  Create a new slice key
                    SliceKey key = new SliceKey(frame, x, y, w, h);

                    //  Add the key to the dictionary
                    keys.Add(key.frame, key);
                }

                //  Create a new Slice
                Slice slice = new Slice(name, colorActual, keys);

                //  Add the slice to the dictionary
                slices.Add(slice.name, slice);
            }

            //  Create a new AnimationDefinition
            AnimationDefinition animationDefinition = new AnimationDefinition(animations, frames, slices);


            return(animationDefinition);
        }