public void changeLevel(Level lev) { if (level != null) { // If the level exists... layerStack.remove( level); // remove it from the layer stack (stops it being rendered, updated, and recieving // key events) level = lev; // Set level to the new level we wish to change to level.add(player); // Add the player to the new level level.setPlayerLocation(); // Set the location to the levels chosen location addLayer( level); // Add the new level into the layerstack for updating, rendering and receiving key // events } }
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 render() { BufferStrategy bs = getBufferStrategy(); // Create a buffer strategy (a way of drawing pixels to the image) if (bs == null) { // If it is null... createBufferStrategy( 3); // Create a buffer strategy. Set it up for 3D as it has a more recise pixel resolution // and more control return; // Exit the method } screen.clear(); // Clear the screen every render, ready for re-drawing Graphics g = bs.getDrawGraphics(); // Get the buffer strategy's default graphics object, used for drawing this.g = g; // Set the global graphics variable to the one we just fetched if (STATE != State.WAITING) { // If the game state isn't waiting... List<UIPanel> panels = uiManager.getPanels(); // Get the list of panels that are currently being drawn for (int i = 0; i < panels.size(); i++) { // For each one... uiManager.removePanel(panels.get(i)); // remove it. } } init( STATE); // Call initiate state to set up the game for if the state has changed during // rendering. if (player != null) { // If the player exists... double xScroll = player.getX() - screen.width / 2; // Set the x scroll variable of the map double yScroll = player.getY() - screen.height / 2; // Set the y scroll variable of the map level.setScroll((int) xScroll, (int) yScroll); // Tell the level these values } // Loop through the stacks, rendering each layer for (int i = 0; i < layerStack.size(); i++) { layerStack.get(i).render(screen); } // Set the pixels in this class to all the pixels stored in the screen class, ready for // rendering to the BufferedImage for (int i = 0; i < pixels.length; i++) { pixels[i] = screen.pixels[i]; } g.setColor( new Color( 0xff00ff)); // Set the background to a bright pink, so we know if anything isn't // rendering correctly g.fillRect(0, 0, getWidth(), getHeight()); // Draw the rectangle to fill thr screen /* scale the image's size to fill the window, scaling the objects inside to also fit. This ensures the game will scale depending on * scree resolution */ int newWidth = 0, newHeight = 0; if (image.getWidth() / image.getHeight() < getAbsoluteWidth() / getAbsoluteHeight()) { newWidth = getAbsoluteWidth(); newHeight = (int) Math.floor( (double) image.getHeight() * (double) getAbsoluteWidth() / (double) image.getWidth()); } else { newHeight = getAbsoluteHeight(); newWidth = (int) Math.floor( (double) image.getWidth() * (double) getAbsoluteHeight() / (double) image.getHeight()); } g.drawImage( image, 0, 0, newWidth, newHeight, null); // Draw the image over the rectangle, filling the window // Loop through the stacks, rendering each layer. Do the UI as the last thing to render, as it // is on top of everything else for (int i = 0; i < UILayerStack.size(); i++) { UILayerStack.get(i).render(g); } g.dispose(); // Dispose of the graphics object as we no longer need it bs.show(); // Show the buffer strategy in the canvas }