Beispiel #1
0
        protected override void LoadContent()
        {
            _spriteBatch = new SpriteBatch(GraphicsDevice);

            _bitmapFont = Content.Load<BitmapFont>("montserrat-32");

            var viewportAdapter = new BoxingViewportAdapter(Window, GraphicsDevice, 800, 480);
            _camera = new Camera2D(viewportAdapter)
            {
                Zoom = 2.0f
            };
            _sceneGraph = new SceneGraph();

            var carHullTexture = Content.Load<Texture2D>("car-hull");
            var carHullSprite = new Sprite(carHullTexture);
            var carWheelTexture = Content.Load<Texture2D>("car-wheel");
            var carWheelSprite = new Sprite(carWheelTexture);

            _carNode = new SceneNode("car-hull", viewportAdapter.Center.ToVector2());
            _carNode.Entities.Add(carHullSprite);

            _leftWheelNode = new SceneNode("left-wheel", new Vector2(-29, 17));
            _leftWheelNode.Entities.Add(carWheelSprite);

            _rightWheelNode = new SceneNode("right-wheel", new Vector2(40, 17));
            _rightWheelNode.Entities.Add(carWheelSprite);

            _carNode.Children.Add(_rightWheelNode);
            _carNode.Children.Add(_leftWheelNode);
            _sceneGraph.RootNode.Children.Add(_carNode);
        }
Beispiel #2
0
        protected override void Update(GameTime gameTime)
        {
            var deltaTime = (float)gameTime.ElapsedGameTime.TotalSeconds;
            var keyboardState = Keyboard.GetState();
            var mouseState = Mouse.GetState();

            if (keyboardState.IsKeyDown(Keys.Escape))
            {
                Exit();
            }

            if (keyboardState.IsKeyDown(Keys.W))
            {
                _speed += deltaTime * 0.5f;
            }

            if (keyboardState.IsKeyDown(Keys.S))
            {
                _speed -= deltaTime * 0.5f;
            }

            _leftWheelNode.Rotation += _speed;
            _rightWheelNode.Rotation = _leftWheelNode.Rotation;
            _carNode.Position += new Vector2(_speed * 5, 0);

            // quick and dirty collision detection
            const int maxX = 535;
            if (_carNode.Position.X >= maxX)
            {
                _speed = -_speed * 0.2f;
                _carNode.Position = new Vector2(maxX, _carNode.Position.Y);
            }

            const int minX = 265;
            if (_carNode.Position.X <= minX)
            {
                _speed = -_speed * 0.2f;
                _carNode.Position = new Vector2(minX, _carNode.Position.Y);
            }

            var worldPosition = _camera.ScreenToWorld(mouseState.X, mouseState.Y);
            _hoveredNode = _sceneGraph.GetSceneNodeAt(worldPosition);

            base.Update(gameTime);
        }
 public SceneGraph()
 {
     RootNode = new SceneNode();
 }
 public SceneGraph()
 {
     RootNode = new SceneNode();
 }