Beispiel #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 bool TryGetCurrentFrameSlice(string sliceName, out SliceKey sliceKey)
 {
     //  Ensure that we have a slice defined with the given name
     if (Slices.TryGetValue(sliceName, out Slice slice))
     {
         //  Ensure we have a slice key at the current animation frame index
         if (slice.Keys.TryGetValue(CurrentFrameIndex, out sliceKey))
         {
             //  Update the xy-coordinate of the slicekey bounds to match the positiona
             //  data of this animated sprite.
             sliceKey.Bounds.X += (int)Position.X;
             sliceKey.Bounds.Y += (int)Position.Y;
             return(true);
         }
         else
         {
             //  There is no slicekey for the current frame index, so we return false.
             return(false);
         }
     }
     else
     {
         //  No slice exists with the given name, return false
         sliceKey = new SliceKey();
         return(false);
     }
 }
Beispiel #2
0
        /// <summary>
        ///     Creates a new <see cref="AnimatedSprite"/> instance.
        /// </summary>
        /// <param name="aseprite">
        ///     An <see cref="AsepriteDocument"/> instace created by
        ///     importing from the content pipeline.
        /// </param>
        /// <param name="position">
        ///     The top-left xy-coordinate position.
        /// </param>
        public AnimatedSprite(AsepriteDocument aseprite, Vector2 position)
            : this(aseprite.Texture, position)
        {
            for (int i = 0; i < aseprite.Frames.Count; i++)
            {
                Frames.Add(new Frame()
                {
                    Bounds   = new Rectangle(aseprite.Frames[i].X, aseprite.Frames[i].Y, aseprite.Frames[i].Width, aseprite.Frames[i].Height),
                    Duration = aseprite.Frames[i].Duration
                });
            }

            foreach (KeyValuePair <string, AsepriteTag> kvp in aseprite.Tags)
            {
                Animation animation = new Animation()
                {
                    Name      = kvp.Value.Name,
                    From      = kvp.Value.From,
                    To        = kvp.Value.To,
                    Direction = (AnimationLoopDirection)kvp.Value.Direction,
                    IsOneShot = kvp.Value.IsOneShot
                }
                ;
                Animations.Add(animation.Name, animation);
            }

            foreach (KeyValuePair <string, AsepriteSlice> kvp in aseprite.Slices)
            {
                Slice slice = new Slice
                {
                    Name  = kvp.Value.Name,
                    Color = kvp.Value.Color,
                    Keys  = new Dictionary <int, SliceKey>()
                };

                foreach (KeyValuePair <int, AsepriteSliceKey> innerKVP in kvp.Value.SliceKeys)
                {
                    SliceKey key = new SliceKey()
                    {
                        Bounds = new Rectangle(innerKVP.Value.X, innerKVP.Value.Y, innerKVP.Value.Width, innerKVP.Value.Height),
                        Frame  = innerKVP.Value.FrameIndex,
                        Color  = slice.Color
                    };

                    slice.Keys.Add(key.Frame, key);
                }
                Slices.Add(slice.Name, slice);
            }

            Play(Animations.First().Key);
        }