Field of View / No Mask

From TRCCompSci - AQA Computer Science
Revision as of 12:57, 24 May 2024 by Admin (talk | contribs) (Created page with "You can create a field of view by checking the distance between the tile to draw and the player. This might involve editing the code for you map drawing library. =Squared.Til...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

You can create a field of view by checking the distance between the tile to draw and the player. This might involve editing the code for you map drawing library.

Squared.Tiled

for example, something like:

if (Vector2.Distance(new Vector2(x * tileWidth, y * tileHeight), player) < distance)
                        {


                            info = _TileInfoCache[index];
                            batch.Draw(info.Texture, destPos - viewPos, info.Rectangle,
                                       Color.White * this.Opacity, rotation, new Vector2(tileWidth / 2f, tileHeight / 2f),
                                       1f, flipEffect, 0);
                        }

The if statement above is around the line that draws an individual tile.

Other Libraries

The other libraries all require a more complex draw method in your game, for example Squared.Tiled literally uses Map.Draw(). The other libraries require you to have a draw method like this:

            foreach (var layer in map.Layers.OfType<TileLayer>())
            {
                for (int y = 0, i = 0; y < layer.Height; y++)
                    for (int x = 0; x < layer.Width; x++, i++)
                    {
                        var gid = layer.Data[i];
                        if (gid == 0)
                            continue;
                        else
                        {
                            var tileset = map.Tilesets.Single(ts => gid >= ts.FirstGid && ts.FirstGid + ts.TileCount > gid);
                            var tile = tileset[gid];

                            Rectangle tilesetRec = new Rectangle(tile.Left, tile.Top, 32, 32);

                            _spriteBatch.Draw(ts, new Rectangle((int)x*32, (int)y*32, 32, 32), tilesetRec, Color.White);
                        }
                    }
            }

So this could become something like:

            foreach (var layer in map.Layers.OfType<TileLayer>())
            {
                for (int y = 0, i = 0; y < layer.Height; y++)
                    for (int x = 0; x < layer.Width; x++, i++)
                    {
                        var gid = layer.Data[i];
                        if (gid == 0)
                            continue;
                        else
                        {
                            if (Vector2.Distance(new Vector2(x * tileWidth, y * tileHeight), player) < distance)
                            {
                                var tileset = map.Tilesets.Single(ts => gid >= ts.FirstGid && ts.FirstGid + ts.TileCount > gid);
                                var tile = tileset[gid];

                                Rectangle tilesetRec = new Rectangle(tile.Left, tile.Top, 32, 32);

                                _spriteBatch.Draw(ts, new Rectangle((int)x*32, (int)y*32, 32, 32), tilesetRec, Color.White);
                            }
                        }
                    }
            }