Saving is working
git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@382 72836036-5685-4462-b002-a69064685172
This commit is contained in:
parent
244abb7e73
commit
8b763a817b
11 changed files with 185 additions and 55 deletions
|
@ -11,6 +11,7 @@ import jrummikub.view.IView.BottomPanelType;
|
|||
* game control
|
||||
*/
|
||||
public class ApplicationControl {
|
||||
private SaveControl saveControl;
|
||||
private IView view;
|
||||
|
||||
/**
|
||||
|
@ -21,6 +22,7 @@ public class ApplicationControl {
|
|||
*/
|
||||
public ApplicationControl(IView view) {
|
||||
this.view = view;
|
||||
saveControl = new SaveControl(view);
|
||||
|
||||
view.getMenuQuitEvent().add(new IListener() {
|
||||
@Override
|
||||
|
@ -38,11 +40,13 @@ public class ApplicationControl {
|
|||
view.setBottomPanel(BottomPanelType.START_GAME_PANEL);
|
||||
SettingsControl settingsControl = new SettingsControl(view,
|
||||
new GameSettings());
|
||||
settingsControl.getStartGameEvent().add(new IListener1<GameSettings>() {
|
||||
|
||||
settingsControl.getStartGameEvent().add(new IListener1<GameSettings>() {
|
||||
@Override
|
||||
public void handle(GameSettings settings) {
|
||||
GameControl gameControl = new GameControl(settings, view);
|
||||
saveControl.setGameSettings(settings);
|
||||
|
||||
GameControl gameControl = new GameControl(settings, saveControl, view);
|
||||
gameControl.getEndOfGameEvent().add(new IListener() {
|
||||
@Override
|
||||
public void handle() {
|
||||
|
@ -55,5 +59,4 @@ public class ApplicationControl {
|
|||
});
|
||||
settingsControl.startSettings();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -20,6 +20,8 @@ import jrummikub.view.IView.BottomPanelType;
|
|||
* Controls a Game, at some point including all Rounds, starts new Rounds
|
||||
*/
|
||||
public class GameControl {
|
||||
private SaveControl saveControl;
|
||||
|
||||
private GameSettings gameSettings;
|
||||
private IView view;
|
||||
RoundControl roundControl;
|
||||
|
@ -31,15 +33,21 @@ public class GameControl {
|
|||
* Constructor
|
||||
*
|
||||
* @param gameSettings
|
||||
* the game settings
|
||||
* the game settings
|
||||
* @param saveControl
|
||||
* the save control
|
||||
* @param view
|
||||
* the view
|
||||
* the view
|
||||
*/
|
||||
public GameControl(GameSettings gameSettings, IView view) {
|
||||
public GameControl(GameSettings gameSettings, SaveControl saveControl,
|
||||
IView view) {
|
||||
this.gameSettings = gameSettings;
|
||||
this.saveControl = saveControl;
|
||||
this.view = view;
|
||||
|
||||
gameState = new GameState();
|
||||
saveControl.setGameState(gameState);
|
||||
|
||||
gameState.setFirstRoundFirstPlayer((int) (Math.random() * gameSettings
|
||||
.getPlayerList().size()));
|
||||
|
||||
|
@ -96,6 +104,7 @@ public class GameControl {
|
|||
view.showScorePanel(false);
|
||||
|
||||
IRoundState roundState = new RoundState(gameSettings);
|
||||
saveControl.setRoundState(roundState);
|
||||
|
||||
roundState.setActivePlayerNumber(gameState.getFirstRoundFirstPlayer()
|
||||
+ gameState.getScores().size());
|
||||
|
@ -133,8 +142,7 @@ public class GameControl {
|
|||
|
||||
view.getScorePanel().setPlayers(gameSettings.getPlayerList());
|
||||
view.getScorePanel().setScores(gameState.getScores());
|
||||
view.getScorePanel().setAccumulatedScore(
|
||||
gameState.getAccumulatedScore());
|
||||
view.getScorePanel().setAccumulatedScore(gameState.getAccumulatedScore());
|
||||
view.getScorePanel().update();
|
||||
view.showScorePanel(true);
|
||||
}
|
||||
|
|
81
src/jrummikub/control/SaveControl.java
Normal file
81
src/jrummikub/control/SaveControl.java
Normal file
|
@ -0,0 +1,81 @@
|
|||
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();
|
||||
}
|
||||
}
|
||||
}
|
Reference in a new issue