Example #1
0
    public override void OnAwake()
    {
        CreateBackground();

        _physicsContainer = PhysicsContainer.NewEmpty(Vector2.up * -30f);
        AddChild(_physicsContainer);

        var field = CinchSprite.NewFromImage("Cinch2D/Field", 512);

        field.Width  = ViewportWidth;
        field.Height = 1f;

        //create a physics body for the field, same size as the CinchSprite
        var fieldBody = BodyFactory.CreateRectangle(_physicsContainer.World, field.Width, field.Height, 1);

        //as soon as we set the fieldBody as field's PhysicsBody, field will take its position and rotation from fieldBody after every frame
        field.PhysicsBody = fieldBody;
        //align it with the bottom of the screen
        //setting physics body position will update sprite's position on next frame
        fieldBody.Position = new Vector2(0, 0 - ViewportHeight / 2 + field.Height / 2f);
        //set the field as static so it uses less CPU
        fieldBody.IsStatic = true;
        //make it a wee bit bouncy
        fieldBody.Restitution = .5f;
        _physicsContainer.AddChild(field);

        //create a soccer ball to roll around
        var ball = CinchSprite.NewFromImage("Cinch2D/SoccerBall", 256);

        _ballBody          = BodyFactory.CreateCircle(_physicsContainer.World, .5f, 1f);
        ball.PhysicsBody   = _ballBody;
        _ballBody.BodyType = BodyType.Dynamic;
        //drop it on the right side of the screen
        _ballBody.Position = new Vector2(ViewportWidth / 2 - 1, 0);

        //we add all physics objects to the same parent.
        //Adding a physics object as a child of another physics object (like a wheel as a child of a car), will produce super-bad results.
        //you can still add non-physics objects to different containers, like a mud splat as a child of the ball
        _physicsContainer.AddChild(ball);

        var player1 = CreatePlayer();
        var player2 = CreatePlayer();

        _physicsContainer.AddChild(player1);
        _physicsContainer.AddChild(player2);

        player1.Y = player2.Y = 0 - ViewportHeight / 2 + field.Height + player1.Height / 2;
        player1.X = 0 - ViewportWidth / 2 + 1;
        player2.X = ViewportWidth / 2 - 1;
        player1.PhysicsBody.OnCollision += BallHitPlayer1;
        player2.PhysicsBody.OnCollision += BallHitPlayer2;

        _debugDrawButton      = Library.New <TextButton>("DebugDrawToggle");
        _debugDrawButton.Text = "Debug: Off";
        AddChild(_debugDrawButton);
        _debugDrawButton.Y = 0 + ViewportHeight / 2 - _debugDrawButton.Height;
        //toggle debug draw when pressed
        _debugDrawButton.AddEventListener <MouseEvent>(MouseEvent.MOUSE_UP, ToggleDebugDraw);
    }
Example #2
0
    public override void OnAwake()
    {
        CreateBackground();

        _physicsContainer = PhysicsContainer.NewEmpty(Vector2.up * -30f);
        AddChild(_physicsContainer);

        var field = CinchSprite.NewFromImage("Cinch2D/Field", 512);
        field.Width = ViewportWidth;
        field.Height = 1f;

        //create a physics body for the field, same size as the CinchSprite
        var fieldBody = BodyFactory.CreateRectangle(_physicsContainer.World, field.Width, field.Height, 1);

        //as soon as we set the fieldBody as field's PhysicsBody, field will take its position and rotation from fieldBody after every frame
        field.PhysicsBody = fieldBody;
        //align it with the bottom of the screen
        //setting physics body position will update sprite's position on next frame
        fieldBody.Position = new Vector2(0, 0 - ViewportHeight/2 + field.Height/2f);
        //set the field as static so it uses less CPU
        fieldBody.IsStatic = true;
        //make it a wee bit bouncy
        fieldBody.Restitution = .5f;
        _physicsContainer.AddChild(field);

        //create a soccer ball to roll around
        var ball = CinchSprite.NewFromImage("Cinch2D/SoccerBall", 256);
        _ballBody = BodyFactory.CreateCircle(_physicsContainer.World, .5f, 1f);
        ball.PhysicsBody = _ballBody;
        _ballBody.BodyType = BodyType.Dynamic;
        //drop it on the right side of the screen
        _ballBody.Position = new Vector2(ViewportWidth/2 - 1, 0);

        //we add all physics objects to the same parent.
        //Adding a physics object as a child of another physics object (like a wheel as a child of a car), will produce super-bad results.
        //you can still add non-physics objects to different containers, like a mud splat as a child of the ball
        _physicsContainer.AddChild(ball);

        var player1 = CreatePlayer();
        var player2 = CreatePlayer();
        _physicsContainer.AddChild(player1);
        _physicsContainer.AddChild(player2);

        player1.Y = player2.Y = 0 - ViewportHeight/2 + field.Height + player1.Height/2;
        player1.X = 0 - ViewportWidth/2 + 1;
        player2.X = ViewportWidth/2 - 1;
        player1.PhysicsBody.OnCollision += BallHitPlayer1;
        player2.PhysicsBody.OnCollision += BallHitPlayer2;

        _debugDrawButton = Library.New<TextButton>("DebugDrawToggle");
        _debugDrawButton.Text = "Debug: Off";
        AddChild(_debugDrawButton);
        _debugDrawButton.Y = 0 + ViewportHeight / 2 - _debugDrawButton.Height;
        //toggle debug draw when pressed
        _debugDrawButton.AddEventListener<MouseEvent>(MouseEvent.MOUSE_UP, ToggleDebugDraw);
    }
    public void Init(World w, PhysicsContainer p)
    {
        _world = w;
        _physicsContainer = p;
        _shader = ShaderCache.GetShader(CinchOptions.DefaultShader);
        _defaultMaterial = new Material (_shader);
        _circleMaterial = new Material (_shader);
        _defaultMaterial.mainTexture = TextureCache.GetCachedTexture("DebugDrawOutline");
        _circleMaterial.mainTexture = TextureCache.GetCachedTexture("DebugDrawCircleOutline");

        _awakeColor = Color.red;
        _asleepColor = Color.green;
        _staticColor = Color.gray;
    }