Giter Club home page Giter Club logo

hexagons's Introduction

Hexagons

This is a hexagon library for JavaFX.

Features:

  • Pathfinding
  • Field of View
  • Calculate distance between hexes, line drawing and other useful functions
  • Transform an image file (e.g. png) into hexagons
  • Render the hexagons on the screen

On http://www.redblobgames.com/grids/hexagons you can find a lot of information about the mathematical properties of hexagons. In fact, I had much help from the information there when writing this library.

Installation

git clone https://github.com/linustornkrantz/hexagons.git

To create a jar that can be used in your project:

./gradlew build

Note that it depends on JavaFX. If you are not using the Oracle JRE, you may have to handle that dependency on your own (e.g. sudo apt-get install openjfx).

Basic usage

When creating your hexagons, make sure that you also add them to a map:

HexagonMap map = new HexagonMap(10);              // The size of the hexagons
map.addHexagon(new Hexagon(3, 3));
map.addHexagon(new Hexagon(3, 4));

To render the hexagons:

map.render(group);                         // Renders into a JavaFX Group 

The Hexagon class extends javafx.scene.shape.Polygon which means that you can change the appearance like this:

for (Hexagon h : map.getAllHexagons()) {
            h.setStrokeWidth(2.0);
            h.setStroke(Color.WHITE);
        }

To be notified when the user clicks on a Hexagon:

map.setOnHexagonClickedCallback(hexagon -> hexagon.setBackgroundColor(Color.BLUE));

Pathfinding

Basic pathfinding:

Hexagon start = map.getHexagon(-21, 74);
Hexagon destination = map.getHexagon(-27, 67);
ArrayList<Hexagon> path = start.getPathTo(destination);

If you need more control, you may want to extend the Hexagon class and implement an IPathInfoSupplier:

ArrayList<Hexagon> path = start.getPathTo(destination, new IPathInfoSupplier() {
    @Override
    public boolean isBlockingPath(Hexagon hexagon) {
        return ((MyHexagon) hexagon).canHeroMoveThroughHere();
    }

    @Override
    public int getMovementCost(Hexagon from, Hexagon to) {
        return ((MyHexagon) hexagon).isThereRoadBetween(from, to) ? 1 : 2;
    }
});

Generate hexagons from an image file

If you have an Image (e.g. png) then you can turn that image into a map of hexagons:

Image image = new Image("path/to/image");
HexagonMap map = new HexagonMap(5, image, 40);

Or if you want more control over the hexagon generation:

Image image = new Image("path/to/image");
HexagonMap map = new HexagonMap(5, image, 40, new IHexagonCreator() {
    @Override
    public void createHexagon(int q, int r, Color imagePixelColor, HexagonMap map) {
        if (imagePixelColor.getBrightness() > 0.7) {
            Hexagon h = new Hexagon(q, r);
            h.setBackgroundColor(Color.GREEN);
            map.addHexagon(h);
        }
    }
});

Full example

Assuming your png looks like this:

alt text

After running the code below you would see this:

alt text

public class Main extends Application {
    @Override
    public void start(Stage primaryStage) throws Exception{
        Image image = new Image("path/to/image");             // Convert the image to hexagons
        HexagonMap map = new HexagonMap(5, image, 40, new IHexagonCreator() {
            @Override
            public void createHexagon(int q, int r, Color imagePixelColor, HexagonMap map) {
                if (imagePixelColor.getBrightness() > 0.7) {
                    Hexagon h = new Hexagon(q, r);
                    h.setBackgroundColor(Color.GREEN);
                    map.addHexagon(h);
                }
            }
        });

        Hexagon start = map.getHexagon(-21, 74);               // Try some pathfinding
        Hexagon destination = map.getHexagon(-27, 67);
        for (Hexagon hexagon : start.getPathTo(destination)) {
            hexagon.setBackgroundColor(Color.RED);
        }

        Group hexagonGroup = new Group();                      // Render on screen
        map.render(hexagonGroup);                              
        Scene hexScene = new Scene(hexagonGroup, 400, 700);
        primaryStage.setScene(hexScene);
        primaryStage.show();
    }
}

hexagons's People

Contributors

linustornkrantz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

barcode36

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.