private void init(State state) { if (state == State.GAME) { // If the state is ingame... level = Level.createLevel(0); // Load the spawn level (level 0) TileCoordinate playerSpawn = new TileCoordinate( 15, 60); // Create player coodinates. This class creates coordinates from pixels and // converts it to tile coordinates (16 pixels -> 1 tile) String playerName = ""; // Initiate the players name if (FileHandler.fileExists(FileHandler.localUserFile)) playerName = FileHandler .getPlayerName(); // If the player already exists, set the players name to the // stored name else playerName = sc .getPlayerName(); // Otherwise retrieve the name from the start screen that the user // has typed into the name box player = new Player( playerName, playerSpawn.x(), playerSpawn.y(), key); // Create a new player. Pass in the name, spawn location (in tile coordinates), // and the keyboard class. changeLevel( Level .activeLevel); // Set the level to the current level (0, as we set that in the // beginning) FileHandler.save(player); // Save everything initPause = false; // Set pause to false, just in case it is true c.clearTextArea(); // Clear the console System.out.println( "Controls: \n W/up -> Player up \n A/left -> Player left \n S/down -> Player down \n D/right -> Player right \n " + "Q -> Inventory \n Shift -> Sprint \n HOME -> home level \n M -> Map \n numbers 1-6 -> Weapons"); // Output the controls so the user can see what to do } else if (state == State.START) { // If the state is the start screen... sc = new StartScreen(); // Initiate the start screen uiManager.addPanel( sc); // Add the startscreen to the UI manager, which will render and update it } else if (state == State.PAUSE) { // If the state is in pause... pause = new Pause(); // Instantiate a new pause menu uiManager.addPanel( pause); // Add the pause menu to the UI manager, ready for rendering and updating initPause = true; // Tell the game we have created a pause menu (prevent more than 1 opening) paused = true; // Tell the game we are paused } STATE = State .WAITING; // Once the correct state is set up, set the state wo WAITING, which tells the // game we are waiting for the state to change. }
public void update() { time++; // Increase the time value by one updated 60 times per second, so if (time % 60) == 0, // one second has passed) if (key.paused && !initPause) STATE = State .PAUSE; // If the pause key is being pressed and pause menue hasn't been made, set the // game state to pause else if (key.paused && initPause) // If we press the pause key and we have created pause, return to the game // state STATE = State.GAME; if (time % 1800 == 0 && player != null) // If 30 seconds has passed, and the player exists, then save the game FileHandler.save(player); init(STATE); // Call the initiate state method, in case it is changed during the game key.update(); // Update the key detector // Update layers for (int i = 0; i < UILayerStack.size(); i++) { UILayerStack.get(i).update(); } for (int i = 0; i < layerStack.size(); i++) { layerStack.get(i).update(); } }