1
\$\begingroup\$

LWJGL 2.9.3 and Slick2D Hello, so Im making this game and its going quite well, but I cant figure this out: Ive got a 2d tile based map (wich is stored in a 2d array) and each tile has a coresponding class and an arrayList for it. Here is what I try to make: I want to be able to click on a stone tile (I have this part done), and then I want the stone tile to disapear after a set ammount of time (e.g. 10 seconds). The code:

    last = (int) (Sys.getTime() / Sys.getTimerResolution());
    globaltimed = last - (int) (Sys.getTime() / Sys.getTimerResolution());

    last = (int) (Sys.getTime() / Sys.getTimerResolution());
    globaltimes = last - (int) (Sys.getTime() / Sys.getTimerResolution());

    lastn = (int) (System.nanoTime() / 100000000);
    timesn = lastn - (int) (System.nanoTime() / 100000000);

^^ This is the part wich goes in the init method in my Play state class. ^^

    timesn = (System.nanoTime() / 100000000);
    // Global time w/ millisecond
    globaltimed = (timesn - lastn) / 10;
    // Global time in seconds
    globaltimes = (timesn - lastn);

    sec = (int) (globaltimed % 60);
    min = (int) (Math.floor(globaltimed/60));
    hour = (int) (Math.floor(min/60));

^^ This part goes in the update method of the Play state class. ^^ Thats all the time stuff.

if(Mouse.isButtonDown(1)){
        for(Stone ss : blocks){
            if(Collision.playerCBlock(new Point(mx, my), ss)){
                if(ss.selected && ss.selectable){
                    //map[Collision.contxy(new Point(ss.x / 32, ss.y / 32), ss, 0)][Collision.contxy(new Point(ss.x / 32, ss.y / 32), ss, 1)] = 3;
                    //blocks.remove(ss);
                    //ss.setSolid(false);
                    //ss.setSelectable(false);
                    ss.addMine(globaltimed, gc);
                    //dirt.add(new Dirt(ss.x,ss.y,false));
                }
            }
        }
    }

^^ Ignore the comments, but here I go through the stone tiles, check if the mouse is on that tile, if that tile is selectable and selected (I select it when left clicking, same for loop pretty much). I want to be able to say ss.addMine(whatever) and then in the stone class to make it wait some time (while displaying that time on the tile) and after that time has passed I want to replace the stone tile with a dirt tile. ^^

    if(!dead){
        //timedl = timed;
        minig = true;
        if(timed + minetime >= Play.getTimed()){
            //gc.sleep(minetime * 1000);
            setSolid(false);
            setSelectable(false);
            Play.dirt.add(new Dirt(x,y,false));
            dead = true;
        }
    }

^^ This is the addMine method in the stone class. I played with this for about an hour but I dont really have any time left now. setSolid and setSelectable baisicly remove its collision and make it unselectable. play.dirt.add(...) adds a dirt tile ontop of the stone tile (Replaces it; not efficiently, but it works for now). ^^ Thats all the important code, now, does anyone have a concept to delay the replacement of the tile(setSolid(false)...dead=true that part) for some time in seconds w/ or w/o milliseconds (globaltimed is w/ millis, and globaltimes is in seconds only(and int)). Thank you

\$\endgroup\$
0
\$\begingroup\$

It looks like you're only checking your timing logic when the user clicks a stone.

Move the timing logic in your addMine method to a different function in your stone class, something like this:

public void UpdateStone()
{
    if(timed + minetime >= Play.getTimed()){
        setSolid(false);
        setSelectable(false);
        Play.dirt.add(new Dirt(x,y,false));
        dead = true;
        minig = false;
    }
}

Then in your update method, iterate over all your stones and call the UpdateStone method for all stones in the "minig" (mining?) state.

for(Stone ss : blocks)
{
     if(ss.IsMining())
     {
          ss.UpdateStone();
     }
}

I'm assuming that somewhere you are updating the "minetime" inside your stones. Hard to say without being able to see all the code.

\$\endgroup\$
  • \$\begingroup\$ I did the stuff that you told me and it works just the way I wanted it to :D. Thank you so much :). Btw minig is a typo but I kindof didnt bother to fix it yet haha, and also do you perhaps know how I can draw text (the time left to destroy the tile) above all other tile, because right now it goes above the tile to the left but underneath the tile on the right, obviously because it renders the tiles that way, but do you maybe know a way around that please. Thanks \$\endgroup\$ – Ivan Jerza Oct 16 '15 at 16:02
  • \$\begingroup\$ Great, glad I could help. Don't forget to mark the answer as accepted if you're satisfied. Not sure about your other problem without being able to see the code. I'm not very familiar with the lwjgl drawing API. You might want to open another question, post some of the code where the problem is happening. \$\endgroup\$ – tandersen Oct 16 '15 at 16:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.