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

📄 util.java

📁 J2ME程序开发实用例子
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
			int rowVal = dis.readUnsignedShort();
			int colVal = dis.readUnsignedShort();
			map_tileW = dis.readUnsignedByte();
			map_tileH = dis.readUnsignedByte();
			mapW = colVal * map_tileW;
			mapH = rowVal * map_tileH;
			map_data = new short[rowVal][colVal];
			for (int i = 0; i < rowVal; i++) {
				for (int j = 0; j < colVal; j++) {
					map_data[i][j] = dis.readShort();
				}
			}
			// 查看物理层数据
			closeStream(dis);
		} catch (Exception e) {
			Error(e,"Lscene");
		}
		map_imgCol = res_getImageWidth(RES_TILE) / map_tileW;
		res_load(RES_UI);
//		map_setFocusSprite(0,true);
	}
	
	/** *********************************TODO:脚本引擎**************************** */
	/** ********************************************************触发器 */
	// 处理if语句
	// GOTO-------------------------------------------------------
	/** ***************************************************************************TODO:cos(ang),sin(ang) */
	/** ******************************************************************TODO:<以图型方式绘制英文字及数字> */
	public static void cls(Graphics g,int cls_color){
		g.setColor(cls_color);
		g.fillRect(0,0,SCREEN_W,SCREEN_H + 20);
	}
	/**
	 * ******************************************************************
	 * 返回换色后的图片
	 */
	/** *****************************************************************声音-管理 */
	// --------------------
	public static DataInputStream openStream(String fileName)throws Exception
	{
		Util.println("openStream "+fileName);
		return new DataInputStream("QN".getClass().getResourceAsStream(fileName));
	}// ~QN
	
	public static void closeStream(DataInputStream dis)throws Exception
	{
		dis.close();
		dis = null;
		System.gc();
	}// ~QN
	
	public static int readUnsignedInt(DataInputStream is) throws Exception{
		return  (( (is.read() & 0xFF) << 24) + ( (is.read() & 0xFF) << 16) +  ( (is.read() & 0xFF) << 8) + (is.read() & 0xFF));
	}// ~QN
	/** **************************************************************TODO:<排行榜> */
	/** ****************************************TODO:<物品管理> */
	/***********************************************************************
	 * 录放像机 by QN 使用内存 vid_len * 9(byte)
	 **********************************************************************/
	/**
	 * *******************************************************TODO:游戏外部资源管理
	 * !byQN
	 */
	public static String[] resNames = {
		"tk.png","bullet.png","bulletBoom.png","m0.png","ui.png"
	};
	public static final int RES_TK = 0;
	public static final int RES_BULLET =1;
	public static final int RES_BULLET_BOOM =2;
	public static final int RES_TILE = 3;
	public static final int RES_UI = 4;
	public static Object res[] = new Object[resNames.length];
	public int res_getImageWidth(int id){
		return ((Image)res_get(id)).getWidth();
	}
	
	public Image res_getImage(int id){
		return ((Image)res_get(id));
	}
	
	public Object res_get(int id){
		String resName = resNames[id];
		if(res[id] == null){
			if(resName.toLowerCase().endsWith("png")){
				res[id] = createImage(resName);
			}
		}
		return res[id];
	}
	
	public void res_load(int[] list){
		for (int i = 0; i < list.length; i++) {
			res_get(list[i]);			
		}
	}
	public void res_load(int id){
		res_get(id);
	}
	
	public static void res_releaseAll(){
		for (int i = 0; i < res.length; i++) {
			res[i] = null;			
		}
		System.gc();
	}
	
	public static void res_release(int id){
		res[id] = null;
		System.gc();
	}

	/** *******************************************************TODO:物理层数据管理 */
	public static final int PHY_X_LT_0 = 0X7F;
	public static final int PHY_X_GT_W = 0X7E;
	public static final int PHY_Y_LT_0 = 0X7D;
	public static final int PHY_Y_GT_H = 0X7C;
	public static final int PHY_BRICK = 0;// 墙
	public static final int PHY_ROCK = 1;// 石
	public static final int PHY_WATER = 2;// 水
	public static final int PHY_GRASS = 3;// 草
	public static final int PHY_ICE = 4;// 冰
	public static final int PHY_BLOCKHOUSE = 5;// 碉堡
	
	public int phy_getIndex(int x, int y){
		if(x < 0
				||y < 0
				||x > map_data[0].length*map_tileW
				||y > map_data.length*map_tileH){
			return - 1;
		}
		x /= map_tileW;
		y /= map_tileH;
		return y * map_data[0].length + x;
	}// ~QN
	public byte phy_getId(int index){
		try {
			int col = map_data[0].length;// width
			return (byte) map_data[index / col][index % col];
		} catch (Exception e) {
			return -1;
		}
	}// ~QN
	public void phy_setDataValue(int index,int nv){
		try {
			int col = map_data[0].length;
			map_data[index / col][index % col] = (short)nv;
//			map_data[index / col][index % col] = (short)( ((flip & 0xff) << 12 ) | (nv &
//			0xff) );
		} catch (Exception e) {
			e.printStackTrace();
		}
	}// ~QN
	
	
	
	/** *******************************************************TODO:未优化 */
	public int[] buttllCanMove(int dir,int x,int y,int vx,int vy,int w,int h){
		int[] a = new int[5];
		int tox = x + vx;
		int toy = y + vy;
		a[1] = tox;
		a[2] = toy;
		a[3] = -1;
		a[4] = -1;
		switch(dir){
			case Sprite.DIR_UP:
				for (int r = y; r >= toy; r --) {for (int c = x; c >= tox; c --) {
					int in = phy_getIndex(c,r + h);
					int id = phy_getId(in);
					if(id != -1){
						a[0] = 1;
						a[1] = c;
						a[2] = r;
						a[3] = id;
						a[4] = in;
						return a;
					}
				}
				}
				break;
			case Sprite.DIR_DOWN:
				for (int r = y; r <= toy; r ++) {for (int c = x; c <= tox; c ++) {
					int in = phy_getIndex(c,r + h);
					int id = phy_getId(in);
					if(id != -1){
						a[0] = 1;
						a[1] = c;
						a[2] = r;
						a[3] = id;
						a[4] = in;
						return a;
					}
				}
				}
				break;
			case Sprite.DIR_LEFT:
				for (int c = x; c >= tox; c --) {for (int r = y; r >= toy; r --) {
					int in = phy_getIndex(c + w,r );
					int id = phy_getId(in);
					if(id != -1){
						a[0] = 1;
						a[1] = c;
						a[2] = r;
						a[3] = id;
						a[4] = in;
						return a;
					}
				}
				}
				break;
			case Sprite.DIR_RIGHT:
				for (int c = x; c <= tox; c ++) {
					for (int r = y; r <= toy; r ++) {
						int in = phy_getIndex(c + w,r);
						int id = phy_getId(in);
						if(id != -1){
							a[0] = 1;
							a[1] = c;
							a[2] = r;
							a[3] = id;
							a[4] = in;
							return a;
						}
					}
				}
				break;
		}
		return a;
	}// ~QN
	
	
	/** *******************************************************TODO:未优化 */
	public Vector sprite_canMove(Sprite spt,int w,int h) {
		int[] a = new int[4];
		for (int i = 0; i < a.length; i ++) {
			a[i]=-1;
		}
		int count = spt.vx / 4;
//		Arraylist al = new Arraylist();
		Vector al = new Vector();
		int x = spt.x;
		int y = spt.y;
		int vx = spt.vx;
		int vy = spt.vy;
		int nextx = x + vx;
		int nexty = y + vy;
		if(nextx <0){
			a[1] = a[3] = PHY_X_LT_0;
			al.addElement(a);
			return al;// ((PHY_X_LT_0 & 0xffff) << 16 ) | (nextIndex & 0xffff)
			// ;
		}else if(nextx + w > mapW){
			a[1] = a[3] = PHY_X_GT_W;
			al.addElement(a);
			return al;
		}else if(nexty <0){
			a[1] = a[3] = PHY_Y_LT_0;
			al.addElement(a);
			return al ;
		}else if(nexty + h > mapH ){
			a[1] = a[3] = PHY_Y_GT_H;
			al.addElement(a);
			return al ;
		}
		int fromX = min(spt.x,nextx) / map_tileW;
		int fromY = min(spt.y,nexty) / map_tileH;
		int sptW = spt.vx > 0? spt.w- 1 :1;
		int sptH = spt.vy > 0 ? spt.h - 1:1;
		
		int toTileX = (max(spt.x,nextx) + sptW ) / map_tileW;
		int toTileY = (max(spt.y,nexty)  + sptH) / map_tileH;
		try {
//			System.out.println("fromX"+fromX+" toTileX="+toTileX+" fromY"+fromY+"
//			toTileY="+toTileY);
			switch (spt.getDir()) {
				case Sprite.DIR_UP: 
				case Sprite.DIR_LEFT: 
					for (int c = toTileX; c >= fromX; c --) {
						for (int r = toTileY; r >= fromY; r --) {
							al.addElement(phy_getTileInfo(spt.getDir(), c, r, w, h));
						}
					}
					break;
					
				case Sprite.DIR_DOWN: 
				case Sprite.DIR_RIGHT: 
					for (int c = fromX; c <= toTileX; c ++) {
						for (int r = fromY; r <= toTileY; r ++) {
							al.addElement(phy_getTileInfo(spt.getDir(), c, r, w, h));
						}
					}
					break;
					
					
			}
			
		}catch (Exception e) {
		}
		return al;
	}
	public int min(int a,int b){
		if(a > b)return b;
		return a;
	}
	
	public int max(int a,int b){
		if(a>b)return a;
		return b;
	}
	
	public int[] phy_getTileInfo(int dir,int c,int r,int w,int h){
		int[] a = new int[4];
		switch (dir) {
			case Sprite.DIR_UP: 
			case Sprite.DIR_DOWN: {
				a[0] = (r * map_data[0].length + c);
				a[1] = map_data[r][c];
				a[2] = ((r) * map_data[0].length + (c+1));
				a[3] = map_data[r][(c+1)];
				return a;
			}
			
			case Sprite.DIR_LEFT: 
			case Sprite.DIR_RIGHT: {
				a[0] = (r * map_data[0].length + c);
				a[1] = map_data[r][c];
				a[2] = ((r+1) * map_data[0].length + c);
				a[3] = map_data[r+1][c];
				return a;
			}
			
		}
		return a;
	}
	
	/** **************************************************************精灵管理,及动作 */
	/** 精灵状态 */
	public static final byte SPT_STATE_FREE = 0;
	public static final short SPT_STATE_FREE_WAIT = 0X0000;
	public static final short SPT_STATE_FREE_STEP = 0X0001;
	public static final byte SPT_STATE_DEAD = 0X0F;
	public static final short SPT_STATE_DEAD_START = 0X0F00;
	public static final short SPT_STATE_DEAD_REMOVE = 0X0F01;
	// -----------------------
	public byte sprite_getMainState(short state) {
		return (byte) ((state >> 8) & (byte)0xff );
	}
	
	public void ai(Sprite spt){
//		runTrigger();
		switch(spt.id){
			case ID_PLAYER:
				ai_player(spt);
				break;
			case ID_BULLET:
				ai_bullet(spt);
				break;
			case ID_BULLET_BOOM:
				ai_bulletBomm(spt);
				break;
		}
	}
	
	// 人物状态设置
	public void sprite_changeState(Sprite spt,short nextState){
		if(nextState == spt.state )return;
		spt.state = nextState;// (short)(nextState <= 0x7f ? ((nextState << 8)
		// & 0xff) : nextState);
		switch(spt.id){
			case ID_PLAYER:
				ai_playerState(spt, nextState);
				break;// 玩家
			case ID_BULLET:
				ai_bulletState(spt, nextState);
				break;
		}
	}
	
	
	public void ai_playerState(Sprite spt,short nextState){
		switch(sprite_getMainState(spt.state)){
			case SPT_STATE_FREE:
				switch(spt.state){
					case SPT_STATE_FREE_WAIT:
						spt.vx = spt.vy = 0;
						break;
					case SPT_STATE_FREE_STEP:
						int v = 4;
						if(spt.getDir() == Sprite.DIR_UP){
							sprite_changeSpeed(spt,0,-v);

⌨️ 快捷键说明

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