示例#1
0
 public virtual void tweenBones(SpriterAnimation animation)
 {
     foreach (SpriterBone key in bonesToTween.Keys)
     {
         SpriterBone     toTween      = key;
         SpriterKeyFrame frame        = animation.frames[bonesToTween[key]];
         long            time         = frame.getTime();
         SpriterKeyFrame currentFrame = animation.getPreviousFrameForBone
                                            (toTween, time);
         SpriterKeyFrame nextFrame = animation.getNextFrameFor
                                         (toTween, currentFrame, 1);
         if (nextFrame != currentFrame)
         {
             SpriterBone bone1 = currentFrame.getBoneFor(toTween
                                                         );
             SpriterBone bone2 = nextFrame.getBoneFor(toTween);
             this.interpolateAbstractObject(toTween, bone1, bone2, currentFrame.getTime(), nextFrame
                                            .getTime(), time);
         }
         SpriterBone[] bones = new SpriterBone
                               [frame.getBones().Length + 1];
         for (int i = 0; i < bones.Length - 1; i++)
         {
             bones[i] = frame.getBones()[i];
         }
         bones[bones.Length - 1] = toTween;
         frame.setBones(bones);
     }
 }
示例#2
0
 /// <summary>Changes the state of each effector to unactive.</summary>
 /// <remarks>Changes the state of each effector to unactive. The effect results in non animated bodyparts.
 ///     </remarks>
 /// <param name="parents">indicates whether parents of the effectors have to be deactivated or not.
 ///     </param>
 public virtual void deactivateEffectors(SpriterAbstractPlayer player, bool parents)
 {
     foreach (SpriterIKObject key in this.ikMap.Keys)
     {
         SpriterAbstractObject obj = this.ikMap[key];
         if (obj is SpriterBone)
         {
             obj = player.getRuntimeBones()[obj.getId()];
         }
         else
         {
             obj = player.getRuntimeObjects()[obj.getId()];
         }
         obj.active = false;
         if (!parents)
         {
             continue;
         }
         SpriterBone par = (SpriterBone)obj.getParent();
         for (int j = 0; j < key.chainLength && par != null; j++)
         {
             player.getRuntimeBones()[par.getId()].active = false;
             par = (SpriterBone)par.getParent();
         }
     }
 }
        private void tweenBone(SpriterBone currentBone, SpriterBone
                               nextBone, int i, long startTime, long endTime)
        {
            currentBone.copyValuesTo(this.tempBones[i]);
            this.tempBones[i].setTimeline((nextBone != null) ? currentBone.getTimeline() : -1
                                          );
            SpriterAbstractObject parent = (this.tempBones[i]
                                            .hasParent()) ? this.tempBones[this.tempBones[i].getParentId()] : this.tempParent;

            if (nextBone != null)
            {
                if (parent != this.tempParent)
                {
                    if (!currentBone.getParent().equals(nextBone.getParent()))
                    {
                        nextBone = (SpriterBone)this.getTimelineObject(currentBone
                                                                       , this.tempBones2);
                        SpriterCalculator.reTranslateRelative(parent, nextBone);
                        nextBone.setAngle(nextBone.getAngle() * this.flippedX * this.flippedY);
                    }
                }
                else
                {
                    if (nextBone.hasParent())
                    {
                        nextBone = (SpriterBone)this.getTimelineObject(currentBone
                                                                       , this.tempBones2);
                        SpriterCalculator.reTranslateRelative(parent, nextBone);
                        nextBone.setAngle(nextBone.getAngle() * this.flippedX * this.flippedY);
                    }
                }
                if (this.tempBones[i].active)
                {
                    this.interpolateAbstractObject(this.tempBones[i], currentBone, nextBone, startTime
                                                   , endTime);
                }
            }
            this.moddedBones[currentBone.getId()].modSpriterBone(this.tempBones[i]);
            if (this.transitionFixed)
            {
                this.tempBones[i].copyValuesTo(this.lastFrame.getBones()[i]);
            }
            else
            {
                this.tempBones[i].copyValuesTo(this.lastTempFrame.getBones()[i]);
            }
            if (!this.tempBones[i].hasParent() || !this.moddedBones[currentBone.getId()].isActive
                    ())
            {
                this.tempBones[i].setX(this.tempBones[i].getX() + this.pivotX);
                this.tempBones[i].setY(this.tempBones[i].getY() + this.pivotY);
            }
            this.translateRelative(this.tempBones[i], parent);
        }
 /// <summary>Searches for the right mod bone for the given bone.</summary>
 /// <remarks>Searches for the right mod bone for the given bone.</remarks>
 /// <param name="bone">bone to search at.</param>
 /// <returns>right mod bone you want access to. Null if not found.</returns>
 public virtual SpriterModObject getModBoneForBone
     (SpriterBone bone)
 {
     try
     {
         return(this.moddedBones[this.getModBoneIndexForBone(bone)]);
     }
     catch (System.IndexOutOfRangeException)
     {
         return(null);
     }
 }
 /// <summary>Searches for the right index for a given bone.</summary>
 /// <remarks>Searches for the right index for a given bone.</remarks>
 /// <param name="bone">bone to search at.</param>
 /// <returns>right index for the mod bone you want access to. -1 if not found.</returns>
 public virtual int getModBoneIndexForBone(SpriterBone
                                           bone)
 {
     for (int i = 0; i < this.tempObjects.Length; i++)
     {
         if (this.tempBones[i].equals(bone))
         {
             return(i);
         }
     }
     return(-1);
 }
示例#6
0
 public virtual SpriterBone getBoneFor(SpriterBone
                                       bone)
 {
     foreach (SpriterBone b in this.bones)
     {
         if (b.equals(bone))
         {
             return(b);
         }
     }
     return(null);
 }
        public virtual SpriterBone merge(BoneRef @ref, Key key)
        {
            Bone        obj  = key.getBone();
            SpriterBone bone = new SpriterBone();

            bone.setTimeline(@ref.getTimeline());
            bone.setId(@ref.getId());
            bone.setParentId(@ref.getParent());
            bone.setAngle(obj.getAngle());
            bone.setScaleX(obj.getScaleX());
            bone.setScaleY(obj.getScaleY());
            bone.setX(obj.getX());
            bone.setY(obj.getY());
            bone.setSpin(key.getSpin());
            return(bone);
        }
        /// <summary>Searches for a keyframe in this animation which has a smaller or equal starting time as the given time and contains the given bone.
        ///     </summary>
        /// <remarks>Searches for a keyframe in this animation which has a smaller or equal starting time as the given time and contains the given bone.
        ///     </remarks>
        /// <param name="bone"></param>
        /// <param name="time"></param>
        /// <returns>A keyframe object which has a smaller or equal starting time than the given time and contains the given bone.
        ///     </returns>
        public virtual SpriterKeyFrame getPreviousFrameForBone
            (SpriterBone bone, long time)
        {
            SpriterKeyFrame frame = null;

            foreach (SpriterKeyFrame key in this.frames)
            {
                if (!key.containsBone(bone))
                {
                    continue;
                }
                if (key.getTime() <= time)
                {
                    frame = key;
                }
                else
                {
                    break;
                }
            }
            return(frame);
        }
 /// <summary>Calculates the bounding box for the current running animation.</summary>
 /// <remarks>
 /// Calculates the bounding box for the current running animation.
 /// Call this method after updating the spriter player.
 /// </remarks>
 /// <param name="bone">root to start at. Set null, to iterate through all objects.</param>
 public virtual void calcBoundingBox(SpriterBone bone
                                     )
 {
     if (bone == null)
     {
         this.calcBoundingBoxForAll();
     }
     else
     {
         bone.boundingBox.set(this.rect);
         foreach (SpriterObject @object in bone.getChildObjects())
         {
             SpriterPoint[] points = this.tempObjects[@object.getId()]
                                     .getBoundingBox();
             bone.boundingBox.left = System.Math.Min(System.Math.Min(System.Math.Min(System.Math
                                                                                     .Min(points[0].x, points[1].x), points[2].x), points[3].x), bone.boundingBox.left
                                                     );
             bone.boundingBox.right = System.Math.Max(System.Math.Max(System.Math.Max(System.Math
                                                                                      .Max(points[0].x, points[1].x), points[2].x), points[3].x), bone.boundingBox.right
                                                      );
             bone.boundingBox.top = System.Math.Max(System.Math.Max(System.Math.Max(System.Math
                                                                                    .Max(points[0].y, points[1].y), points[2].y), points[3].y), bone.boundingBox.top
                                                    );
             bone.boundingBox.bottom = System.Math.Min(System.Math.Min(System.Math.Min(System.Math
                                                                                       .Min(points[0].y, points[1].y), points[2].y), points[3].y), bone.boundingBox.bottom
                                                       );
         }
         this.rect.set(bone.boundingBox);
         foreach (SpriterBone child in bone.getChildBones(
                      ))
         {
             calcBoundingBox(child);
             bone.boundingBox.set(child.boundingBox);
         }
         this.rect.set(bone.boundingBox);
     }
     this.rect.calculateSize();
 }
        /// <summary>Generates data which is necessary to animate all animations as intended.
        ///     </summary>
        /// <remarks>
        /// Generates data which is necessary to animate all animations as intended.
        /// This method has to called inside the specific constructor.
        /// </remarks>
        protected internal void generateData()
        {
            int maxObjects               = 0;
            int maxBones                 = 0;
            int maxBonesFrameIndex       = 0;
            int maxObjectsFrameIndex     = 0;
            int maxBonesAnimationIndex   = 0;
            int maxObjectsAnimationIndex = 0;

            foreach (SpriterAnimation animation in this.animations)
            {
                foreach (SpriterKeyFrame frame in animation.frames)
                {
                    maxBones = System.Math.Max(frame.getBones().Length, maxBones);
                    if (maxBones == frame.getBones().Length)
                    {
                        maxBonesFrameIndex     = frame.getId();
                        maxBonesAnimationIndex = animation.id;
                    }
                    maxObjects = System.Math.Max(frame.getObjects().Length, maxObjects);
                    if (maxObjects == frame.getObjects().Length)
                    {
                        maxObjectsFrameIndex     = frame.getId();
                        maxObjectsAnimationIndex = animation.id;
                    }
                    foreach (SpriterObject o in frame.getObjects())
                    {
                        o.setLoader(this.loader);
                        o.setRef(this.loader.findReference(o.getRef()));
                    }
                }
            }
            this.instructions  = new DrawInstruction[maxObjects];
            this.moddedObjects = new SpriterModObject[this.instructions
                                                      .Length];
            this.tempObjects = new SpriterObject[this.instructions
                                                 .Length];
            this.tempObjects2 = new SpriterObject[this.instructions
                                                  .Length];
            this.nonTransformedTempObjects = new SpriterObject
                                             [this.instructions.Length];
            for (int i = 0; i < this.instructions.Length; i++)
            {
                this.instructions[i] = new DrawInstruction(null, 0,
                                                           0, 0, 0, 0, 0, 0, 0);
                this.tempObjects[i]  = new SpriterObject();
                this.tempObjects2[i] = new SpriterObject();
                this.nonTransformedTempObjects[i] = new SpriterObject
                                                        ();
                this.nonTransformedTempObjects[i].setId(i);
                this.moddedObjects[i] = new SpriterModObject();
                this.moddedObjects[i].setId(i);
            }
            this.tempBones  = new SpriterBone[maxBones];
            this.tempBones2 = new SpriterBone[tempBones.Length
                              ];
            this.nonTransformedTempBones = new SpriterBone[tempBones
                                                           .Length];
            this.moddedBones = new SpriterModObject[this.tempBones
                                                    .Length];
            for (int i_1 = 0; i_1 < this.tempBones.Length; i_1++)
            {
                this.tempBones[i_1]  = new SpriterBone();
                this.tempBones2[i_1] = new SpriterBone();
                this.nonTransformedTempBones[i_1] = new SpriterBone
                                                        ();
                this.nonTransformedTempBones[i_1].setId(i_1);
                this.moddedBones[i_1] = new SpriterModObject();
                this.moddedBones[i_1].setId(i_1);
            }
            SpriterBone[] tmpBones1 = new SpriterBone
                                      [this.tempBones.Length];
            SpriterBone[] tmpBones2 = new SpriterBone
                                      [this.tempBones.Length];
            SpriterObject[] tmpObjs1 = new SpriterObject
                                       [this.instructions.Length];
            SpriterObject[] tmpObjs2 = new SpriterObject
                                       [this.instructions.Length];
            for (int i_2 = 0; i_2 < tmpObjs1.Length; i_2++)
            {
                tmpObjs1[i_2] = new SpriterObject();
                tmpObjs2[i_2] = new SpriterObject();
            }
            for (int i_3 = 0; i_3 < tmpBones1.Length; i_3++)
            {
                tmpBones1[i_3] = new SpriterBone();
                tmpBones2[i_3] = new SpriterBone();
            }
            this.lastFrame.setBones(tmpBones1);
            this.lastFrame.setObjects(tmpObjs1);
            this.lastTempFrame.setBones(tmpBones2);
            this.lastTempFrame.setObjects(tmpObjs2);
            foreach (SpriterObject @object in this.animations
                     [maxObjectsAnimationIndex].frames[maxObjectsFrameIndex].getObjects())
            {
                for (int i_4 = 0; i_4 < this.nonTransformedTempObjects.Length; i_4++)
                {
                    if (this.nonTransformedTempObjects[i_4].getId() == @object.getId())
                    {
                        @object.copyValuesTo(this.nonTransformedTempObjects[i_4]);
                    }
                }
            }
            foreach (SpriterBone bone in this.animations[maxBonesAnimationIndex
                     ].frames[maxBonesFrameIndex].getBones())
            {
                for (int i_4 = 0; i_4 < this.nonTransformedTempBones.Length; i_4++)
                {
                    if (this.nonTransformedTempBones[i_4].getId() == bone.getId())
                    {
                        bone.copyValuesTo(this.nonTransformedTempBones[i_4]);
                    }
                }
            }
            this.generated = true;
        }
        protected override void resolve(float x, float y, int chainLength, SpriterAbstractObject effector, SpriterAbstractPlayer player)
        {
            base.updateRecursively(player, effector);
            float xx = effector.getX() + (float)System.Math.Cos(SpriterCalculator.DegreeToRadian(effector
                                                                                                 .getAngle())) * Com.Brashmonkey.Spriter.draw.AbstractDrawer.BONE_LENGTH * effector
                       .getScaleX();
            float yy = effector.getY() + (float)System.Math.Sin(SpriterCalculator.DegreeToRadian(effector
                                                                                                 .getAngle())) * Com.Brashmonkey.Spriter.draw.AbstractDrawer.BONE_LENGTH * effector
                       .getScaleX();

            effector.setAngle(Com.Brashmonkey.Spriter.SpriterCalculator.angleBetween(effector
                                                                                     .getX(), effector.getY(), x, y));
            if (player.getFlipX() == -1)
            {
                effector.setAngle(effector.getAngle() + 180f);
            }
            SpriterBone parent = null;

            if (effector.hasParent())
            {
                parent = player.getRuntimeBones()[effector.getParentId()];
            }
            //effector.copyValuesTo(temp);
            //SpriterCalculator.reTranslateRelative(parent, temp);
            //if(effector instanceof SpriterBone)	temp.copyValuesTo(player.lastFrame.getBones()[effector.getId()]);
            //else temp.copyValuesTo(player.lastFrame.getObjects()[effector.getId()]);
            for (int i = 0; i < chainLength && parent != null; i++)
            {
                if (Com.Brashmonkey.Spriter.SpriterCalculator.distanceBetween(xx, yy, x, y) <= this
                    .tolerance)
                {
                    SpriterBone p = null;
                    if (parent.hasParent())
                    {
                        p = player.getRuntimeBones()[parent.getParentId()];
                    }
                    int j = 0;
                    while (p != null && j < chainLength)
                    {
                        base.updateRecursively(player, p);
                        if (p.hasParent())
                        {
                            p = player.getRuntimeBones()[p.getParentId()];
                        }
                        else
                        {
                            p = null;
                        }
                        j++;
                    }
                    return;
                }
                parent.setAngle(parent.getAngle() + Com.Brashmonkey.Spriter.SpriterCalculator.angleDifference
                                    (Com.Brashmonkey.Spriter.SpriterCalculator.angleBetween(parent.getX(), parent.getY
                                                                                                (), x, y), Com.Brashmonkey.Spriter.SpriterCalculator.angleBetween(parent.getX(),
                                                                                                                                                                  parent.getY(), xx, yy)));
                base.updateRecursively(player, parent);
                if (parent.hasParent())
                {
                    parent = player.getRuntimeBones()[parent.getParent().getId()];
                }
                else
                {
                    parent = null;
                }
                xx = effector.getX() + (float)System.Math.Cos(SpriterCalculator.DegreeToRadian(effector.getAngle
                                                                                                   ())) * Com.Brashmonkey.Spriter.draw.AbstractDrawer.BONE_LENGTH * effector.getScaleX
                         ();
                yy = effector.getY() + (float)System.Math.Sin(SpriterCalculator.DegreeToRadian(effector.getAngle
                                                                                                   ())) * Com.Brashmonkey.Spriter.draw.AbstractDrawer.BONE_LENGTH * effector.getScaleX
                         ();
            }
        }
示例#12
0
 /// <summary>Returns whether this frame has information about the given bone.</summary>
 /// <remarks>Returns whether this frame has information about the given bone.</remarks>
 /// <param name="bone"></param>
 /// <returns>True if this frame contains the bone, false otherwise.</returns>
 public virtual bool containsBone(SpriterBone bone
                                  )
 {
     return(this.getBoneFor(bone) != null);
 }
示例#13
0
        //import Com.Brashmonkey.Spriter.converters.SpriterObjectConverter;
        //import AnimationObject;
        //final private SpriterObjectConverter objectConverter = new SpriterObjectConverter();
        public virtual SpriterAnimation buildAnimation(
            Animation animation)
        {
            MainLine         mainline  = animation.getMainline();
            IList <TimeLine> timeLines =
                animation.getTimeline();
            IList <Key> keyFrames = mainline
                                    .getKey();

            bonesToTween = new Dictionary <SpriterBone
                                           , int>();
            objectsToTween = new Dictionary <SpriterObject
                                             , int>();
            SpriterAnimation spriterAnimation = new SpriterAnimation(animation.getId(), animation.getName(), animation.getLength());

            for (int k = 0; k < keyFrames.Count; k++)
            {
                Key mainlineKey = keyFrames[k];
                IList <SpriterObject> tempObjects
                    = new List <SpriterObject
                                >();
                IList <SpriterBone> tempBones
                    = new List <SpriterBone
                                >();
                SpriterKeyFrame frame = new SpriterKeyFrame
                                            ();
                frame.setTime(mainlineKey.getTime());
                frame.setId(mainlineKey.getId());
                foreach (BoneRef boneRef in mainlineKey.getBoneRef())
                {
                    TimeLine    timeline    = timeLines[boneRef.getTimeline()];
                    Key         timelineKey = timeline.getKey()[boneRef.getKey()];
                    SpriterBone bone        = boneMerger.merge(boneRef, timelineKey
                                                               );
                    bone.setName(timeline.getName());
                    if (mainlineKey.getTime() != timelineKey.getTime())
                    {
                        bonesToTween.Add(bone, k);
                    }
                    else
                    {
                        tempBones.Add(bone);
                    }
                }
                //}
                foreach (AnimationObjectRef objectRef in mainlineKey.getObjectRef
                             ())
                {
                    TimeLine timeline    = timeLines[objectRef.getTimeline()];
                    Key      timelineKey = timeline.getKey()[objectRef.getKey()
                                           ];
                    SpriterObject @object = objectMerger.merge(objectRef
                                                               , timelineKey);
                    @object.setName(timeline.getName());
                    if (mainlineKey.getTime() != timelineKey.getTime())
                    {
                        objectsToTween.Add(@object, k);
                    }
                    else
                    {
                        tempObjects.Add(@object);
                    }
                }
                //}
                SpriterObject[] objArray = new SpriterObject[tempObjects.Count];
                tempObjects.CopyTo(objArray, 0);
                frame.setObjects(objArray);
                SpriterBone[] boneArray = new SpriterBone[tempBones.Count];
                tempBones.CopyTo(boneArray, 0);
                frame.setBones(boneArray);
                spriterAnimation.frames.Add(frame);
            }
            this.tweenBones(spriterAnimation);
            this.tweenObjects(spriterAnimation);
            return(spriterAnimation);
        }