Пример #1
0
 /// <summary>
 /// Creates a new Sprite with the specified data.
 /// </summary>
 public Sprite(Texture2D Texture, SpriteFrameCollection Frames, SpriteAnimationCollection Animations)
 {
     this._Texture = Texture;
     this._Frames = Frames;
     this._Animations = Animations;
     this._ActiveAnimation = GetDefaultAnimation();
 }
Пример #2
0
        /// <summary>
        /// Creates a new Sprite from the specified SpriteData instance.
        /// </summary>
        public Sprite(SpriteData Data)
        {
            SpriteFrameCollection Frames = new SpriteFrameCollection();
            foreach(var FrameData in Data.Frames) {
                var SpriteFrame = new SpriteFrame(FrameData.Name, FrameData.Source, this);
                Frames.Add(SpriteFrame);
            }
            SpriteAnimationCollection Animations = new SpriteAnimationCollection();
            foreach(var Animation in Data.Animations) {
                List<SpriteAnimationFrame> AnimationFrames = new List<SpriteAnimationFrame>();
                foreach(var Duration in Animation.FrameDurations) {
                    var Frame = Frames[Duration.Key];
                    SpriteAnimationFrame AnimationFrame = new SpriteAnimationFrame(Frame, Duration.Value);
                    AnimationFrames.Add(AnimationFrame);
                }
                Animations.Add(new SpriteAnimation(Animation.Name, Animation.IsLooped, Animation.IsDefault, AnimationFrames));
            }

            this._Texture = Data.Texture;
            this._Frames = Frames;
            this._Animations = Animations;
            this._ActiveAnimation = GetDefaultAnimation();
        }
Пример #3
0
 /// <summary>
 /// Stops the currently playing animation, returning back to one of the default animations.
 /// </summary>
 public void StopAnimation()
 {
     this._ActiveAnimation = GetDefaultAnimation();
 }
Пример #4
0
 private float CalculateSpeedModifier(SpriteAnimation Animation, TimeSpan Duration)
 {
     TimeSpan OriginalTime = TimeSpan.FromTicks(0);
     foreach(var Frame in Animation.Frames)
         OriginalTime += Frame.Duration;
     return (float)OriginalTime.Ticks / (float)Duration.Ticks;
 }
Пример #5
0
 /// <summary>
 /// Stops the current animation, playing the specified animation instead.
 /// </summary>
 /// <param name="Name">The name of the animation to play.</param>
 /// <param name="Duration">If not zero, indicates how long the animation should take to play. Otherwise, plays using the default duration.</param>
 public void PlayAnimation(string Name, TimeSpan? Duration = null)
 {
     // TODO: Determine what to do if this is the active animation.
     // Chances are, that if it's loopable we carry on, and if not we reset the animation.
     var Animation = this.Animations[Name];
     Animation.Reset();
     Animation.SpeedModifier = (Duration == null ? 1 : CalculateSpeedModifier(Animation, Duration.Value));
     this._ActiveAnimation = Animation;
 }