Exemple #1
0
        public void Awake()
        {
            World            = new World(new Vector2(0, 0));
            World.AllowSleep = false;
            World.SetContactListener(this);

            Body ground;
            var  bd = new BodyDef();

            ground      = World.CreateBody(bd);
            FixedUpdate = new FixedUpdate(TimeSpan.FromSeconds(0.01d), () => { Step(true); });
            FixedUpdate.Start();
        }
        /// <summary>
        /// Handles all key presses on the window and manages input for the game.
        /// </summary>
        /// <param name="sender">The form that is sending the event.</param>
        /// <param name="e">Arguments about the key down event. Includes which key was pressed.</param>
        private void KeyboardInput(object sender, KeyEventArgs e)
        {
            if (pGameManager != null)
            {
                if (pGameManager.IsGameRunning())
                {
                    if (e.KeyCode == Keys.Left)
                    {
                        pGameManager.GetPlayerManager.Movement("Left");
                    }

                    if (e.KeyCode == Keys.Right)
                    {
                        pGameManager.GetPlayerManager.Movement("Right");
                    }
                }
            }

            if (e.KeyCode == Keys.Up)
            {
                pMenuManager.NavigateTo(pMenuManager.GetCurrentMenu - 1);
                this.Invalidate();
            }

            if (e.KeyCode == Keys.Down)
            {
                pMenuManager.NavigateTo(pMenuManager.GetCurrentMenu + 1);
                this.Invalidate();
            }

            if (e.KeyCode == Keys.Back)
            {
                // Go back
                pMenuManager.NavigateTo(1);
                this.Invalidate();
            }

            if (e.KeyCode == Keys.Enter)
            {
                if (pMenuManager.GetCurrentMenu == 1)
                {
                    pGameManager = new GameManager();

                    FixedUpdate.Start();
                    pGameManager.StartGame();
                }

                if (pMenuManager.GetCurrentMenu == 2)
                {
                    pMenuManager.NavigateTo(6);
                }

                if (pMenuManager.GetCurrentMenu == 3)
                {
                    pMenuManager.NavigateTo(7);
                }

                if (pMenuManager.GetCurrentMenu == 4)
                {
                    pMenuManager.NavigateTo(8);
                }

                this.Invalidate();
            }
        }
Exemple #3
0
        public void Tumbler(bool stressTest = false)
        {
            Body ground;

            {
                var bd = new BodyDef();
                ground = World.CreateBody(bd);
            }

            {
                var bd = new BodyDef
                {
                    BodyType   = BodyType.DynamicBody,
                    AllowSleep = false,
                    Position   = new Vector2(0.0f, 10.0f)
                };
                var body = World.CreateBody(bd);

                var shape = new PolygonShape();
                shape.SetAsBox(0.5f, 10.0f, new Vector2(10.0f, 0.0f), 0.0f);
                body.CreateFixture(shape, 5.0f);
                shape.SetAsBox(0.5f, 10.0f, new Vector2(-10.0f, 0.0f), 0.0f);
                body.CreateFixture(shape, 5.0f);
                shape.SetAsBox(10.0f, 0.5f, new Vector2(0.0f, 10.0f), 0.0f);
                body.CreateFixture(shape, 5.0f);
                shape.SetAsBox(10.0f, 0.5f, new Vector2(0.0f, -10.0f), 0.0f);
                body.CreateFixture(shape, 5.0f);

                var jd = new RevoluteJointDef
                {
                    BodyA          = ground,
                    BodyB          = body,
                    LocalAnchorA   = new Vector2(0.0f, 10.0f),
                    LocalAnchorB   = new Vector2(0.0f, 0.0f),
                    ReferenceAngle = 0.0f,
                    MotorSpeed     = 0.05f * Settings.Pi,
                    MaxMotorTorque = 1e8f,
                    EnableMotor    = true
                };
                _joint = (RevoluteJoint)World.CreateJoint(jd);
            }

            _bodyCount = 0;
            if (stressTest)
            {
                var timer = Stopwatch.StartNew();
                for (int i = 0; i < FrameCount; i++)
                {
                    Step(false);
                }

                timer.Stop();
                Console.WriteLine($"{timer.ElapsedMilliseconds} ms");
            }
            else
            {
                FixedUpdate = new FixedUpdate(TimeSpan.FromSeconds(1 / 60d), () => { Step(true); });
                FixedUpdate.Start();
                while (true)
                {
                    FixedUpdate.Update();
                }
            }
        }