?? board.java
字號:
package isoj2me;import javax.microedition.lcdui.*;import java.util.Hashtable;import java.util.Vector;import java.util.Enumeration;/** * <p>Title: isoj2me: J2ME Isometric Engine</p> * <p>Description: An engine/framework for isometric games (like japanese RPGs) for mobile devices supporting J2ME (MIDP 1.0). This engine will manage maps, objects and characters. Visit http://sourceforge.net/projects/isoj2me</p> * <p>Copyright: Copyright (c) 2004</p> * <p>License: Lesser GPL (http://www.gnu.org)</p> * <p>Company: Mondonerd.com</p> * @author Massimo Maria Avvisati * @version 0.2 */public class Board { public Map map; public static Hashtable tiles = new Hashtable(20); private int tileWidth, tileHeight; private Hashtable charactersPositions = new Hashtable(20); private Hashtable itemsPositions = new Hashtable(20); private Vector characters = new Vector(); private Vector items = new Vector(); public String tileSet = ""; /** * 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 */ public Board(String tileSet) { this.tileSet = tileSet; } /** * After adding a Character to the Board you'll be able to get it as an object using this method. * @param name name of the Character you want to get * @return Character with that name */ public isoj2me.Character getCharacter(String name) { Enumeration list = characters.elements(); while (list.hasMoreElements()) { isoj2me.Character temp_c = (isoj2me.Character) list.nextElement(); if (temp_c.name.equals(name)) { return temp_c; } } return null; } public isoj2me.Item getItem(String name) { Enumeration list = items.elements(); while (list.hasMoreElements()) { isoj2me.Item temp_i = (isoj2me.Item) list.nextElement(); if (temp_i.name.equals(name)) { return temp_i; } } return null; } /** * Add a Character to the Board. * @param c Character object to add */ public void putCharacter(isoj2me.Character c) { charactersPositions.put(c.x + "_" + c.y + "_" + c.z, c); characters.addElement(c); } public void putItem(isoj2me.Item i) { itemsPositions.put(i.x + "_" + i.y + "_" + i.z, i); items.addElement(i); } /** * Create and then add a Character to the Board. * @param name name of the Character * @param x x * @param y y * @param z z */ public void putCharacter(String name, int x, int y, int z) { isoj2me.Character c = new isoj2me.Character(name, x, y, z); charactersPositions.put(x + "_" + y + "_" + z, c); characters.addElement(c); } /** * If there is a Character in the x, y, z position of the Board it returns its name. Return null if there is no character. * @param x x * @param y y * @param z z * @return Character name */ public String isCharacter(int x, int y, int z) { String result = null; if (charactersPositions.containsKey(x + "_" + y + "_" + z)) { result = charactersPositions.get(x + "_" + y + "_" + z).toString(); } return result; } public String isItem(int x, int y, int z) { String result = null; if (itemsPositions.containsKey(x + "_" + y + "_" + z)) { result = itemsPositions.get(x + "_" + y + "_" + z).toString(); } return result; } /** * Move a Character on the Board * @param c Character to move * @param oldX old x * @param oldY old y * @param oldZ old z * @param newX new x * @param newY new y * @param newZ new z * @param overwrite true if you want to overwrite an existing Character that stays in the new position * @return true if the movement was done */ public boolean moveCharacter(isoj2me.Character c, int oldX, int oldY, int oldZ, int newX, int newY, int newZ, boolean overwrite) { if (charactersPositions.containsKey(oldX + "_" + oldY + "_" + oldZ)) { if (overwrite != true) { if (!charactersPositions.containsKey(newX + "_" + newY + "_" + newZ)) { charactersPositions.put(newX + "_" + newY + "_" + newZ, c); charactersPositions.remove(oldX + "_" + oldY + "_" + oldZ); return true; } else { return false; } } else { charactersPositions.put(newX + "_" + newY + "_" + newZ, charactersPositions.get(oldX + "_" + oldY + "_" + oldZ)); charactersPositions.remove(oldX + "_" + oldY + "_" + oldZ); } return true; } else { return false; } } public boolean moveItem(isoj2me.Item i, int oldX, int oldY, int oldZ, int newX, int newY, int newZ, boolean overwrite) { if (itemsPositions.containsKey(oldX + "_" + oldY + "_" + oldZ)) { if (overwrite != true) { if (!itemsPositions.containsKey(newX + "_" + newY + "_" + newZ)) { itemsPositions.put(newX + "_" + newY + "_" + newZ, i); itemsPositions.remove(oldX + "_" + oldY + "_" + oldZ); return true; } else { return false; } } else { itemsPositions.put(newX + "_" + newY + "_" + newZ, charactersPositions.get(oldX + "_" + oldY + "_" + oldZ)); itemsPositions.remove(oldX + "_" + oldY + "_" + oldZ); } return true; } else { return false; } } /** * This method change the shape of the basic tile. This affect the coordinates of the tiles painted by the <b>draw()</b> method * @param width the width of the tile * @param height the distance between the base of the tile file and the "floor" of the basic tile */ public void setTileSize(int width, int height) { tileWidth = width; tileHeight = height; } /** * 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); } /** * 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 void draw(int x, int y, int cellX, int cellY, int width, int height, Graphics g) { if (cellX < 0) { cellX = 0; } if (cellY < 0) { cellY = 0; } if (cellX > map.getWidth()) { cellX = map.getWidth(); } if (cellY > map.getHeight()) { cellY = map.getHeight(); } int x_temp, y_temp; x_temp = x; y_temp = y; int x_init = x_temp; boolean iso = true; int y_temp2 = 0; for (int i = 0; i < height; i++) { y_temp = y + tileHeight * i; x_temp = x_init - (tileWidth / 2) * i; for (int j = 0; j < width; j++) { y_temp2 = y_temp; for (int k = 0; k < map.numberOfLayers; k++) { y_temp = y_temp2 - (tileHeight * k); if (map.layers[k] != null) { if (i + cellY < map.getHeight() && j + cellX < map.getWidth()) { g.drawImage(Utility.loadTile(tileSet + map.layers[k][ ( (i + cellY) * map.getWidth()) + (j + cellX)], tiles), x_temp, y_temp - (Utility.loadTile(tileSet + map.layers[k][ ( (i + cellY) * map.getWidth()) + (j + cellX)], tiles). getHeight() - tileHeight), g.TOP | g.LEFT); } } if (charactersPositions.containsKey( (j + cellX) + "_" + (i + cellY) + "_" + k)) { isoj2me.Character temp_character = (isoj2me.Character) charactersPositions.get( (j + cellX) + "_" + (i + cellY) + "_" + k); int temp_char_x = x_temp + (tileWidth - temp_character.getCurrentImage().getWidth()) / 2; temp_character.draw(temp_char_x, y_temp - tileHeight , g); } if (itemsPositions.containsKey( (j + cellX) + "_" + (i + cellY) + "_" + k)) { isoj2me.Item temp_character = (isoj2me.Item) itemsPositions.get( (j + cellX) + "_" + (i + cellY) + "_" + k); int temp_char_x = x_temp + (tileWidth - temp_character.getCurrentImage().getWidth()) / 2; temp_character.draw(temp_char_x, y_temp - tileHeight , g); } } y_temp = y_temp2; x_temp = x_temp + tileWidth / 2; y_temp = y_temp + tileHeight; } } }}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -