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

📄 npc.java

📁 一个小游戏.可以在手机上用的
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
import java.util.Vector;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
/**
* 非作者授权,请勿用于商业用途。
* 玩家控制的精灵
* bruce.fine@gmail.com
*/
public class NPC extends MySprite {
	// up down left ,right
	// x0, y0, x1, y1
	short nUpRectNum = 0;

	short nDownRectNum = 0;

	short nLeftRectNum = 0;

	short nRightRectNum = 0;
	//碰撞处理区域
	short[][][] nRectCollosion = null;

	Anim anim = null;

	boolean isHandleCPC = false;

	// 可放置炸弹数量
	int nBoomNum = 1;

	public NPC() {
		nUpRectNum = 1;
		nDownRectNum = 1;
		nLeftRectNum = 1;
		nRightRectNum = 1;
		nRectCollosion = new short[4][][];

		nRectCollosion[0] = new short[nUpRectNum][4];
		nRectCollosion[1] = new short[nDownRectNum][4];
		nRectCollosion[2] = new short[nLeftRectNum][4];
		nRectCollosion[3] = new short[nRightRectNum][4];

		nRectCollosion[0][0] = new short[] { 0, 0, 16, 4 };
		nRectCollosion[1][0] = new short[] { 0, 12, 16, 16 };
		nRectCollosion[2][0] = new short[] { 0, 0, 4, 16 };
		nRectCollosion[3][0] = new short[] { 12, 0, 16, 16 };

	}

	public void initAnim(Anim anim) {
		this.anim = anim;

	}

	//
	int nTimerMP = 0;

	int nTimerMPMax = 10;

	int nMPPlus = 1;

	public void update() {

		if (nHpTimer < nHpTimerMax) {
			nHpTimer++;
		}
		// MP 自动恢复:)
		if (nMP < nMPMax) {
			if (nTimerMP < nTimerMPMax) {
				nTimerMP++;
			} else {
				nTimerMP = 0;
				nMP += nMPPlus;

			}
		}
		if (nEX >= nEXMax) {
			nEX = 0;
			// int nt = nEXMax - nEX;
			// nEXMax *= nEXMax;
			// nMPMax *= 2;
			// nHPMax *= 2;
			// nHP = nHPMax;
			// nMP = nMPMax;
			// nEX = nt;
			// nLevel += 1;
			// MyGameCanvas.instance.strInfo = "升级喽:)";
		}
		if (++nFrameCac > nFrameCacMax) {
			nFrameCac = 0;
			if (isMoving) {
				anim.update();
				switch (bDirection) {
				case Canvas.UP: {
					nY -= nSpeed;
					if (isCollsionWithMap(Canvas.UP,
							getCollosionWithMap(nX, nY))
							|| isCollsionWithCPC(Canvas.UP)) {
						nY += nSpeed;
					}
				}
					break;
				case Canvas.DOWN: {
					nY += nSpeed;
					if (isCollsionWithMap(Canvas.DOWN, getCollosionWithMap(nX,
							nY))
							|| isCollsionWithCPC(Canvas.DOWN)) {
						nY -= nSpeed;
					}
				}
					break;
				case Canvas.LEFT: {
					nX -= nSpeed;
					if (isCollsionWithMap(Canvas.LEFT, getCollosionWithMap(nX,
							nY))
							|| isCollsionWithCPC(Canvas.LEFT)) {
						nX += nSpeed;
					}
				}
					break;
				case Canvas.RIGHT: {
					nX += nSpeed;
					if (isCollsionWithMap(Canvas.RIGHT, getCollosionWithMap(nX,
							nY))
							|| isCollsionWithCPC(Canvas.RIGHT)) {
						nX -= nSpeed;
					}
				}
					break;

				}
				isMoving = false;
			}
		}

		if (nX < 0) {
			nX = 0;
		}
		if (nX > MyGameCanvas.snMapWidth) {
			nX = MyGameCanvas.snMapWidth;
		}
		if (nY < 0) {
			nY = 0;
		}
		if (nY > MyGameCanvas.snMapHeight) {
			nY = MyGameCanvas.snMapHeight;
		}

		nXInScreen = nX - MyGameCanvas.snMapX+ MyGameCanvas.snWindowX;
		nYInScreen = nY - MyGameCanvas.snMapY+ MyGameCanvas.snWindowY;
		//
		if (anim!=null) {
			if (MyGameCanvas.instance.bGameResult != 0) {
				if (anim.nActionId != 1) {
					anim.changeAction(Canvas.DOWN);
				}
			}
		}
	}

	public void draw(Graphics g) {
		if (nHpTimer < nHpTimerMax) {
			if (nHpTimer % 2 == 0) {
				anim.draw(g, nXInScreen, nYInScreen - 10);
			}
		} else {
			anim.draw(g, nXInScreen, nYInScreen - 10);
		}

	}

	public void doKey(int key) {
		if (MyGameCanvas.instance.bGameResult != 0) {
			return;
		}
		if (key == 0) {
			// anim.isLoop = false;
		}
		if (nHP <= 0) {
			isMoving = false;
			return;
		}
		if (nX % Consts.SN_TILE_WIDTH != 0 || nY % Consts.SN_TILE_HEIGHT != 0) {
			isMoving = true;
			return;
		} else {
			isMoving = false;
		}
		switch (key) {

		case Canvas.UP: {
			isMoving = true;
			if (bDirection == Canvas.UP) {
				anim.isStop = false;
			} else {
				isMoving = false;
				bDirection = Canvas.UP;
				anim.changeAction(Canvas.UP);
			}

		}
			break;
		case Canvas.DOWN: {
			isMoving = true;
			if (bDirection == Canvas.DOWN) {
				anim.isStop = false;
			} else {
				isMoving = false;
				bDirection = Canvas.DOWN;
				anim.changeAction(Canvas.DOWN);
			}
		}
			break;
		case Canvas.LEFT: {
			isMoving = true;
			if (bDirection == Canvas.LEFT) {
				anim.isStop = false;
			} else {
				isMoving = false;
				bDirection = Canvas.LEFT;
				anim.changeAction(Canvas.LEFT);
			}
		}
			break;
		case Canvas.RIGHT: {
			isMoving = true;
			if (bDirection == Canvas.RIGHT) {
				anim.isStop = false;
			} else {
				isMoving = false;
				bDirection = Canvas.RIGHT;
				anim.changeAction(Canvas.RIGHT);
			}
		}
			break;
		case Canvas.FIRE: {
			isMoving = false;
			int r1x1 = 0;
			int r1y1 = 0;
			int r1x2 = 0;
			int r1y2 = 0;
			int r2x1 = nX;
			int r2y1 = nY;
			int r2x2 = nX + 16;
			int r2y2 = nY + 16;

			if (!isHandleCPC) {
				boolean isFighting = true;
				for (int i = 0; i < MyGameCanvas.vecticUnits.size(); i++) {
					CPC cpc = (CPC) MyGameCanvas.vecticUnits.elementAt(i);
					if (cpc.bVar == CPC.B_VAR_BOX) {
						if (!cpc.isHandleByNPC) {
							// Consts.log("hello");
							if (r2y1 == cpc.nY) {
								if (r2x1 + 16 == cpc.nX) {
									if (Canvas.RIGHT == bDirection) {
										cpc.upBox();
										isHandleCPC = true;
									}

								}
								if (r2x1 - 16 == cpc.nX) {
									if (Canvas.LEFT == bDirection) {
										cpc.upBox();
										isHandleCPC = true;
									}

								}

							}
							if (r2x1 == cpc.nX) {
								if (r2y1 + 16 == cpc.nY) {
									if (Canvas.DOWN == bDirection) {
										cpc.upBox();
										isHandleCPC = true;
									}

								}
								if (r2y1 - 16 == cpc.nY) {
									if (Canvas.UP == bDirection) {
										cpc.upBox();
										isHandleCPC = true;
									}

								}

							}
							if (isHandleCPC) {
								isFighting = false;
								break;
							}
						}
					}
				}

				boolean ttdia = false;
				for (int i = 0; i < MyGameCanvas.vecticUnits.size(); i++) {
					CPC cpc = (CPC) MyGameCanvas.vecticUnits.elementAt(i);
					if (cpc.bVar == CPC.B_VAR_NPC) {
						r1x1 = cpc.nX;
						r1y1 = cpc.nY;
						r1x2 = cpc.nX + 16;
						r1y2 = cpc.nY + 16;
						r2x1 = nX;
						r2y1 = nY;
						r2x2 = nX + 16;
						r2y2 = nY + 16;

						if (!MyGameCanvas.instance.isDialogWorking) {
							if (r2y1 == cpc.nY) {
								if (r2x1 + 16 == cpc.nX) {
									if (bDirection == Canvas.RIGHT) {
										if (cpc.bDirection != Canvas.LEFT) {
											cpc.anim.changeAction(Canvas.LEFT);
											cpc.anim.isStop = true;

										}
										ttdia = true;
									}

								}
								if (r2x1 - 16 == cpc.nX) {
									if (bDirection == Canvas.LEFT) {
										if (cpc.bDirection != Canvas.RIGHT) {
											cpc.anim.changeAction(Canvas.RIGHT);
											cpc.anim.isStop = true;

										}
										ttdia = true;
									}
								}

							}
							if (r2x1 == cpc.nX) {
								if (r2y1 + 16 == cpc.nY) {
									if (bDirection == Canvas.DOWN) {
										if (cpc.bDirection != Canvas.UP) {
											cpc.anim.changeAction(Canvas.UP);
											cpc.anim.isStop = true;

										}
										ttdia = true;
									}
								}
								if (r2y1 - 16 == cpc.nY) {
									if (bDirection == Canvas.UP) {
										if (cpc.bDirection != Canvas.DOWN) {
											cpc.anim.changeAction(Canvas.DOWN);
											cpc.anim.isStop = true;

										}
										ttdia = true;
									}
								}

							}

						}

					}

				}
				if (ttdia) {
					MyGameCanvas.instance.initDialog();
					isFighting = false;
					// Consts.log("激活对画");
					break;
				}
				// 地图转动:)
				boolean isMapChange = false;
				for (int i = 0; i < MyGameCanvas.vecticUnits.size(); i++) {
					CPC cpc = (CPC) MyGameCanvas.vecticUnits.elementAt(i);

⌨️ 快捷键说明

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