Env Game Reset issues

3 replies [Last post]
Offline
Joined: 01/29/2011

 I'm working on Part II - Lesson 4 and was fooling around trying to implement a way to let people reset the predator and prey stimulation. I am currently having some issues thou. When I start the stimulation and reset it, it seems to exit env automatically during the introduction of the stimulation...

 

Here is the game class's code:

 

import env3d.Env;

import java.util.ArrayList;

 

public class Game

{

    private ArrayList<Creature> creatures, new_creatures, dead_creatures;

    private Env env;

    private boolean finished;

    private boolean reset;

    private double playerSetFoxSpawnRate, playerSetTuxSpawnRate;

    private int playerSetTuxOverCrowdedRate;

 

    /**

     * Constructor for the Game class. It sets up the foxes, tuxes and hunter.

     */

    public Game()

    {

        finished = false;

        //         reset = false;

 

        new_creatures = new ArrayList<Creature>();

        dead_creatures = new ArrayList<Creature>();

        creatures = new ArrayList<Creature>();

 

        playerSetFoxSpawnRate = Fox.getSpawnRate(-1);

        playerSetTuxSpawnRate = Tux.getSpawnRate(-1);

        playerSetTuxOverCrowdedRate = Tux.getOverCrowdedRate(-1);

    }

 

    /**

     * Play the game

     */

    public void play()

    {

        gameSetUp();

 

        gameReset();

 

        gameLoop();

    }

 

    /**

     * Set up the game.

     * Creates the env environment.

     */

    private void gameSetUp()

    {

        env = new Env();

        env.setRoom(new Room());

    }

 

    /**

     * Reset or initilize the game content

     */

    private void gameReset()

    {

        // Reset variables, clear list, clean display

        finished = false;

        reset = false;

        creatures.clear();

        new_creatures.clear();

        dead_creatures.clear();

        env.setDisplayStr(null);

 

        // Set the camera for introduction

        env.setCameraXYZ(0, 0, 0);

        env.setCameraPitch(90);

        env.setDefaultControl(false);

 

        // Instructions

        instruction();

 

        // Clean the screen

        env.setDisplayStr(null);

 

        // Set the camera for gaming

        env.setCameraXYZ(25, 50, 55);

        env.setCameraPitch(-63);

        env.setDefaultControl(false);

 

        for (int i = 0; i < 55; i++) {

         // Add creatures into array list

         //... omitted...

 

 

        }

 

        // Add creatures into the env environment for display

        for (Creature c: creatures) {

            env.addObject(c);

        }

    }

 

    /**

     * Show the instrutions.

     */

    private void instruction()

    {

          // ... omitted...

 

        selectionProgress();

    }

 

    /**

     * Selection process for player

     */

    private void selectionProgress()

    {

            //... omitted...

    }

 

    /**

     * Game loop

     */

    private void gameLoop()

    {

        displayKey();

        //         while (!finished && !reset)

        while (!finished) {

            if (env.getKey() == 1) finished = true; // Key Esc

            if (env.getKey() == 19) reset = true; // Key R

 

            for (Creature c : creatures) {

                c.move(creatures, dead_creatures, new_creatures);

            }

 

            for (Creature c: new_creatures) {

                env.addObject(c);

                creatures.add(c);

                setPlayerValues(c);

            }

 

            for (Creature c : dead_creatures) {

                env.removeObject(c);

                creatures.remove(c);

            }

 

            dead_creatures.clear();

            new_creatures.clear();

            

            env.advanceOneFrame();

        }

 

        if (finished) env.exit();

        if (reset) gameReset();

    }

 

    /**

     * Pass player's set values to creature objects

     */

    private void setPlayerValues(...)

    {

 

          // ... omitted...

 

    }

}

 

 

 

The problem seems to be this part:

        for (Creature c: creatures) {

            env.addObject(c);

        }

in the gameReset() method.

I tried to breakpoint it to see where the game suddenly closed itself, it's usually during this part of the code.

The env isn't exactly closed either, as I have to reset it to run the stimulation again.

 

Any ideas?

 

Thanks in advance!

 

 

Offline
Joined: 12/13/2010
The problem here is that at

The problem here is that at the end of the gameLoop() method, you call gameReset() if reset is true.  This will NOT result in the gameLoop() being called again, which I suspect is what you wanted.

 

I think that the best way to approach this problem is to restructure you code so the game loop looks something like this:

 

while (!finished) {

   // Reset the game by clearing and re-creating the creatures.

   if (reset) gameReset();

   // Now you are free to implement the simulation logic

   // ...

   // ...

   env.advanceOneFrame();

}

 

Offline
Joined: 01/29/2011
 ah that worked, thanks!

 ah that worked, thanks! =)

 

By the way, why is it that it has to be within the loop?

Is there a way to do it after the loop?

Is the error because of the fact that the same method is being invoked twice?

 

Thank you

Offline
Joined: 12/13/2010
It's more the strcture of the

It's more the strcture of the code that's the problem, not calling a method twice.

 

If you look at your code, the gameReset does not invoke the loop again.