One of the most useful classes I've ever written is this Interval class. Games always have a main game loop, which gets run every game cycle. However, often you don't need something to be done every cycle, such as the AI re-examining it's environment. For this reason I wrote a simple Java class which is used in the following way:-
Interval check_env = new Interval(1000); // Millisecs
while (game_looping) {
...
if (check_env.hitInterval()) {
// Do slightly more involved stuff.
}
...
}
It's not the worlds best class ever, but I find myself using it all the time. Here it is:-
public class Interval {
private long last_check_time, duration;
public Interval(long dur) {
super();
this.duration = dur;
this.last_check_time = System.currentTimeMillis();
}
public boolean hitInterval() {
if (System.currentTimeMillis() - duration > this.last_check_time) {
this.last_check_time = System.currentTimeMillis();
return true;
}
return false;
}
}
No comments:
Post a Comment