?? board.java
字號:
package isoj2me;import java.util.*;import javax.microedition.lcdui.*;/** * <p> * isoj2me: Board * </p> * <p> * This is the main class. It manages a Board where you can add, remove and move * Characters and Items. It manages Maps too. This class perform the draw method * to paint the Board on the device * </p> * <p> * Copyright: Copyright (c) 2006 * </p> * <p> * License: Lesser GPL (http://www.gnu.org) * </p> * <p> * Company: <a href=http://www.mondonerd.com target="_blank">Mondonerd.com</a> * </p> * * @author Massimo Maria Avvisati * @version 0.5 */public class Board { public static Hashtable tiles = new Hashtable(12); //TODO this value have to be a variable to optimize the result /** * This one of the main method of this class. It paint on the given Graphics * the board with its tiles and characters. * * @param x * where to paint on the Graphics (x) * @param y * where to paint on the Graphics (y) * @param cellX * starting cell on the map (x) * @param cellY * starting cell on the mpa (y) * @param width * width of the map area to be painted (starting from cellX) * @param height * height of the map area to be painted (starting from cellY) * @param g * Graphics object where to paint */ public int cellX = 0; public int cellY = 0; public int charactersClipWidth = 60; public int charactersClipHeight = 60; public Hashtable characters = new Hashtable(1); private Hashtable charactersPositions = new Hashtable(1);// public Hashtable items = new Hashtable();//// private Hashtable itemsPositions = new Hashtable(20); public Map map; public String tileSet = ""; public int tileWidth, tileHeight; public int width, height; /** * Constructor. The tileset basic name will be an empty string. */ public Board() { } /** * Constructor. The tileset basic name will be tileSet and it will be used * when loading tiles image files. * * @param tileSet * basic name for tile files */ private Canvas canvas = null; public Board(String tileSet, Canvas canvas) { this.canvas = canvas; this.tileSet = tileSet; } /** * This method call the Map() constructor passing the base name for the map * files and the number of layers * * @param baseFilename * base name for the map files * @param numberOfLayers * number of layers to load */ public void createMap(String baseFilename, int numberOfLayers) { map = new Map(baseFilename, numberOfLayers); } boolean lastDrawn = false; int initI = 0; int lastI = 30; public void drawTile(int mapX, int mapY, int x, int y, Graphics g) { Image tempImage = null; if (map.layers[0][(mapY * map.getWidth()) + mapX] != 48) { try { String imageCode = "" + map.layers[0][(mapY * map.getWidth()) + mapX]; tempImage = Utility.loadImage(tileSet + imageCode, tiles); } catch (Exception e) { } if (tempImage != null) { g.drawImage(tempImage, x - (tempImage.getWidth() / 2), y - tempImage.getHeight() + tileHeight * 2, Graphics.TOP | Graphics.LEFT); } } } public void drawCharacter(int mapX, int mapY, int x, int y, Graphics g) { // Paint characters if (charactersPositions.containsKey(mapX + "_" + mapY + "_0")) { isoj2me.Character tempCharacter = (isoj2me.Character) charactersPositions .get(mapX + "_" + mapY + "_0"); int tempCharX = x + (-tempCharacter.getCurrentImage().getWidth()) / 2; if ("player".equals(tempCharacter.name)) { int xMod = 0; int yMod = 0;// if (tempCharacter.speed > 2) {// tempCharacter.speed = 2;// } if (tempCharacter.getCurrentAction().indexOf("SW") > 0) { // System.out.println("SW"); if (tempCharacter.speed > 2) { // System.out.println("Step 1"); tempCharacter.speed = 2; xMod = +11; yMod = -5; } else if (tempCharacter.speed > 1) { // System.out.println("Step 2"); tempCharacter.speed = 1; xMod = +8; yMod = -4; } else if (tempCharacter.speed > 0) { // System.out.println("Step 3"); tempCharacter.speed = 0; tempCharacter.isMoving = false; } } else if (tempCharacter.getCurrentAction().indexOf("NW") > 0) { // System.out.println("NW"); if (tempCharacter.speed > 2) { // System.out.println("Step 1"); tempCharacter.speed = 2; xMod = +11; yMod = +5; } else if (tempCharacter.speed > 1) { // System.out.println("Step 2"); tempCharacter.speed = 1; xMod = +8; yMod = +4; } else if (tempCharacter.speed > 0) { // System.out.println("Step 3"); tempCharacter.speed = 0; tempCharacter.isMoving = false; } } else if (tempCharacter.getCurrentAction().indexOf("SE") > 0) { // System.out.println("SE"); if (tempCharacter.speed > 2) { // System.out.println("Step 1"); tempCharacter.speed = 2; xMod = -11; yMod = -5; } else if (tempCharacter.speed > 1) { // System.out.println("Step 2"); tempCharacter.speed = 1; xMod = -8; yMod = -4; } else if (tempCharacter.speed > 0) { // System.out.println("Step 3"); tempCharacter.speed = 0; tempCharacter.isMoving = false; } } else if (tempCharacter.getCurrentAction().indexOf("NE") > 0) { // System.out.println("NE"); if (tempCharacter.speed > 2) { // System.out.println("Step 1"); tempCharacter.speed = 2; xMod = -11; yMod = +5; } else if (tempCharacter.speed > 1) { // System.out.println("Step 2"); tempCharacter.speed = 1; xMod = -8; yMod = +4; } else if (tempCharacter.speed > 0) { // System.out.println("Step 3"); tempCharacter.speed = 0; tempCharacter.isMoving = false; } } tempCharacter.draw(tempCharX + xMod, y + yMod, g); tempCharacter.lastDrawnX = tempCharX + xMod + tempCharacter.getCurrentImage().getWidth() / 2; tempCharacter.lastDrawnY = y + yMod - tempCharacter.getCurrentImage().getHeight() / 2 + tempCharacter.modifierY; } else { tempCharacter.draw(tempCharX, y, g); tempCharacter.lastDrawnX = tempCharX + tempCharacter.getCurrentImage().getWidth() / 2; tempCharacter.lastDrawnY = y - tempCharacter.getCurrentImage().getHeight() / 2 + tempCharacter.modifierY; tempCharacter.speed = 0; tempCharacter.isMoving = false; } } }// private void drawItem(int mapX, int mapY, int x, int y, Graphics g) {// // Paint items//// if (itemsPositions.containsKey(mapX + "_" + mapY + "_0")) {// Item tempItem = (Item) itemsPositions.get(mapX + "_" + mapY + "_0");// Image tempImage = tempItem.getCurrentImage();// g.drawImage(tempImage, x - (tempImage.getWidth() / 2), y// - tempImage.getHeight() + tileHeight * 2, Graphics.TOP// | Graphics.LEFT);// }// } public Image cachedBoard = null; public boolean changed = true; // Image characterBoard = null; //Image cuttedBoard = null; //int characterCounter = 0; private int clipWidth; private int clipHeight; public void draw(int x, int y, Graphics gInit, boolean centered) { Character c = null; if (!centered) { cachedBoard = Image.createImage(gInit.getClipWidth(), canvas.getHeight()); drawEmpty(x, y, cachedBoard.getGraphics(), true); gInit.drawImage(cachedBoard, 0, 0, Graphics.TOP | Graphics.LEFT); } else { if (changed || cachedBoard == null) { cachedBoard = Image.createImage(gInit.getClipWidth(), canvas.getHeight()); drawEmpty(x, y, cachedBoard.getGraphics(), false); gInit .drawImage(cachedBoard, 0, 0, Graphics.TOP | Graphics.LEFT); changed = false; } else { gInit .drawImage(cachedBoard, 0, 0, Graphics.TOP | Graphics.LEFT); Enumeration charEnum = characters.elements(); int charactersCounter = 0; for (Enumeration iter = charEnum; iter.hasMoreElements();) { c = (Character) iter.nextElement(); //if (c.x >= cellX && c.y >= cellY && c.x < cellX + width && c.y < cellY + height) { //TODO verify if (true) { charactersCounter++; clipWidth = gInit.getClipWidth(); clipHeight = canvas.getHeight(); int clipWidthTemp = charactersClipWidth; //TODO configuration int clipHeightTemp = charactersClipHeight; //TODO configuration gInit.setClip(c.lastDrawnX - clipWidthTemp / 2, c.lastDrawnY - clipHeightTemp / 2, clipWidthTemp, clipHeightTemp); drawCharacterBoard(c, x, y, gInit); // characterboard on // draw gInit.setClip(0, 0, clipWidth, clipHeight); } } } } } boolean repaint = true; public void drawEmpty(int x, int y, Graphics gInit, boolean withPlayer) { gInit.setColor(0x000000); gInit.fillRect(0, 0, gInit.getClipWidth(), canvas.getHeight()); boolean firstPainted = false; int xTemp = x; int yTemp = 0; int xInit = x; lastDrawn = false; //int lastItemp = lastI; for (int i = 0; i < height; i++) { // y cycle (counter is "i") yTemp = y + tileHeight * i; xTemp = xInit - (tileWidth / 2) * i; for (int j = 0; j < height; j++) { if (onScreen(xTemp - tileWidth / 2, yTemp, gInit) && (j + cellX < map.getWidth()) && (i + cellY) < map.getHeight() && (j + cellX >= 0) && (i + cellY >= 0)) { //TODO verify// && (map.getCell(0, j + cellX, i + cellY) > 65 || "player".equals(isCharacter(j + cellX, i + cellY, 0))) if (firstPainted != true) { firstPainted = true; } if (withPlayer) { drawCharacter(j + cellX, i + cellY, xTemp, yTemp, gInit); } else { drawTile(j + cellX, i + cellY, xTemp, yTemp, gInit); } if (!lastDrawn) { lastI = i + 1; } } xTemp = xTemp + tileWidth / 2;
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -