vendredi 23 février 2018

How would I make a Generator that generates 1 ____ every __ seconds in my run method without using Thread.sleep()?

Right now, this is the code that I have:

@Override
public void run() {

    init();

    int fps = 60;
    double timePerTick = 1000000000 / fps;
    double delta = 0;
    long now;
    long lastTime = System.nanoTime();
    long timer = 0;
    int ticks = 0;

    makeMaps();
    makeTeams();

    boolean madeIron = false, madeGold = false, madeDim = false, madeEm = false, madeRuby = false;

    while(running){
        if(madeIron == false && iron < 48) {
            madeIron = true;
            iron++;
        }
        //if(loops==(loops/5)*5) { I don't know how I would put something here that would set madeIron to false every ___ seconds
        //  
        //}
        loops++;
        now = System.nanoTime();
        delta += (now - lastTime) / timePerTick;
        timer += now - lastTime;
        lastTime = now;

        if(delta >= 1) {
            if(random.nextInt(50)==random.nextInt(50)) {
                update();
            }
            render();
        }

        if(timer >= 1000000000){
            System.out.println("Ticks and Frames: " + ticks);
            ticks = 0;
            timer = 0;
        }
    }

    stop();
}

Would there be any way that I would be able to set madeIron, or any of the made____, every ____ seconds?
Explaining the code:
init() is the method where I initialize everything, right now all I have in it is the code to make the JFrame.
The next few lines, starting at int fps and ending at int ticks is how I make a specific fps for every computer; I can change int fps's value to whatever I want and the other code will make up for it.
The two make()s are how I make the maps and the teams for my game.
The booleans are how, in the future, I will make a Generator for each island on each map.
There's then the while(running) loop.
After that is how I test if I need to increment my iron.
Next is the point that I wish to test if it's been ____ second(s), and if it has, set madeIron to false so that iron will again increment.
Everything else in the while-loop is setting fps to a specific amount so that on every computer, the max is the value of fps.
Outside of the while-loop, there's the stop() method, which basically stops my game by joining Threads; I don't know how it works, I've just been using it ever since I watched a Youtube tutorial on making games.
I just started messing around and I think that a solution might have something to do with if loops is a multiple of 5*60 (5 seconds, multiplied by 60 (fps) to get to seconds).

Aucun commentaire:

Enregistrer un commentaire