⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gamemap.java

📁 用java编写的魔塔小游戏,经过应用很实用,运行良好
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;

public class GameMap
{
	//when animating,switch the dymatic pic 
	public static final int SWITCH_OFFSET = 44;
	public static final int TILE_WIDTH = 16;
	public static final int TILE_HEIGHT = TILE_WIDTH;//16;
	private static final int TILE_NUM_COL = 11;
	private static final int TILE_NUM_ROW = TILE_NUM_COL;//11;
	public static final int MAP_WIDTH = TILE_WIDTH*TILE_NUM_COL;
	public static final int MAP_HEIGHT = MAP_WIDTH;
	//hold the number of floors
	public static final int FLOOR_NUM = 22;
	//hold the number of tiles
	public static final int TILE_NUM = TILE_NUM_COL*TILE_NUM_ROW;
	//hold the position of hero,upstair = 0,downstair = 1.
	private static final int[][] heroPosition = {
	//floor 0,1,2,3,4...21
		{115,104,11,111,109,111,109,115,11,39,70,111,119,111,104,3,5, 93, 111,119,39,60},
		{16, 1,  99,109,99, 108,103,1,  51,83,99,119,111,114,5,  7,71,111,119,49, 71,60}
		};
	//judge the hero is upstair or downstair
	private int curUpDown = 0;

	//hold the current map that player in
	private int[] curFloorArray = new int[TILE_NUM];
	//hold the index of the floor that player in
	public int curFloorNum = 0;
	public int reachedHighest = 0;
	//public int curRow = 10,curCol = 5;
	private TiledLayer floorMap;
	private HeroSprite hero;
	//hold the ahead cell's index
	private int aheadIndex;
	//hold the ahead row and col;
	private int aheadCol,aheadRow;
	
	
	
	public GameMap(HeroSprite hero)
	{
		this.hero = hero;
		floorMap = new TiledLayer(
			TILE_NUM_COL,TILE_NUM_ROW,
			GameScreen.getImage(GameScreen.IMAGE_MAP),
			TILE_WIDTH,TILE_HEIGHT);
		setMap(curFloorNum);
	}
	
	public void setMap(int floorNum)
	{
		//floorMap.createAnimatedTile(6);
        //floorMap.createAnimatedTile(8);
	/*	switch (floor)
		{
			case 1: curFloor = floor1;break;
			case 2: curFloor = floor2;break;
		}
		
        for (int row=0; row<TILE_NUM_ROW; row++)
      		for (int col=0; col<TILE_NUM_COL; col++) 
      			floorMap.setCell(col,row,curFloor[row][col]);//*/
     /* for (int i=0; i < map.length; i++) {
      int column = i % 10;
      int row = (i - column) / 10;
      floorMap.setCell(column,row,map[i]);
      }*/
      for(int i = 0;i < TILE_NUM;i++)
      {
          curFloorArray[i] = floorArray[floorNum][i];
      }
/*      for (int i = 0;i < curFloorArray.length;i ++)
      {
      	//int column = i % TILE_NUM_COL;
      	//int row = (i - column) / TILE_NUM_ROW;
      	//floorMap.setCell(column,row,curFloorArray[i]);
      	int[] colrow = getColRow(i);
      	floorMap.setCell(colrow[0],colrow[1],curFloorArray[i]);
  		
      }
*/
	  for (int i = 0;i < TILE_NUM;i ++)
      {
      	int[] colrow = getColRow(i);
      	floorMap.setCell(colrow[0],colrow[1],floorArray[curFloorNum][i]);
  		
      }
      int[] colrow = getColRow(heroPosition[curUpDown][floorNum]);
      int x = (int)(colrow[0] * TILE_WIDTH + TILE_WIDTH / 2);
      int y = (int)(colrow[1] * TILE_HEIGHT + TILE_HEIGHT / 2);
      hero.setRefPixelPosition(x,y);
      	
	}
	private int[] getColRow(int index)
	{
		int[] result = new int[2];
		//col
		result[0] = index % TILE_NUM_COL;
		//row
		result[1] = (index - result[0]) / TILE_NUM_ROW;
		return result; 
	}
	public void animateMap()
	{
		/*if (floorMap.getAnimatedTile(-1) == MAP_STAR1)
		{
			floorMap.setAnimatedTile(-1,MAP_STAR2);
		}else
		{
			floorMap.setAnimatedTile(-1,MAP_STAR1);
		}
		
		if (floorMap.getAnimatedTile(-2) == MAP_WATER1)
		{
			floorMap.setAnimatedTile(-2,MAP_WATER2);
		}else
		{
			floorMap.setAnimatedTile(-2,MAP_WATER1);
		}*/
		int switchedCell;
		for(int i = 0;i < TILE_NUM;i ++){
			switchedCell = 0;
			int type = floorArray[curFloorNum][i];
			if(type == MAP_SHOP1){
				switchedCell = MAP_SHOP2;
			}else if(type == MAP_SHOP2){
				switchedCell = MAP_SHOP1;
			}else if(type == MAP_STAR1){
				switchedCell = MAP_STAR2;
			}else if(type == MAP_STAR2){
				switchedCell = MAP_STAR1;
			}else if(type == MAP_WATER1){
				switchedCell = MAP_WATER2;
			}else if(type == MAP_WATER2){
				switchedCell = MAP_WATER1;
			}else if((type >= MAP_ANGLE)&&(type <= MAP_ORGE31)){
				switchedCell =type + SWITCH_OFFSET;
			}else if(type > MAP_ORGE31){
				switchedCell =type - SWITCH_OFFSET;
			}
			if(switchedCell != 0){
				/*int[] colrow = getColRow(i);
				floorArray[curFloorNum][i] = switchedCell;
				floorMap.setCell(colrow[0],colrow[1],switchedCell);*/
				changeCell(i,switchedCell);				
			}
		}
		
	}
	
	public TiledLayer getFloorMap()
	{
		return floorMap;
	}
	
	public int upstair()
	{
		if ((curFloorNum + 1) < FLOOR_NUM)
		{
			curUpDown = 0;
			setMap(++curFloorNum);
			if(curFloorNum > reachedHighest)
				reachedHighest = curFloorNum;
		}
		return curFloorNum;	
	}
	
	public int downstair()
	{
		if ((curFloorNum - 1) >= 0)
		{
			curUpDown = 1;
			setMap(--curFloorNum);
		}
		return curFloorNum;
	}
	//return the value of the index in array
	public int canPass(int direction)
	{
		int col = hero.getRefPixelX() / TILE_WIDTH ;
		int row = hero.getRefPixelY() / TILE_HEIGHT ;
		//curCol = col; curRow = row;
		int result;
		boolean isBound = true;
		//System.out.println(Integer.toString(row)+"col:"+Integer.toString(col));
		//according to the direction to determine the ahead cell
		switch (direction)
		{
			case GameScreen.UP://row = ((row-1)>0) ? row-- : row;break;
				if(row-1>=0){row--;isBound = false;}break;
			case GameScreen.DOWN://row = ((row+1)<TILE_NUM_ROW) ? row++ : row;break;
				if(row+1<TILE_NUM_ROW){row++;isBound = false;}break;
			case GameScreen.LEFT://col = ((col-1)>0) ? col-- : col;break;
				if(col-1>=0){col--;isBound = false;}break;
			case GameScreen.RIGHT://col = ((col+1)<TILE_NUM_COL) ? col++ : col;break;
				if(col+1<TILE_NUM_COL){col++;isBound = false;}break;
		}
		if(isBound == true){result = 0;}else{
			aheadCol = col;
			aheadRow = row;
			aheadIndex = TILE_NUM_ROW * row + col;
	//		result = curFloorArray[aheadIndex];
			result = floorArray[curFloorNum][aheadIndex];
		}
			
		//System.out.println(Integer.toString(row)+"col:"+Integer.toString(col));
		//System.out.println(Integer.toString(curFloorArray[index]));
		return result;
	}
    public void changeCell(int index,int type)
    {
		int[] colrow = getColRow(index);
		floorArray[curFloorNum][index] = type;
		floorMap.setCell(colrow[0],colrow[1],type);		
    }

	//replace the current cell value with 1
	public void remove()
	{
		floorArray[curFloorNum][aheadIndex] = 1;
		floorMap.setCell(aheadCol,aheadRow,1);
	}
	public void remove(int floor,int index)
	{
		floorArray[floor][index] = 1;
	}
	public void remove(int floor,int index,int type)
	{
		floorArray[floor][index] = type;
	}
	public void jump(int floor)
	{
		if (reachedHighest >= floor){
			if(floor >= curFloorNum)curUpDown = 0;
			else curUpDown = 1;
			curFloorNum = floor;
			setMap(curFloorNum);
		}
	}
	//get the array of orge in current floor
	//result[TILE_NUM - 1] hold the number of orge
	public int[] getOrgeArray()
	{
		int[] result = new int[TILE_NUM];
		int type,num = 0;
		boolean repeated = false;
		for(int i = 0;i < TILE_NUM;i ++)
		{
			repeated = false;
			if((type = floorArray[curFloorNum][i]) >= FightCalc.MIN_ORGE_INDEX){
				if (type > FightCalc.MAX_ORGE_INDEX)type -= SWITCH_OFFSET;
				if(type < MAP_ORGE1)continue;
				//detect if repeated or not
				for(int j = 0;j < num;j ++)
				{
					if(result[j] == type){
						repeated = true;
						break;
					}
				}
				if(repeated == false){
					result[num] = type;
					//repeated = true;
					num++;//System.out.println(type+"  num="+num);
				}
			}
			
		}
		result[TILE_NUM-1] = num;
		return result;
	}
	
	public byte[] getFloorArray(int floor)
	{
		byte[] result = new byte[TILE_NUM];
		int type;
		for(int i = 0;i < TILE_NUM;i ++)
		{
			type = 	floorArray[floor][i];
			if (type > FightCalc.MAX_ORGE_INDEX)type -= SWITCH_OFFSET;
			result[i] = (byte)type;
		}
		return result;
	}
	
	public void setFloorArray(int floor,byte[] data)
	{
		for(int i = 0;i < TILE_NUM;i ++)
		{
			floorArray[floor][i] = data[i];
		}
	}
	
	private int[][] floorArray =
	{
//floor0
/*{
2,-1,-1,-1,-1,11,-1,-1,-1,-1,2,
2,-1,-1,-1,-1,1,-1,-1,-1,-1,2,
2,-1,-1,-1,-1,1,-1,-1,-1,-1,2,
2,-1,-1,-1,-1,1,-1,-1,-1,-1,2,
2,-1,-1,-1,-1,1,-1,-1,-1,-1,2,
2,-1,-1,-1,-1,1,-1,-1,-1,-1,2,
2,2,-1,-1,-1,1,-1,-1,-1,2,2,
2,2,2,2,2,1,2,2,2,2,2,
-2,2,-2,2,1,1,1,2,-2,2,-2,
-2,-2,-2,-2,-2,1,-2,-2,-2,-2,-2,
-2,-2,-2,-2,-2,1,-2,-2,-2,-2,-2			
},*/
{
2,6,6,6,6,11,6,6,6,6,2,
2,6,6,6,6,1,6,6,6,6,2,
2,6,6,6,6,1,6,6,6,6,2,
2,6,6,6,6,1,6,6,6,6,2,
2,6,6,6,6,1,6,6,6,6,2,
2,6,6,6,6,1,6,6,6,6,2,
2,2,6,6,6,1,6,6,6,2,2,
2,2,2,2,2,13,2,2,2,2,2,
8,2,8,2,1,45,1,2,8,2,8,
8,8,8,8,8,1,8,8,8,8,8,
8,8,8,8,8,1,8,8,8,8,8
},
//floor1 
{
11,1,19,50,51,50,1,1,1,1,1,
2,2,2,2,2,2,2,2,2,2,1,
23,1,53,13,1,2,23,19,23,2,1,
19,53,25,2,1,2,23,19,23,2,1,
2,13,2,2,1,2,2,2,54,2,1,
19,55,1,2,1,13,56,50,52,2,1,
26,1,20,2,1,2,2,2,2,2,1,
2,13,2,2,1,1,1,1,1,1,1,
1,55,1,2,2,15,2,2,2,13,2,
23,24,19,2,21,1,1,2,19,58,20,
23,36,19,2,1,12,1,2,19,19,19,		
},
//floor2
{
12,2,1,74,1,2,25,26,19,21,2,
1,2,26,2,24,2,25,26,19,20,2,
1,2,19,2,19,2,25,26,19,67,2,
1,2,19,2,19,2,2,2,2,13,2,
1,2,1,2,1,1,1,13,1,1,2,
1,2,13,2,2,13,2,2,13,2,2,
1,5,1,1,1,1,2,1,67,1,2,
1,2,13,2,2,14,2,16,2,16,2,
1,2,19,2,24,23,2,1,2,1,2,
1,2,19,2,24,23,2,1,2,1,2,
11,2,25,2,24,23,2,48,2,49,2
},
//floor3
{
27,51,19,2,3,17,4,2,2,2,2,
51,19,1,2,1,1,1,2,1,52,1,
19,53,1,2,2,13,2,2,1,2,1,

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -