Пример #1
0
        private static void BoundFrom(MovableEntity mov, Entity ent)
        {
            int       replay = 0;
            FloatRect rect   = mov.GetFloatRect();

            while (CollisionDetector.IsSticking(rect, ent.GetFloatRect()))
            {
                float angle = (float)(Math.PI - Math.Atan2(mov.PreviousVelocity.Y, mov.PreviousVelocity.X) * 2);
                if (mov.GetType() == Entities.EntitiesContainer.Ball.GetType() && ent.GetType() == Entities.EntitiesContainer.Player1.GetType())
                {
                    angle = GetValidAngleForBallAndPlayer(mov, ent);
                    Entities.EntitiesContainer.Ball.WasHitWith(Entities.EntitiesContainer.GetPlayer(ent.ID));
                    AudioSystem.AudioController.PlaySound("HIT");
                }
                mov.Velocity = GetBoundVector(mov, angle, replay > 0 ? true : false);
                rect.Left    = mov.GetFloatRect().Left + mov.Velocity.X;
                rect.Top     = mov.GetFloatRect().Top + mov.Velocity.Y;
                replay++;

                if (replay > 3)
                {
                    mov.ResetPosition();
                }
            }
        }
Пример #2
0
        private static float GetValidAngleForBallAndPlayer(MovableEntity ball, Entity player)
        {
            float lenghtOfArea = player.GetFloatRect().Height * 1.3f / 8;
            float middleOfBall = ball.GetFloatRect().Top + ball.GetFloatRect().Height / 2;
            float touchingArea = (middleOfBall - player.GetFloatRect().Top) / lenghtOfArea;

            float bounceAngle;

            switch ((int)touchingArea)
            {
            case 1:
                bounceAngle = 15;
                break;

            case 2:
                bounceAngle = 45;
                break;

            case 3:
                bounceAngle = 75;
                break;

            case 4:
                bounceAngle = 105;
                break;

            case 5:
                bounceAngle = -105;
                break;

            case 6:
                bounceAngle = -75;
                break;

            case 7:
                bounceAngle = -45;
                break;

            case 8:
                bounceAngle = -15;
                break;

            default:
                bounceAngle = 90;
                break;
            }

            return((float)((Math.PI * bounceAngle) / 180));
        }
Пример #3
0
        public static bool WillBeColliding(MovableEntity mov, Entity ent)
        {
            FloatRect rect = mov.GetFloatRect();

            rect.Left += mov.Velocity.X;
            rect.Top  += mov.Velocity.Y;

            if (ent.GetFloatRect().Intersects(rect))
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
Пример #4
0
        private static void TeleportToCollidingObjectAndSetVelocityTo0(MovableEntity mov, Entity ent)
        {
            Vector2f dis = CollisionDetector.DistanceToCollision(mov.GetFloatRect(), ent.GetFloatRect());

            mov.PreviousVelocity = mov.Velocity;

            if (Math.Abs(dis.X) > Math.Abs(dis.Y))
            {
                if (mov.Velocity.Y != 0)
                {
                    mov.Body.Position = new Vector2f(mov.Body.Position.X, mov.Body.Position.Y + dis.Y);
                    if (mov.Velocity.X != 0)
                    {
                        mov.Velocity = new Vector2f((float)Math.Sqrt(mov.Velocity.X * mov.Velocity.X + mov.Velocity.Y * mov.Velocity.Y), 0);
                    }
                    else
                    {
                        mov.Velocity = new Vector2f(mov.Velocity.X, 0);
                    }
                }
            }
            else
            {
                if (mov.Velocity.X != 0)
                {
                    mov.Body.Position = new Vector2f(mov.Body.Position.X + dis.X, mov.Body.Position.Y);
                    if (mov.Velocity.Y != 0)
                    {
                        mov.Velocity = new Vector2f(0, (float)Math.Sqrt(mov.Velocity.X * mov.Velocity.X + mov.Velocity.Y * mov.Velocity.Y));
                    }
                    else
                    {
                        mov.Velocity = new Vector2f(0, mov.Velocity.Y);
                    }
                }
            }
        }