Skip to content

Commit

Permalink
call savegameScreen init only after an onClick event
Browse files Browse the repository at this point in the history
introduce constants
  • Loading branch information
rubo77 committed Apr 14, 2024
1 parent a52f1bc commit ecd380c
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 38 deletions.
13 changes: 13 additions & 0 deletions app/src/main/java/roboyard/eclabs/Constants.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package roboyard.eclabs;

public class Constants {
// Screen indices
public static final int SCREEN_START = 0;
public static final int SCREEN_SECOND_START = 1;
public static final int SCREEN_SETTINGS = 2;
public static final int SCREEN_CREDITS = 3;
public static final int SCREEN_RANDOM_GAME = 4;
public static final int SCREEN_LEVEL_GAME_START = 5;
public static final int SCREEN_LEVEL_GAME_END = 8;
public static final int SCREEN_SAVE_GAMES = 9;
}
28 changes: 11 additions & 17 deletions app/src/main/java/roboyard/eclabs/GameButtonGotoSavedGame.java
Original file line number Diff line number Diff line change
@@ -1,29 +1,28 @@
package roboyard.eclabs;

import android.util.SparseArray;

import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;

/**
* Represents a button that navigates to a saved game.
* Represents a button to save or load a game.
*/
public class GameButtonGotoSavedGame extends GameButtonGoto {

private String mapPath = null;

/**
* Constructs a GameButtonGotoSavedGame object.
* Constructor for GameButtonGotoSavedGame.
* @param x The x-coordinate of the button.
* @param y The y-coordinate of the button.
* @param w The width of the button.
* @param h The height of the button.
* @param imageUp The image resource ID when the button is not pressed.
* @param imageDown The image resource ID when the button is pressed.
* @param target The target screen to navigate to.
* @param filePath The file path of the saved game.
* @param imageUp The image when the button is not pressed.
* @param imageDown The image when the button is pressed.
* @param target The target screen number.
* @param filePath The file path associated with the button.
*/
public GameButtonGotoSavedGame(float x, float y, float w, float h, int imageUp, int imageDown, int target, String filePath) {
super((int)x, (int)y, (int)w, (int)h, imageUp, imageDown, target);
Expand All @@ -38,19 +37,14 @@ public GameButtonGotoSavedGame(float x, float y, float w, float h, int imageUp,
public void onClick(GameManager gameManager) {
if (gameManager.getPreviousScreenKey() == 4) {
// Screen to save or overwrite a savegame

ArrayList gridElements = ((GridGameScreen)gameManager.getScreens().get(gameManager.getPreviousScreenKey())).getGridElements();

ArrayList gridElements = ((GridGameScreen) gameManager.getScreens().get(gameManager.getPreviousScreenKey())).getGridElements();
FileReadWrite.clearPrivateData(gameManager.getActivity(), mapPath);
FileReadWrite.writePrivateData(gameManager.getActivity(), mapPath, MapObjects.createStringFromList(gridElements, false));
addMapsSaved(gameManager);
SparseArray<GameScreen> screens = gameManager.getScreens();
for (int i = 0; i < screens.size(); i++) {
if (screens.get(i).getClass() == SaveGameScreen.class) {
// redraw save buttons with new state of the saved game
((SaveGameScreen) screens.get(i)).createButtons();
}
}

// redraw save buttons with new state of the saved game
SaveGameScreen saveGameScreen = (SaveGameScreen) gameManager.getScreens().get(Constants.SCREEN_SAVE_GAMES);
saveGameScreen.createButtons();
} else {
// Screen to select a savegame
SaveManager saver = new SaveManager(gameManager.getActivity());
Expand Down
27 changes: 14 additions & 13 deletions app/src/main/java/roboyard/eclabs/GameManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,27 +47,28 @@ public GameManager(InputManager inputManager, RenderManager renderManager, int s
this.activity = activity;

// List of all screens
/* screen 1: second start screen
/* screen 0: start screen
* screen 1: second start screen
* screen 2: settings
* screen 3: credits
* screen 4: start random game
* screen 5-8: start level game
* screen 9: save games
*/
this.screens.append(0, new MainMenuGameScreen(this));
this.screens.append(1, new GameOptionsGameScreen(this));
this.screens.append(2, new SettingsGameScreen(this));
this.screens.append(3, new CreditsGameScreen(this, activity));
this.screens.append(4, new GridGameScreen(this));
this.screens.append(5, new LevelChoiceGameScreen(this, 0, -1, 6));
this.screens.append(6, new LevelChoiceGameScreen(this, 15, 5, 7));
this.screens.append(7, new LevelChoiceGameScreen(this, 30, 6, 8));
this.screens.append(8, new LevelChoiceGameScreen(this, 45, 7, -1));
this.screens.append(9, new SaveGameScreen(this));
this.screens.append(Constants.SCREEN_START, new MainMenuGameScreen(this));
this.screens.append(Constants.SCREEN_SECOND_START, new GameOptionsGameScreen(this));
this.screens.append(Constants.SCREEN_SETTINGS, new SettingsGameScreen(this));
this.screens.append(Constants.SCREEN_CREDITS, new CreditsGameScreen(this, activity));
this.screens.append(Constants.SCREEN_RANDOM_GAME, new GridGameScreen(this));
this.screens.append(Constants.SCREEN_LEVEL_GAME_START, new LevelChoiceGameScreen(this, 0, -1, 6));
this.screens.append(Constants.SCREEN_LEVEL_GAME_START + 1, new LevelChoiceGameScreen(this, 15, 5, 7));
this.screens.append(Constants.SCREEN_LEVEL_GAME_START + 2, new LevelChoiceGameScreen(this, 30, 6, 8));
this.screens.append(Constants.SCREEN_LEVEL_GAME_END, new LevelChoiceGameScreen(this, 45, 7, -1));
this.screens.append(Constants.SCREEN_SAVE_GAMES, new SaveGameScreen(this));
// End of list of all screens

this.currentScreen = this.screens.get(0);
this.previousScreen = this.screens.get(0);
this.currentScreen = this.screens.get(Constants.SCREEN_START);
this.previousScreen = this.screens.get(Constants.SCREEN_START);
}

/**
Expand Down
11 changes: 3 additions & 8 deletions app/src/main/java/roboyard/eclabs/SaveGameScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import java.util.ArrayList;

/**
* Screen for saving and loading games.
* Screen for saving and loading games (screen 9)
*/
public class SaveGameScreen extends GameScreen {
private float ratioW;
Expand All @@ -18,7 +18,6 @@ public class SaveGameScreen extends GameScreen {
private int autosaveButtonY;
private int backButtonX;
private int backButtonY;
private boolean isButtonPressed = false;

public SaveGameScreen(GameManager gameManager) {
super(gameManager);
Expand All @@ -38,7 +37,7 @@ public void create() {
/**
* calculate Button Positions and load all saved maps to create a unique string for each from the mapElements
*/
private void init() {
public void init() {
// Button positions and dimensions
ratioW = ((float) gameManager.getScreenWidth()) / ((float) 1080);
ratioH = ((float) gameManager.getScreenHeight()) / ((float) 1920);
Expand Down Expand Up @@ -88,6 +87,7 @@ private void init() {
* Create buttons for saving and loading games.
*/
public void createButtons() {
init();
ArrayList<GameButtonGotoSavedGame> aRemove = new ArrayList<>();
for (Object currentObject : this.instances) {
if (currentObject.getClass() == GameButtonGotoSavedGame.class) {
Expand Down Expand Up @@ -132,11 +132,6 @@ public void load(RenderManager renderManager) {

@Override
public void draw(RenderManager renderManager) {
if (isButtonPressed) {
init(); // Call init() only if a button was pressed
isButtonPressed = false; // Reset the flag after calling init()
}

// Draw background and text
renderManager.setColor(Color.parseColor("#cccccc"));
renderManager.paintScreen();
Expand Down

0 comments on commit ecd380c

Please sign in to comment.