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

📄 room.java

📁 gobee j2me
💻 JAVA
字号:
import java.awt.*;

public class Room
{
	public Block[] rmBlock;
	public int numBlocks;
	public int worldSize;

	public int numFlowers;
	public int timeLeft;
	public boolean dead;

	public Room(int i, int w, Graphics gx, Image ix)
		// Create an array of blocks for this room
	{
		this.numBlocks=i;
		this.rmBlock=new Block[i];
		this.timeLeft=0;
		this.numFlowers=0;
		this.worldSize=w;
		this.dead=false;
		for (int a=0; a<i; a++)
		{
			rmBlock[a]=new Block(gx, ix);
		}
	}

	public void drawRoom()
		// Loop through and draw all blocks that need to (animate too)
	{
		// Loop through all of our ents
		for (int j=0; j<numBlocks; j++)
		{
			theMove(j);
		}
	}

	public void theMove(int j)
	{
		int oldx, oldy;

		// If an ent animates, do so
		if (rmBlock[j].animates) rmBlock[j].animate();

		// If an ent moves, do so, taking into account
		// collision
		if (rmBlock[j].moves)
		{
			rmBlock[j].delete();
			oldx=rmBlock[j].xpos;
			oldy=rmBlock[j].ypos;
			// Move it
			rmBlock[j].move(worldSize);

			// Does it collide in this position?
			int k;
			k=collide(j);
			if (k>0)
			{
				// If collision returns false we can't move, so restore
				// our old position
				if (!doCollision(j, k))
				{
					// Restore it and change direction
					rmBlock[j].xpos=oldx;
					rmBlock[j].ypos=oldy;
					if (j!=99) rmBlock[j].changeDir();
				}
			}
		}
		// After all this, so we need to draw this block?
		// This is set to true if we animated or moved
		if (rmBlock[j].needsToBeDrawn) rmBlock[j].draw();
	}

	// Handle all collisons
	public boolean doCollision(int j, int k)
	{
		// If ent is a blue or green mover
		if (rmBlock[j].ID==40)
		{
			// and collision is with a dead flower
			if (rmBlock[k].ID==30)
			{
				// retore flower to life and kill mover
				rmBlock[j].die();

				rmBlock[k].ID=31;
				rmBlock[k].setAnimArray(1, 2, 1, 2, 1, 2, 1, 2, 1, 2);
				rmBlock[k].animates=true;
				return true;
			}

			// and collision is with a green beetle block
			if (rmBlock[k].ID==47)
			{
				// swap places with beetle block
				rmBlock[j].jumpBlock(worldSize);
				return true;
			}

			// and collision is with a red beetle block
			if (rmBlock[k].ID==49)
			{
				// An updown beetle
				if (rmBlock[k].currentDir==1)
				{
					if (rmBlock[j].currentDir==1 || rmBlock[j].currentDir==2)
					{
						// swap places with beetle block & rotate
						rmBlock[k].setAnimArray(49, 49, 49, 49, 49, 49, 49, 49, 49, 49);
						rmBlock[k].currentDir=3;
						rmBlock[k].draw();
						rmBlock[j].jumpBlock(worldSize);
						return true;
					}
				}
				// A left right beetle
				else
				{
					if (rmBlock[j].currentDir==3 || rmBlock[j].currentDir==4)
					{
						// swap places with beetle block & rotate
						rmBlock[k].setAnimArray(48, 48, 48, 48, 48, 48, 48, 48, 48, 48);
						rmBlock[k].currentDir=1;
						rmBlock[k].draw();
						rmBlock[j].jumpBlock(worldSize);
						return true;
					}
				}
			}


			// Spikey objects kill arrow creatures
			if (rmBlock[k].ID==48)
			{
				rmBlock[j].die();
				return true;
			}

			// and collision is with a bomb, kill flower and turn bomb
			// into moving fireball
			if (rmBlock[k].ID==35)
			{
				rmBlock[j].die();
				rmBlock[k].ID=45;
				rmBlock[k].setAnimArray(45, 46, 45, 46, 45, 46, 45, 46, 45, 46);
				rmBlock[k].animates=true;
				rmBlock[k].moves=true;
				rmBlock[k].moveType=3;
				rmBlock[k].killer=true;
				return true;
			}
		}

		// If ent is a pushy green block
		if (rmBlock[k].moveAble==true)
		{
			rmBlock[k].currentDir=rmBlock[j].currentDir;
			rmBlock[k].moves=true;
			rmBlock[k].animates=true;
			theMove(k);
			rmBlock[k].animates=false;
			rmBlock[k].moves=false;
			if (collide(j)>0)
				return false;
			else
				return true;
		}

		// A nasty runs into you
		if (rmBlock[j].killer && rmBlock[k].ID==99)
		{
			dead=true;
			return false;
		}

		// Things involving you only
		if (rmBlock[j].ID==99)
		{
			// If we pick up a flower, it is good!
			if (rmBlock[k].ID==31)
			{
				numFlowers--;
				rmBlock[k].delete();
				rmBlock[k].die();
				return true;
			}

			// Soluable block, only by you though
			if (rmBlock[k].ID==5)
			{
				rmBlock[k].delete();
				rmBlock[k].die();
				return false;
			}

			// Something that kills you
			if (rmBlock[k].killer)
			{
				dead=true;
				return false;
			}
		} // Things only you

		// Spike block before spiked appear
		if (rmBlock[k].ID==46)
		{
			rmBlock[k].ID=48;
			rmBlock[k].killer=true;
			rmBlock[k].setAnimArray(4, 4, 4, 4, 4, 4, 4, 4, 4, 4);
			rmBlock[k].draw();
			return false;
		}

		return false;
	}

	public void forceRoom()
		// Loop through and draw all blocks that need to (animate too)
	{
		for (int j=0; j<numBlocks; j++)
		{
			rmBlock[j].draw();
		}
	}

	public int collide(int a)
		// Does block i collide with another in this position?
		// If so, return block ID
	{
		int x1,y1,x2,y2;
		x1=rmBlock[a].xpos;
		y1=rmBlock[a].ypos;

		for (int i=0; i<numBlocks; i++)
		{
			if (i!=a)
			{
				x2=rmBlock[i].xpos;
				if (x2>x1-20 && x2<x1+20)
				{
					y2=rmBlock[i].ypos;
					if (y2>y1-20 && y2<y1+20) return i;
				}
			}
		}
		return 0;
	}

}

⌨️ 快捷键说明

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