Java: GameData

getters and setters coming out of the wazoo.

I like to use getters and setters even when the use of public variables could save me some lines of code. The main reason is that I can always come back later and add some extra logic to how a value is set or returned.

But in Java, getters and setters for properties that don't require extra logic are even more pointless. So I would recommend using public variables here.

Getters and setters in Java are nothing more than a coding convention: the use of the word get and set in the methods that update a property.

Other than that, the only noticeable difference here is that hex values for color are stored as Strings. If you will remember from GameStage, or GameInfo, the string is decoded through a static method in the Color class.

The GameData Class


package atlantis;

public class GameData {
	
	private static GameData instance;
	
	private GameData (){}
	
	public static GameData getInstance () {
		if (instance == null) {
			instance = new GameData();
		}
		return instance;
	}
	
	private String _gameMode;
	private int _numBullets = 0;
	private int _numShips = 4;
	private int _score = 0;
	
	private int _timeBetweenShips = 2000;
	private int _timeBetweenPasses = 1000;
	private int _shipSpeed = 10;
	private String _backgroundColor;
	
	
	public String getGameMode () {
		return _gameMode;
	}
	public void setGameMode (String value) {
		_gameMode = value;
	}
	/*public int geLevel () {
		return _level;
	}
	public void setLevel (int value) {
		_level = value;
	}
	*/
	public int getScore () {
		return _score;
	}
	public void setScore (int value){
		_score = value;
		GameInfo.getInstance().update(String.valueOf(_score));
	}
	public int getNumBullets () {
		return _numBullets;
	}
	public void setNumBullets (int value){
		if (value > GameConstants.NUM_BULLETS_ON_SCREEN) return;
		_numBullets = value;
	}
	public void setNumShips (int value){
		_numShips = value;
	}
	public int getNumShips () {
		return _numShips;
	}
	
	public void setTimeBetweenShips (int value){
		_timeBetweenShips = value;
	}
	public int getTimeBetweenShips () {
		return _timeBetweenShips;
	}
	
	public void setTimeBetweenPasses (int value){
		_timeBetweenPasses = value;
	}
	public int getTimeBetweenPasses () {
		return _timeBetweenPasses;
	}
	
	public void setShipSpeed (int value){
		_shipSpeed = value;
	}
	public int getShipSpeed () {
		return _shipSpeed;
	}
	
	public String getBackgroundColor () {
		if (_backgroundColor == null) _backgroundColor = GameConstants.BACKGROUND_COLOR;
		return _backgroundColor;
	}
	public void setBackgroundColor (String value){
		_backgroundColor = value;
	}

	public void reset (){
		_backgroundColor = GameConstants.BACKGROUND_COLOR;
	}
	public void newLevel (){
		reset();
		_numShips += 2;
		_timeBetweenShips -= 50;
		_timeBetweenPasses -= 50;
		_shipSpeed += 1;
		
		if (_timeBetweenShips < 300) _timeBetweenShips = 300;
		if (_timeBetweenPasses  < 200) _timeBetweenPasses  = 200;
	}
	
}