www.pudn.com > litwiz.rar > lev.java


import java.awt.*; 
 
class lev 
{ 
	// All the ents on the level - a lot 
	public ent[] curEnt; 
 
	// An array of the ents that move at any moment in a 'room' 
	public ent[] movInRm; 
	public int numInRm; 
 
	// Items class used to get IDs etc 
	private items item; 
 
	// Always point to correct page to draw on 
	private Graphics paper; 
	private Image gfx; 
	private Image gfx2; 
 
	public lev(String st, Graphics gr, Image im, Image im2) 
	{ 
		int len=st.length()-1; 
		this.curEnt=new ent[len]; 
		this.movInRm=new ent[100]; 
		this.numInRm=0; 
		this.paper=gr; 
		this.gfx=im; 
		this.gfx2=im2; 
		this.item=new items(); 
		int x=0; 
		int y=0; 
		int z=0; 
		int ms=0; 
		char s; 
		boolean mv; 
		boolean dirU; 
		boolean dirD; 
		boolean dirL; 
		boolean dirR; 
 
		// Create level from string passed in st, char by char 
		for (int a=0; a199) 
			{ 
				x=0; 
				y++; 
			} 
		} 
	} 
 
	// Takes an x block, yblock and returns int i which is the block nearest to us 
	// that contains nothing (for dropping an item for example) 
	// return 0 if nothing available 
	public int getNearestSpace(int x, int y) 
	{ 
		int y2; 
		y2=y; 
		for (int x2=x-1; x227399) a=0; 
				int xoff=curEnt[a].z; 
				doDraw(xabs, yabs, xoff, true); 
				xabs+=20; 
 
				// An ent that moves is in the room, add to the movInRm list 
				if (curEnt[a].moves) 
				{ 
					addToMvArray(a); 
				} 
			} 
			xabs=0; 
			yabs+=20; 
		} 
	} 
 
	// Redistribute item when used randomly, means that we always 
	// have enough food, health and plants to complete the game 
	public void reDist(int a) 
	{ 
		int c=200*135; 
		int b=(int)(c*Math.random())+1; 
		while (curEnt[b].z>0) 
		{ 
			b=(int)(c*Math.random())+1; 
		} 
		curEnt[b].z=a; 
	} 
 
	// Update window (re-draw), leaving monsters as they are 
	public void updateWindow(int xp, int yp) 
	{ 
		int xabs=0; 
		int yabs=0; 
 
		for (int enty=yp-5; enty <= yp+6; enty++) 
		{ 
			for (int entx=xp-5; entx <= xp+6; entx++) 
			{ 
				int a=entx+(enty*200); 
				if (a<0 || a>27399) a=0; 
				int xoff=curEnt[a].z; 
				doDraw(xabs, yabs, xoff, true); 
				xabs+=20; 
			} 
			xabs=0; 
			yabs+=20; 
		} 
	} 
 
	// Add ent 'a' to movInRm array list 
	public void addToMvArray(int a) 
	{ 
			movInRm[numInRm]=new ent(curEnt[a].x, curEnt[a].y, curEnt[a].z); 
			movInRm[numInRm].moves=curEnt[a].moves; 
			movInRm[numInRm].dirU=curEnt[a].dirU; 
			movInRm[numInRm].dirD=curEnt[a].dirD; 
			movInRm[numInRm].dirL=curEnt[a].dirL; 
			movInRm[numInRm].moveSpeed=curEnt[a].moveSpeed; 
			movInRm[numInRm++].dirR=curEnt[a].dirR; 
			curEnt[a].z=0; 
			curEnt[a].moves=false; 
	} 
 
	// Remove ent 'a' to movInRm array list 
	// Method: Copy last member into position to delete and subtract 1 from number of members 
	public void removeFromRm(int a) 
	{ 
			numInRm--; 
			if (numInRm<0) numInRm=0; 
			if (numInRm>0 && a!=numInRm) 
			{ 
				movInRm[a].moves=movInRm[numInRm].moves; 
				movInRm[a].dirU=movInRm[numInRm].dirU; 
				movInRm[a].dirD=movInRm[numInRm].dirD; 
				movInRm[a].dirL=movInRm[numInRm].dirL; 
				movInRm[a].dirR=movInRm[numInRm].dirR; 
				movInRm[a].x=movInRm[numInRm].x; 
				movInRm[a].y=movInRm[numInRm].y; 
				movInRm[a].z=movInRm[numInRm].z; 
				movInRm[a].i=movInRm[numInRm].i; 
				movInRm[a].moveSpeed=movInRm[numInRm].moveSpeed; 
			} 
			movInRm[numInRm].z=0; 
	} 
 
// ** Routines for moving left, right, up, down.  There is a better way of doing 
//		this, I know. 
 
	// We're moving downwards, copy screen to top and draw in bottom 
	public void moveDown() 
	{ 
		paper.copyArea(0,20,240,220, 0, -20); 
	} 
	public void drawBottom(int xp, int yp) 
	{ 
		int xabs=0; 
		int yabs=220; 
		int enty=yp+6; 
			for (int entx=xp-5; entx <= xp+6; entx++) 
			{ 
				int a=entx+(enty*200); 
				if (a<0 || a>27399) a=0; 
				int xoff=curEnt[a].z; 
				doDraw(xabs, yabs, xoff, true); 
				xabs+=20; 
 
				// An ent that moves is in the room, add to the movInRm list 
				if (curEnt[a].moves) 
				{ 
					addToMvArray(a); 
				} 
			} 
	} 
 
	// We're moving upwards, copy screen to bottom and draw in top 
	public void moveUp() 
	{ 
		paper.copyArea(0,0,240,220, 0, 20); 
	} 
	public void drawTop(int xp, int yp) 
	{ 
		int xabs=0; 
		int yabs=0; 
		int enty=yp-5; 
			for (int entx=xp-5; entx <= xp+6; entx++) 
			{ 
				int a=entx+(enty*200); 
				if (a<0 || a>27399) a=0; 
				int xoff=curEnt[a].z; 
				doDraw(xabs, yabs, xoff, true); 
				xabs+=20; 
 
				// An ent that moves is in the room, add to the movInRm list 
				if (curEnt[a].moves) 
				{ 
					addToMvArray(a); 
				} 
			} 
	} 
 
	// We're moving left, copy screen to right and draw in left 
	public void moveLeft() 
	{ 
		paper.copyArea(0,0,220,240, 20, 0); 
	} 
	public void drawLeft(int xp, int yp) 
	{ 
		int xabs=0; 
		int yabs=0; 
		int entx=xp-5; 
			for (int enty=yp-5; enty <= yp+6; enty++) 
			{ 
				int a=entx+(enty*200); 
				if (a<0 || a>27399) a=0; 
				int xoff=curEnt[a].z; 
				doDraw(xabs, yabs, xoff, true); 
				yabs+=20; 
 
				// An ent that moves is in the room, add to the movInRm list 
				if (curEnt[a].moves) 
				{ 
					addToMvArray(a); 
				} 
			} 
	} 
 
	// We're moving right, copy screen to left and draw in right 
	public void moveRight() 
	{ 
		paper.copyArea(20,0,220,240, -20, 0); 
	} 
	public void drawRight(int xp, int yp) 
	{ 
		int xabs=220; 
		int yabs=0; 
		int entx=xp+6; 
			for (int enty=yp-5; enty <= yp+6; enty++) 
			{ 
				int a=entx+(enty*200); 
				if (a<0 || a>27399) a=0; 
				int xoff=curEnt[a].z; 
				doDraw(xabs, yabs, xoff, true); 
				yabs+=20; 
 
				// An ent that moves is in the room, add to the movInRm list 
				if (curEnt[a].moves) 
				{ 
					addToMvArray(a); 
				} 
			} 
	} 
 
	public void doDraw(int xabs, int yabs, int xoff, boolean b) 
	{ 
		int yoff=0; 
 
		if (xoff>0) 
		{ 
			yoff=((int)xoff/10)*20; 
			xoff%=10; 
			xoff*=20; 
			if (b) 
			{ 
				paper.drawImage(gfx, 
					xabs, yabs, xabs+20, yabs+20, 
					xoff, yoff, xoff+20, yoff+20, 
					null); 
			} 
			else 
			{ 
				paper.drawImage(gfx2, 
					xabs, yabs, xabs+20, yabs+20, 
					xoff, yoff, xoff+20, yoff+20, 
					null); 
			} 
 
		} 
		else 
		{ 
			paper.setColor(Color.black); 
			paper.fillRect(xabs, yabs, 20,20); 
		} 
	} 
 
	// ent Collision ?  Does ent e collide with ent f 
	// If so, return value of ent f's z 
	public int collide(ent e, ent f) 
	{ 
		int x1,y1,x2,y2; 
 
		if (f.z==0) return 0; 
 
		x1=e.x; 
		y1=e.y; 
 
		x2=f.x; 
 
		if (x2>x1-20) 
		{ 
			if (x2y1-20) 
				{ 
					if (y2