I am making a game using Java where a lot of events in that game are time based. The game engine has an act method for all entities where I can update everything. As of right now each object that has a time based events, have an integer counter that ++ every time act is called. My question is if i'm going to have entities in the hundreds, would it be worth it to add a global counter to a singleton and let each object use that or is increment integers and then setting them back to zero all the time not going to be that big of a deal? Also is there a better way of counting up time than incrementing integers?
Your question is a little confusing, but I think I know what you mean. You should be using a Game loop. This basically means you should have two main methods in your game class. This would be the traditional way to do this:
class Game{
List<Entity> entities;
//your "act" method
public void update(){
for(Entity e : entities){
e.update();
}
}
public void draw(Graphics2D g){
for(Entity e : entities){
e.draw(g);
}
}
}
But to address your question, since you are using time based events, maybe you could do this instead:
class Game{
private List<Entity> entities;
private int time = 0;
//your "act" method
public void update(){
for(Entity e : entities){
e.update(time);
}
time++;
}
public void draw(Graphics2D g){
for(Entity e : entities){
e.draw(g);
}
}
}
And for each entity, just use the time passed in. You do not need to reset it. Like, for example, if you wanted an entity to do something every 300 frames, you could do this:
class Dog{
public void update(int time){
if(time % 300 == 0){
bark();
}
}
public void draw(Graphics2D g){}
public void bark(){}
}