This repository has been archived on 2025-03-02. You can view files and clone it, but cannot push or open issues or pull requests.
JRummikub/src/jrummikub/control/SaveControl.java

82 lines
1.6 KiB
Java
Raw Normal View History

package jrummikub.control;
import java.io.File;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import jrummikub.model.GameSettings;
import jrummikub.model.GameState;
import jrummikub.model.IRoundState;
import jrummikub.util.IListener1;
import jrummikub.view.IView;
/**
* The save control is responsible for loading and saving game and round states
*/
public class SaveControl {
private GameSettings gameSettings;
private GameState gameState;
private IRoundState roundState;
/**
* Creates a new SaveControl
*
* @param view
* the view to use
*/
public SaveControl(IView view) {
view.getSaveEvent().add(new IListener1<File>() {
@Override
public void handle(File file) {
save(file);
}
});
}
/**
* Sets the current game settings
*
* @param gameSettings
* the game settings
*/
public void setGameSettings(GameSettings gameSettings) {
this.gameSettings = gameSettings;
}
/**
* Sets the current game state
*
* @param gameState
* the game state
*/
public void setGameState(GameState gameState) {
this.gameState = gameState;
}
/**
* Sets the current round state
*
* @param roundState
* the round state
*/
public void setRoundState(IRoundState roundState) {
this.roundState = roundState;
}
private void save(File file) {
try {
ObjectOutputStream stream = new ObjectOutputStream(new FileOutputStream(
file));
stream.writeObject(gameSettings);
stream.writeObject(gameState);
stream.writeObject(roundState);
stream.flush();
stream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}