Auto Generated Dungeon with libgdx

Rougelike is a interesting game genre, the most important thing in Roguelike game maybe is the procedural content generation(PCG), which provide player tons of game content. From  triple A game Diablo series to the game seldom known to mass Dwarf Fortress, one similar point is they all use PCG.

My target is make a Roguelike game on Android with libgdx. This game use tile map to present game content. Luckily, the powerful libgdx support tile map. I only have to do two things on dungeon generation, generate dungeon map then use tile map present it.

1.Generate dungeon map data.

There’re a lot of resource about dungeon generation, I recommend below ones.

Random Dungeon Generator It explain the algorithm and provide simple source code(Perl) and online demo.

Unangband Dungeon Generation It has 9 parts and source code can get from Unangband.

2.Tile map

The current libgdx 0.92 only support read map data from TMX file, new version of TiledLoader can read map data from String. Because we generate the map data and don’t have TMX file, so we should build one.


TiledMap map;

public void writeTMX(String pFileName){

StringWriter writer = new StringWriter();

XmlWriter xml = new XmlWriter(writer);

//in libgdx 0.92, TileLoader only can read tmx file from FileHander, future version will support tmx string

//this implement is NOT use on Android version, just for test

FileHandle tmxFile = new FileHandle(pFileName);

try {

//map's start

xml.element("map")

.attribute("orientation", "orthogonal")

.attribute("version", "1.0")

.attribute("width", tileIds[0][0].length)

.attribute("height", tileIds[0].length)

.attribute("tilewidth", tilewide)

.attribute("tileheight", tilehigh);

//tileset

xml.element("tileset")

.attribute("name", tilesetName)

.attribute("firstgid", tilesetFirstId)

.attribute("tilewidth", tilesetTilewide)

.attribute("tileheight", tilesetTilehigh)

.element("image")

.attribute("source", tilesetSource)

.pop()

.pop();

//layer

for(int layer = 0;layer < tileIds.length;layer++){

String csv = "";

for(int row = 0;row < tileIds[layer].length;row++){

csv += toCSV(tileIds[layer][row]) + "\n\t\t\t";

}

xml.element("layer")

.attribute("name", "layer"+(layer+1))

.attribute("width", tileIds[0][0].length)

.attribute("height", tileIds[0].length)

.element("data")

.attribute("encoding", "csv")

.text(csv)

.pop()

.pop();

}

//map's end

xml.pop();

//write to a tmp file

Writer fileWriter = tmxFile.writer(false);

fileWriter.write(writer.toString());

fileWriter.close();

} catch(IOException e){

throw new RuntimeException(e);

}

map = TiledLoader.createMap(tmxFile);

}

Then use TileMapRenderer draw the tile map.

The full code of this sample you can find on Github. As I said it’s just a sample, to be a real tile dungeon generator, it still have lots of work to do like different room type, monsters and other game objects.

Resist the Temptation of Crossplatform

Porting game to different platform is boring compared with developing new game, that’s why there are so many technicals to solve this problem. Developed once, deploy everywhere. It’s really awesome for everyone want their software run on different platforms.

iPhone and Android are two big tribes in mobile industry and they are so different, obj-c and java, that make crossplatform developing very difficult. Right now the best way is Unity, but I have to say it really a bit expensive for small budget developer(like me :-) . Recently the cocos2d-x attract my attention a lot, it seems like very promising and there’re already many games released with it. But after careful consideration, I resist the temptation. Below are my reasons.

1.It’s not mature enough, for Android, there some phones they don’t support. Maybe it’s best tool for porting iPhone game developed with cocos2d-iPhone to Android, but I don’t have strong sense of security on Android.

2.Learn new technology cost a lot. cocos2d-x use c++ and the API is like cocos2d family, I know c++ but the API is stranger to me.

3.Debug is difficult on Android.

4.Native technology is the best friend of platform, always.

5.The most important things in making game is making a good game. If the game is not a game on a platform, so it’s not neither on the other. Technical complexity would attract more attention than we should paid, means less attention to the game itself.

So, libgdx is still my best friend on Android :-)

Before Evil Come Postmortem

Before Evil Come

Well, I really made it. This is my first Ludum Dare, and actually my second playable game.

Before this competition start, I’m  so exciting and also doubt whether I can finish it. I made a strategy for myself, no collision detection, no physic and no animation. I know they’re important for a game, but I can’t count them in before I really conquer them.

Game Overview

This is an evil game like the title said. You’re a demon, came to our world, because good man protect us you have nothing to do without some buddies. So you go to the wild and hope get some buddies join you. Although it’s wild, still some enemy out there. You can have rest in house and graveyard, but be careful your enemy also rest in some house. After defeat your enemy, you gain exp and may level up. The more buddies and hight level you have, you can kill more people. If you don’t have any buddies, you die.

Developing Tools

I used below great tools.

Libgdx: an open source Java game engine, also supports Android. It’s really good and best game engine on Android in my mind. The texture packer and bitmap font generator tools are very helpful for me in this competition.

Gimp and Inkscape: all graphic assets are made by this two software.  I think Inkscape is a good tool to draw cartoon style graphic.

Bfxr, LMMS and Audacity: The BGM was made by LMMS and other sounds were from Bfxr. Audacity handle the polish work.

Developing Process

Ok, enough background, I want talk about how I made this game. When I knew the theme It’s Dangerous to Go Alone, Take This., the first idea was ant. Tiny ants group a great army to defeat strong army. Based this idea, I split gameplay to two steps, first summon army and second defeat big boss. The bigger army got in step 1, the easier in step 2.

Because I like dead creatures so I replace ants with zombie and skeleton. I’m not expert of complicated control game, so one click control is my choice. Top view, tile map and my cute dead creature, first stage is done.

When I start made second stage, I hesitated. It’s actually a totally different game, did I have enough time to finish it? I give it up at last, instead I made a slot machine mini game, whose digit number is lead by gain points in first stage.  (OK, actually I am just lazy ;-)

After add story board of game background, this game finally done. It’s mini and some kind shabby, but I really love it. So do my girlfriend, though she just like click things and don’t care game content.

Conclution

Cons:

1.Not enough feedback when player made decision.

2.Inside game mechanic is too simple. OK, maybe it’s some kind pro :-) .

3.Not enough introduction, player would be confused :-(

Pros :

1.It’s really very easy.

2.My cartoon characters are cute, isn’t it, especially when player lose. I love the skeleton on the cross.

Hope you like this game and my poor English not disturb you. :-)

 

[Solved]Can’t resume from Sleep on Nexus S with Libgdx

When I test my game on my Nexus S, strange things happened. I pressed power button in my game, the phone fall asleep and my game paused. After touched the power button again, the screen is black and nothing showed up.

After check the forum of Libgdx, I found the method to solve this problem.

by mzechner » Mon Feb 21, 2011 8:05 pm

Ay. Could you do me a favor and add

<activity android:configChanges=”orientation|keyboard|keyboardHidden” … >

to your activity tag?

I’ll try to reproduce this locally with the phones i got.

After add the android:configChanges parameter in my AndroidManifest.xml, everything is OK.

ClassCastException when use TexturePacker of Libgdx

The weekly report of Android Game Untitled is paused, the game is still in  progress.

TexturePacker is useful extension class of Libgdx, it packed all images in one folder to one image with power of 2 width and height and create a pack file record the coordinate info of each sub image. With TextureAtlas you can  load subimage from the assembled one easily. Why we use this not directly use several images. That’s because

1.The width and height of Texture should be pow of 2, that’s not convenient when you want use other width and length.

2.Loading several images is slower than loading one.

When I use the TexturePacker I get an exception

Exception in thread “main” java.lang.ClassCastException: [B cannot be cast to [S
at sun.awt.image.ShortInterleavedRaster.getDataElements(ShortInterleavedRaster.java:245)
at com.matrix.libgdx.util.TexturePacker.squeeze(TexturePacker.java:362)
at com.matrix.libgdx.util.TexturePacker.addImage(TexturePacker.java:98)
at com.matrix.libgdx.util.TexturePacker.<init>(TexturePacker.java:78)
at com.matrix.libgdx.util.TexturePacker.process(TexturePacker.java:748)
at com.matrix.libgdx.util.TexturePacker.process(TexturePacker.java:837)
at com.matrix.libgdx.util.MyPacker.main(TexPacker.java:12)

Because I don’t find any others who confronted the same problem, I think it’s maybe my fault. After digging a while, using debug I found the reason, Every time when squeeze a special file the source.getAlphaRaster() return a ShortInterleavedRaster and that lead to the exception.

I have no idea of this png file, it’s same as others to me. And the only difference between them maybe the special one made by Photoshop and others by GIMP. It seems like the alpha raster of image made by Photoshop is short not byte. It’s just my guess, not sure about that. After removed it, everything is OK. :lol:

Android Game Untitled Week Six

Alpha 1 released, hurrah!

The whole game is nasty right now, I should add real graphic stuff, animation, sound, music, better UI and more funny gameplay. Although there is a lot of things to do and at the same time I learned a lot of things.

The art style in this game is cartoon like, I found a great vector design tool Inkscape. It’s easy to use and have a lot of tutorial, and it’s free and open source, great tool for indie game group with limited budget like us.

In this game, I want some sprites always on the top. However, libgdx can not create multi-layer or set the z-order directly with the Stage class. Update: Different from AndEngine, the multi-layer is implement with groups in libgdx(thx the heroes  behind libgdx, I’ll think twice before post :-) ). All actor in stage drew in the order of adding in stage. Later added actor on the top of before ones. There’s a easier way to implement multi-layer in stage. Create some Groups, each group behalf of one layer, the later added group has bigger z-order, the last group is always on the top. Then just add actors to the  group  instead of adding them to the stage directly. The basic multi-layer function has done.

In this week, I’ll concentrate on the graphic and gameplay.

Android Game Untitled Week Three

This weekly report is a little late, my weekends are busier than weekdays, can you belivie that?

Last week I refactored many classes, most of them are related to Scene and Role, now I have factory class to build them. There’s some sprite in my game can change their size, not scale themselves but looks like openging a scroll. I implement this feature with changing the region size I applied to the texture. Although it doesn’t support circle, it’s good for me right now.

I also add real contact handler and a boundary. I found a JVM crash after colliding many times, I’m not sure whether my fault or something wrong else where. I will put it to debug period to handle.

Android Game Untitled Week Two

Well, another week passed, how fast the time is. The LibGdx engine has another awesome feature I forgot on last post, and this is most important feature besides  the speed. LibGdx allows you programs on desktop and debug it, and later run it on Android only need change several lines of code. That’s really speed up my development.

This week I add sprite animation function which LibGdx not support it. And some physic manager class to associate Box2D body with my sprite, and because Box2D not support radial gravity so I made my own with apply force to body.

Beside the programming stuff, Karen is watching the Painter tutorial video, beginning pain with her. I made a draft of  the game play and some basic idea of the background music. A lot of stuff need to do, fortunately I enjoy it.

Android Game Untitled Week One

As my plan for my first Android game is first three month in  2011, the past week surely be my first week on it. I’ll write weekly report of the development.

For the game engine, I have three candidate Rokon, Andengine and LibGdx. Rokon is suck of course, I even can’t run the example Hello World. That’s not my fault, at first I thought I messed it up and after check the Google I found a lot of guys confront the same problem, so Rokon is out. and I glad to see this project is canceled, that’s good for the world. Am I so mean?:-)

I developed with Andengine three weeks for a prototype, and I have to say it’s a good game engine. The reason why I transfer from it to LibGdx only because the later one is better for me. The guys in Badlogic Games is so hard working, I like the updating speed of their blog and the code trunk and I got a lot of useful knowledge from them. The running speed on device is another reason I choose LibGdx. Actually the profiling post let me know this awesome engine, and my own profiling test finally let me make the decision.

In the past week, I wrote a XML parser with SAX and implemented some visual related class based on Stage and Actor. Right now, I can load a XML file and create the game scene. Not too much work I have done in this week, I should hard working.

The logo of our studio is done, but I think it need polish. I will show it at the right time.