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

📄 tiledlayer.java

📁 手机射击游戏源代码,nokia s60模拟器开发包,eclipse工具开发.不可用于商业用途.
💻 JAVA
字号:
package src;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

public class TiledLayer extends Layer
{
	int screenW,screenH,tileWidth,tileHeight;//**************************************
	public TiledLayer(int columns, int rows, Image image, int tileWidth,int tileHeight) 
	{
		super(columns >= 1 && tileWidth >= 1 ? columns * tileWidth : -1,
				rows >= 1 && tileHeight >= 1 ? rows * tileHeight : -1);
		updated = true;
		x_src = null;
		y_src = null;
		x_dest = null;
		y_dest = null;
		screenW=screenH=0;//***********************************
		this.tileWidth=tileWidth;//***********************************
		this.tileHeight=tileHeight;//*************************
		if (image.getWidth() % tileWidth != 0|| image.getHeight() % tileHeight != 0) 
		{
			throw new IllegalArgumentException();
		} 
		else 
		{
			this.columns = columns;
			this.rows = rows;
			cellMatrix = new int[rows][columns];
			int noOfFrames = (image.getWidth() / tileWidth)* (image.getHeight() / tileHeight);
			createStaticSet(image, noOfFrames + 1, tileWidth, tileHeight, true);
			return;
		}
	}
	public void setScreen(int w,int h)//************************************
	{
		screenW=w;screenH=h;
	}
	
//	public int createAnimatedTile(int staticTileIndex) 
//	{
//		if (staticTileIndex < 0 || staticTileIndex >= numberOfTiles)
//			throw new IndexOutOfBoundsException();
//		if (anim_to_static == null) 
//		{
//			anim_to_static = new int[4];
//			numOfAnimTiles = 1;
//		} 
//		else if (numOfAnimTiles == anim_to_static.length)
//		{
//			int new_anim_tbl[] = new int[anim_to_static.length * 2];
//			System.arraycopy(anim_to_static, 0, new_anim_tbl, 0,
//					anim_to_static.length);
//			anim_to_static = new_anim_tbl;
//		}
//		anim_to_static[numOfAnimTiles] = staticTileIndex;
//		numOfAnimTiles++;
//		
//		return -(numOfAnimTiles - 1);
//	}

//	public void setAnimatedTile(int animatedTileIndex, int staticTileIndex) 
//	{
//		if (staticTileIndex < 0 || staticTileIndex >= numberOfTiles)
//			throw new IndexOutOfBoundsException();
//		animatedTileIndex = -animatedTileIndex;
//		if (anim_to_static == null || animatedTileIndex <= 0
//				|| animatedTileIndex >= numOfAnimTiles) 
//		{
//			throw new IndexOutOfBoundsException();
//		}
//		else 
//		{
//			anim_to_static[animatedTileIndex] = staticTileIndex;
//			return;
//		}
//	}

	public int getAnimatedTile(int animatedTileIndex) 
	{
		animatedTileIndex = -animatedTileIndex;
		if (anim_to_static == null || animatedTileIndex <= 0
				|| animatedTileIndex >= numOfAnimTiles)
			throw new IndexOutOfBoundsException();
		else
			return anim_to_static[animatedTileIndex];
	}

	public void setCell(int col, int row, int tileIndex)
	{
		if (col < 0 || col >= columns || row < 0 || row >= rows)
			throw new IndexOutOfBoundsException();
		if (tileIndex > 0) 
		{
			if (tileIndex >= numberOfTiles)
				throw new IndexOutOfBoundsException();
		} 
		else if (tileIndex < 0&& (anim_to_static == null || -tileIndex >= numOfAnimTiles))
			throw new IndexOutOfBoundsException();
		cellMatrix[row][col] = tileIndex;
		updated = true;
	}

	public int getCell(int col, int row) 
	{
		if (col < 0 || col >= columns || row < 0 || row >= rows)
			throw new IndexOutOfBoundsException();
		else
			return cellMatrix[row][col];
	}

//	public void fillCells(int col, int row, int numCols, int numRows,int tileIndex) 
//	{
//		if (numCols < 0 || numRows < 0)
//			throw new IllegalArgumentException();
//		if (col < 0 || col >= columns || row < 0 || row >= rows
//				|| col + numCols > columns || row + numRows > rows)
//			throw new IndexOutOfBoundsException();
//		if (tileIndex > 0) 
//		{
//			if (tileIndex >= numberOfTiles)
//				throw new IndexOutOfBoundsException();
//		}
//		else if (tileIndex < 0&& (anim_to_static == null || -tileIndex >= numOfAnimTiles))
//			throw new IndexOutOfBoundsException();
//		for (int rowCount = row; rowCount < row + numRows; rowCount++)
//		{
//			for (int columnCount = col; columnCount < col + numCols; columnCount++)
//				cellMatrix[rowCount][columnCount] = tileIndex;
//		}
//		updated = true;
//	}

	public final int getCellWidth() 
	{
		return cellWidth;
	}

	public final int getCellHeight()
	{
		return cellHeight;
	}

	public final int getColumns() 
	{
		return columns;
	}

	public final int getRows() 
	{
		return rows;
	}

//	public void setStaticTileSet(Image image, int tileWidth, int tileHeight) 
//	{
//		updated = true;
//		if (tileWidth < 1 || tileHeight < 1
//			|| image.getWidth() % tileWidth != 0
//			|| image.getHeight() % tileHeight != 0)
//			throw new IllegalArgumentException();
//		setWidthImpl(columns * tileWidth);
//		setHeightImpl(rows * tileHeight);
//		int noOfFrames = (image.getWidth() / tileWidth)
//				* (image.getHeight() / tileHeight);
//		if (noOfFrames >= numberOfTiles - 1)
//			createStaticSet(image, noOfFrames + 1, tileWidth, tileHeight, true);
//		else
//			createStaticSet(image, noOfFrames + 1, tileWidth, tileHeight, false);
//	}

	public final void paint(Graphics g)
	{
		this.paint(g, false);
	}
	
	public final void paintLookDown(Graphics g)
	{
		this.paint(g, true);
	}
	private final void paint(Graphics g, boolean lookDown) 
	{
		if (g == null)
			throw new NullPointerException();
		if (visible)
		{
			int tileIndex = 0;
			if (updated) 
			{
				int arrayLength = 0;
				for (int row = 0; row < cellMatrix.length; row++) 
				{
					int totalCols = cellMatrix[row].length;
					for (int column = 0; column < totalCols; column++) 
					{
						tileIndex = cellMatrix[row][column];
						if (tileIndex != 0)
							arrayLength++;
					}
				}
				if (x_src == null || arrayLength > x_src.length) 
				{
					x_src = new int[arrayLength];
					y_src = new int[arrayLength];
					x_dest = new int[arrayLength];
					y_dest = new int[arrayLength];
				}
				tileArrayLength = arrayLength;	
				updated = false;//*********************************
			}//**************************************************			
				//**************************
			int ty = lookDown?y - this.cellHeight/2:y;
			int arrayIndex = 0;
			for (int row = 0; row < cellMatrix.length;) 
			{
				int tx = lookDown?x-this.cellWidth/2:x;
				int totalCols = cellMatrix[row].length;
				for (int column = 0; column < totalCols;) 
				{
					tileIndex = cellMatrix[row][column];
					if (tileIndex != 0) 
					{
						if (tileIndex < 0)
							tileIndex = getAnimatedTile(tileIndex);
						x_src[arrayIndex] = tileSetX[tileIndex];
						y_src[arrayIndex] = tileSetY[tileIndex];
						x_dest[arrayIndex] = tx;
						y_dest[arrayIndex] = ty;
						arrayIndex++;
					}
					column++;
					tx += lookDown?cellWidth/2:cellWidth;
				}

				row++;
				ty += lookDown?cellHeight/2:cellHeight;
			}			
			
//            drawTiledRegion(g, sourceImage, tileArrayLength, x_src, y_src, cellWidth, cellHeight, 0, x_dest, y_dest, 20);
			
			for(int i=0; i<tileArrayLength; i++)
			{
				if(x_dest[i]>0-tileWidth&&x_dest[i]<screenW&&y_dest[i]>0-tileHeight&&y_dest[i]<screenH)
				{
					g.setClip(this.x_dest[i], this.y_dest[i], this.cellWidth, this.cellHeight);
					g.drawImage(this.sourceImage, this.x_dest[i] - x_src[i], this.y_dest[i] - y_src[i], Graphics.LEFT | Graphics.TOP);
				}
			}
			
		}
	}
//		private void drawTiledRegion(Graphics g, Image image, int tileArrayLength, int x_src[], int y_src[], int cellWidth, int cellHeight, int l, int x_dest[], int y_dest[], int i1){
//		
//	}

	private void createStaticSet(Image image, int noOfFrames, int tileWidth,int tileHeight, boolean maintainIndices) 
	{
		cellWidth = tileWidth;
		cellHeight = tileHeight;
		int imageW = image.getWidth();
		int imageH = image.getHeight();
		sourceImage = image;
		numberOfTiles = noOfFrames;
		tileSetX = new int[numberOfTiles];
		tileSetY = new int[numberOfTiles];
		if (!maintainIndices) 
		{
			for (rows = 0; rows < cellMatrix.length; rows++) 
			{
				int totalCols = cellMatrix[rows].length;
				for (columns = 0; columns < totalCols; columns++)
					cellMatrix[rows][columns] = 0;
			}
			anim_to_static = null;
		}
		int currentTile = 1;
		for (int y = 0; y < imageH; y += tileHeight) 
		{
			for (int x = 0; x < imageW; x += tileWidth) 
			{
				tileSetX[currentTile] = x;
				tileSetY[currentTile] = y;
				currentTile++;
			}
		}
	}

	/**
	 * <code>cellHeight</code> 单元格的高度
	 */
	private int cellHeight;
	/**
	 * <code>cellWidth</code> 单元格的宽度
	 */
	private int cellWidth;
	/**
	 * <code>rows</code> 行数
	 */
	private int rows;
	/**
	 * <code>columns</code> 列数
	 */
	private int columns;
	/**
	 * <code>cellMatrix</code> 保存地图贴图数据索引值,第一维保存列数据,第二维保存行数据,每个数组元素保存一个贴图的索引值
	 */
	private int cellMatrix[][];
	/**
	 * <code>sourceImage</code> 贴图源图片
	 */
	protected Image sourceImage;
	/**
	 * <code>numberOfTiles</code> 地图贴图索引的总数
	 */
	private int numberOfTiles;
	/**
	 * <code>tileSetX</code> 记录每个贴图在源图中的起始X坐标
	 */
	int tileSetX[];
	/**
	 * <code>tileSetY</code> 记录每个贴图在源图中的起始Y坐标
	 */
	int tileSetY[];
	private int anim_to_static[];
	private int numOfAnimTiles;
	private boolean updated;
	/**
	 * <code>tileArrayLength</code> 需绘制的单元格总数
	 */
	private int tileArrayLength;
	/**
	 * <code>x_src</code> 地图单元格需绘制的图片在源图中的X坐标
	 */
	private int x_src[];
	/**
	 * <code>y_src</code> 地图单元格需绘制的图片在源图中的X坐标
	 */
	private int y_src[];
	/**
	 * <code>x_dest</code> 每个地图单元格的X坐标
	 */
	private int x_dest[];
	/**
	 * <code>y_dest</code> 每个地图单元格的Y坐标
	 */
	private int y_dest[];
}

⌨️ 快捷键说明

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