I'm try out a few game mechanics and I tried to mimic WoW's periodic events such as "x damage every z seconds". I've used Timers so far to schedule periodic tasks in this case where I'm trying to regenerate health but is it really necessary to pass "this" in order to modify the instance variable, _mana? Do I really need a separate TimerTask class to "regenerate x health every z seconds"?
In C#, I know I would be able to do this by simply creating a timer that handles ticks through a method. How would I do this with Java? Or is this code fine (efficient)?
private int _mana;
public void regenerate(int amount, int rate)
{
_regenerator = new Timer();
_regenerator.scheduleAtFixedRate(new regenerator(this, amount), 0, rate*1000);
}
public class regenerator extends TimerTask
{
private ManaResource _mr;
private int _amount = 0;
public regenerator(ManaResource mr, int amount)
{
_mr = mr;
_amount = amount;
}
public void run()
{
_mr._mana += _amount;
System.out.println(_mr._mana);
}
}