Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (536)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  [LibGDX] Storing Leveldata in 2D Arrays  (Read 972 times)
0 Members and 2 Guests are viewing this topic.
Offline sirkarpfen

Junior Member


Medals: 2
Exp: 2 years



« Posted 2013-07-29 16:29:51 »

I am pretty new to LibGDX and game developing in general and i am currently writing my first game (a breakout clone) with LibGDX.

I have tried to store the Leveldata (positions of Blocks) in a 2-dimensional array like this:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
// 1 = block, 0 = empty
levelArray = new int[][] {
      {0,0,0,0,0,0,0,0,0,0,0,0},
      {0,0,0,0,0,0,0,0,0,0,0,0},
      {0,0,0,0,0,0,0,0,0,0,0,0},
      {0,0,0,0,0,0,0,0,0,0,0,0},
      {0,0,0,0,0,0,0,0,0,0,0,0},
      {0,0,0,0,0,0,0,0,0,0,0,0},
      {0,0,0,0,0,0,0,0,0,0,0,0},
      {0,0,0,0,0,0,0,0,0,0,0,0},
      {0,0,0,0,0,0,0,0,0,0,0,0},
      {0,0,0,0,0,0,0,0,0,0,0,0},
      {0,0,0,0,0,0,0,0,0,0,0,0},
      {0,0,0,0,0,0,0,0,0,0,0,0},
      {0,0,0,0,0,0,0,0,0,0,0,0},
      {0,0,0,0,0,0,0,0,0,0,0,0},
      {0,0,0,0,0,0,0,0,0,0,0,0},
      {0,0,0,0,0,0,0,0,0,0,0,0},
      {0,0,0,0,0,0,0,0,0,0,0,0},
      {1,1,1,1,1,1,1,1,1,1,1,1},
      {0,0,0,0,0,0,0,0,0,0,0,0},
      {0,0,0,0,0,0,0,0,0,0,0,0}
};


Currently there are only normal blocks, that can be destroyed by the ball. In the future i will implement unbreakable blocks and blocks that drop upgrades like more balls or balls that break through blocks.

When a new game (or level) is created i iterate over the Array and create the blocks like this:

1  
2  
3  
4  
5  
6  
7  
8  
brickStorage = BrickStorage.getInstance();
for(int y = 0; y < levelArray.length; y++) {
   for(int x = 1; x < levelArray[y].length; x++) {
      if(levelArray[y][x] == 1)
         brickStorage.add(
            new Brick((x*BRICK_WIDTH+MAP_EDGE)/PIXEL_TO_METER,(y*BRICK_HEIGHT-MAP_EDGE)/PIXEL_TO_METER));
   }
}


My question now is, if i want to create a new level i have to write another array. Is there a better way to store Block positions and types when loading a new level?

If further code is needed i will of course edit it here Smiley
Offline SHC
« Reply #1 - Posted 2013-07-29 16:52:53 »

I use classes to denote levels. For example,

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
public class Map01 extends MapInfo
{
    public Map01()
    {
        MapData = "BBBBBBBBBBBBBBBBB\n" +
                  "B               B\n" +
                  "B  S  S   S  S  B\n" +
                  "B S S S S S S S B\n" +
                  "B S S S S S S S B\n" +
                  "B S S S S S S S B\n" +
                  "B  S  S   S  S  B\n" +
                  "B               B\n" +
                  "B       o       B\n" +
                  "B        b      B\n" +
                  "BBBBBBBBBBBBBBBBB";
        MapLoader = Game.MapLoader;
    }
}

And MapLoader has method called
getObject()
in which I hardcode the objects.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
public class MapLoader extends BasicMapLoader
{
    public GObject getObject(char id, int tileX, int tileY)
    {
        switch (id)
        {
            case 'B':  return new Block(tileX, tileY); break;
            case ' ':  return null; break;
            case 'S':  return new Stone(tileX, tileY); break;
            case 'o':  return new Ball(tileX, tileY); break;
            case 'b':  return new Bat(tileX, tileY); break;
        }
    }
}

And here is my
loadMap()
method.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
public void loadMap(MapInfo map)
{
    int tileX = 0;  int tileY = 0;
    int tileSize = 32;   // Fixed for my game

    currentMap.destroyAllObjects();

    for (char ch : map.MapData.toCharArray())
    {
        if (ch == '\n')
        {
            tileY += tileSize;
            tileX = 0;
            continue;
        }

        GObject obj = map.MapLoader.getObject(ch, tileX, tileY);

        if (obj != null)
            currentMap.addObject(obj);

        tileX += tileSize;
    }
}

This works and is easier for me to change maps. You can guess what the other objects has.

Offline sirkarpfen

Junior Member


Medals: 2
Exp: 2 years



« Reply #2 - Posted 2013-07-29 17:05:24 »

Thank you for pointing my to a better direction. I will come up with my own solution to this and then post it here as a refernce for other ppl Smiley.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Longarmx
« Reply #3 - Posted 2013-07-29 18:03:04 »

I would use files to store the level data. You could then load a certain file based on the level you want.

Offline SHC
« Reply #4 - Posted 2013-07-29 18:13:02 »

@Longarmx

Using files is easier too. But I prefer classes because I don't want others to edit my levels.

Offline Longarmx
« Reply #5 - Posted 2013-07-29 18:15:34 »

@Longarmx

Using files is easier too. But I prefer classes because I don't want others to edit my levels.

If people really wanted to, they could edit your classes  Wink

Offline SHC
« Reply #6 - Posted 2013-07-29 18:19:40 »

@Longarmx

That's a possibility too. But it may be a bit enough from getting edited by common players. I can still obfuscate my code using progaurd.

Offline sirkarpfen

Junior Member


Medals: 2
Exp: 2 years



« Reply #7 - Posted 2013-07-30 20:25:29 »

I now have my own solution (well pretty much as it has been very inspired by SHCs solution^^)

I create a class for each Level:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
public class Level1 extends BasicLevel {
   public Level1() {
      levelData = "MMMMMMMMMMMMMM\n" +
               "MMMMMMMMMMMMMM\n" +
               "EEEEEEEEEEEEEE\n" +
               "E            E\n" +
               "E BBBBBBBBBB E\n" +
               "E BBBBBBBBBB E\n" +
               "E BBBBBBBBBB E\n" +
               "E BBBBBBBBBB E\n" +
               "E            E\n" +
               "EEEEEEEEEEEEEE\n";
   }
}


After trying it with 2D-Arrays i figured SHCs solution is much easier to handle so i use chars to figure out where to put everything...

Every Level is initialised when the game is started in a Storage and there put into an ArrayList. That way i can get every level by just calling:

1  
levelStorage.getLevel(1) //returns level 1


The current level is also saved as an int value in the Storage class, so that i can change to the next level by just adding 1 Smiley.
Offline SHC
« Reply #8 - Posted 2013-07-31 03:55:59 »

@sirkarpfen

You can also make a MapManager. You can see mine here MapInfo.java. You can even extend it to make layered maps. Make sure you read that package's every class.

Offline sirkarpfen

Junior Member


Medals: 2
Exp: 2 years



« Reply #9 - Posted 2013-07-31 10:43:34 »

Well for my purpose the storage is enough. If i will sometimes create more complex maps i will think about actually writing a MapManager Smiley.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

CogWheelz (9 views)
2014-07-30 21:08:39

Riven (20 views)
2014-07-29 18:09:19

Riven (14 views)
2014-07-29 18:08:52

Dwinin (12 views)
2014-07-29 10:59:34

E.R. Fleming (32 views)
2014-07-29 03:07:13

E.R. Fleming (12 views)
2014-07-29 03:06:25

pw (42 views)
2014-07-24 01:59:36

Riven (42 views)
2014-07-23 21:16:32

Riven (29 views)
2014-07-23 21:07:15

Riven (30 views)
2014-07-23 20:56:16
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!