📄 dungeonmanager.java
字号:
package net.frog_parrot.dungeon;import javax.microedition.lcdui.*;import javax.microedition.lcdui.game.*;import net.frog_parrot.util.*;/** * This class handles the graphics objects. * * @author Carol Hamer */public class DungeonManager extends LayerManager { //--------------------------------------------------------- // dimension fields // (constant after initialization) /** * The x-coordinate of the place on the game canvas where * the LayerManager window should appear, in terms of the * coordiantes of the game canvas. */ static int CANVAS_X; /** * The y-coordinate of the place on the game canvas where * the LayerManager window should appear, in terms of the * coordiantes of the game canvas. */ static int CANVAS_Y; /** * The width of the display window. */ static int DISP_WIDTH; /** * The height of this object's visible region. */ static int DISP_HEIGHT; /** * the (right or left) distance the player * goes in a single keystroke. */ static int MOVE_LENGTH; /** * the minimum (right or left) distance the player * must stay away from the walls (to avoid getting * stuck when the sprite image changes). */ static int MOVE_BUFFER; /** * The width of the square tiles that this game is divided into. * This is the width of the stone walls as well as the princess. */ static int SQUARE_WIDTH; /** * The number of background tiles per row. */ static int BACK_TILES; /** * A constant number of pixels to use in calculating the * height of a jump. */ static int JUMP_INT; /** * A constant number of pixels to use in calculating the * height of a jump. */ static int JUMP_FRAC_NUM; /** * A constant number of pixels to use in calculating the * height of a jump. */ static int JUMP_FRAC_DENOM; /** * The jump index that indicates that no jump is * currently in progress.. */ static final int NO_JUMP = -6; /** * The maximum speed for the player's fall. */ static final int MAX_FREE_FALL = 3; /** * The maximum horizontal running speed. */ static final int MAX_SPEED = 3; //--------------------------------------------------------- // game object fields /** * the handle back to the canvas. */ DungeonCanvas myCanvas; /** * the class that handles the differences from one handset to * another. */ Customizer myCustomizer; /** * the walls of the dungeon. */ TiledLayer myWalls; /** * the tiled layer that goes behind the walls. */ TiledLayer myBackground; /** * the player. */ Sprite myPrincess; /** * the goal. */ Sprite myCrown; /** * the image to construct the doors and keys. * cached because it is used more than once. */ Image myKeyImage; /** * the image to construct the numbers. * cached because it is used more than once. */ Image myNumberImage; /** * the doors. */ DoorKey[] myDoors; /** * the keys. */ DoorKey[] myKeys; /** * the key currently held by the player. */ DoorKey myHeldKey; /** * The leftmost x-coordinate that should be visible on the * screen in terms of this objects internal coordinates. */ int myViewWindowX; /** * The top y-coordinate that should be visible on the * screen in terms of this objects internal coordinates. */ int myViewWindowY; /** * Where the princess is in the jump sequence. */ int myIsJumping = NO_JUMP; /** * Where the princess is moving horizontally. */ int myIsRunning = 0; /** * Which board we're playing on. */ int myCurrentBoardNum = 0; /** * Whether the menu is currently being displayed. */ boolean myMenuMode; //----------------------------------------------------- // gets/sets /** * . */ public Image getNumberImage() { return myNumberImage; } //----------------------------------------------------- // initialization // set up or save game data. /** * Constructor merely sets the data. * @param x The x-coordinate of the place on the game canvas where * the LayerManager window should appear, in terms of the * coordiantes of the game canvas. * @param y The y-coordinate of the place on the game canvas where * the LayerManager window should appear, in terms of the * coordiantes of the game canvas. * @param width the width of the region that is to be * occupied by the LayoutManager. * @param height the height of the region that is to be * occupied by the LayoutManager. * @param customizer the object that loads the correct * custom data for the current platform. * @param canvas the DungeonCanvas that this LayerManager * should appear on. */ public DungeonManager(int x, int y, int width, int height, Customizer customizer, DungeonCanvas canvas) { myCustomizer = customizer; myCanvas = canvas; CANVAS_X = x; CANVAS_Y = y; DISP_WIDTH = width; DISP_HEIGHT = height; } /** * Set up all of the data. * * This is called from a separate init method in order * to limit the amount of resource loading that is done * by the thread that called the startApp method. */ public void init() throws Exception { myCustomizer.init(); MOVE_LENGTH = myCustomizer.getInt("move.length"); MOVE_BUFFER = myCustomizer.getInt("move.buffer"); SQUARE_WIDTH = myCustomizer.getInt("square.width"); BACK_TILES = myCustomizer.getInt("back.tiles"); JUMP_INT = myCustomizer.getInt("jump.int"); JUMP_FRAC_NUM = myCustomizer.getInt("jump.frac.numerator"); JUMP_FRAC_DENOM = myCustomizer.getInt("jump.frac.denominator"); // create a decoder object that creates the dungeon and // its associated Sprites from data. BoardDecoder decoder = new BoardDecoder(myCurrentBoardNum, myCustomizer); // get the dungeon walls layer: myWalls = decoder.getLayer(); // the background behind the walls is a single image, // so the easiest way to add it to the layer manager // is to make it a sprite: Image bi = myCustomizer.getImage("background"); myBackground = new TiledLayer(BACK_TILES, BACK_TILES, bi, bi.getWidth(), bi.getHeight()); // set all cells to use tile 1 instead of the default // (blank) tile 0: myBackground.fillCells(0, 0, BACK_TILES, BACK_TILES, 1); // get the coordinates of the square that the princess // starts on. int[] playerCoords = decoder.getPlayerSquare(); // create the player sprite myPrincess = new Sprite(myCustomizer.getImage("princess"), SQUARE_WIDTH, SQUARE_WIDTH); myPrincess.setFrame(1); // we define the reference pixel to be in the middle // of the princess image so that when the princess turns // from right to left (and vice versa) she does not // appear to move to a different location. myPrincess.defineReferencePixel(SQUARE_WIDTH/2, 0); // the dungeon is a 16x16 grid, so the array playerCoords // gives the player's location in terms of the grid, and // then we multiply those coordinates by the SQUARE_WIDTH // to get the precise pixel where the player should be // placed (in terms of the LayerManager's coordinate system) myPrincess.setPosition(SQUARE_WIDTH * playerCoords[0], SQUARE_WIDTH * playerCoords[1]); // we append all of the Layers (TiledLayer and Sprite) // so that this LayerManager will paint them when // flushGraphics is called. append(myPrincess); // get the coordinates of the square where the crown // should be placed. int[] goalCoords = decoder.getGoalSquare(); Image crownImage = myCustomizer.getImage("crown"); myCrown = new Sprite(crownImage); myCrown.defineReferencePixel(crownImage.getWidth()/2, crownImage.getHeight()); myCrown.setRefPixelPosition( (SQUARE_WIDTH * goalCoords[0]) + (SQUARE_WIDTH/2), (SQUARE_WIDTH * goalCoords[1]) + SQUARE_WIDTH); append(myCrown); // The decoder creates the door and key sprites and places // them in the correct locations in terms of the LayerManager's // coordinate system. myNumberImage = myCustomizer.getImage("numbers"); myKeyImage = myCustomizer.getImage("keys"); myDoors = decoder.createDoors(myKeyImage); myKeys = decoder.createKeys(myKeyImage); for(int i = 0; i < myDoors.length; i++) { append(myDoors[i]); } for(int i = 0; i < myKeys.length; i++) { append(myKeys[i]); } // append the background last so it will be painted first. append(myWalls); append(myBackground); // this sets the view screen so that the player is // in the center. myViewWindowX = SQUARE_WIDTH * playerCoords[0] - ((DISP_WIDTH - SQUARE_WIDTH)/2); myViewWindowY = SQUARE_WIDTH * playerCoords[1] - ((DISP_HEIGHT - SQUARE_WIDTH)/2); // a number of objects are created in order to set up the game, // but they should be eliminated to free up memory: decoder = null; System.gc(); } /** * sets all variables back to their initial positions. */ void reset() throws Exception { // first get rid of the old board: for(int i = 0; i < myDoors.length; i++) { remove(myDoors[i]); } myHeldKey = null; for(int i = 0; i < myKeys.length; i++) { remove(myKeys[i]); } remove(myBackground); remove(myWalls); // now create the new board: myCurrentBoardNum++; // in this version we go back to the beginning if // all boards have been completed. if(myCurrentBoardNum >= BoardReader.getNumBoards() + BoardDecoder.getNumDefaultBoards()) { myCurrentBoardNum = 0; } // we create a new decoder object to read and interpret // all of the data for the current board. BoardDecoder decoder = new BoardDecoder(myCurrentBoardNum, myCustomizer); // get the background TiledLayer myWalls = decoder.getLayer(); // get the coordinates of the square that the princess // starts on. int[] playerCoords = decoder.getPlayerSquare(); // the dungeon is a 16x16 grid, so the array playerCoords // gives the player's location in terms of the grid, and // then we multiply those coordinates by the SQUARE_WIDTH // to get the precise pixel where the player should be // placed (in terms of the LayerManager's coordinate system) myPrincess.setPosition(SQUARE_WIDTH * playerCoords[0], SQUARE_WIDTH * playerCoords[1]); myPrincess.setFrame(1); // get the coordinates of the square where the crown // should be placed. int[] goalCoords = decoder.getGoalSquare(); myCrown.setPosition((SQUARE_WIDTH * goalCoords[0]) + (SQUARE_WIDTH/4), (SQUARE_WIDTH * goalCoords[1]) + (SQUARE_WIDTH/2)); // The decoder creates the door and key sprites and places // them in the correct locations in terms of the LayerManager's // coordinate system. myDoors = decoder.createDoors(myKeyImage); myKeys = decoder.createKeys(myKeyImage); for(int i = 0; i < myDoors.length; i++) { append(myDoors[i]); } for(int i = 0; i < myKeys.length; i++) { append(myKeys[i]); } // append the background last so it will be painted first. append(myWalls); append(myBackground); // this sets the view screen so that the player is // in the center. myViewWindowX = SQUARE_WIDTH * playerCoords[0] - ((DISP_WIDTH - SQUARE_WIDTH)/2); myViewWindowY = SQUARE_WIDTH * playerCoords[1] - ((DISP_HEIGHT - SQUARE_WIDTH)/2); // a number of objects are created in order to set up the game,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -