Code Monkey home page Code Monkey logo

graph-to-grid's Introduction

Nuget

Graph To Grid

Given a single undirected planar graph as well as an array of shapes, procedurally generates multiple unique layouts each mapped onto a 2d cartesian grid. These layouts can be used directly in your graphics framework or game engine of choice to represent maps in a 2d game, be it a Tiled or regular map. Implementation of an algorithm defined in a paper 'Game Level Layout from Design Specification' by Chongyang Ma, et al..

Example Graph Output

Usage

dotnet add package GraphToGrid

Create blueprints representing shapes

RoomBlueprint squareBlueprint = new RoomBlueprint(
    points: new List<Vector2F>(
        new Vector2F[] {
            new Vector2F(0, 0), 
            new Vector2F(0, 10),
            new Vector2F(10, 10),
            new Vector2F(10, 0)}));

Create definitions that can be assigned to any given graph node to control available blueprints

RoomDefinition regularRoom = new RoomDefinition( 
    blueprints: new List<RoomBlueprint>() {
        squareBlueprint});

Create a graph assigning definitions to nodes and connections between nodes

LayoutGraph graph = new LayoutGraph();

graph.AddRoom(0, regularRoom);
graph.AddRoom(1, regularRoom);
graph.AddRoom(2, regularRoom);

graph.Connection(0, 1);
graph.Connection(1, 2);

Optionaly override default configuration and provide logger

G2GConfig.DoorWidth = 10;
G2GConfig.DoorToCornerMinGap = 5;
G2GConfig.TargetSolutions = 1;

G2GDebug.Logger = LoggerFactory.Create(builder => {
    builder
        .AddSimpleConsole(options => options.SingleLine = true)
        .SetMinimumLevel(LogLevel.Debug);
}).CreateLogger("GraphToGrid");

Initialize a layout generator.

LayoutGenerator generator = new LayoutGenerator(graph, config);
generator.Initialize(); 

Generate and retrieve a layout snapped to an integer grid useful for generating Tiled Maps

if (generator.TryGenerate()) {
    var layout = generator.Vend();

    layout.SnapToGrid(); 
}

Contribute

Feel free to open an issue and reference it in a Pull Request. Please ensure there is a corresponding unit test and that the demo project is updated and renders as expected. The demo project is uses the Monogame SDK for rendering.

Continuous Deployment

A GitHub workflow will deploy a new nuget package upon Pull Request merge to main. Publish has been set to ignore if the version matches an existing packge. Version can be updated via GraphToGrid.csproj.

graph-to-grid's People

Contributors

karatakos avatar

Stargazers

 avatar

Watchers

 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.