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

📄 mappy.java

📁 一个JAVA的地图编辑器,适合快速开发移动终端游戏的好工具.
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
		// do dat ting!
		drawLayer(gfx, m_intCurrentMapLayer, intBlockX, intBlockY, intSrcPixX, intSrcPixY, m_intScreenOffsetX, m_intScreenOffsetY, m_intScreenWidth, m_intScreenHeight, intBlockLayer, blnTransparency);
	}



	/**
	 * The all encompassing drawLayer routine. <br>
	 **/
	private void drawLayer(Graphics gfx, int intMapLayer, int intMapX, int intMapY, int intSrcPixX, int intSrcPixY, int intDstPixX, int intDstPixY, int intDstPixWidth, int intDstPixHeight, int intBlockLayer, boolean blnTransparency) {
		Shape	shpOldClip;
		int		intMapXStart;
		int		intDstXStart;
		int		intBlockIndex;
		int		intImageIndex;
		short	shtMap[][];
		AnimStructure animBlock;
		Color	colBackground;
		Image	imgBlocks = null;


		if (gfx == null) {
			log.error(new NullPointerException("drawLayer() - 'gfx' was null."));
			return;
		}

		// grab a map layer
		shtMap = m_shtTheMap[intMapLayer];


		// let the system do the clipping for us
		shpOldClip = gfx.getClip();
		gfx.setClip(intDstPixX, intDstPixY, intDstPixWidth, intDstPixHeight);


		// cater for offset
		intDstPixWidth  += intDstPixX;
		intDstPixHeight += intDstPixY;

		// initialise loop variables
		intDstPixX		-= intSrcPixX;
		intDstPixY		-= intSrcPixY;

		if (m_intBlockStaggerX != 0 || m_intBlockStaggerY != 0) {
			blnTransparency = true;
			intDstPixX -= m_intBlockStaggerX;
			intDstPixY -= m_intBlockStaggerY;
			intMapY *= 2;
		}

		intMapXStart	 = intMapX;
		intDstXStart	 = intDstPixX;

		// loop for all blocks to be rendered
		for ( ;intDstPixY < intDstPixHeight; intDstPixY += m_intBlockGapY) {

			intDstPixX 	= intDstXStart;
			intMapX 	= intMapXStart;
			for ( ;intDstPixX < intDstPixWidth; intDstPixX += m_intBlockGapX) {

				// get the current block from the map
				intBlockIndex = (int) shtMap[intMapY][intMapX];

				// is it an animation block?
				if (intBlockIndex < 0) {
					// if so, grab the current block in the anim sequence
					animBlock 		= m_clsMapAniStrPt[-intBlockIndex - 1];
					intBlockIndex	= animBlock.anframelist[animBlock.ancurframe];
				}

				// grab the image layer that we're about to draw
				intImageIndex 	= m_clsMapBlockStrPt[intBlockIndex].getImageIndex(intBlockLayer);

				// the first block / image is always transparent
				if ((!blnTransparency) || (intImageIndex != 0)) {

					if (blnTransparency) {
						// draw transparent image
						gfx.drawImage(m_imgTransparentBlocks[intImageIndex], intDstPixX, intDstPixY, intDstPixX + m_intBlockWidth, intDstPixY + m_intBlockHeight,
									  	     0, 0, m_intBlockWidth, m_intBlockHeight, null);
					} else {
						// draw opaque image
						gfx.drawImage(m_imgOpaqueBlocks[intImageIndex], intDstPixX, intDstPixY, intDstPixX + m_intBlockWidth, intDstPixY + m_intBlockHeight,
									  	     0, 0, m_intBlockWidth, m_intBlockHeight, null);
					}
				}
				intMapX++;
			}

			if (m_intBlockStaggerX != 0 || m_intBlockStaggerY != 0) {
				intMapX 	= intMapXStart;
				intMapY++;
				intDstPixX 	= intDstXStart+m_intBlockStaggerX;
				intDstPixY 	+= m_intBlockStaggerY;
				for ( ;intDstPixX < intDstPixWidth; intDstPixX += m_intBlockGapX) {

				// get the current block from the map
				intBlockIndex = (int) shtMap[intMapY][intMapX];

				// is it an animation block?
				if (intBlockIndex < 0) {
					// if so, grab the current block in the anim sequence
					animBlock 		= m_clsMapAniStrPt[-intBlockIndex - 1];
					intBlockIndex	= animBlock.anframelist[animBlock.ancurframe];
				}

				// grab the image layer that we're about to draw
				intImageIndex 	= m_clsMapBlockStrPt[intBlockIndex].getImageIndex(intBlockLayer);

				// the first block / image is always transparent
				if (intImageIndex != 0) {

					// draw transparent image
					gfx.drawImage(m_imgTransparentBlocks[intImageIndex], intDstPixX, intDstPixY, intDstPixX + m_intBlockWidth, intDstPixY + m_intBlockHeight,
								  	     0, 0, m_intBlockWidth, m_intBlockHeight, null);
				}
				intMapX++;
			}
			intDstPixY 	-= m_intBlockStaggerY;
			}
			intMapY++;
		}

		// restore Clip
		gfx.setClip(shpOldClip);
	}



//______________________________________________________________________________
//------------------------------------------------------------------------------
// Public Functions : General
//______________________________________________________________________________
//------------------------------------------------------------------------------

	/**
	 * Sets the transparent colour for Block Images. This is used to denote the
	 * transparent colour of block images when a Map is loaded. Hence this
	 * method need only be used <i>before</i> a <code>loadMap()</code> method is
	 * called. <br>
	 * <br>
	 * The default transparent colour is <code>Color.black</code>. <br>
	 * <br>
	 * Thinking about it, if I can't find another use for the transparent colour
	 * then I may make it a parameter in <code>loadMap()</code>.
	 * <br>
	 * @param	intNewTransparentColour	the blocks transparent colour in <code>RGB</code>.
	 * @see		#getTransparentColour()
	 * @see		#loadMap(File, Component)
	 * @see		#loadMap(String, Component)
	 **/
	public void setTransparentColour(int intNewTransparentColour) {
		if (m_blnMapLoaded) {
			// only warn in case user is about to load another map
			log.warn("setTransparentColour() - A Map has already been loaded, this new Colour will not take effect until you load a new map.");
		}
		m_intTransparentColour = intNewTransparentColour;
	}



	/**
	 * Returns the transparent colour used for Block Images in <code>RGB</code>
	 * format. <br>
	 * <br>
	 * @return	the transparent colour used for Block Images.
	 * @see		#setTransparentColour(int)
	 **/
	public int getTransparentColour() {
		return m_intTransparentColour;
	}



	/**
	 * Loads a <code>.FMP</code> Map from a given filename. The Mappy class
	 * is rather useless until you load a Map into it! <br>
	 * <br>
	 * @param	strFileName	the filename of the Mappy <code>.FMP</code> file.
	 * @param	component	this is required to create a compatible image, it
	 *			can be a <code>Window</code> or an <code>Applet</code> or any
	 *			<code>component</code> really!
	 * @see		#loadMap(File, Component)
	 **/
	public void loadMap(String strFileName, Component component) {
		if (strFileName == null) {
			throw new NullPointerException("loadMap() - 'strFileName' was null.");
		}

		loadMap(new File(strFileName), component);
	}



	/**
	 * Loads a <code>.FMP</code> Map from a given <code>File</code>. The Mappy
	 * class is rather useless until you load a Map into it! <br>
	 * <br>
	 * @param	file	the file of the Mappy <code>.FMP</code> file.
	 * @param	component	this is required to create a compatible image, it
	 *			can be a <code>Window</code> or an <code>Applet</code> or any
	 *			<code>component</code> really!
	 * @see		#loadMap(InputStream, Component)
	 **/
	public void loadMap(File file, Component component) {
		InputStream	stream;

		if (file == null) {
			throw new NullPointerException("loadMap() - 'file' was null.");
		}

		try {
			log.info("loadMap() - loading \"" + file.toString() + "\"...");
			loadMap(new FileInputStream(file), component);
			log.info("loadMap() - \"" + file.toString() + "\" loaded succesfully.");
		} catch (IOException e) {
			log.error("loadMap() - cannot create InputStream", e);
		}
	}



	/**
	 * Loads a <code>.FMP</code> Map from a given <code>InputStream</code>. The
	 * Mappy class is rather useless until you load a Map into it! <br>
	 * <br>
	 * @param	stream	the InputStream where the <code>.FMP</code> file can be found.
	 * @param	component	this is required to create a compatible image, it
	 *			can be a <code>Window</code> or an <code>Applet</code> or any
	 *			<code>component</code> really!
	 **/
	public void loadMap(InputStream	stream, Component component) {
		String		strID;
		String		strForm;
		int			intChunkLength;
		int			intBytesToRead;
		boolean		blnChunkRead;
		boolean		blnLSB;

		if (stream == null) {
			throw new NullPointerException("loadMap() - 'stream' was null.");
		}

		log.info("loadMap() - loading Map");

		// reset map attributes
		m_shtTheMap = new short[MAX_NO_OF_LAYERS + 1][][];
		m_intMaxLayerNo = 0;

		try {
			// do a read12
			strID 			= readID(stream);
			intChunkLength	= readChunkLength(stream);
			strForm 		= readID(stream);

			if (strID.equals("FORM") && strForm.equals("FMAP")) {
				intBytesToRead 	= intChunkLength - 4;
				blnLSB 			= false;

				// read in chunks while there's still some file left!
				while (intBytesToRead > 0) {
					// do a read8
					strID 			= readID(stream);
					intChunkLength	= readChunkLength(stream);
					intBytesToRead -= 8;
					blnChunkRead 	= false;

					// load Author and Map Desc
					if (strID.equals("ATHR")) {
						loadATHR(stream, intChunkLength);
						blnChunkRead = true;
					}

					// load Map Header
					if (strID.equals("MPHD")) {
						blnLSB = loadMPHD(stream, intChunkLength);
						blnChunkRead = true;
					}


					// Colour Map
						if (strID.equals("CMAP")) {
						loadCMAP(stream, intChunkLength);
						blnChunkRead = true;
					}


					// load Block...DT??
					if (strID.equals("BKDT")) {
						loadBKDT(stream, intChunkLength, blnLSB);
						blnChunkRead = true;
					}


					// load Animation...DT??
					if (strID.equals("ANDT")) {
						loadANDT(stream, intChunkLength, blnLSB);
						blnChunkRead = true;
					}


					// load Background Effects??
					if (strID.equals("BGFX")) {
						loadBGFX(stream, intChunkLength, component);
						blnChunkRead = true;
					}


					// load Body & Layers
					if (strID.equals("BODY")) {
						loadLayer(stream, intChunkLength, blnLSB);
						blnChunkRead = true;
					}


					// load Layer 1
					if (strID.equals("LYR1")) {
						loadLayer(stream, intChunkLength, blnLSB);
						blnChunkRead = true;
					}


					if (!blnChunkRead) {
						log.info("Found Chunk - " + strID + ", skipping...");
						stream.skip(intChunkLength);
					}
					intBytesToRead -= intChunkLength;
				}

				m_blnMapLoaded = true;
				log.info("loadMap() - loaded succesfully.");
			}

			stream.close();

		} catch(Exception e) {
			log.error(e);
		}
	}

	/**
	 * Loads a <code>.MAR</code> map array file called strFileName. 
	 * The corresponding FMP map must be loaded first.<br>
	 * <br>
	 * @param	strFileName the name of the .MAR file to load
	 * @param	marlyr the number of the layer you want to load to
	 **/
	public void loadMAR (String strFileName, int marlyr) {

		if (marlyr < 0 || marlyr > 7) return;

		if (strFileName == null) {
			throw new NullPointerException("loadMAR() - 'strFileName' was null.");
		}
		File file = new File(strFileName);

		if (file == null) {
			throw new NullPointerException("loadMAR() - 'file' was null.");
		}

		InputStream	stream;
		try {
			stream = new FileInputStream(file);
			int intTempMaxLayerNo = m_intMaxLayerNo;
			int intTempMapType = m_intMapType;
			if (m_shtTheMap[marlyr] == null) intTempMaxLayerNo++;
			m_intMaxLayerNo = marlyr; //layer to load to
			m_intMapType = 1; //must be 1 for uncompressed unit based (FMP1.0)
			
			loadLayer (stream, m_intMapWidth*m_intMapHeight*2, true);
			
			m_intMaxLayerNo = intTempMaxLayerNo;
			m_intMapType = intTempMapType;
	
			int k = 0; //test for new style MAR
			for (int j=0;j<m_intMapHeight;j++) {
				for (int i=0;i<m_intMapWidth;i++) {
					if ((m_shtTheMap[marlyr][j][i]&0xF) != 0) k = 1;
				}
			}
			if (k == 0) {
				for (int j=0;j<m_intMapHeight;j++) {
					for (int i=0;i<m_intMapWidth;i++) {
						if (m_shtTheMap[marlyr][j][i] >= 0) m_shtTheMap[marlyr][j][i] /= 32;
						else m_shtTheMap[marlyr][j][i] /= 16;
					}
				}
			}
		} catch (IOException e) {
			log.error("loadMAR() - cannot create InputStream", e);
		}

	}

//______________________________________________________________________________
//------------------------------------------------------------------------------
// Public Functions : Map Attributes
//______________________________________________________________________________
//------------------------------------------------------------------------------

	/**
	 * Returns a description of the Map. The description is a concatonation of
	 * the strings found in the <code>ATHR</code> chunk of the map, seperated by
	 * new-line ("<code>\n</code>") characters. <br>
	 * <br>
	 * @return	a description of the Map.
	 **/
	public String getMapDescription() {
		return m_strMapDescription;

⌨️ 快捷键说明

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