diff --git a/core/src/cz/nic/tablexia/TablexiaSettings.java b/core/src/cz/nic/tablexia/TablexiaSettings.java index aafacd229e78d9a80ca65a6e20e056e71c822c0b..b48ae93c41f77e4e5787d6693275b4e1b01fd25f 100644 --- a/core/src/cz/nic/tablexia/TablexiaSettings.java +++ b/core/src/cz/nic/tablexia/TablexiaSettings.java @@ -15,11 +15,8 @@ import java.util.List; import java.util.Locale; import cz.nic.tablexia.bus.ApplicationBus; -import cz.nic.tablexia.game.GameDefinition; -import cz.nic.tablexia.game.difficulty.GameDifficulty; import cz.nic.tablexia.loader.application.ApplicationTextManager; import cz.nic.tablexia.model.UserDAO; -import cz.nic.tablexia.model.UserDifficultySettingsDAO; import cz.nic.tablexia.screen.AbstractTablexiaScreen; import cz.nic.tablexia.screen.gamemenu.GameMenuScreen; import cz.nic.tablexia.screen.loader.LoaderScreen; @@ -231,10 +228,6 @@ public class TablexiaSettings { return BUILD_TYPE; } - public GameDifficulty getGameDifficulty(GameDefinition gameDefinition) { - return UserDifficultySettingsDAO.getUserSettingsByGame(getSelectedUser().getId(), gameDefinition.getGameNumber()); - } - public static Color getDefaultBackgroundColor() { return DEFAULT_BACKGROUND_COLOR; } diff --git a/core/src/cz/nic/tablexia/game/AbstractTablexiaGame.java b/core/src/cz/nic/tablexia/game/AbstractTablexiaGame.java index 36485cede63e9eab5f3bce858a9780891e0df592..f4820bb5c8e2a9a6a0600e753264dc3100d799ec 100644 --- a/core/src/cz/nic/tablexia/game/AbstractTablexiaGame.java +++ b/core/src/cz/nic/tablexia/game/AbstractTablexiaGame.java @@ -51,6 +51,7 @@ import cz.nic.tablexia.loader.application.ApplicationTextManager; import cz.nic.tablexia.menu.AbstractMenu; import cz.nic.tablexia.menu.main.MainMenu; import cz.nic.tablexia.model.UserDAO; +import cz.nic.tablexia.model.UserDifficultySettingsDAO; import cz.nic.tablexia.model.game.GameDAO; import cz.nic.tablexia.screen.AbstractTablexiaScreen; import cz.nic.tablexia.screen.gamemenu.GameMenuScreen; @@ -428,7 +429,7 @@ public abstract class AbstractTablexiaGame<T> extends AbstractTablexiaScreen<T> // create new random = TablexiaSettings.GAMES_RANDOM_SEED == null ? new TablexiaRandom() : new TablexiaRandom(TablexiaSettings.GAMES_RANDOM_SEED); GameDefinition gameDefinition = GameDefinition.getGameDefinitionForClass(AbstractTablexiaGame.this.getClass()); - GameDifficulty gameDifficulty = gameDefinition.hasTutorial() && UserDAO.isTutorialForGameDefinition(gameDefinition, getSelectedUser()) ? GameDifficulty.TUTORIAL : TablexiaSettings.getInstance().getGameDifficulty(gameDefinition); + GameDifficulty gameDifficulty = gameDefinition.hasTutorial() && UserDAO.isTutorialForGameDefinition(gameDefinition, getSelectedUser()) ? GameDifficulty.TUTORIAL : getGameDifficulty(); game = GameDAO.createGame(getSelectedUser(), gameDifficulty, gameDefinition, random); Log.info(getClass(), "[DB] Created new GAME object: " + game); } else { @@ -653,7 +654,7 @@ public abstract class AbstractTablexiaGame<T> extends AbstractTablexiaScreen<T> } public GameDifficulty getGameDifficulty() { - return game == null ? TablexiaSettings.getInstance().getGameDifficulty(GameDefinition.getGameDefinitionForClass(AbstractTablexiaGame.this.getClass())) : GameDifficulty.getGameDifficultyForDifficultyNumber(game.getGameDifficulty()); + return game == null ? UserDifficultySettingsDAO.getUserSettingsByGame(getSelectedUser().getId(), GameDefinition.getGameDefinitionForClass(AbstractTablexiaGame.this.getClass()).getGameNumber()) : GameDifficulty.getGameDifficultyForDifficultyNumber(game.getGameDifficulty()); } protected Game getGame() { diff --git a/core/src/cz/nic/tablexia/screen/gamemenu/pages/GameMenuPage.java b/core/src/cz/nic/tablexia/screen/gamemenu/pages/GameMenuPage.java index fca9761df74e48c448dcf7eda5266d778f1c5d51..a7716c2d75123c2ea616a0254a2214cd0b458296 100644 --- a/core/src/cz/nic/tablexia/screen/gamemenu/pages/GameMenuPage.java +++ b/core/src/cz/nic/tablexia/screen/gamemenu/pages/GameMenuPage.java @@ -34,8 +34,8 @@ import cz.nic.tablexia.screen.profile.ProfileScreen; import cz.nic.tablexia.util.MusicUtil; import cz.nic.tablexia.util.ui.ClickListenerWithSound; import cz.nic.tablexia.util.ui.Switch; -import cz.nic.tablexia.util.ui.TablexiaProgressBar; import cz.nic.tablexia.util.ui.TablexiaLabel; +import cz.nic.tablexia.util.ui.TablexiaProgressBar; import cz.nic.tablexia.util.ui.button.TablexiaButton; /** @@ -105,7 +105,6 @@ public class GameMenuPage extends MenuPage { private Actor buttonDifficultyEasy, buttonDifficultyMedium, buttonDifficultyHard; private Stack diffStack; - private GameDifficulty gameDifficulty = GameDifficulty.EASY; private Switch.DragSwitchListener dragSwitchListener; //hack to keep paralax layers out of picture before scrolled for first time @@ -141,6 +140,8 @@ public class GameMenuPage extends MenuPage { private TextureRegion midLayer; private TextureRegion foregroundLayer; + boolean updateGameDifficultyInDB = true; + public GameMenuPage(GameMenuScreen screen, GameDefinition game) { super(screen); this.game = game; @@ -359,7 +360,7 @@ public class GameMenuPage extends MenuPage { @Override public void clicked(InputEvent event, float x, float y) { super.clicked(event, x, y); - dragSwitchListener.switchToStep(GameDifficulty.EASY.getDifficultyNumber() - 1); + updateDifficultySeekBar(GameDifficulty.EASY); } }); @@ -367,7 +368,7 @@ public class GameMenuPage extends MenuPage { @Override public void clicked(InputEvent event, float x, float y) { super.clicked(event, x, y); - dragSwitchListener.switchToStep(GameDifficulty.MEDIUM.getDifficultyNumber() - 1); + updateDifficultySeekBar(GameDifficulty.MEDIUM); } }); @@ -375,7 +376,7 @@ public class GameMenuPage extends MenuPage { @Override public void clicked(InputEvent event, float x, float y) { super.clicked(event, x, y); - dragSwitchListener.switchToStep(GameDifficulty.HARD.getDifficultyNumber() - 1); + updateDifficultySeekBar(GameDifficulty.HARD); } }); @@ -420,15 +421,30 @@ public class GameMenuPage extends MenuPage { dragSwitchListener.setSwitchSelectedListener(new Switch.DragSwitchListener.SwitchSelectedListener() { @Override public void stepSelected(int step) { - difficultyChanged(GameDifficulty.getVisibleGameDifficultyList().get(step)); + if (updateGameDifficultyInDB) { + UserDifficultySettingsDAO.saveSettingsForUser(TablexiaSettings.getInstance().getSelectedUser().getId(), game.getGameNumber(), GameDifficulty.getVisibleGameDifficultyList().get(step).getDifficultyNumber()); + } } }); - gameDifficulty = UserDifficultySettingsDAO.getUserSettingsByGame(TablexiaSettings.getInstance().getSelectedUser().getId(), game.getGameNumber()); - dragSwitchListener.switchToStep(gameDifficulty.getDifficultyNumber() - 1); + updateGameDifficultyFromDB(); stepSoundsEnabled = true; } + private void updateGameDifficultyFromDB() { + updateDifficultySeekBar(UserDifficultySettingsDAO.getUserSettingsByGame(TablexiaSettings.getInstance().getSelectedUser().getId(), game.getGameNumber()), false); + } + + private void updateDifficultySeekBar(GameDifficulty gameDifficulty) { + updateDifficultySeekBar(gameDifficulty, true); + } + + private void updateDifficultySeekBar(GameDifficulty gameDifficulty, boolean updateGameDifficultyInDB) { + this.updateGameDifficultyInDB = updateGameDifficultyInDB; + dragSwitchListener.switchToStep(gameDifficulty.getDifficultyNumber() - 1); + this.updateGameDifficultyInDB = true; + } + public void prepareGameProgress() { this.gameMenuPageProgress = new TablexiaProgressBar( getScreen().getScreenTextureRegion(GameMenuScreen.GameMenuPageDefinition.getResourcePath(game, GameMenuAssets.AssetType.PROGRESSBAR)), @@ -477,21 +493,6 @@ public class GameMenuPage extends MenuPage { return (rankProgress.getCurrXP() - rankProgress.getMinXP()) + Character.toString(PROGRESS_STATUS_SEPARATOR) + (rankProgress.getNextXP() - rankProgress.getMinXP()); } - /** - * Called when difficulty is changed - * - * @param difficulty - */ - public void difficultyChanged(final GameDifficulty difficulty) { - gameDifficulty = difficulty; - new Runnable() { - @Override - public void run() { - UserDifficultySettingsDAO.saveSettingsForUser(TablexiaSettings.getInstance().getSelectedUser().getId(), game.getGameNumber(), difficulty.getDifficultyNumber()); - } - }.run(); - } - private void playStepSound(GameDifficulty previous, GameDifficulty current) { switch (previous) { case EASY: @@ -639,8 +640,7 @@ public class GameMenuPage extends MenuPage { public void run() { if (selectedUserEvent.isUserSelected()) { stepSoundsEnabled = false; - gameDifficulty = UserDifficultySettingsDAO.getUserSettingsByGame(TablexiaSettings.getInstance().getSelectedUser().getId(), game.getGameNumber()); - dragSwitchListener.switchToStep(gameDifficulty.getDifficultyNumber() - 1); + updateGameDifficultyFromDB(); stepSoundsEnabled = true; updateGameProgress(); diff --git a/core/src/cz/nic/tablexia/util/ui/Switch.java b/core/src/cz/nic/tablexia/util/ui/Switch.java index 45f035f764fe82a14da2e63107458461ca10526a..3d8522a7b0da7d37c83706313d6846b65d00bf46 100644 --- a/core/src/cz/nic/tablexia/util/ui/Switch.java +++ b/core/src/cz/nic/tablexia/util/ui/Switch.java @@ -202,12 +202,12 @@ public class Switch extends Group { this.switchSelectedListener = switchSelectedListener; } - public static interface SwitchMovedListener { - public void movedToStep(int step); + public interface SwitchMovedListener { + void movedToStep(int step); } - public static interface SwitchSelectedListener { - public void stepSelected(int step); + public interface SwitchSelectedListener { + void stepSelected(int step); } } } \ No newline at end of file