This site requires JavaScript, please enable it in your browser!
Greenfoot back
askgriff
askgriff wrote ...

2013/1/18

How would you create a world based on text strings?

askgriff askgriff

2013/1/18

#
Okay, so I want to build a world that is easily changeable based on a text "grid". For example, let's say that: 00 = Water 01 = Sand 02 = Rock 03 = Dirt 04 = Grass 05 = Lava etc. My data could look something like: Row 00 = 01, 01, 01, 01, 02, 02, 00, 00, 00, 01 Row 01 = 01, 01, 02, 02, 02, 02, 02, 00, 00, 00 Row 03 = 01, 02, 02, 02, 02, 02, 00, 00, 00, 00 That would build a world where if a coordinate was 01 it would display a 16x16 block (for example) that looks like sand -- and has the properties associated with sand (i.e. you can't plant some things in it, it's easier to dig, etc.). And now that I think of it, how would you change the block (i.e. move from a dirt to a wheat block if it's planted... or from a grass to a burnt grass block if it has been on fire). I just feel like a system like this would make map-making/level-editing a quicker, easier process -- and perhaps even doable by those without programming experience. In fact... maybe even making the grid an external text file: 01, 01, 01, 01, 02, 02, 00, 00, 00, 01 01, 01, 02, 02, 02, 02, 02, 00, 00, 00 01, 02, 02, 02, 02, 02, 00, 00, 00, 00 Any thoughts or ideas? Is this already done somewhere? Thanks! Robert Griffith
danpost danpost

2013/1/18

#
For those without programming experience, creating external text files would not be a problem; however producing the code to read the text file data into the program would be something for the more experienced. Anyway, after creating the text file, the data could be copy/pasted right into the code. Your data above could look something like this:
int[][][] levelMap={ { { 1, 1, 1, 1, 2, 2, 0, 0, 0, 1 },
                       { 1, 1, 2, 2, 2, 2, 2, 0, 0, 0 },
                       { 1, 2, 2, 2, 2, 2, 0, 0, 0, 0 } },
                     { { // next level...etc
                         // last line of data         } } };
danpost danpost

2013/1/18

#
Another way would be to assign values to variables with the objects name and use them in the array:
public static final int WTR=0, SND=1, RCK=2, DRT=3, GRS=4, LVA=5;

int[][][] levelMap={ { { SND, SND, SND, SND, RCK, RCK, WTR, WTR, WTR, SND },
    { SND, SND, RCK, RCK, RCK, RCK, RCK, WTR, WTR, WTR },
    { SND, RCK, RCK, RCK, RCK, RCK, WTR, WTR, WTR, WTR } },
  { { // next level....etc
      // last line of data                             } } };
danpost danpost

2013/1/18

#
Your concern about changing Grass to BurntGrass and Dirt to Wheat should just be a matter of adding a new Actor and removing the old one. If you want to keep the array up to date then you would also add a line something like this:
// in the Grass class code, when planted
levelMap[level][getY()][getX()]=WHT; // for Wheat
// followed by
getWorld().addObject(new Wheat(), getX(), getY());
getWorld().removeObject(this);
actinium actinium

2013/1/18

#
danpost danpost

2013/1/19

#
I was already working on the scenario MapWorld SuperClass when you first started this thread; however, I did not want to mention it until it was up. I believe it is what you were suggesting. I did modify it so the actors would be how you mentioned them above.
askgriff askgriff

2013/1/21

#
Wow... thank you both for your help. I am to use a system like this to create a scrolling top-down "Zelda-like" game which can be edited easily. This should help with my understanding.
You need to login to post a reply.