Exemplo n.º 1
0
        private void ResetGame()
        {
            world.Remove(ball.Body);
            var circle = world.CreateCircle(Ball.Radius, 1f, new Vector2(0, Ball.Radius));

            circle.BodyType = BodyType.Dynamic;
            circle.SetRestitution(1f);
            circle.SetFriction(0.0f);

            ball = new Ball(circle, textureHelper.Get <Texture2D>("ball"));
            ball.Body.ApplyLinearImpulse(RandomHelper.GetRandomBool() ? new Vector2(8f, 0) : new Vector2(-8f, 0));
        }
Exemplo n.º 2
0
        protected override void Initialize()
        {
            world = new World(Vector2.Zero);

            var circle = world.CreateCircle(Ball.Radius, 1f, new Vector2(0, Ball.Radius));

            circle.BodyType = BodyType.Dynamic;
            circle.SetRestitution(1f);
            circle.SetFriction(0.0f);

            ball = new Ball(circle);

            var horizontalWallSize = new Vector2(20f, 0.1f);
            var lowerWallBody      =
                world.CreateRectangle(horizontalWallSize.X, horizontalWallSize.Y, 1f, new Vector2(0, -6));

            lowerWallBody.BodyType = BodyType.Static;
            lowerWallBody.SetRestitution(0.3f);
            lowerWallBody.SetFriction(0.5f);

            var upperWallBody = lowerWallBody.DeepClone(world);

            upperWallBody.Position = new Vector2(0, 5);

            cage.LowerWall = new Wall(lowerWallBody, horizontalWallSize);
            cage.UpperWall = new Wall(upperWallBody, horizontalWallSize);


            var verticalWallSize = new Vector2(0.1f, 12f);
            var leftWallBody     = world.CreateRectangle(verticalWallSize.X, verticalWallSize.Y, 1f, new Vector2(-10f, 0));

            leftWallBody.BodyType = BodyType.Static;
            leftWallBody.SetRestitution(0.3f);
            leftWallBody.SetFriction(0.5f);

            var rightWallBody = leftWallBody.DeepClone(world);

            rightWallBody.Position = new Vector2(10f, 0);

            cage.LeftWall  = new Wall(leftWallBody, verticalWallSize);
            cage.RightWall = new Wall(rightWallBody, verticalWallSize);


            var playerSize     = new Vector2(0.25f, 2f);
            var leftPlayerBody =
                world.CreateRectangle(playerSize.X, playerSize.Y, 5f, new Vector2(-9f, 0.5f));

            leftPlayerBody.BodyType = BodyType.Kinematic;
            leftPlayerBody.SetRestitution(1f);
            leftPlayerBody.SetFriction(0.5f);
            leftPlayer = new Player(leftPlayerBody, playerSize);

            var rightPlayerBody = leftPlayerBody.DeepClone(world);

            rightPlayerBody.Position = new Vector2(9f, 0.5f);
            rightPlayer = new Player(rightPlayerBody, playerSize);

            var leftScoreWallBody =
                world.CreateRectangle(verticalWallSize.X, verticalWallSize.Y, 1f, new Vector2(-9.3f, 0));

            leftScoreWallBody.BodyType = BodyType.Static;

            var rightScoreWallBody = leftScoreWallBody.DeepClone(world);

            rightScoreWallBody.Position = new Vector2(9.3f, 0);

            leftScoreWall  = new ScoreWall(leftScoreWallBody, rightPlayer);
            rightScoreWall = new ScoreWall(rightScoreWallBody, leftPlayer);

            leftPlayer.AddCallback(DeactivateBall);
            rightPlayer.AddCallback(DeactivateBall);

            ball.Body.ApplyLinearImpulse(RandomHelper.GetRandomBool() ? new Vector2(8f, 0) : new Vector2(-8f, 0));

            base.Initialize();
        }