PageRenderTime 17ms CodeModel.GetById 8ms app.highlight 5ms RepoModel.GetById 2ms app.codeStats 0ms

/Code/src/com/game/Regulator.java

https://bitbucket.org/DeveloperUX/behaviortree
Java | 48 lines | 21 code | 5 blank | 22 comment | 1 complexity | d15c351cdc2dc1d5f96d6c005fa1dcf8 MD5 | raw file
 1package com.game;
 2
 3/**
 4 * Time step class, for regulating update speed of updates.
 5 * @author Ying
 6 *
 7 */
 8public class Regulator 
 9{
10	/**
11	 * Last time the regulator was ready
12	 */
13	private long lastUpdateTime;
14	
15	/**
16	 * Speed at which the regulator updates
17	 */
18	private long updateSpeed;
19	
20	/**
21	 * Millisecond constant, to avoid magic numbers
22	 */
23	private static final int MILSECS = 1000;
24	
25	/**
26	 * Creates a new instance of the Regulator class
27	 * @param framesPerSecond is the update speed for this regulator.
28	 */
29	public Regulator(float framesPerSecond)
30	{
31		this.updateSpeed = (long) (MILSECS / framesPerSecond);
32		this.lastUpdateTime = System.currentTimeMillis();
33	}
34	
35	/**
36	 * Returns true if the regulator is ready for a update, false otherwise
37	 * @return
38	 */
39	public boolean IsReady()
40	{
41		if(System.currentTimeMillis() - this.lastUpdateTime > updateSpeed)
42		{
43			this.lastUpdateTime = System.currentTimeMillis();
44			return true;
45		}
46		return false;
47	}	
48}