Hello peoples.
I was wondering if someone could tell me if this was a good idea of how to do animation and other time based tasks.
Its a work around I made so I would not have to use Threads to time things.
/*
* Manages different rates based on what the rate is for the main loop
* of a game.
*/
public class RateManager
{
// The current update step
private int currentUpdate;
// The rate that we will reset at.
private int rate;
// The main rate of the loop
private int mainRate;
/*
* Takes the rate that you would like to update from and
* divides it by the mainRate which will result in a fairly accurate
* update rate.
* @param newRate the rate of this manager
* @param newMainRate the mainRate for this manager
*/
public RateManager( int newRate, int newMainRate )
{
this.rate = newRate / newMainRate;
this.mainRate = newMainRate;
this.currentUpdate = 0;
}
/*
* This will return a boolean showing whether this Manager has reached the end of its
* rate. It will then reset and wait for another update.
* @return True if it hit the end and false if not.
*/
public boolean update()
{
if( this.currentUpdate == this.rate )
{
this.currentUpdate = 0;
return true;
}
else
{
this.currentUpdate++;
return false;
}
}
/*
* Sets a new rate based on the mainRate already in the manager.
* @param newRate the new rate for this manager
*/
public void setRate( int newRate )
{
this.rate = newRate / this.mainRate;
}
/*
* Sets a new rate and a new mainRate for this manager
* @param newRate new rate
* @param newMainRate new mainRate
*/
public void setRate( int newRate, int newMainRate )
{
this.rate = newRate / newMainRate;
this.mainRate = newMainRate;
}
}
Basically, if you were updating a loop with Thread.sleep(someTime)
you could animate/time/whatever by having an object that you create that would get the amount of updates
it would take for it to “update” based on what speed you’re main loop is running at.
Lets say we are sleeping our loop every 33mils so about 30fps and we want something to shoot off every 280mils.
Then we create a object that will see how many times the main loop needs to update before it would shoot off.
So we update the object and it will return whether it has shot off or not.