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

2012/10/3

So I'm trying load a world...

MidichlorianIce MidichlorianIce

2012/10/3

#
OK, so I am making an RPG style game and I am presently in the frameworks. I've got a text document loading enemies in, but at the moment I need some way to figure out how to add some of these enemies(and later other objects) to respective arrays so that they are not all in one "cell" of my world.(World is made by a 2D array that is controlled by horizontal and vertical variables.) Here is the scenario and if you have any questions I can elaborate on what I'm doing.
danpost danpost

2012/10/3

#
If you need each one to be in a specific place, you should save the coordinates of each one in the text file. If you do not mind where they end up, you could randomly place them into the world with
addObject(enemy, Greenfoot.getRandomNumber(getWidth()), Greenfoot.getRandomNumber(getHeight()));
If you want them randomly place AND not intersecting each other, then
addObject(enemy, 0, 0); // adds enemy into world
while (!enemy.entryOK()) enemy.setLocation(Greenfoot.getRandomNumber(getWidth()), Greenfoot.getRandomNumber(getHeight()));
// and add the 'entryOK()' method in the Enemy class
public boolean entryOK()
{
    // check world edges
    if (getX() < getImage().getWidth() / 2) return false;
    if (getY() < getImage().getHeight() / 2) return false;
    if (getX() >= getWorld().getWidth() - getImage().getWidth() / 2) return false;
    if (getY() >= getWorld().getHeight() - getImage().getHeight() / 2) return false;
    // check other objects
    if (!getIntersectingObjects(null).isEmpty()) return false;
    return true;
}
MidichlorianIce MidichlorianIce

2012/10/4

#
This is actually gonna help considerably in the late stages, but I meant I have literal world cells. Each of these segments are representing a different "room" or "land". In the end product it will be much larger with several spaces filled with nothing at all. Hence my use of the text doc as I figured that would be more efficient. My mind tells me that each respective string could be added to a array to be loaded later after the text doc loads this system. I don't know how to do that and still follow the organization of my world however.
    private String[][] fillCells = {{c1, w10, w8, w6, w4, w2, c2},
                                   {w9, w7, w5, w3, w1, w10, w0},
                                   {w2, w4, w6, w8, w9, w7, w2},
                                   {w8, w9, w7, w3, w4, w1, w6},
                                   {w10, w2, w6, w9, w0, w3, w7},
                                   {w4, w8, w3, w10, w1, w6, w2},
                                   {c3, w1, w4, w7, w3, w9, c4}};
These are all null FYI and right side would be top of the world.
You need to login to post a reply.