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

📄 actor.java

📁 一个j2me游戏代码
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
		for (int i = 0; i < times; i++)
		{
			m_posX += deltaX;
			// check map
			if (m_vX != 0)
			{
				if (m_vX > 0)
					checkX = checkLeftRight(CHECK_RIGHT, false);
				else// if (m_vX < 0)
					checkX = checkLeftRight(CHECK_LEFT, false);
				if (checkX)
					deltaX = 0;
			}
			m_posY += deltaY;
			if (m_vY != 0)
			{
				if (m_vY > 0)
					checkY = checkTopBottom(CHECK_BOTTOM, false);
				else// if (m_vY < 0)
					checkY = checkTopBottom(CHECK_TOP, false);
				if (checkY)
					deltaY = 0;
			}
			// check relative misc
			if (m_relativeMisc != null)
				if (checkActorBox(m_relativeMisc))
				{
					if (m_vX == 0) deltaX = 0;
					if (m_vY == 0) deltaY = 0;
				}
		}
		/*if (deltaX == 0)
		{
			m_vX = 0;
			m_aX = 0;
		}*/
		if (deltaY == 0)
		{
			if (m_vY > 0) m_aY = 0;
			m_vY = 0;
			//if (m_vY > 0) m_aY = 0;
			//if (m_vY < 0) m_vY = -1 << 8;
			//else          m_vY = 0 << 8;
		}
		// final check
		if (m_phbLeft == Def.PHB_NONE)
			checkLeftRight(CHECK_LEFT, true);
		if (m_phbRight == Def.PHB_NONE)
			checkLeftRight(CHECK_RIGHT, true);
		if (m_phbTop == Def.PHB_NONE)
			checkTopBottom(CHECK_TOP, true);
		if (m_phbBottom == Def.PHB_NONE)
			checkTopBottom(CHECK_BOTTOM, true);
		return true;
	}
	// this actor meet the other actor, make
	boolean checkActorBox(Actor actor)
	{
		int posX = (m_posX - actor.m_posX) >> 8;
		int posY = (m_posY - actor.m_posY) >> 8;
		byte[] miscBox = actor.getColBox();
		if (posX + m_colBoxNow[0] > miscBox[2] || posX + m_colBoxNow[2] < miscBox[0])
			return false;
		if (posY + m_colBoxNow[1] > miscBox[3] || posY + m_colBoxNow[3] < miscBox[1])
			return false;
		// top & bottom first
		//if (posX >= miscBox[0] && posX <= miscBox[2])
		{
			if (m_vY > 0)
			{
				if (posY + m_colBoxNow[3] >= miscBox[1])
				{
					posY = (posY + m_colBoxNow[3] - miscBox[1]) << 8;
					if (posY <= m_vY)
					{
						m_posY -= posY;
						m_phbBottom = Def.PHB_BOX;
						m_vX = 0;
						m_vY = 0;
						m_aY = 0;
						return true;
					}
				}
			}
			else if (m_vY < 0)
			{
				if (posY + m_colBoxNow[1] <= miscBox[3])
				{
					posY = (posY + m_colBoxNow[1] - miscBox[3]) << 8;
					if (posY >= m_vY)
					{
						m_posY -= posY;
						m_phbTop = Def.PHB_BOX;
						m_vY = 0;
						return true;
					}
				}
			}
			// m_vY == 0
			else if (posY + m_colBoxNow[3] == miscBox[1])
			{
				m_phbBottom = Def.PHB_BOX;
				return true;
			}
		}
		// left & right last
		if (posX + m_colBoxNow[0] < miscBox[0])//m_vX > 0)
		{
			if (posX > miscBox[2])
			{
				m_phbLeft = Def.PHB_BOX;
			}
			else// if (posX + m_colBoxNow[2] >= miscBox[0])
			{
				m_phbRight = Def.PHB_BOX;
			}
		}
		else if (posX + m_colBoxNow[2] > miscBox[2])//m_vX < 0)
		{
			if (posX < miscBox[0])
			{
				m_phbRight = Def.PHB_BOX;
			}
			else// if (posX + m_colBoxNow[0] <= miscBox[2])
			{
				m_phbLeft = Def.PHB_BOX;
			}
		}
		else
		{
			if (m_flag[0])
			{
				m_phbLeft = Def.PHB_BOX;
			}
			else
			{
				m_phbRight = Def.PHB_BOX;
			}
		}
		if (m_phbLeft == Def.PHB_BOX)
		{
			if (m_phbRight <= Def.PHB_BOX)
			    m_posX -= (posX + m_colBoxNow[0] - miscBox[2]) << 8;
		}
		else if (m_phbRight == Def.PHB_BOX)
		{
			if (m_phbLeft <= Def.PHB_BOX)
	            m_posX -= (posX + m_colBoxNow[2] - miscBox[0]) << 8;
		}
		return true;
	}
	final static int CHECK_LEFT = -1;
	final static int CHECK_TOP = -1;
	final static int CHECK_CENTER = 0;
	final static int CHECK_RIGHT = 1;
	final static int CHECK_BOTTOM = 1;
	// -1: left 0: center 1: right
	boolean checkLeftRight(int LeftRight, boolean getPhb)
	{
		int tileX = m_posX >> 8;
		if (LeftRight == CHECK_LEFT)
		{
			tileX += m_colBoxNow[0];
			if (getPhb)	tileX --;
		}
		else if (LeftRight == CHECK_RIGHT)
		{
			tileX += m_colBoxNow[2];
			if (getPhb)	tileX++;
		}
		if (tileX < 0) tileX = -1;
		else tileX /= GE.m_tileWidth;

		int tileY0 = ((m_posY >> 8) + m_colBoxNow[1]+1) / GE.m_tileHeight; // +1 is for box through 2 tile height gap
		int tileY1 = ((m_posY >> 8) + m_colBoxNow[3]) / GE.m_tileHeight;
		//if (m_vY > 0)
		//	tileY1--;
		//else if (m_vY < 0)
		//	tileY0++;

		byte phb = Def.PHB_AIR;
		for (int y = tileY1; y >= tileY0; y--)
		{
			phb = GE.getPhyParam(tileX, y);
			if (phb == Def.PHB_SOLID)
			{
                if (y != tileY0)
                {
                    if (GE.getPhyParam(tileX, y-1) == Def.PHB_SOLID)
                        phb = Def.PHB_WALLJUMP;
                }
				if (LeftRight == CHECK_LEFT || (LeftRight == CHECK_CENTER && m_flag[0]))
					m_posX = ((tileX+1) * GE.m_tileWidth - m_colBoxNow[0]) << 8;
				else
					m_posX = (tileX * GE.m_tileWidth - 1 - m_colBoxNow[2]) << 8;
				getPhb = true;
				break;
			}
		}
		if (getPhb)
		{
			if (LeftRight == CHECK_LEFT)
				m_phbLeft = phb;
			else if (LeftRight == CHECK_RIGHT)
				m_phbRight = phb;
		}
		return phb >= Def.PHB_SOLID;
	}
	boolean checkTopBottom(int TopBottom, boolean getPhb)
	{
		int tileX0 = ((m_posX >> 8) + m_colBoxNow[0]) / GE.m_tileWidth;
		int tileX1 = ((m_posX >> 8) + m_colBoxNow[2]) / GE.m_tileWidth;
		int tileY = m_posY >> 8;
		if (TopBottom == CHECK_TOP)
		{
			tileY += m_colBoxNow[1];
			if (getPhb)	tileY--;
		}
		else
		{
			tileY += m_colBoxNow[3];
			if (getPhb)	tileY++;
		}
		tileY /= GE.m_tileHeight;

		byte phb = Def.PHB_AIR;
		for (int x = tileX0; x <= tileX1; x++)
		{
			phb = GE.getPhyParam(x, tileY);
			if (phb == Def.PHB_SOLID)
			{
				if (TopBottom == CHECK_TOP)
					m_posY = ((tileY + 1) * GE.m_tileHeight - m_colBoxNow[1]) << 8;
				else
					m_posY = (tileY * GE.m_tileHeight - 1 - m_colBoxNow[3]) << 8;
				getPhb = true;
				break;
			}
		}
		if (getPhb)
		{
			if (TopBottom == CHECK_TOP)
				m_phbTop = phb;
			else
				m_phbBottom = phb;
		}
		return phb == Def.PHB_SOLID;
	}
	// if ground is solid, return 0 and if can't climb down, return 1, otherwise return 2.
	int checkGround(boolean left)
	{
		int offsetX0, offsetX1, offsetY, deltaX;
		offsetY = m_colBoxNow[3];
		if (left)
		{
			offsetX0 = m_colBoxNow[0];
			offsetX1 = m_colBoxNow[2];
			deltaX = 1;
		}
		else
		{
			offsetX0 = m_colBoxNow[2];
			offsetX1 = m_colBoxNow[0];
			deltaX = -1;
		}

		int tileX0 = ((m_posX >> 8) + offsetX0) / GE.m_tileWidth;
		int tileX1 = ((m_posX >> 8) + offsetX1) / GE.m_tileWidth + deltaX;
		int tileY = ((m_posY >> 8) + offsetY + 1) / GE.m_tileHeight;
		byte lastPhb = Def.PHB_SOLID, phb;
		for (int x = tileX0; x != tileX1; x += deltaX)
		{
			phb = GE.getPhyParam(x, tileY);
			if (phb == Def.PHB_SOLID)
			{
				if (lastPhb != Def.PHB_SOLID)
				{
                    x -= deltaX;
                    for (int y = tileY + 1; y > tileY; y--)
                    {
                        phb = GE.getPhyParam(x, y);
                        if (phb == Def.PHB_SOLID)
                            return 1;
                    }
                    x += deltaX;
                    if (left)
                        m_grabPosX = (x * GE.m_tileWidth - 1) << 8;
                    else
                        m_grabPosX = ((x+1) * GE.m_tileWidth) << 8;
                    m_grabPosY = (tileY * GE.m_tileHeight - 1) << 8;
					return 2;
				}
				break;
			}
			lastPhb = phb;
		}
		return 0;
	}
	//===================================================================================
	// Enemy Part
	//===================================================================================
	void initAI()
	{
		switch(m_animID)
		{
		case Def.ID_SANDMAN:
            m_hp = m_maxhp = (GE.m_levelSelected < 3) ? 2 : 3; // 2 blood for level 1,2,3
			m_counter = 0;
			if (m_actionIDNow == Def.SANDMAN_HM_GUARD)
				m_start1 = m_start2 = m_posX;
			else if (m_actionIDNow == Def.SANDMAN_HM_WAIT /*|| m_actionIDNow == Def.SANDMAN_BM_CROUNCH*/)
				m_start1 = m_posX;
			break;
		case Def.ID_SAW:
			switch(m_actionIDNow)
			{
				case Def.SAW_MOVELEFT:
				case Def.SAW_MOVERIGHT:
					m_start1 = m_posX - ((m_parameters[0] & 0xff) << 8);
					m_start2 = m_posX + ((m_parameters[1] & 0xff) << 8);
					break;
				default:
					m_start1 = m_posY - ((m_parameters[0] & 0xff) << 8);
					m_start2 = m_posY + ((m_parameters[1] & 0xff) << 8);
			}
			break;
		case Def.ID_GHOST:
			m_hp = m_maxhp = 2;
			break;
		case Def.ID_NAIL:
			m_start1 = m_start2 = m_posY;
			break;
		case Def.ID_BONUS:
			switch(m_actionIDNow)
			{
			case Def.BONUS_CHECKPOINT:
			case Def.BONUS_CHECKPOINT2:
				setAction(m_actionIDNow - Def.BONUS_CHECKPOINT + Def.BONUS_CHECKPOINT_WAIT, 1);
			case Def.BONUS_CHECKPOINT_WAIT:
			case Def.BONUS_CHECKPOINT2_WAIT:
				if (Player.m_prince.m_showDirection > Prince.SHOWDIRECTION_FRAMES && Player.m_prince.intersectColBox(this))
				{
					setAction(m_actionIDNow - Def.BONUS_CHECKPOINT_WAIT + Def.BONUS_CHECKPOINT, 1);
					Player.m_prince.m_showDirection = Prince.SHOWDIRECTION_FRAMES;
				}
				break;
			case Def.BONUS_IND_WALLING:
			case Def.BONUS_IND_WALLJUMP:
			case Def.BONUS_IND_ROLL:
			case Def.BONUS_IND_JUMP:
			case Def.BONUS_IND_UP:
			case Def.BONUS_IND_CLIMBDOWN:
			case Def.BONUS_IND_BOX:
				setAction(m_actionIDNow, 0);
				stopAction(true);
				break;
			case Def.BONUS_WAIT:
			    if (Player.m_prince.ability[m_parameters[0]] > 0)
				{
					m_flag[3] = false;
				}
				break;
			}
			break;
		}
	}
	boolean checkBoxDamage()
	{
		Actor a = Player.checkDamage(this);
		if(a == null) return false;
		switch(a.m_animID)
		{
		case Def.ID_PRINCE:
		case Def.ID_SANDMAN:
			return m_parameters[1] > 0;
		case Def.ID_SAW:
			a.setAction(Def.SAW_CRASH, 1);
		case Def.ID_NAIL:
		case Def.ID_RSWORD:
			return true;
		}
		return false;
	}

	void aiBox()
	{
        switch(m_actionIDNow)
        {
            case Def.BOX_WAIT:
            case Def.BOX_WAIT2:
			    m_vX = 0; // give box velocity each frame
                if(checkBoxDamage())
                {
                    if(m_parameters[2] >= 0)
                    {
						int i = m_parameters[(m_parameters[3] >= 0 && (GE.m_curFrameTime%2) == 0)?3:2];
						if (Player.m_actors[i] == null)
					        Player.m_actors[i] = Player.allocActor(i);
						Actor a = Player.m_actors[i];
						a.m_flag[3] = true;
						a.m_flag[2] = true;
						a.m_posX = (m_posX + ((getColBox()[0] + getColBox()[2]) << 7));
						a.m_posY = m_posY;
                    }
                    Player.m_prince.postMessage(Def.MSG_BOX_AWAY, this);
                    setAction(m_actionIDNow+2, 0);
                }

⌨️ 快捷键说明

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