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

2013/11/17

Levelgenerator

Kartoffelbrot Kartoffelbrot

2013/11/17

#
I want to make a game with many levels, but I don't have enough time and I am to lazy to build them myself. So I want to generate them randomly. But the levels shouldn't be randomly every start of the game. I want to generate them on my computer, and saving them as the levels, so that they are always the same. But how can I save a world object, that I've generated with my code forever?
You can save it in a text file, if using it on client. If you're doing a simple grid sort of world, just take the array, print it out into a file. Then when you need it, you read it out of the file. I haven't done file writing/reading in a while, so I forgot the basics, but search up online and you should find some tutorials. If you have these files in your project (I recommend in a folder called "levels"), they will be exported to the site as well. Actually, you need them in your project folder for you to read them anyways...
If you aren't making a grid world, you can go through all the actors, list out what actor they are, and then put their coordinates into the text file, instead of an array.
danpost danpost

2013/11/17

#
You could try using my MapWorld SuperClass scenario.
Kartoffelbrot Kartoffelbrot

2013/11/17

#
@ FlyingRabidUnicornPig: I didn't know, that I can save objects in a txt file. Sounds interesting. I saved Strings a month ago in txt files with C#. What is a GridWorld? @ danpost: This looks really nice, but it could become a bit more complicat because my cellSize is 1 (ok, that shouldn't be a problem) and my objects have all different rotations. What if my Actors save different Actors? Can my World handle all that in a txt or do I have to make methods to convert them into useful Strings and otherwise around?
A grid world was just one where you use a 2D array to place objects in the world, a grid. I would make a Class with static methods that read a text file, and returns the actors and their locations and stuff. Have the world(s) use this class to load each "world" saved in the text file. I would call the class something like WorldLoader, and then would have methods like these:
public static Actor[] loadActors(String fileName); //Use getClass().getName() to find what class they are.

public static int[][] loadCoordinates(String fileName);

public static int[] loadRotations(String fileName);

public static Object[][] loadEverything (String fileName); //How this would work is that you have an array for each actor you load. The first section of that array would be the actor, the second section would be the x-coordinate, 3rd would by y-coordinate, and the 4th section would be rotation.

public static void save(String fileName, Actor[] acts, int[][] coords, int[] rots); //If making a method like this, make sure that each array is set up correctly with the correct actors

//or

public static void save(String fileName, World wrld);

//or

public static void save(String fileName, Object[][];//In the same format as loadEverything()
Hope this helps!
Kartoffelbrot Kartoffelbrot

2013/11/17

#
Yes this helps. These are very nice ideas! Thank you. Is getClass() a method of Object? What does getName() return? And is it possible to write into java files like into txts? SO it would be possible to generate code into a level class. But I think I should try yours first. But very first I should finish the game and the generator :)
getClass() returns the Class of that object (pretty sure it's not an object itself) and getName() returns the name of that object. I don't think you can put java files into text files, but they really shouldn't be used like that anyways.
danpost danpost

2013/11/17

#
You can still use my scenario to map out your worlds; even it your worlds are pixel cell sized. You just need move the output to your world and build the world multiplying the locations by a unit of separation.
You need to login to post a reply.