Coder Social home page Coder Social logo

benmakesgames / fov Goto Github PK

View Code? Open in Web Editor NEW
1.0 3.0 0.0 71 KB

A collection of field-of-view/line-of-sight algorithms designed for tile-based games.

License: MIT License

C# 100.00%
field-of-view fov fov-algorithms line-of-sight los los-algorithms tile-based tile-based-game

fov's Introduction

What Is It?

BenMakesGames.FoV is a collection of field-of-view algorithms designed for square tile grids. It features the following algorithms:

  • Diamond-wall
  • Milazzo's Beveled-wall
  • Raycasting
  • Shadowcasting

Field-of-view/line-of-sight is useful for roguelikes, and other tactical, tile-based games where vision plays an important role.

Buy Me a Coffee at ko-fi.com

How to Use

Install

dotnet add package BenMakesGames.FoV 

Create a Map

Your map must implement IFoVMap, which requires a Width and Height property, and a method that returns whether or not a given tile is opaque:

For example:

public sealed class MyMap: IFoVMap
{
    public int Width { get; }
    public int Height { get; }

    // store your tiles however you want; here's one possibility:
    public MyTile[] Tiles { get; }

    // BlocksLight is required by IFoVMap; here's one possible implementation:
    bool BlocksLight(int x, int y)
    {
        if(x < 0 || x >= Width || y < 0 || y >= Height)
            return true;

        return Tiles[x + y * Width].IsOpaque;
    }

    ...
}

Another common implementation is to use a Dictionary<(int X, int Y), MyTile> collection to store the map.

Call One of the FoV Algorithms

All of the algorithms have the same signature:

HashSet<(int X, int Y)> Compute(IFoVMap map, (int X, int Y) origin, int radius)

They take a map, origin point, and sight radius, and returns a set of points that are visible from the origin.

Basic usage:

var visibleTiles = DiamondWallsFoV.Compute(Map, (Player.X, Player.Y), Player.SightRadius);

for(int y = 0; y < Map.Height; y++)
{
    for(int x = 0; x < Map.Width; x++)
    {
        if(visibleTiles.Contains((x, y)))
        {
            // tile is visible; draw it!
        }
        else
        {
            // don't draw the tile, or draw it as a fog of war tile
        }
    }
}

When implementing field-of-view in your game, you should only compute a new field of view when the player moves, or the map changes (such as a door opening or closing).

Available Algorithms, and Their Features

  • DiamondWallsFoV
    • Relatively fast algorithm. Compared to other algorithms, reveals more tiles in a given sight radius.
  • MilazzoFoV
    • Medium speed; designed to have intuitive lines of sight, especially for maps which contain many single-tile walls/pillars.
  • RayCastFoV
    • Slowest algorithm, with occasionally unintuitive lines of sight. Not generally recommended; included for historical reasons.
  • ShadowCastFoV
    • The fastest algorithm of the bunch, especially when used in large, open spaces with large sight radii.

fov's People

Contributors

benmakesgames avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.