Ejemplo n.º 1
0
        private static int GetDrawablesCount(SpriterAnimation animation, SpriterMainlineKey key)
        {
            int drawablesCount = 0;

            foreach (SpriterObjectRef obj in key.ObjectRefs)
            {
                SpriterTimeline timeline = animation.Timelines[obj.TimelineId];
                if (timeline.ObjectType == SpriterObjectType.Sprite)
                {
                    ++drawablesCount;
                }
                else if (timeline.ObjectType == SpriterObjectType.Entity)
                {
                    Spriter spriter = animation.Entity.Spriter;
                    HashSet <SpriterAnimation> animations = new HashSet <SpriterAnimation>();
                    foreach (SpriterTimelineKey timelineKey in timeline.Keys)
                    {
                        SpriterObject    spriterObject = timelineKey.ObjectInfo;
                        SpriterAnimation newAnim       = spriter.Entities[spriterObject.EntityId].Animations[spriterObject.AnimationId];
                        if (!animations.Contains(newAnim))
                        {
                            animations.Add(newAnim);
                        }
                    }
                    IEnumerable <int> drawableCount = animations.Select <SpriterAnimation, int>(GetDrawablesCount);
                    drawablesCount += drawableCount.Max();
                }
            }

            return(drawablesCount);
        }
Ejemplo n.º 2
0
        protected override void ApplyBoxTransform(SpriterObjectInfo objInfo, SpriterObject info)
        {
            GameObject child          = childData.Boxes[boxIndex];
            GameObject pivot          = childData.BoxPivots[boxIndex];
            Transform  childTransform = childData.BoxTransforms[boxIndex];
            Transform  pivotTransform = childData.BoxPivotTransforms[boxIndex];

            child.SetActive(true);
            pivot.SetActive(true);

            float w = objInfo.Width / DefaultPPU;
            float h = objInfo.Height / DefaultPPU;

            BoxCollider2D collider = child.GetComponent <BoxCollider2D>();

            collider.size = new Vector2(w, h);

            child.name = objInfo.Name;

            float deltaX = (DefaultPivot - info.PivotX) * w * info.ScaleX;
            float deltaY = (DefaultPivot - info.PivotY) * h * info.ScaleY;

            pivotTransform.localEulerAngles = new Vector3(0, 0, info.Angle);
            pivotTransform.localPosition    = new Vector3(info.X / DefaultPPU, info.Y / DefaultPPU, 0);
            childTransform.localPosition    = new Vector3(deltaX, deltaY, childTransform.localPosition.z);
            childTransform.localScale       = new Vector3(info.ScaleX, info.ScaleY, 1);
            ++boxIndex;
        }
Ejemplo n.º 3
0
        protected override void ApplySpriteTransform(Sprite sprite, SpriterObject info)
        {
            GameObject child          = childData.Sprites[index];
            GameObject pivot          = childData.SpritePivots[index];
            Transform  childTransform = childData.SpriteTransforms[index];
            Transform  pivotTransform = childData.SpritePivotTransforms[index];

            child.SetActive(true);
            pivot.SetActive(true);
            SpriteRenderer renderer = renderers[index];

            float ppu = sprite.pixelsPerUnit;

            renderer.sprite = sprite;
            Vector3 size         = sprite.bounds.size;
            float   spritePivotX = sprite.pivot.x / ppu / size.x;
            float   spritePivotY = sprite.pivot.y / ppu / size.y;

            float deltaX = (spritePivotX - info.PivotX) * size.x * info.ScaleX;
            float deltaY = (spritePivotY - info.PivotY) * size.y * info.ScaleY;

            Color c = renderer.color;

            renderer.color = new Color(c.r, c.g, c.b, info.Alpha);
            pivotTransform.localEulerAngles = new Vector3(0, 0, info.Angle);
            pivotTransform.localPosition    = new Vector3(info.X / ppu, info.Y / ppu, 0);
            childTransform.localPosition    = new Vector3(deltaX, deltaY, childTransform.localPosition.z);
            childTransform.localScale       = new Vector3(info.ScaleX, info.ScaleY, 1);

            renderer.sortingLayerName = SortingLayer;
            renderer.sortingOrder     = index;

            ++index;
        }
Ejemplo n.º 4
0
 public virtual void tweenObjects(SpriterAnimation animation)
 {
     foreach (SpriterObject key in objectsToTween.Keys)
     {
         SpriterObject   toTween      = key;
         SpriterKeyFrame frame        = animation.frames[objectsToTween[key]];
         long            time         = frame.getTime();
         SpriterKeyFrame currentFrame = animation.getPreviousFrameForObject
                                            (toTween, time);
         SpriterKeyFrame nextFrame = animation.getNextFrameFor
                                         (toTween, currentFrame, 1);
         if (nextFrame != currentFrame)
         {
             SpriterObject object1 = currentFrame.getObjectFor
                                         (toTween);
             SpriterObject object2 = nextFrame.getObjectFor(toTween
                                                            );
             this.interpolateSpriterObject(toTween, object1, object2, currentFrame.getTime(),
                                           nextFrame.getTime(), time);
         }
         SpriterObject[] objects = new SpriterObject
                                   [frame.getObjects().Length + 1];
         for (int i = 0; i < objects.Length - 1; i++)
         {
             objects[i] = frame.getObjects()[i];
         }
         objects[objects.Length - 1] = toTween;
         frame.setObjects(objects);
     }
 }
Ejemplo n.º 5
0
        public override void Draw(SpriterAnimationPlayer player, SpriterObject obj, SpriteBatch sb)
        {
            Texture2D texture = Loader[obj.@ref];
            Vector2   flip    = new Vector2(obj.Scale.X < 0f ? -1.0f : 1.0f, obj.Scale.Y < 0f ? -1.0f : 1.0f);

            float newPivotX = texture.Width * obj.Pivot.X;
            float newPivotY = texture.Height * (1f - obj.Pivot.Y);

            Point   x           = obj.Position;
            Vector2 playerPos   = new Vector2(player.X, player.Y);
            Vector2 objPosition = new Vector2(x.X, x.Y);


            // normal case scenario
            Vector2 diff = objPosition - playerPos;

            diff.Y = -diff.Y;


            Vector2 origin = new Vector2(newPivotX, newPivotY);

            SpriteEffects effects = SpriteEffects.None;

            bool flipx = flip.X < 0f;
            bool flipy = flip.Y < 0f;

            float rotation = -MathHelper.ToRadians(obj.Angle);

            if (flipx)
            {
                effects  = SpriteEffects.FlipHorizontally;
                origin.X = texture.Width - origin.X;

                diff = playerPos - objPosition;
            }

            // TODO EI TOIMI
            if (flipy)
            {
                effects |= SpriteEffects.FlipVertically;
                origin.Y = texture.Height - origin.Y;
                diff.Y   = Math.Abs(diff.Y);
            }

            Vector2 position = playerPos + Vector2.Multiply(diff, flip);

            sb.Draw(
                texture,
                position,
                null,
                null,
                origin,
                rotation,
                new Vector2(Math.Abs(obj.Scale.X), Math.Abs(obj.Scale.Y)),
                Color.White * obj.Alpha,
                effects,
                0f
                );
        }
 protected override void ApplyBoxTransform(SpriterObjectInfo objInfo, SpriterObject info)
 {
     if (DrawBoxOutlines)
     {
         Box bounds = GetBoundingBox(info, objInfo.Width, objInfo.Height);
         AddForDrawing(bounds);
     }
 }
Ejemplo n.º 7
0
 protected override void ApplyPointTransform(string name, SpriterObject info)
 {
     if (!GameConfig.DebugPhysics || _pointTexture == null)
     {
         return;
     }
     ApplySpriteTransform(_pointTexture, info);
 }
 //else this.tweenObject(this.animations.get(0).frames.get(0).getObjectFor(this.nonTransformedTempObjects[i]), nextFrame.getObjectFor(this.nonTransformedTempObjects[i]), i, currentFrame.getTime(), nextFrame.getTime());
 protected internal virtual void setInstructionRef(DrawInstruction
                                                   dI, SpriterObject obj1, SpriterObject
                                                   obj2)
 {
     dI.@ref   = obj1.getRef();
     dI.loader = obj1.getLoader();
     dI.obj    = obj1;
 }
Ejemplo n.º 9
0
 protected override void ApplyPointTransform(string name, SpriterObject info)
 {
     if (pointTexture == null)
     {
         return;
     }
     ApplySpriteTransform(pointTexture, info);
 }
 protected override void ApplySpriteTransform(ISprite drawable, SpriterObject info)
 {
     base.ApplySpriteTransform(drawable, info);
     if (DrawSpriteOutlines)
     {
         AddForDrawing(GetBoundingBox(info, drawable.Width, drawable.Height));
     }
 }
Ejemplo n.º 11
0
        protected override void ApplyBoxTransform(SpriterObjectInfo objInfo, SpriterObject info)
        {
            if (!GameConfig.DebugPhysics)
            {
                return;
            }

            ApplySpriteTransform(_boxTextures[objInfo.Name], info);
        }
Ejemplo n.º 12
0
 /// <summary>Searches for the right index for a given object.</summary>
 /// <remarks>Searches for the right index for a given object.</remarks>
 /// <param name="object">object to search at.</param>
 /// <returns>right index for the mod object you want access to. -1 if not found.</returns>
 public virtual int getModObjectIndexForObject(SpriterObject
                                               @object)
 {
     for (int i = 0; i < this.tempObjects.Length; i++)
     {
         if (this.tempObjects[i].equals(@object))
         {
             return(i);
         }
     }
     return(-1);
 }
Ejemplo n.º 13
0
 /// <summary>Searches for the right mod object for the given object.</summary>
 /// <remarks>Searches for the right mod object for the given object.</remarks>
 /// <param name="object">object to search at.</param>
 /// <returns>right mod object you want access to. Null if not found.</returns>
 public virtual SpriterModObject getModObjectForObject
     (SpriterObject @object)
 {
     try
     {
         return(this.moddedObjects[this.getModObjectIndexForObject(@object)]);
     }
     catch (System.IndexOutOfRangeException)
     {
         return(null);
     }
 }
Ejemplo n.º 14
0
 public virtual SpriterObject getObjectFor(SpriterObject
                                           @object)
 {
     foreach (SpriterObject obj in this.objects)
     {
         if (obj.equals(@object))
         {
             return(obj);
         }
     }
     return(null);
 }
Ejemplo n.º 15
0
        /// <summary>
        /// Fills all the values of target object from the given source object.
        /// </summary>
        public static void FillFrom(this SpriterObject target, SpriterObject source)
        {
            target.AnimationId = source.AnimationId;
            target.EntityId    = source.EntityId;
            target.FileId      = source.FileId;
            target.FolderId    = source.FolderId;
            target.PivotX      = source.PivotX;
            target.PivotY      = source.PivotY;
            target.T           = source.T;

            target.FillFrom((SpriterSpatial)source);
        }
        protected override void ApplyPointTransform(string name, SpriterObject info)
        {
            Vector2 position = GetPosition(info);
            Box     box      = new Box
            {
                Point1 = position + new Vector2(-PointBoxSize, -PointBoxSize),
                Point2 = position + new Vector2(PointBoxSize, -PointBoxSize),
                Point3 = position + new Vector2(PointBoxSize, PointBoxSize),
                Point4 = position + new Vector2(-PointBoxSize, PointBoxSize)
            };

            AddForDrawing(box);
        }
        protected override void ApplyPointTransform(string name, SpriterObject info)
        {
            GameObject point = childData.Points[pointIndex];

            point.name = name;
            point.SetActive(true);

            float x = info.X / DefaultPPU;
            float y = info.Y / DefaultPPU;

            point.transform.localPosition = new Vector3(x, y, point.transform.localPosition.z);

            ++pointIndex;
        }
Ejemplo n.º 18
0
        public static Vector2 GetSpriterWorldPosition(SpriterObject spriterObject, MonoGameAnimator animator)
        {
            var worldPosition = animator.Position;

            if (animator.FrameData != null)
            {
                // Compute translation
                var animationOffset   = new Vector2(spriterObject.X, -spriterObject.Y);
                var scale             = new Vector2(spriterObject.ScaleX, spriterObject.ScaleY);
                var realPivotPosition = new Vector2(1 - spriterObject.PivotX, 1 - spriterObject.PivotY);

                // TOFIX...
                realPivotPosition = new Vector2(0f, 1f);

                var spriterFile = GetSpriterFile(spriterObject, animator);

                var spriteCenter = new Vector2(
                    spriterFile.Width * realPivotPosition.X,
                    spriterFile.Height * realPivotPosition.Y
                    );
                var worldTopLeftCornerPosition = animator.Position - (spriteCenter * scale);

                worldPosition = worldTopLeftCornerPosition + animationOffset;

                // Compute rotation
                var pivot = new Vector2(
                    (spriterFile.Width * realPivotPosition.X) + spriterObject.X,
                    (spriterFile.Height * realPivotPosition.Y) - spriterObject.Y
                    );

                var origin = animator.Position + pivot - spriteCenter;

                var rotation = -spriterObject.Angle;
                rotation = MathHelper.WrapAngle(MathHelper.ToRadians(rotation));

                // Take the animation angle into account
                worldPosition = MathExtension.RotatePoint(
                    worldPosition, rotation, origin
                    );

                // Take the global angle into account
                worldPosition = MathExtension.RotatePoint(
                    worldPosition, animator.Rotation, animator.Position
                    );
            }

            return(worldPosition);
        }
Ejemplo n.º 19
0
 private void interpolateSpriterObject(SpriterObject
                                       target, SpriterObject obj1, SpriterObject
                                       obj2, float startTime, float endTime)
 {
     if (obj2 == null)
     {
         return;
     }
     this.interpolateAbstractObject(target, obj1, obj2, startTime, endTime);
     target.setPivotX(this.interpolate(obj1.getPivotX(), obj2.getPivotX(), startTime,
                                       endTime, this.frame));
     target.setPivotY(this.interpolate(obj1.getPivotY(), obj2.getPivotY(), startTime,
                                       endTime, this.frame));
     target.setAlpha(this.interpolateAngle(obj1.getAlpha(), obj2.getAlpha(), startTime
                                           , endTime, this.frame));
 }
Ejemplo n.º 20
0
 /// <summary>
 /// Fills all the values of target object by interpolating values from the two given object with the given factor and spin.
 /// </summary>
 public static void Interpolate(this SpriterObject target, SpriterObject a, SpriterObject b, float factor, int spin)
 {
     target.Angle       = MathHelper.AngleLinear(a.Angle, b.Angle, spin, factor);
     target.Alpha       = MathHelper.Linear(a.Alpha, b.Alpha, factor);
     target.X           = MathHelper.Linear(a.X, b.X, factor);
     target.Y           = MathHelper.Linear(a.Y, b.Y, factor);
     target.ScaleX      = MathHelper.Linear(a.ScaleX, b.ScaleX, factor);
     target.ScaleY      = MathHelper.Linear(a.ScaleY, b.ScaleY, factor);
     target.PivotX      = a.PivotX;
     target.PivotY      = a.PivotY;
     target.FileId      = a.FileId;
     target.FolderId    = a.FolderId;
     target.EntityId    = a.EntityId;
     target.AnimationId = a.AnimationId;
     target.T           = MathHelper.Linear(a.T, b.T, factor);
 }
Ejemplo n.º 21
0
 /// <summary>Compares the z_index of the given SpriterObject with this.</summary>
 /// <remarks>Compares the z_index of the given SpriterObject with this.</remarks>
 /// <param name="o">SpriterObject to compare with.</param>
 public virtual int compareTo(SpriterObject o)
 {
     if (this.zIndex < o.zIndex)
     {
         return -1;
     }
     else
     {
         if (this.zIndex > o.zIndex)
         {
             return 1;
         }
         else
         {
             return 0;
         }
     }
 }
Ejemplo n.º 22
0
        protected override void ApplySpriteTransform(ISprite drawable, SpriterObject info)
        {
            float posX, posY, rotation;

            GetPositionAndRotation(info, out posX, out posY, out rotation);

            SpriteDrawInfo di = DrawInfoPool.Count > 0 ? DrawInfoPool.Pop() : new SpriteDrawInfo();

            di.Drawable = drawable;
            di.Pivot    = new Vector2(info.PivotX, (1 - info.PivotY));
            di.Position = new Vector2(posX, posY);
            di.Scale    = new Vector2(info.ScaleX, info.ScaleY) * Scale;
            di.Rotation = rotation;
            di.Color    = Color * info.Alpha;
            di.Depth    = Depth + DeltaDepth * DrawInfos.Count;

            DrawInfos.Add(di);
        }
Ejemplo n.º 23
0
 /// <summary>Compares the z_index of the given SpriterObject with this.</summary>
 /// <remarks>Compares the z_index of the given SpriterObject with this.</remarks>
 /// <param name="o">SpriterObject to compare with.</param>
 public virtual int compareTo(SpriterObject o)
 {
     if (this.zIndex < o.zIndex)
     {
         return(-1);
     }
     else
     {
         if (this.zIndex > o.zIndex)
         {
             return(1);
         }
         else
         {
             return(0);
         }
     }
 }
Ejemplo n.º 24
0
        public static void sort(SpriterObject[] objects)
        {
            bool PaarSortiert;

            do
            {
                PaarSortiert = true;
                for (int i = 0; i < objects.Length - 1; i++)
                {
                    if (objects[i].getZIndex() > objects[i + 1].getZIndex())
                    {
                        SpriterObject temp = objects[i];
                        objects[i]     = objects[i + 1];
                        objects[i + 1] = temp;
                        PaarSortiert   = false;
                    }
                }
            } while (!PaarSortiert);
        }
Ejemplo n.º 25
0
        public static SpriterFile GetSpriterFile(SpriterObject spriterObject, MonoGameAnimator animator)
        {
            if (animator == null)
            {
                throw new ArgumentException("Please make sure that a Spriter animator is linked to the given entity.");
            }

            foreach (var folder in animator.Entity.Spriter.Folders)
            {
                var spriterFileId = Array.FindIndex(folder.Files, (file) => file.Id == spriterObject.FileId);

                if (spriterFileId != -1)
                {
                    return(folder.Files[spriterFileId]);
                }
            }

            throw new ArgumentException("Please make sure that the given Spriter entity has a sprite part with file ID: " + spriterObject.FileId);
        }
        private bool CheckBoundingBox(MonoGameAnimator animator, int id, SpriterObject info, int x, int y)
        {
            var          objectData   = animator.Entity.ObjectInfos[id];
            Box          box          = animator.GetBoundingBox(info, objectData.Width, objectData.Height);
            GraphicsPath graphicsPath = new GraphicsPath();

            graphicsPath.AddPolygon(new System.Drawing.PointF[] {
                new System.Drawing.PointF(box.Point1.X, box.Point1.Y),
                new System.Drawing.PointF(box.Point2.X, box.Point2.Y),
                new System.Drawing.PointF(box.Point3.X, box.Point3.Y),
                new System.Drawing.PointF(box.Point4.X, box.Point4.Y)
            });

            if (graphicsPath.IsVisible(new System.Drawing.Point(x, y)))
            {
                return(true);
            }

            return(false);
        }
Ejemplo n.º 27
0
 private void Draw(SpriterAnimationPlayer player, IEnumerator <SpriterObject> objectIterator, Entity.CharacterMap[] characterMaps, SpriteBatch sb)
 {
     while (objectIterator.MoveNext())
     {
         SpriterObject obj = objectIterator.Current;
         if ([email protected]())
         {
             if (characterMaps != null)
             {
                 foreach (var characterMap in characterMaps)
                 {
                     if (characterMap != null)
                     {
                         [email protected](characterMap.Get(obj.@ref));
                     }
                 }
             }
             Draw(player, obj, sb);
         }
     }
 }
Ejemplo n.º 28
0
        /// <summary>Searches for a keyframe in this animation which has a smaller or equal starting time as the given time and contains the given object.
        ///     </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 object.
        ///     </remarks>
        /// <param name="object"></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 object.
        ///     </returns>
        public virtual SpriterKeyFrame getPreviousFrameForObject
            (SpriterObject @object, long time)
        {
            SpriterKeyFrame frame = null;

            foreach (SpriterKeyFrame key in this.frames)
            {
                if (!key.containsObject(@object))
                {
                    continue;
                }
                if (key.getTime() <= time)
                {
                    frame = key;
                }
                else
                {
                    break;
                }
            }
            return(frame);
        }
Ejemplo n.º 29
0
        private void LoadObjectOrBone(ContentReader input, TimelineKey key, Timeline timeline)
        {
            string name = input.ReadString();
            Point  pivot = new Point(input.ReadSingle(), input.ReadSingle());
            Point  scale = new Point(input.ReadSingle(), input.ReadSingle());
            Point  position = new Point(input.ReadSingle(), input.ReadSingle());
            float  angle = input.ReadSingle();
            int    folder = -1, file = -1;
            float  alpha = 1f;

            if (name == "object")
            {
                if (timeline.ObjectInfo.Type == Entity.ObjectType.Sprite)
                {
                    folder = input.ReadInt32();
                    file   = input.ReadInt32();
                    alpha  = input.ReadSingle();


                    File f = data.GetFolder(folder).GetFile(file);
                    // TODO objectilla voi olla kans pivot, pitäs kirjottaa?
                    pivot = new Point(f.Pivot.X, f.Pivot.Y);
                    timeline.ObjectInfo.Size.Set(f.Size);
                }
            }
            SpriterObject obj;

            if (name == "bone")
            {
                obj = new SpriterObject(position, scale, pivot, angle, alpha, new FileReference(folder, file));
            }
            else
            {
                obj = new SpriterObject(position, scale, pivot, angle, alpha, new FileReference(folder, file));
            }
            key.Object = obj;
            timeline.AddKey(key);
        }
Ejemplo n.º 30
0
        protected override void ApplySpriteTransform(Texture2D texture, SpriterObject info)
        {
            Vector2       origin   = new Vector2(info.PivotX * texture.Width, (1 - info.PivotY) * texture.Height);
            Vector2       location = charLocation + new Vector2(info.X, -info.Y) * Scale;
            Vector2       scale    = new Vector2(Math.Abs(info.ScaleX), Math.Abs(info.ScaleY)) * Scale;
            float         angle    = -(float)(Math.PI / 180.0f) * info.Angle;
            Color         color    = Color.White * info.Alpha;
            SpriteEffects effects  = SpriteEffects.None;

            if (info.ScaleX < 0)
            {
                effects |= SpriteEffects.FlipHorizontally;
                origin   = new Vector2(texture.Width - origin.X, origin.Y);
            }

            if (info.ScaleY < 0)
            {
                effects |= SpriteEffects.FlipVertically;
                origin   = new Vector2(origin.X, texture.Height - origin.Y);
            }

            spriteBatch.Draw(texture, location, null, color, angle, origin, scale, effects, 1);
        }
Ejemplo n.º 31
0
        public Box GetBoundingBox(SpriterObject info, float width, float height)
        {
            float posX, posY, rotation;

            GetPositionAndRotation(info, out posX, out posY, out rotation);

            float w = width * info.ScaleX * Scale.X;
            float h = height * info.ScaleY * Scale.Y;

            float rs = (float)Math.Sin(rotation);
            float rc = (float)Math.Cos(rotation);

            Vector2 originDelta = Rotate(new Vector2(-info.PivotX * w, -(1 - info.PivotY) * h), rs, rc);

            Box     cb         = new Box();
            Vector2 horizontal = Rotate(new Vector2(w, 0), rs, rc);

            cb.Point1 = new Vector2(posX, posY) + originDelta;
            cb.Point2 = cb.Point1 + horizontal;
            cb.Point4 = cb.Point1 + Rotate(new Vector2(0, h), rs, rc);
            cb.Point3 = cb.Point4 + horizontal;

            return(cb);
        }
        public static void sort(SpriterObject[] objects)
        {
            bool PaarSortiert;
            do
            {
                PaarSortiert = true;
                for (int i = 0; i < objects.Length - 1; i++)
                {
                    if (objects[i].getZIndex() > objects[i + 1].getZIndex())
                    {
                        SpriterObject temp = objects[i];
                        objects[i] = objects[i + 1];
                        objects[i + 1] = temp;
                        PaarSortiert = false;
                    }
                }

            } while (!PaarSortiert);
        }