📄 zontilemap.java
字号:
package zonja;
import java.io.IOException;
import java.io.InputStream;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
public class ZonTileMap extends ZonLayer {
protected int tileWidth = 0;
protected int tileHeight = 0; //
protected int mapWidth = 0; // the vertical tile number
protected int mapHeight = 0;
/* private byte [] bufPast = new byte [9];
private byte [] bufCurrent = new byte [9];*/
/* private byte [] bufPast = new byte[12];
private byte [] bufCurrent = new byte[12];*/
/* public int bufDrawOffsetX = 0;
public int bufDrawOffsetY = 0;
public static int BUF_TILECOUNTX = 9;
public static int BUF_TILECOUNTY = 16;
public boolean reDraw = true;*/
private char [] mapTileData16Bit;
private Image [] imageMap = null;
// Constructor
public ZonTileMap(int scrWidth, int scrHeight) {
super(scrWidth, scrHeight);
}
// inherited
/* private void calculateBuf(int rowHOffset){
int indexYinBuf = bufDrawOffsetX / tileWidth;
if(reDraw){
for(int i = 0; i < BUF_TILECOUNTX; i++){
bufCurrent[(indexYinBuf + i) % BUF_TILECOUNTX] =(byte)(rowHOffset + i);
}
}else{
for(int i = 0; i < indexYinBuf; i++){
bufCurrent[i] =(byte)(rowHOffset + i + (BUF_TILECOUNTX - indexYinBuf));
}
bufCurrent[indexYinBuf] = (byte)rowHOffset;
if(indexYinBuf == 0)
bufCurrent[BUF_TILECOUNTX -1] = (byte)(rowHOffset + BUF_TILECOUNTX -1);
else
bufCurrent[indexYinBuf - 1] = (byte)(rowHOffset + BUF_TILECOUNTX -1);
}
}*/
/* void paint(Graphics g, boolean bReverse, boolean bWrap) {
int arrayIndexX = 0, arrayIndexY = 0;
int arrayIndex = 0;
int tileIndex = 0;
int currentX = 0;
int currentY = 0;
int hTileCount = BUF_TILECOUNTX;
int vTileCount = BUF_TILECOUNTY;
int beginPosX = 0;
int beginPosY = 0;
arrayIndexY = -offsetX / tileWidth;
arrayIndexX = 0;
bufDrawOffsetX = (-offsetX) % (BUF_TILECOUNTX * tileWidth);
bufDrawOffsetY = 0;
calculateBuf(arrayIndexY);
if(reDraw){
reDraw = false;
for(int i = 0; i < bufPast.length; i++){
bufPast[i] = bufCurrent[i];
}
for(int j = 0; j < hTileCount; j++){
currentX = (beginPosX + j * tileWidth) % (hTileCount * tileWidth);
for(int i = 0; i < vTileCount; i++){
currentY = beginPosY + i * tileHeight;
g.setClip(currentX, currentY, tileWidth, tileHeight);
arrayIndex = (int)(mapTileData16Bit[((arrayIndexX + i) % mapHeight)* mapWidth + (bufCurrent[j]) % mapWidth]);
int x = 0, y = 0;
tileIndex = arrayIndex & 0x00FF;
int imageIndex = (arrayIndex & 0x0F00) >> 8;
x = currentX;
y = currentY - (tileIndex & 0x00FF) * tileHeight;
if((0x00FF & tileIndex) == 0x0000 || imageIndex == 0){
g.drawImage(imageMap[0], currentX, currentY, Graphics.TOP | Graphics.LEFT);
}else{
g.drawImage(imageMap[imageIndex], x, y, Graphics.TOP | Graphics.LEFT);
}
}
}
return;
}
for(int j = 0; j < hTileCount; j++){
currentX = (beginPosX + j * tileWidth) % (hTileCount * tileWidth);
if(bufPast[j] == bufCurrent[j]){
continue;
} else {
bufPast[j] = bufCurrent[j];
}
for(int i = 0; i < vTileCount; i++){
currentY = beginPosY + i * tileHeight;
g.setClip(currentX, currentY, tileWidth, tileHeight);
arrayIndex = (int)(mapTileData16Bit[((arrayIndexX + i) % mapHeight)* mapWidth + (bufCurrent[j]) % mapWidth]);
int x = 0, y = 0;
tileIndex = arrayIndex & 0x00FF;
int imageIndex = (arrayIndex & 0x0F00) >> 8;
x = currentX;
y = currentY - (tileIndex & 0x00FF) * tileHeight;
if((0x00FF & tileIndex) == 0x0000 || imageIndex == 0){
g.drawImage(imageMap[0], currentX, currentY, Graphics.TOP | Graphics.LEFT);
}else{
g.drawImage(imageMap[imageIndex], x, y, Graphics.TOP | Graphics.LEFT);
}
}
}
}*/
void paint(Graphics g, boolean bReverse, boolean bWrap) {
//simulate planar Array
int arrayIndexX = 0, arrayIndexY = 0;
int arrayIndex = 0;
int tileIndex = 0;
int currentX = 0;
int currentY = 0;
int hTileCount = (scrWidth + tileWidth) / tileWidth + 1;
int vTileCount = (scrHeight + tileHeight) / tileHeight + 1;
int beginPosX = 0;
int beginPosY = 0;
if(bWrap){
if(offsetX > 0){
arrayIndexX = - offsetX / tileWidth - 1;
beginPosX = (offsetX % tileWidth) - tileWidth;
}else{
arrayIndexX = - offsetX / tileWidth;
beginPosX = offsetX % tileWidth;
}
if(offsetY > 0){
arrayIndexY = - offsetY / tileHeight - 1;
beginPosY = (offsetY % tileHeight) - tileHeight;
}else{
arrayIndexY = - offsetY / tileHeight;
beginPosY = offsetY % tileHeight;
}
while(arrayIndexX < 0)
arrayIndexX += mapWidth;
while(arrayIndexY < 0)
arrayIndexY += mapHeight;
}else{
if(offsetX > 0){
arrayIndexY = 0;
beginPosX = offsetX;
}else{
arrayIndexY = - offsetX / tileWidth;
beginPosX = offsetX % tileWidth;
}
if(offsetY > 0){
arrayIndexX = 0;
beginPosY = offsetY;
}else{
arrayIndexX = - offsetY / tileHeight;
beginPosY = offsetY % tileHeight;
}
}
for(int i = 0; i < vTileCount; i++){
currentY = beginPosY + i * tileHeight;
for(int j = 0; j < hTileCount; j++){
currentX = beginPosX + j * tileWidth;
g.setClip(currentX, currentY, tileWidth, tileHeight);
arrayIndex = (int)(mapTileData16Bit[((arrayIndexX + i) % mapHeight)* mapWidth + (arrayIndexY + j) % mapWidth]);
int x = 0, y = 0;
tileIndex = arrayIndex & 0x00FF;
int imageIndex = (arrayIndex & 0x0F00) >> 8;
x = currentX;
y = currentY - (tileIndex & 0x00FF) * tileHeight;
// g.drawImage(images[0], currentX, currentY - tileHeight * tileIndex, Graphics.TOP | Graphics.LEFT);
if((0x00FF & tileIndex) == 0x0000 || imageIndex == 0){
//g.drawImage(imageMap[0], currentX, currentY, Graphics.TOP | Graphics.LEFT);
}else{
g.drawImage(imageMap[imageIndex], x, y, Graphics.TOP | Graphics.LEFT);
}
}
}
//g.translate(- g.getTranslateX(), - g.getTranslateY());
}
//initialize
public boolean initMap(String picNameBase, int picCount, int tileWidth,
int tileHeight, boolean buildReverse) {
imageMap = null;
imageMap = new Image[picCount];
imageMap[0] = findImage(picNameBase + 0);
imageMap[4] = findImage(picNameBase + 4);
switch(ZonCanvas.gameStage){
case ZonCanvas.GAMESTAGE_ONE:
imageMap[1] = findImage(picNameBase + 1);
imageMap[5] = findImage(picNameBase + 5);
break;
case ZonCanvas.GAMESTAGE_TWO:
imageMap[2] = findImage(picNameBase + 2);
imageMap[6] = findImage(picNameBase + 6);
break;
case ZonCanvas.GAMESTAGE_THREE:
imageMap[3] = findImage(picNameBase + 3);
imageMap[6] = findImage(picNameBase + 6);
break;
}
/* for(int i = 0; i < picCount; i ++) {
imageMap[i] = findImage(picNameBase + i);
if(imageMap[i] == null)
return false;
}*/
setTileSize(tileWidth, tileHeight);
return true;
}
//methods
public void setTileSize(int width, int height) {
tileWidth = width;
tileHeight = height;
}
public int getTileWidth() {
return tileWidth;
}
public int getTileHeight() {
return tileHeight;
}
public void setMapSize(int width, int height) {
mapWidth = width;
mapHeight = height;
}
public int getMapWidth() {
return mapWidth;
}
public int getMapHeight() {
return mapHeight;
}
public void setTileData16Bit(int x, int y, int data) {
mapTileData16Bit[y * mapWidth + x] = (char)(data & 0xFFFF);
}
public int getTileData(int x, int y) {
return mapTileData16Bit[y * mapWidth + x];
}
public void setMapArray(final char[] array){
mapTileData16Bit = array;
}
//high 8 bit present the picname:
//low 4 bits is index of image, 0x01XX is png16_1
//high 4 bit present type of tile 1:wall 2:solic box 3:brick 4:water 5: ground
//low 8 bits is the index of sprite
public boolean loadMapData(String mapName) {
try {
InputStream is = getClass().getResourceAsStream(mapName);
mapTileData16Bit = null;
mapTileData16Bit = new char[2048];
byte [] tempArray = new byte[4096];
is.read(tempArray);
is.close();
is = null;
for(int i = 0; i < mapTileData16Bit.length; i++){
mapTileData16Bit[i] = (char)((tempArray[2 * i] << 8) | tempArray[2 * i + 1]);
}
tempArray = null;
} catch (IOException e) {
System.out.println(e);
} catch (Exception e) {
System.out.println(e);
}
System.gc();
return true;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -