Jump to content
  • Advertisement
Sign in to follow this  
TheFruityKing

How to programmatically create Tile Maps

This topic is 948 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Unfortunately, I don't have access to a computer, or buy one, so I can't use tools like Tiled to create maps. I'm using Libgdx, so normal java or libgdx answers will be accepted. And don't provide links to websites such as wikis, they usually don't help, as they don't use layman terms(I learn better without overly complicated explaining). Plus, I think learning such things will be very valuable. So, answer away.

Share this post


Link to post
Share on other sites
Advertisement

So you dont have a computer, but you're coding in Java... how exactly? In paper?


I'm on Android, using AIDE(Android Integrated Development Environment). It's a pretty well made IDE, on Android. Check it if you want to. Developing an app or game is completely possible. Now then, do you have some sort of answer, or no?

Share this post


Link to post
Share on other sites

Look at the source code for Pixel Dungeon. It is a popular open-source game for android written in Java that randomly generates tile maps.

All you have to do is implement your own RNG (I recommend some variant of xorshift for this) and save the initial RNG state for maps you like. You can then package the initial RNG state instead of a finished map with the game to save precious storage space if you want.

Edited by nfries88

Share this post


Link to post
Share on other sites

What you're looking for is called procedural generation, and is a fairly rich field. Too rich to be summarized appropriately in a thread, really.

 

If you can narrow down the problem then you're a lot more likely to get a useful discussion.

Share this post


Link to post
Share on other sites

I'm not sure why my last response was downvoted. It wasn't a thorough explanation, but it pointed the OP towards readily available code that could be used to learn what's needed.

Basically what Pixel Dungeon (and most roguelikes) do is first randomly generate rooms (as large, rectangular spaces), then connect all of them with hallways, then place stairs randomly in some room (some variants start with the stairs). This works very well for mostly cramped spaces like dungeons, castles, mines, caverns, etc; but doesn't work quite so well for overworld maps or for generating whole game worlds.

I can't really help further without knowing what specific needs you have, because I'd wind up (pretty much) literally writing you a book.

What I can tell you is that procedural generation techniques don't change by what graphics package you use, so it's up to you to know how to translate the general idea into actual code (I don't program in Java and have no familiarity with libgdx, but I do know a fair bit about procedural generation of tile-based maps).

Share this post


Link to post
Share on other sites

If I read correctly you want to know how to create a basic tilemap. Im not a Java programmer so I will try and translate how I would do it best, you will have to be able to at least understand the concepts from the examples which should be pretty straight forward. I could write this in C++, PHP, Javascript if anyone of those helps you better but for now ill give my best Java shot.

 

Well you start with a 2D array of values i.e. 

// Creates a 2x2 tilemap array, where the value represents the index of a texture in an array of textures
int[][] map = { {"grass", "grass"}, {"grass", "dirt"} };

Example of a texture array psuedo-java

// Add 2 textures an array
Texture[] textures = new String[];

textures["grass"] = new Texture("path/to/grass_file.jpg");
textures["dirt"] = new Texture("path/to/dirt_file.jpg");

Now to  build the tilemap

for (int y = 0; y < 2; y++) { 
    for (int x = 0; x < 2; x++) {
        String textureIndex = map[y][x];
        Texture temp = textures[textureIndex];
        temp.setPosition(x * (temp.getSize().x), y * (temp.getSize().y);
        // Draw the temp texture to the screen
    }
}

Share this post


Link to post
Share on other sites

I'm not sure why my last response was downvoted. It wasn't a thorough explanation, but it pointed the OP towards readily available code that could be used to learn what's needed.
Basically what Pixel Dungeon (and most roguelikes) do is first randomly generate rooms (as large, rectangular spaces), then connect all of them with hallways, then place stairs randomly in some room (some variants start with the stairs). This works very well for mostly cramped spaces like dungeons, castles, mines, caverns, etc; but doesn't work quite so well for overworld maps or for generating whole game worlds.
I can't really help further without knowing what specific needs you have, because I'd wind up (pretty much) literally writing you a book.
What I can tell you is that procedural generation techniques don't change by what graphics package you use, so it's up to you to know how to translate the general idea into actual code (I don't program in Java and have no familiarity with libgdx, but I do know a fair bit about procedural generation of tile-based maps).

Sorry for the very late reply. I understand the technique used for Pixel Dungeon, and I've already have the source code. Overworlds/whole game worlds were exactly what I was looking for unfortunately. I'll take a look at procedural generation nonetheless, doesn't hurt to try. The type of game I'm thinking of making is a top-down RTS, and I intend to use tile maps in that particular case.

If I read correctly you want to know how to create a basic tilemap. Im not a Java programmer so I will try and translate how I would do it best, you will have to be able to at least understand the concepts from the examples which should be pretty straight forward. I could write this in C++, PHP, Javascript if anyone of those helps you better but for now ill give my best Java shot.

Well you start with a 2D array of values i.e.

// Creates a 2x2 tilemap array, where the value represents the index of a texture in an array of texturesint[][] map = { {"grass", "grass"}, {"grass", "dirt"} };
Example of a texture array psuedo-java
// Add 2 textures an arrayTexture[] textures = new String[];textures["grass"] = new Texture("path/to/grass_file.jpg");textures["dirt"] = new Texture("path/to/dirt_file.jpg");
Now to build the tilemap
for (int y = 0; y < 2; y++) {     for (int x = 0; x < 2; x++) {        String textureIndex = map[y][x];        Texture temp = textures[textureIndex];        temp.setPosition(x * (temp.getSize().x), y * (temp.getSize().y);        // Draw the temp texture to the screen    }}
Thanks. I will try it out.

Share this post


Link to post
Share on other sites

The type of game I'm thinking of making is a top-down RTS, and I intend to use tile maps in that particular case.

It's not too uncommon for these to use procedurally generated maps. Often these games are "2.5D" or fixed perspective 3D. In the latter case, they typically generate a mesh from a tilemap where each tile is basically just a terrain type and a height map. Usually they use diamond tiles, but for the case of generation, you can generally look at it as normal axis-aligned orthogonal map.

Since you said "top down", I'm guessing you're doing 2D or 2.5D, rather than a fixed perspective 3D. In this case you'll have a flat map. Am I wrong?

  Edited by nfries88

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!