Esempio n. 1
0
 public virtual void Draw(SpriteBatch spriteBatch, Vector2 playerPosition, Camera camera)
 {
     //TODO(philipp): maybe make it a ctor param, then get rid of this nullcheck
     if (Map == null)
     {
         throw new System.NullReferenceException("Map must not be null.");
     }
 }
        /// <summary>
        /// actually draws the map ;) playerPosition is in mapspace
        /// </summary>
        /// <param name="spriteBatch"></param>
        /// <param name="playerPosition"></param>
        public override void Draw(SpriteBatch spriteBatch, Vector2 playerPosition, Camera camera)
        {
            base.Draw(spriteBatch, playerPosition, camera);

            var mapWidth = Map.Width;
            var mapHeight = Map.Height;

            var transparentWhite = new Color(255, 255, 255, 255);

            for (var y = 0; y < mapHeight; ++y)
            {
                for (var x = 0; x < mapWidth; ++x)
                {
                    var tileIndex = Map.PointToIndex(x, y);
                    var tile = Map.Tiles.ElementAt(tileIndex);

                    var tilePositionX = DrawBounds.X + (x * TileSize.X);
                    var tilePositionY = DrawBounds.Y + (y * TileSize.Y);

                    if (tile != null)
                    {
                        var transparentTileColor = new Color((int)tile.Color.R, (int)tile.Color.G, (int)tile.Color.B, transparentWhite.A);
                        spriteBatch.Draw(tile.Texture, new Rectangle(tilePositionX, tilePositionY, TileSize.X, TileSize.Y), transparentTileColor);
                    }
                    foreach (var subTile in tile.SubTiles)
                    {
                        var transparentSubTileColor = new Color((int)subTile.Color.R, (int)subTile.Color.G, (int)subTile.Color.B, transparentWhite.A);
                        spriteBatch.Draw(subTile.Texture, new Rectangle(tilePositionX, tilePositionY, TileSize.X, TileSize.Y), transparentSubTileColor);
                    }
                    //
                    // draw player
                    //
                    //TODO(philipp): move that into separate class for Player, Mobiles? Entities?
                    spriteBatch.Draw(_playerTile, new Rectangle(DrawBounds.X + (int)playerPosition.X * TileSize.X, DrawBounds.Y + (int)playerPosition.Y * TileSize.Y, TileSize.X, TileSize.Y), Color.White);

                    //draw the items
                    var item = Map.Items.FirstOrDefault(itm => itm.Position.Equals(Map.IndexToPoint(tileIndex)));
                    if (item != null)
                    {
                        spriteBatch.Draw(item.Template.Texture, new Rectangle(tilePositionX, tilePositionY, TileSize.X, TileSize.Y), transparentWhite);
                    }

                    //draw the mobiles
                    var mobile = Map.Mobiles.FirstOrDefault(mob => mob.Position.Equals(Map.IndexToPoint(tileIndex)));
                    if (mobile != null)
                    {
                        spriteBatch.Draw(mobile.Texture, new Rectangle(tilePositionX, tilePositionY, TileSize.X, TileSize.Y), transparentWhite);
                    }
                }
            }
        }
        public override void Draw(SpriteBatch spriteBatch, Vector2 playerPosition, Camera camera)
        {
            var mapWidth = Map.Width;
            var mapHeight = Map.Height;

            _effect.View = camera.ViewMatrix;
            _effect.Projection = camera.ProjectionMatrix;

            //TODO(philipp): this is total bullshit and very ineffective. create 1 custom vbo instead of that many ;)
            for (var z = -TilesToDisplayHalf; z < TilesToDisplayHalf + 1; ++z)
            {
                for (var x = -TilesToDisplayHalf; x < TilesToDisplayHalf + 1; ++x)
                {

                    var tileX = (int)playerPosition.X + x; //tileX in global mapspace
                    var tileZ = (int)playerPosition.Y + z; //tiley in global mapspace
                    if (tileX < 0 || tileZ < 0 || tileX >= mapWidth || tileZ >= mapHeight)
                    {
                        continue;
                    }

                    var tileIndex = Map.PointToIndex(tileX, tileZ);
                    if (tileIndex < 0)
                    {
                        continue;
                    }

                    var tile = Map.Tiles.ElementAt(tileIndex);

                    _effect.World = Matrix.CreateTranslation(-x * 0.5f, 0, z * 0.5f);

                    _effect.DiffuseColor = Color.Black.ToVector3();
                    /*
                    if (z == 0)
                    {
                        _effect.DiffuseColor = new Vector3(0, 0, 1.0f);
                    }
                    else if (x == 0)
                    {
                        _effect.DiffuseColor = new Vector3(1.0f, 0, 0.0f);
                    }
                    else*/ if (x==0 && z==0)
                    {
                        _effect.DiffuseColor = new Vector3(1.0f, 1.0f, 0.0f);
                    }
                    else
                    {
                        _effect.DiffuseColor = new Vector3(tile.Color.R / 4.0f, tile.Color.G / 4.0f, tile.Color.B / 4.0f);
                    }

                    var cubeIndex = ((z + TilesToDisplayHalf) * TilesToDisplay) + (x + TilesToDisplayHalf);
                    //TODO(deccer) replace with MonoGame/custom counterpart
                    //_cubes[cubeIndex].Draw(_effect);

                    if (tile.IsSolid)
                    {
                        if (tile.Type == Tiles.TileType.Tree)
                        {
                            _effect.World = Matrix.CreateTranslation(-x * 0.5f, 0.5f, z * 0.5f);
                            _treeModel.Draw(_effect.World, _effect.View, _effect.Projection);
                        }
                        else if (tile.Type == Tiles.TileType.Rock)
                        {
                            _effect.World = Matrix.CreateScale(1 / (2*_rockScale)) * Matrix.CreateTranslation(-x * 0.5f, 0.5f, z * 0.5f);
                            _rockModel.Draw(_effect.World, _effect.View, _effect.Projection);
                        }
                        else if (tile.Type == Tiles.TileType.Mountain)
                        {
                            _graphicsDevice.RasterizerState = RasterizerState.CullNone;

                            _effect.World = Matrix.CreateScale(1 / 1) * Matrix.CreateTranslation(-x * 0.5f, 0.5f, z * 0.5f);
                            _mountainModel.Draw(_effect.World, _effect.View, _effect.Projection);
                        }
                    }
                }
            }
        }
Esempio n. 4
0
 public void DrawMaps(SpriteBatch spriteBatch, Camera camera)
 {
     _mapOverviewRenderer.Draw(spriteBatch, _player.Position.ToVector2(), camera);
     _mapDetailsRenderer2d.Draw(spriteBatch, _player.Position.ToVector2(), camera);
     //_mapDetailsRenderer3d.Draw(spriteBatch, _player.Position, camera);
 }
Esempio n. 5
0
        protected override void LoadContent()
        {
            base.LoadContent();

            DetailTileSize = (ClientHeightHalf - (2 * Constants.Controls.Margin)) / MapDetailsRenderer2d.TilesToDisplay;
            DetailTileSizeHalf = DetailTileSize / 2;
            DetailsWidth = DetailTileSize * MapDetailsRenderer2d.TilesToDisplay;
            DetailsHeight = DetailsWidth;
            DetailsWidthHalf = DetailsWidth / 2;

            _camera = new Camera(ClientWidth, ClientHeight);
            _camera.Viewport = new Viewport(0, ClientHeightHalf, ClientWidthHalf, ClientHeightHalf);
            _camera.Position = Vector3.UnitZ * 4 + Vector3.UnitY * 6;
            _camera.FieldOfView = MathHelper.PiOver4;
            _camera.LookAt(-2 * Vector3.UnitZ);

            Tile.LoadContent(Content);

            _ui = new Window(null, this)
            {
                Position = new Point(ClientWidthHalf + DetailsWidthHalf + Constants.Controls.Margin, ClientHeightHalf - (2 * Constants.Controls.Margin)),
                Size = new Point(ClientWidth, ClientHeight),
            };

            var uixml = _ui.ToXElement();

            //_playerScreen = new PlayerScreen(_ui, this);
            //_playerScreen.Position = new Point(Constants.Controls.Margin, ClientHeightHalf - (2 * Constants.Controls.Margin));
            //_playerScreen.Player = _world.Player;

            //_controlScreen = new ControlScreen(_ui, this);
            //_controlScreen.Position = new Point(ClientWidthHalf + DetailsWidthHalf + Constants.Controls.Margin, ClientHeightHalf - (2 * Constants.Controls.Margin));

            //_ui.Load(Content);

            _world.LoadContent(Content);
            _world.Player.Position = new Point(9, 9);

            //_ui.SaveToFile(Path.Combine(WinForms.Application.StartupPath, "Data", "UI.xml"));
            _ui.LoadFromFile(Path.Combine(WinForms.Application.StartupPath, "Data", "UI.xml"));
            _ui.Load(Content);
            _ui.Layout();

            _playerScreen = _ui.GetControl<PlayerScreen>();
            _playerScreen.Player = _world.Player;
            _controlScreen = _ui.GetControl<ControlScreen>();

            _previousMilliseconds = 0;
        }
        /// <summary>
        /// actually draws the map ;) playerPosition is in mapspace
        /// </summary>
        /// <param name="spriteBatch"></param>
        /// <param name="playerPosition"></param>
        public override void Draw(SpriteBatch spriteBatch, Vector2 playerPosition, Camera camera)
        {
            base.Draw(spriteBatch, playerPosition, camera);

            var mapWidth = Map.Width;
            var mapHeight = Map.Height;

            for (var y = -TilesToDisplayHalf; y < TilesToDisplayHalf + 1; ++y)
            {
                for (var x = -TilesToDisplayHalf; x < TilesToDisplayHalf + 1; ++x)
                {
                    var tileX = (int)playerPosition.X + x; //tileX in global mapspace
                    var tileY = (int)playerPosition.Y + y; //tiley in global mapspace
                    if (tileX < 0 || tileY < 0 || tileX >= mapWidth || tileY >= mapHeight)
                    {
                        continue;
                    }

                    var tileIndex = Map.PointToIndex(tileX, tileY);
                    if (tileIndex < 0)
                    {
                        continue;
                    }

                    //TODO(philipp): proper naming, screen <-> position vs local/global
                    var tile = Map.Tiles.ElementAt(tileIndex);
                    var tileScreenX = x + TilesToDisplayHalf; //tileScreenX in local mapspace // 0..TilesToDisplay
                    var tileScreenY = y + TilesToDisplayHalf; //tileScreenY in local mapspace // 0..TilesToDisplay

                    var tilePositionX = DrawBounds.X + (tileScreenX * TileSize.X); //tilePositionX in screenspace (pixels) (relative to windowposition)
                    var tilePositionY = DrawBounds.Y + (tileScreenY * TileSize.Y); //tilePositionY in screenspace (pixels) (relative to windowposition)

                    //TODO(philipp): add options: "Advanced Lighting" :P
                    var colorTile = new Vector2(tileScreenX - TilesToDisplayHalf, tileScreenY - TilesToDisplayHalf);
                    var colorDistance = colorTile.Length();
                    var color = 1.0f - 0.0f; // (colorDistance / 12f); // 3.5 (cave), >= 8 (outside) TODO(philipp): maybe use that only inside caves
                    var tileColor = new Color(color, color, color, 1.0f);

                    var tileRect = new Rectangle(tilePositionX, tilePositionY, TileSize.X, TileSize.Y);

                    if (tile != null)
                    {
                        spriteBatch.Draw(tile.Texture, tileRect, tile.Color);
                        foreach (var subTile in tile.SubTiles)
                        {
                            spriteBatch.Draw(subTile.Texture, tileRect, tileColor);
                        }
                    }
                    //
                    // draw player
                    //
                    if (x == 0 && y == 0)
                    {
                        spriteBatch.Draw(_playerTile, new Rectangle(DrawBounds.X + TilesToDisplayHalf * TileSize.X,
                                                                    DrawBounds.Y + TilesToDisplayHalf * TileSize.Y,
                                                                    TileSize.X, TileSize.Y), Color.White);
                    }

                    var items = Map.Items.Where(ent => ent.Position.Equals(Map.IndexToPoint(tileIndex)));
                    var moreThanOneItemAtSamePosition = items.Count() > 1;
                    var item = items.FirstOrDefault();
                    if (item != null)
                    {
                        //TODO(philipp): make sure .Texture != null
                        spriteBatch.Draw(item.Template.Texture, tileRect, Color.White);
                        if (moreThanOneItemAtSamePosition)
                        {
                            spriteBatch.Draw(_itemStackOverlay, tileRect, Color.White);
                        }
                    }

                    var mobile = Map.Mobiles.FirstOrDefault(mob => mob.Position.Equals(Map.IndexToPoint(tileIndex)));
                    if (mobile != null)
                    {
                        spriteBatch.Draw(mobile.Texture, tileRect, Color.White);
                    }
                }
            }
            if (_lightMap != null)
            {
                //spriteBatch.Draw(_lightMap, new Vector2(DrawBounds.X, DrawBounds.Y), new Color(20, 20, 20, 200));
            }
        }