Пример #1
0
    public SpriteSheetAnimator Build(Point mousePos)
    {
        SpriteSheetAnimator ssa = new SpriteSheetAnimator();

        ssa.Load(SpriteSheetTexture, NumberOfFrames, 1, 0, FrameDurationMs, new Vector2(mousePos.X, mousePos.Y), 1f, Color.Black);

        return(ssa);
    }
    //Build collision animation on either side of the contact surface scaled to the rec1 scale.
    public List <SpriteSheetAnimator> Build(Rectangle rec1, Rectangle rec2, Vector2 collisionOffset, Vector2 obj1Direction, float rec1Scale, Color color)
    {
        List <SpriteSheetAnimator> rtnAnims = new List <SpriteSheetAnimator>();

        SpriteSheetAnimator anim1 = new SpriteSheetAnimator();
        SpriteSheetAnimator anim2 = new SpriteSheetAnimator();

        //Reduce scale as we get above 1f since it starts to look strange
        if (rec1Scale > 1)
        {
            float overage = rec1Scale - 1f;
            overage = overage * .5f;

            rec1Scale = 1f + overage;
        }

        if (Math.Abs(collisionOffset.X) < Math.Abs(collisionOffset.Y)) //Left or Right side collision
        {
            if (obj1Direction.X > 0)                                   //Object 1 Right side collision
            {
                if (rec1.Top > rec2.Top)
                {
                    anim1.Load(SpriteSheetTextureWestTop, NumberOfFrames, 1, 0, FrameDurationMs, new Vector2(0, 0), rec1Scale, color);

                    float x = rec2.Left - (anim1.Bounds.Width / 2);
                    float y = rec1.Top - (anim1.Bounds.Height / 2);

                    anim1.Position = new Vector2(x, y);

                    rtnAnims.Add(anim1);
                }
                if (rec1.Bottom < rec2.Bottom)
                {
                    anim2.Load(SpriteSheetTextureWestBottom, NumberOfFrames, 1, 0, FrameDurationMs, new Vector2(0, 0), rec1Scale, color);

                    float x = rec2.Left - (anim2.Bounds.Width / 2);
                    float y = rec1.Bottom + (anim2.Bounds.Height / 2);

                    anim2.Position = new Vector2(x, y);

                    rtnAnims.Add(anim2);
                }
            }
            else //Object 1 Left side collision
            {
                if (rec1.Top > rec2.Top)
                {
                    anim1.Load(SpriteSheetTextureEastTop, NumberOfFrames, 1, 0, FrameDurationMs, new Vector2(0, 0), rec1Scale, color);

                    float x = rec2.Right + (anim1.Bounds.Width / 2);
                    float y = rec1.Top - (anim1.Bounds.Height / 2);

                    anim1.Position = new Vector2(x, y);

                    rtnAnims.Add(anim1);
                }
                if (rec1.Bottom < rec2.Bottom)
                {
                    anim2.Load(SpriteSheetTextureEastBottom, NumberOfFrames, 1, 0, FrameDurationMs, new Vector2(0, 0), rec1Scale, color);

                    float x = rec2.Right + (anim2.Bounds.Width / 2);
                    float y = rec1.Bottom + (anim2.Bounds.Height / 2);

                    anim2.Position = new Vector2(x, y);

                    rtnAnims.Add(anim2);
                }
            }
        }
        else //Top or Bottom side collision
        {
            if (obj1Direction.Y > 0) //Object 1 Bottom side collision
            {
                if (rec1.Left > rec2.Left)
                {
                    anim1.Load(SpriteSheetTextureNorthLeft, NumberOfFrames, 1, 0, FrameDurationMs, new Vector2(0, 0), rec1Scale, color);

                    float x = rec1.Left - (anim1.Bounds.Width / 2);
                    float y = rec2.Top - (anim1.Bounds.Height / 2);

                    anim1.Position = new Vector2(x, y);

                    rtnAnims.Add(anim1);
                }
                if (rec1.Right < rec2.Right)
                {
                    anim2.Load(SpriteSheetTextureNorthRight, NumberOfFrames, 1, 0, FrameDurationMs, new Vector2(0, 0), rec1Scale, color);

                    float x = rec1.Right + (anim2.Bounds.Width / 2);
                    float y = rec2.Top - (anim2.Bounds.Height / 2);

                    anim2.Position = new Vector2(x, y);

                    rtnAnims.Add(anim2);
                }
            }
            else //Object 1 Top side collision
            {
                if (rec1.Left > rec2.Left)
                {
                    anim1.Load(SpriteSheetTextureSouthLeft, NumberOfFrames, 1, 0, FrameDurationMs, new Vector2(0, 0), rec1Scale, color);

                    float x = rec1.Left - (anim1.Bounds.Width / 2);
                    float y = rec2.Bottom + (anim1.Bounds.Height / 2);

                    anim1.Position = new Vector2(x, y);

                    rtnAnims.Add(anim1);
                }
                if (rec1.Right < rec2.Right)
                {
                    anim2.Load(SpriteSheetTextureSouthRight, NumberOfFrames, 1, 0, FrameDurationMs, new Vector2(0, 0), rec1Scale, color);

                    float x = rec1.Right + (anim2.Bounds.Width / 2);
                    float y = rec2.Bottom + (anim2.Bounds.Height / 2);

                    anim2.Position = new Vector2(x, y);

                    rtnAnims.Add(anim2);
                }
            }
        }


        return(rtnAnims);
    }