/// <summary>
 /// Sole constructor. Creates a new instance which animates the given entity.
 /// </summary>
 protected SpriterAnimator(SpriterEntity entity)
 {
     Entity       = entity;
     animations   = entity.Animations.ToDictionary(a => a.Name, a => a);
     Speed        = 1.0f;
     Metadata     = new FrameMetadata();
     DataProvider = new DefaultAnimationDataProvider();
 }
Esempio n. 2
0
 public SpriterAnimator(SpriterEntity entity)
 {
     Entity     = entity;
     Spriter    = entity.Spriter;
     animations = entity.Animations.ToDictionary(a => a.Name, a => a);
     Speed      = 1.0f;
     Play(animations.Keys.First());
     Metadata = new FrameMetadata();
 }
        public static FrameMetadata GetFrameMetadata(SpriterAnimation animation, float targetTime, float deltaTime, SpriterSpatial parentInfo = null)
        {
            FrameMetadata metadata = new FrameMetadata();

            AddVariableAndTagData(animation, targetTime, metadata);
            AddEventData(animation, targetTime, deltaTime, metadata);
            AddSoundData(animation, targetTime, deltaTime, metadata);
            return(metadata);
        }
Esempio n. 4
0
        protected virtual void Animate(float deltaTime)
        {
            FrameData     frameData;
            FrameMetadata metaData;

            if (NextAnimation == null)
            {
                frameData = SpriterProcessor.GetFrameData(CurrentAnimation, Time);
                metaData  = SpriterProcessor.GetFrameMetadata(CurrentAnimation, Time, deltaTime);
            }
            else
            {
                frameData = SpriterProcessor.GetFrameData(CurrentAnimation, NextAnimation, Time, factor);
                metaData  = SpriterProcessor.GetFrameMetadata(CurrentAnimation, NextAnimation, Time, deltaTime, factor);
            }

            foreach (SpriterObject info in frameData.SpriteData)
            {
                int folderId;
                int fileId;
                if (!GetSpriteIds(info, out folderId, out fileId))
                {
                    continue;
                }
                TSprite obj = GetFromDict(folderId, fileId, sprites);
                ApplySpriteTransform(obj, info);
            }

            foreach (SpriterSound info in metaData.Sounds)
            {
                TSound sound = GetFromDict(info.FolderId, info.FileId, sounds);
                PlaySound(sound, info);
            }

            foreach (var entry in frameData.PointData)
            {
                ApplyPointTransform(entry.Key, entry.Value);
            }
            foreach (var entry in frameData.BoxData)
            {
                ApplyBoxTransform(Entity.ObjectInfos[entry.Key], entry.Value);
            }
            foreach (string eventName in metaData.Events)
            {
                DispatchEvent(eventName);
            }

            Metadata = metaData;
        }
 public static void UpdateFrameMetadata(FrameMetadata metadata, SpriterAnimation animation, float targetTime, float deltaTime, SpriterSpatial parentInfo = null)
 {
     if (SpriterConfig.VarsEnabled || SpriterConfig.TagsEnabled)
     {
         AddVariableAndTagData(animation, targetTime, metadata);
     }
     if (SpriterConfig.EventsEnabled)
     {
         AddEventData(animation, targetTime, deltaTime, metadata);
     }
     if (SpriterConfig.SoundsEnabled)
     {
         AddSoundData(animation, targetTime, deltaTime, metadata);
     }
 }
        private static void AddSoundData(SpriterAnimation animation, float targetTime, float deltaTime, FrameMetadata metadata)
        {
            if (animation.Soundlines == null)
            {
                return;
            }

            float previousTime = targetTime - deltaTime;

            for (int i = 0; i < animation.Soundlines.Length; ++i)
            {
                SpriterSoundline soundline = animation.Soundlines[i];
                for (int j = 0; j < soundline.Keys.Length; ++j)
                {
                    SpriterSoundlineKey key   = soundline.Keys[j];
                    SpriterSound        sound = key.SoundObject;
                    if (sound.Trigger && IsTriggered(key, targetTime, previousTime, animation.Length))
                    {
                        metadata.Sounds.Add(sound);
                    }
                }
            }
        }
        private static void AddEventData(SpriterAnimation animation, float targetTime, float deltaTime, FrameMetadata metadata)
        {
            if (animation.Eventlines == null)
            {
                return;
            }

            float previousTime = targetTime - deltaTime;

            for (int i = 0; i < animation.Eventlines.Length; ++i)
            {
                SpriterEventline eventline = animation.Eventlines[i];
                for (int j = 0; j < eventline.Keys.Length; ++j)
                {
                    SpriterKey key = eventline.Keys[j];
                    if (IsTriggered(key, targetTime, previousTime, animation.Length))
                    {
                        metadata.Events.Add(eventline.Name);
                    }
                }
            }
        }
        private static void AddVariableAndTagData(SpriterAnimation animation, float targetTime, FrameMetadata metadata)
        {
            if (animation.Meta == null)
            {
                return;
            }

            if (SpriterConfig.VarsEnabled && animation.Meta.Varlines != null && animation.Meta.Varlines.Length > 0)
            {
                for (int i = 0; i < animation.Meta.Varlines.Length; ++i)
                {
                    SpriterVarline varline  = animation.Meta.Varlines[i];
                    SpriterVarDef  variable = animation.Entity.Variables[varline.Def];
                    metadata.AnimationVars[variable.Name] = GetVariableValue(animation, variable, varline, targetTime);
                }
            }

            SpriterElement[] tags    = animation.Entity.Spriter.Tags;
            SpriterTagline   tagline = animation.Meta.Tagline;

            if (SpriterConfig.TagsEnabled && tagline != null && tagline.Keys != null && tagline.Keys.Length > 0)
            {
                SpriterTaglineKey key = LastKeyForTime <SpriterTaglineKey>(tagline.Keys, targetTime);
                if (key != null && key.Tags != null)
                {
                    for (int i = 0; i < key.Tags.Length; ++i)
                    {
                        SpriterTag tag = key.Tags[i];
                        metadata.AnimationTags.Add(tags[tag.TagId].Name);
                    }
                }
            }

            for (int i = 0; i < animation.Timelines.Length; ++i)
            {
                SpriterTimeline timeline = animation.Timelines[i];
                SpriterMeta     meta     = timeline.Meta;
                if (meta == null)
                {
                    continue;
                }

                SpriterObjectInfo objInfo = GetObjectInfo(animation, timeline.Name);

                if (SpriterConfig.VarsEnabled && meta.Varlines != null && meta.Varlines.Length > 0)
                {
                    for (int j = 0; j < timeline.Meta.Varlines.Length; ++j)
                    {
                        SpriterVarline varline  = timeline.Meta.Varlines[j];
                        SpriterVarDef  variable = objInfo.Variables[varline.Def];
                        metadata.AddObjectVar(objInfo.Name, variable.Name, GetVariableValue(animation, variable, varline, targetTime));
                    }
                }

                if (SpriterConfig.TagsEnabled && meta.Tagline != null && meta.Tagline.Keys != null && meta.Tagline.Keys.Length > 0)
                {
                    SpriterTaglineKey key = LastKeyForTime <SpriterTaglineKey>(tagline.Keys, targetTime);
                    if (key != null && key.Tags != null)
                    {
                        for (int j = 0; j < key.Tags.Length; ++j)
                        {
                            SpriterTag tag = key.Tags[j];
                            metadata.AddObjectTag(objInfo.Name, tags[tag.TagId].Name);
                        }
                    }
                }
            }
        }
        public static void GetFrameMetadata(FrameMetadata metadata, SpriterAnimation first, SpriterAnimation second, float targetTime, float deltaTime, float factor)
        {
            SpriterAnimation currentAnimation = factor < 0.5f ? first : second;

            UpdateFrameMetadata(metadata, currentAnimation, targetTime, deltaTime);
        }
        private static void AddSoundData(SpriterAnimation animation, float targetTime, float deltaTime, FrameMetadata metadata)
        {
            if (animation.Soundlines == null)
            {
                return;
            }

            float previousTime = targetTime - deltaTime;

            foreach (SpriterSoundline soundline in animation.Soundlines)
            {
                foreach (SpriterSoundlineKey key in soundline.Keys)
                {
                    SpriterSound sound = key.SoundObject;
                    if (sound.Trigger && IsTriggered(key, targetTime, previousTime, animation.Length))
                    {
                        metadata.Sounds.Add(sound);
                    }
                }
            }
        }
        private static void AddEventData(SpriterAnimation animation, float targetTime, float deltaTime, FrameMetadata metadata)
        {
            if (animation.Eventlines == null)
            {
                return;
            }

            float previousTime = targetTime - deltaTime;

            foreach (SpriterEventline eventline in animation.Eventlines)
            {
                foreach (SpriterKey key in eventline.Keys)
                {
                    if (IsTriggered(key, targetTime, previousTime, animation.Length))
                    {
                        metadata.Events.Add(eventline.Name);
                    }
                }
            }
        }