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

📄 npc.java

📁 手机射击游戏源代码,nokia s60模拟器开发包,eclipse工具开发.不可用于商业用途.
💻 JAVA
字号:
package src;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

public class NPC extends Character {
	boolean isAlive = true;

	boolean isHit = false;

	int Type; //类型

	int HP; //生命值

	int npcDirect = DIRECTION_LEFT;

	int birthX, birthY;

	int left, right, up, down, v;

	int paoState1 = 0;

//	int paoState2 = 0;
//
//	int paoState3 = 0;

	int cellWidth, cellHeight;

	int npcBoom[] = { 0, 0 };

	int npc01_run[] = { 0, 1 };

	int npc02_run_left[] = { 0, 0, 1, 1, 2, 2, 0, 0 };

//	int npc03_run_left[] = { 7, 7, 8, 8, 9, 9, 7, 7 };

	int npc02_fire_left[] = { 3, 3, 4, 4, 4 };

//	int npc03_fire_left[] = { 10, 11, 11 };

	int stand_left[] = { 1 };

//	int stand_right[] = { 8 };

	int hit_left[] = { 6, 6, 6 };

//	int hit_right[] = { 13, 13, 13 };

	int npc02_death_left[] = { 5, 5, 5, 5 };

//	int npc02_death_right[] = { 12, 12, 12, 12, 12, 12, 12 };

	int npc04_stand[] = { 0 };

	int npc04_fire[] = { 1, 1, 1, 1, 0, 0, 0, 0 };

	int npc04_death[] = { 2, 2, 2, 2};

//	Player player;

	Image imgNPC;

//	Bullets npc4Bullet;

	public NPC(int id, String name, Image image, int width, int height) {
		super(id, name, image, width, height);
		Type = id;
		HP = Type * 5;
		imgNPC = image;
		left = right = up = down = 0;
		v = 0;
		cellWidth = width;
		cellHeight = height;
		isAlive = false;
	}

	public void initNPC(int birth_x, int birth_y) {
		birthX = birth_x;
		birthY = birth_y;
		setPosition(birth_x, birth_y);
		if (id == 1)
			drawMove(DIRECTION_LEFT);
		if (id == 2)
			drawMove(DIRECTION_LEFT);
		if (id == 3)
			drawStand(DIRECTION_LEFT);
		if (id == 4)
			drawStand(DIRECTION_LEFT);
		if (id == 5)
			drawStand(DIRECTION_LEFT);
		if (id == 6)
			drawStand(DIRECTION_LEFT);
	}

	void drawFire(int direct) {
		switch (id) {
		case 2:
			if (direct == DIRECTION_LEFT) {
				frameSequence = npc02_fire_left;
			} 
//			else if (direct == DIRECTION_RIGHT) {
//				frameSequence = npc02_fire_right;
//			}
			v = 0;
			break;
		case 3:
			if (direct == DIRECTION_LEFT) {
				frameSequence = npc02_fire_left;
			} 
//			else if (direct == DIRECTION_RIGHT) {
//				frameSequence = npc02_fire_right;
//			}
			v = 0;
			break;
		case 4:
			if (direct == DIRECTION_LEFT) {
				frameSequence = npc04_fire;
			}
			v = 0;
			break;
		case 5:
			if (direct == DIRECTION_LEFT) {
				frameSequence = npc04_fire;
			}
			v = 0;
			break;
		case 6:
			if (direct == DIRECTION_LEFT) {
				frameSequence = npc04_fire;
			}
			v = 0;
			break;
		}
	}

	void drawMove(int direct) {
		switch (id) {
		case 1:
			setImage(imgNPC, cellWidth, cellHeight);
			frameSequence = npc01_run;
			v = 0;
			break;
		case 2:
			if (direct == DIRECTION_LEFT) {
				frameSequence = npc02_run_left;
			}
//			else if (direct == DIRECTION_RIGHT) {
//				frameSequence = npc02_run_right;
//			}
			v = 1;
			break;
		case 3:
			if (direct == DIRECTION_LEFT) {
				frameSequence = npc02_run_left;
			} 
//			else if (direct == DIRECTION_RIGHT) {
//				frameSequence = npc02_run_right;
//			}
			v = 1;
			break;
		}
	}

	void drawStand(int direct) {
		switch (id) {
		case 2:
			if (direct == DIRECTION_LEFT) {
				frameSequence = stand_left;
			}
//			else if (direct == DIRECTION_RIGHT) {
//				frameSequence = stand_right;
//			}
			v = 0;
			break;
		case 3:
			if (direct == DIRECTION_LEFT) {
				frameSequence = stand_left;
			}
//			else if (direct == DIRECTION_RIGHT) {
//				frameSequence = stand_right;
//			}
			v = 0;
			break;
		case 4:
			frameSequence = npc04_stand;
			break;
		case 5:
			frameSequence = npc04_stand;
			break;
		case 6:
			frameSequence = npc04_stand;
			break;
		}
	}

//	void drawHit(int direct) {
//		switch (id) {
//		case 2:
//			if (direct == DIRECTION_LEFT) {
//				frameSequence = hit_left;
//			} 
////			else if (direct == DIRECTION_RIGHT) {
////				frameSequence = hit_right;
////			}
//			break;
//		case 3:
//			if (direct == DIRECTION_LEFT) {
//				frameSequence = hit_left;
//			}
////			else if (direct == DIRECTION_RIGHT) {
////				frameSequence = hit_right;
////			}
//			break;
//		}
//	}

	void drawDead(int direct) { //npc02_death_right
		switch (id) {
		case 2:
			frameSequence = npc02_death_left;
			v = 0;
			if (getFrame() == 3) {
				isAlive = false;
			}
			break;
		case 3:
			frameSequence = npc02_death_left;
			v = 0;
			if (getFrame() == 3) {	//??????????
				isAlive = false;
			}
			break;
		case 4:
			frameSequence = npc04_death;
			v = 0;
			if (getFrame() == 3) {
				isAlive = false;
			}
			break;
		}
	}

	public void Move(int direct) {
		switch (direct) {
		case DIRECTION_LEFT:
			left = -v;
			break;
		case DIRECTION_RIGHT:
			right = v;
			break;
		case DIRECTION_UP:
			up = -v;
			break;
		case DIRECTION_DOWN:
			down = v;
			break;
		}
		x += right + left;
	}

	boolean ss = true;

	/** 要移动Sprite时所调用的方法 */
	public void MoveNPC01(TiledLayer map, Sprite sprite) {
		if (paoState1 == 0) {
			x--;
		}

		if (x <= 150) {
			paoState1 = 1; //paoState1=1
		}

		if (paoState1 == 1) {
			y += 5;
		}

		if (sprite.collidesWith(map)) {
			paoState1 = 2;
			if (ss) {
				sprite.y += 10;
				ss = false;
			}
		}

		if (paoState1 == 2) {
			y -= 5;
			x -= 5;
		}
	}

	public void NPC1Manage(Graphics g, int direct, TiledLayer map, Sprite sprite) {
		if (isAlive) {
			nextFrame();
			paint(g);
			MoveNPC01(map, sprite);
		}
//		if (isHit == true) {
//			//调用NPC被击中图片
//		}
	}

	int m = 0;

	public void NPC4Manage(Graphics g, int direct,int firetime) {
		if (isAlive) {
			m++;
			nextFrame();
			paint(g);
			if (HP <= 0) {
				drawDead(direct);
				return;
			}
			if (m == firetime) {
				drawFire(direct);
				isFire = true;
			}
			if (m == firetime+4) {
				drawStand(direct);
				m = 0;
			}
		}
	}

	int i = 0;	

	boolean isFire = false;

	public void NPC2Manage(Graphics g, int direct, int mission,int firetime) {
		if (isAlive) {
			i++;
			nextFrame();
			paint(g);
			if (HP <= 1) {
				drawDead(direct);
				return;
			}
			if (i == firetime) {
				drawFire(direct);
				isFire = true;
			}
			if (i == firetime+5) {
				drawMove(direct);
				i = 0;
			}
			if (id==2||id==3&&mission>1)
				Move(direct);
		}
	}
	
	//test
	int npcBulletsCount = 0;

	public void setDirection(int direction) {
		npcDirect = direction;
	}

	void attack(Sprite target) {

	}

	void destroy() {

	}

	void setAlive(boolean isAlive) {
		this.isAlive = isAlive;
	}

	boolean isAlive() {
		return isAlive;
	}

//	void setHit(boolean isHit) {
//		this.isHit = isHit;
//	}
//
//	boolean isHit() {
//		return isHit;
//	}
}

⌨️ 快捷键说明

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