/** Adds an animation to be played delay seconds after the current or last queued animation.
         * @param delay May be <= 0 to use duration of previous animation minus any mix duration plus the negative delay. */
        public QueuedAnimation AddAnimation(Animation animation, bool loop, float delay)
        {
            QueuedAnimation entry = new QueuedAnimation();

            entry.animation = animation;
            entry.loop      = loop;
            entry.time      = 0;
            entry.endTime   = animation != null ? animation.Duration : 0;

            if (delay <= 0)
            {
                QueuedAnimation previousEntry = queue.Count > 0 ? queue[queue.Count - 1] : current;
                if (previousEntry != null)
                {
                    delay += previousEntry.endTime;
                    if (animation != null)
                    {
                        delay += -data.GetMix(previousEntry.animation, animation);
                    }
                }
                else
                {
                    delay = 0;
                }
            }
            entry.delay = delay;

            queue.Add(entry);
            return(entry);
        }
        private void SetAnimationEntry(QueuedAnimation entry)
        {
            previous = null;

            QueuedAnimation current = this.current;

            if (current != null)
            {
                current.OnEnd(this);
                if (End != null)
                {
                    End(this, EventArgs.Empty);
                }

                mixDuration = data.GetMix(current.animation, entry.animation);
                if (mixDuration > 0)
                {
                    mixTime  = 0;
                    previous = current;
                }
            }
            this.current = entry;

            entry.OnStart(this);
            if (Start != null)
            {
                Start(this, EventArgs.Empty);
            }
        }
        /** Set the current animation. Any queued animations are cleared. */
        public QueuedAnimation SetAnimation(Animation animation, bool loop)
        {
            queue.Clear();
            QueuedAnimation entry = new QueuedAnimation();

            entry.animation = animation;
            entry.loop      = loop;
            entry.time      = 0;
            entry.endTime   = animation.Duration;
            SetAnimationEntry(entry);
            return(entry);
        }
Beispiel #4
0
        protected override void LoadContent()
        {
            skeletonRenderer = new SkeletonRenderer(GraphicsDevice);
            skeletonRenderer.PremultipliedAlpha = true;

            String name = "spineboy";             // "goblins";

            Atlas        atlas = new Atlas("data/" + name + ".atlas", new XnaTextureLoader(GraphicsDevice));
            SkeletonJson json  = new SkeletonJson(atlas);

            skeleton = new Skeleton(json.ReadSkeletonData("data/" + name + ".json"));
            if (name == "goblins")
            {
                skeleton.SetSkin("goblingirl");
            }
            skeleton.SetSlotsToSetupPose();             // Without this the skin attachments won't be attached. See SetSkin.

            // Define mixing between animations.
            AnimationStateData stateData = new AnimationStateData(skeleton.Data);

            if (name == "spineboy")
            {
                stateData.SetMix("walk", "jump", 0.2f);
                stateData.SetMix("jump", "walk", 0.4f);
            }

            state = new AnimationState(stateData);

            if (false)
            {
                // Event handling for all animations.
                state.Start    += new EventHandler(Start);
                state.End      += new EventHandler(End);
                state.Complete += new EventHandler <CompleteArgs>(Complete);
                state.Event    += new EventHandler <EventTriggeredArgs>(Event);

                state.SetAnimation("drawOrder", true);
            }
            else
            {
                state.SetAnimation("walk", false);
                QueuedAnimation entry = state.AddAnimation("jump", false);
                entry.End += new EventHandler(End);                 // Event handling for queued animations.
                state.AddAnimation("walk", true);
            }

            skeleton.X = 320;
            skeleton.Y = 440;
            skeleton.UpdateWorldTransform();

            headSlot = skeleton.FindSlot("head");
        }
        public void Update(float delta)
        {
            QueuedAnimation current = this.current;

            if (current == null)
            {
                return;
            }

            float time     = current.time;
            float duration = current.endTime;

            current.time = time + delta;
            if (previous != null)
            {
                previous.time += delta;
                mixTime       += delta;
            }

            // Check if completed the animation or a loop iteration.
            if (current.loop ? (current.lastTime % duration > time % duration) : (current.lastTime < duration && time >= duration))
            {
                int count = (int)(time / duration);
                current.OnComplete(this, count);
                if (Complete != null)
                {
                    Complete(this, new CompleteArgs(count));
                }
            }

            if (queue.Count > 0)
            {
                QueuedAnimation entry = queue[0];
                if (time >= entry.delay)
                {
                    if (entry.animation == null)
                    {
                        ClearAnimation();
                    }
                    else
                    {
                        SetAnimationEntry(entry);
                        queue.RemoveAt(0);
                    }
                }
            }
        }
        public void Apply(Skeleton skeleton)
        {
            QueuedAnimation current = this.current;

            if (current == null)
            {
                return;
            }

            List <Event> events = this.events;

            events.Clear();

            QueuedAnimation previous = this.previous;

            if (previous != null)
            {
                previous.animation.Apply(skeleton, int.MaxValue, previous.time, previous.loop, null);
                float alpha = mixTime / mixDuration;
                if (alpha >= 1)
                {
                    alpha         = 1;
                    this.previous = null;
                }
                current.animation.Mix(skeleton, current.lastTime, current.time, current.loop, events, alpha);
            }
            else
            {
                current.animation.Apply(skeleton, current.lastTime, current.time, current.loop, events);
            }

            foreach (Event e in events)
            {
                current.OnEvent(this, e);
                if (Event != null)
                {
                    Event(this, new EventTriggeredArgs(e));
                }
            }

            current.lastTime = current.time;
        }
 public void ClearAnimation()
 {
     previous = null;
     current  = null;
     queue.Clear();
 }