GithubHelp home page GithubHelp logo

nsmryan / shadowcasting Goto Github PK

View Code? Open in Web Editor NEW
6.0 3.0 2.0 21 KB

A Rust translation of the algorithm found here: https://www.albertford.com/shadowcasting/

License: Creative Commons Zero v1.0 Universal

Rust 100.00%
fov roguelike rust crate

shadowcasting's Introduction

Symmetric Shadow Casting

This repository is just a Rust translation of the shadow casting algorithm from the Python implementation shown here. It is a very nice article, with visuals and explaination.

This implementation is tested against the examples in the article, and produces identical results.

Usage

The use of this crate is simple- there is only a single function exposed:

pub type Pos = (isize, isize);

pub fn compute_fov<F, G>(origin: Pos, is_blocking: &mut F, mark_visible: &mut G)
    where F: FnMut(Pos) -> bool,
          G: FnMut(Pos), {

This function takes a position, which is a pair of isizes (you may have to cast into and out of this type), and two closures.

The closure is_blocking tells the algorithm when a tile is blocked, such as by a wall. This may capture some kind of user structure in its environment, such as a grid or map.

The closure mark_visible is called for each visible tile, allowing the user to handle the tile. This may capture some mutable state in its environment, modifying it by indicating visible tiles.

See the tests for examples on how this might be used.

Limitations

I would have liked to have this interface provide a way to lookup blocking tiles and modify the same structure, but I couldn't get the lifetimes to work out. As it is, you will have to capture your map structure immutably in is_blocking, and some other structure mutably in mark_visible which tracks the results. I would expect that this mutated structure will by used to modify the map or grid, perhaps something cloned first if we are marking visibility within the structure itself.

Similar Crates

There is already the shadowcast crate. This crate seems to implement additional algorithms, and is likely a fine choice. It seems to provide some additional features such as casting different shapes, which is cool. It also seems to make some additional assumptions, requiring more dependencies, and providing a more complex API. Check it out if you are looking for different types of casting.

Alternate Name

My brother suggested another name for this crate- I Don't Eat Anything That Casts a Shadow. This is much funnier, but I went with the boring option of using the title of the tutorial that the algorithm came from.

License

As with the original algorith, this is licensed as CC0.

shadowcasting's People

Contributors

nsmryan avatar terrapass avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  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.