Ejemplo n.º 1
0
        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.Black);

            _spriteBatch.Begin();

            var gameState = _game.GetCurrentState();

            for (var y = 0; y < _level.Height; y++)
            {
                for (var x = 0; x < _level.Width; x++)
                {
                    var tile     = new Tile(x, y);
                    var position = new Vector2(x * Size, y * Size);

                    _spriteBatch.Draw(GetTileTexture(tile), position, Color.White);
                }
            }

            foreach (var box in gameState.BoxPositions)
            {
                _spriteBatch.Draw(_box, new Vector2(box.X * Size, box.Y * Size), Color.White);
            }

            var player = gameState.PlayerPosition;

            _spriteBatch.Draw(_player, new Vector2(player.X * Size, player.Y * Size), Color.White);

            if (_game.HasWon())
            {
                _spriteBatch.Draw(_screen, new Vector2(0, 0), Color.Black * 0.5f);
                _spriteBatch.DrawString(_font, "You've won!", new Vector2(100, 100), Color.Azure);
            }

            _spriteBatch.End();

            base.Draw(gameTime);

            Texture2D GetTileTexture(Tile tile)
            {
                if (_level.Walls.Contains(tile))
                {
                    return(_wall);
                }

                if (_level.TargetPositions.Contains(tile))
                {
                    return(_target);
                }

                return(_empty);
            }
        }