2

I'm currently trying to implement a scrollable hexmap with LibGDX. The framework has some nice map apis but they seem to be pretty useless for what I want to do :(

An example of the map view that I would want. The pink rectangle represents the area actually visible on the screen while everything around will get cropped

HexMap

This is a basic rundown of what my map needs to be able to do:

  1. The map needs to be an Actor on the screen that can simply be drawn on a stage and be disposable. (Already have that)

  2. The map needs to be scrollable. Meaning clicks and drags need to be registered.

  3. The individual map tiles need to be clickable. When a tile is clicked, I would like to know what tile it was. (Edit: I can currently register clicks on the actor but I would have to manually calculate what tile was actually clicked. Is there a better way for this?)

  4. Then, the map needs to know what tile is being displayed in the top left or right position to determine how many tiles to request from the QuadTree and essentially what to draw on screen.

  5. The map data is stored in a quadtree. In the end there is a method I will have to call with what tiles I want to draw and the Quadtree will return the tiledata to me.

  6. And last but not least: Tiles that reach outside the map boundries should be cropped (less priority)

Now. This is what I currently have:

http://pastebin.com/aqjSNPy3 (THe class is 200 lines long, not gonna spam this in here :) )

I can already draw the map actor on screen. It looks ugly as hell but what the heck. Not important now. I think the most important thing for now would be to register clicks on tiles, figure out how many tiles to display/ request and maybe do something with the camera to make it scrollable?

I'm really kinda out of answers here. Help will be highly appreciated. Thanks alot!

Daahrien
  • 10,190
  • 6
  • 39
  • 71
AreusAstarte
  • 1,958
  • 2
  • 17
  • 29

1 Answers1

0

Okay i am trying to help. I currently work with a normal OrthogonalTiledMapRenderer and i am sure you could get some good points at the Render class. For exmaple how you check if a tile is visible. Just take a look at the repository of libgdx.

At first. The dispose is never called in any way. You can call it yourself if you really dont need your Screen anymore. You dispose your assets in it if you use it.

So how to get a click detection. You can add Listener to every Actor for example like this:

actor.addListener(new InputListener() {
    public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
            System.out.println("down");
            return true;
    }

    public void touchUp (InputEvent event, float x, float y, int pointer, int button) {
            System.out.println("up");
    }
})

You can add more Listener like this. For example the ActorGestureListener() to detact pinches and so on.
Make sure that you use the cam.unprojectright so the touchevents are fired correct.

resource: libgdx/wiki/scene2d#Inputlistener

Your current method hit has an other functionality:

Hit detection The Actor hit method receives a point and returns the deepest actor at that point, or null if no actor was hit. Here is the default hit method:

public Actor hit (float x, float y, boolean touchable) {
        if (touchable && getTouchable() != Touchable.enabled) return null;
        return x >= 0 && x < width && y >= 0 && y < height ? this : null; }

The coordinates are given in the actor's coordinate system. This simply returns the actor if the point is inside the actor's bounds. More sophisticated checks could be used, eg if the actor was round. The touchable boolean parameter indicates if the actor's touchability should be respected. This enables hit detection for purposes other than touch on actors that are not touchable.

When hit is called on the stage, hit is called on the stage's root group, which in turn calls hit on each child. The first non-null actor found is returned as the actor deepest in the hierarchy that contains the given point.

Hit detaction You do not need to override this. Its for a stage togive a touch to the Actors if i get that right.


So how do get it scrollAble? You need to detect slide events(as shown above). If there is such event you need to move your camera around or you move your stage/tilemap (by the value of the movement maybe even while the slide is detected). Both has the same effect. At a Pinch you could change the zoom of the camera by the pinch value for example.
I do move my camera in every render cycle so always centers my Character:

this.gameCam.position.x = this.character.getX()
                + this.character.sprite.getWidth() / 2;
        this.gameCam.position.y = this.character.getY()
                + this.character.sprite.getHeight() / 2;



Also take a closer look to this repository! It's a working Hexmap from a game:
Hexmap
refares to this game: Mathematiles and found at Libgdx Post

I hope this helps a bit! Maybe start with an regulare Tilemap System befor you start with hexagon. It seems to be a bit harder. But there are alot of tutorials that explain the theorie and how to start creating such system. Even for libgdx.

Generally i would say you should wait with that project till they give us the Hextile API. They are working on it but it does take some time i am sure.

Regards

bemeyer
  • 6,154
  • 4
  • 36
  • 86