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

📄 player.java

📁 J2ME开发的手机游戏需要安装诺基亚手机模拟器才能正常运行
💻 JAVA
字号:
package org.gamecollege.j2me.rpg;

import javax.microedition.lcdui.game.Layer;
import javax.microedition.lcdui.game.LayerManager;
import javax.microedition.lcdui.game.Sprite;

import java.util.Vector;

/**
 * 该类封装主角对象
 */
public class Player {
	/**
	 * 主角运动方向常量 向下
	 */
	public static final int DOWN = 0;

	/**
	 * 主角运动方向常量 向上
	 */

	public static final int UP = 3;

	/**
	 * 主角运动方向常量 向左
	 */

	public static final int LEFT = 6;

	/**
	 * 主角运动方向常量 向右
	 */

	public static final int RIGHT = 9;

	/**
	 * 主角运动方向常量 静止
	 */

	public static final int STILL = -1;

	//当前运动方向
	int dir;

	//某一方向移动帧数:比如向右走的动画有3帧
	int stepCount;

	//某一方向最大移动帧数
	public static final int FRAMES_MOVE = 3;

	//武器集合
	Vector weaponVec = new Vector();

	//护具集合
	Vector jacketVec = new Vector();

	//药品集合
	Vector medicVec = new Vector();

	//技能集合
	Vector skillVec = new Vector();

	//主角名称
	String name;

	//攻击状态 -1:未攻击,0:普通攻击状态0 1:普通攻击状态1 2:普通攻击状态2 1979:魔法攻击0 1980 魔法攻击1
	int attackStatus = -1;

	/**
	 * 主角上次移动的距离,x方向
	 * 
	 * @see #undoMove()
	 */
	int lastStep_x;

	/**
	 * 主角上次移动的距离,y方向
	 * 
	 * @see #undoMove()
	 */
	int lastStep_y;

	//移动速度
	int rate = -1;

	//金钱
	int money = -1;

	//hp
	int hp = -1;

	//mp
	int mp = -1;

	//关卡初始化后,具有的经验值
	int levelInitExp = -1;

	//经验值
	int exp = -1;

	//当前等级
	int rank = -1;

	//下一级所要求的经验值
	int nextExp = -1;

	//攻击力
	int attackPoint = -1;

	//防御力
	int defendPoint = -1;

	//所在地图的行号和列号
	int col = -1, row = -1;

	//当前武器ID
	int curWeaponID = -1;

	//当前护具ID
	int curDefendID = -1;

	//主角移动次数,利用该次数可以控制什么时候出现随机敌人
	int moveTimes;

	//主角所拥有的物品的id字串,用"|"分割,这是持久存储用的
	String propertyIDs;

	//主角所拥有的技能的id字串,用"|"分割,这是持久存储用的
	String skillIDs;

	//攻击之前的主角精灵
	private Sprite beforeAttackSprite;

	//攻击中的主角精灵
	private Sprite attackingSprite;

	//行走中的主角精灵
	private Sprite walkSprite;

	//主角的状态常量:行走中
	public static final int STATUS_WALK = 0;

	//主角的状态常量:攻击前
	public static final int STATUS_BEFORE_ATTACK = 1;

	//主角的状态常量:攻击中
	public static final int STATUS_ATTACk = 2;

	//	战斗中释放的技能
	Skill fireSkill;

	/**
	 * 根据主角名字够造主角对象
	 * 
	 * @param name
	 *            主角名字
	 */
	public Player(String name) {

		this.name = name;
		constructSprites();
	}

	/**
	 * 初始化主角的所有精灵
	 *  
	 */

	private void constructSprites() {
		walkSprite = new Sprite(ResourceLoader.players[0], 32, 32);
		beforeAttackSprite = new Sprite(ResourceLoader.players[1], 41, 46);
		attackingSprite = new Sprite(ResourceLoader.players[2], 32, 32);
	}

	/**
	 * 移动方法
	 * @param _dir
	 */
	public void move(int _dir) {
		//方向和原方向一致,则修改动画帧
		if (this.dir == _dir) {
			stepCount = stepCount + 1 >= FRAMES_MOVE ? 0 : stepCount + 1;

		} else if (_dir != STILL) {
			//新方向,从第一帧开始
			this.dir = _dir;
			stepCount = 0;
		} else {
			
			//静止状态
			stepCount = 0;
			walkSprite.setFrame(dir);
			return;
		}
		//修改动画帧
		walkSprite.setFrame(dir + stepCount);

		int stepx = 0;
		int stepy = 0;

		switch (dir) {
		case UP:

			stepy = -rate;
			break;
		case DOWN:

			stepy = rate;
			break;
		case LEFT:

			stepx = -rate;
			break;
		case RIGHT:

			stepx = rate;
			break;

		}
		//移动位置
		walkSprite.setPosition(walkSprite.getX() + stepx, walkSprite.getY()
				+ stepy);
		this.lastStep_x = stepx;
		this.lastStep_y = stepy;
		//更新col,row属性
		this.col = walkSprite.getX() / MyRPGGameMIDlet.mc.curLevel.tileWidth;
		this.row = walkSprite.getY() / MyRPGGameMIDlet.mc.curLevel.tileHeight;
		//增加移动次数
		moveTimes++;
	}

	/**
	 * 撤销主角上次移动动作
	 */
	public void undoMove() {
		walkSprite.move(-lastStep_x, -lastStep_y);
	}

	/**
	 * 添加主角携带物品
	 * 
	 * @param p
	 * 1:武器类 2:药品类 3:护具类,4:技能 物品/道具
	 */
	public void addSth(RPGObject p) {
		if (p instanceof Skill) {
			this.skillVec.addElement(p);
		} else {
			Property p2 = (Property) p;
			switch (p2.type) {
			case 1:
				this.weaponVec.addElement(p);
				break;
			case 2:
				this.medicVec.addElement(p);
				break;
			case 3:
				this.jacketVec.addElement(p);
				break;
			}
		}
	}
	
	/**
	 * 删除主角拥有的某个物品
	 * @param p
	 */

	public void removeSth(RPGObject p) {
		if (p instanceof Skill) {
			this.skillVec.removeElement(p);
		} else {
			Property p2 = (Property) p;
			switch (p2.type) {
			case 1:
				this.weaponVec.removeElement(p);
				break;
			case 2:
				this.medicVec.removeElement(p);
				break;
			case 3:
				this.jacketVec.removeElement(p);
				break;
			}
		}

	}
	
	/**
	 * 获取主角在不同状态下的精灵
	 * @param status
	 * @return
	 */

	public Sprite getSprite(int status) {
		switch (status) {
		case STATUS_ATTACk:
			return attackingSprite;
		case STATUS_BEFORE_ATTACK:
			
			return this.beforeAttackSprite;
		case STATUS_WALK:
		default:
			return this.walkSprite;

		}

	}
	
	/**
	 * 主角使用各种物品后的效果
	 * @param ro
	 */

	public void useObject(RPGObject ro) {
		if (ro instanceof Skill) {

		} else {
			Property p = (Property) ro;
			if (p.type == 1) {
				this.curWeaponID = p.ID;
			} else if (p.type == 2) {
				this.hp += p.addHP;
				this.mp += p.addMP;
				this.medicVec.removeElement(p);
			} else if (p.type == 3) {
				this.curDefendID = p.ID;
			}
		}
	}
	
	
	/**
	 * 设置攻击状态。攻击状态为0的情况无需设置。一旦有对象把状态设置为0,则游戏后台线程会自动使用该方法进行状态转换
	 * @param status
	 */

	public void setAttackStatusSprite(int status) {

		this.attackStatus = status;
		switch (status) {
		case -1:
			//攻击之前
			beforeAttackSprite.setPosition(80, 120);
			beforeAttackSprite.setFrame(0);

			break;

		case 1:
			//攻击1
			attackingSprite.setPosition(83, 108);
			attackingSprite.setFrame(1);
			break;
		case 2:
			//攻击2
			attackingSprite.setFrame(0);
			attackingSprite.setPosition(83, 108);
			break;
		//魔法攻击状态0
		case 1979:
			beforeAttackSprite.setPosition(80, 120);
			beforeAttackSprite.setFrame(1);

			fireSkill.sprite.setFrame(0);
			fireSkill.sprite.setPosition(110, 120);
			break;
		case 1980:
			beforeAttackSprite.setPosition(80, 120);
			beforeAttackSprite.setFrame(1);
			fireSkill.sprite.setFrame(0);
			fireSkill.sprite.setPosition(110, 100);
			break;
		}
		
		//修改战斗时所用的LayerManager

		LayerManager lm = MyRPGGameMIDlet.mc.fightLayerManager;

		if (lm != null) {

			if (status < 1979) {

				if (lm.getSize() == 3) {

					Layer oldLayer = lm.getLayerAt(0);
					lm.remove(oldLayer);

				} else {
					Layer oldLayer = lm.getLayerAt(0);
					lm.remove(oldLayer);
					oldLayer = lm.getLayerAt(1);
					lm.remove(oldLayer);
				}

				if (status == -1) {
					lm.insert(beforeAttackSprite, 0);
				} else {
					lm.insert(attackingSprite, 0);
				}
			} else {
				if (lm.getSize() == 3) {

					Layer oldLayer = lm.getLayerAt(0);
					lm.remove(oldLayer);

				} else {
					Layer oldLayer = lm.getLayerAt(0);
					lm.remove(oldLayer);
					oldLayer = lm.getLayerAt(1);
					lm.remove(oldLayer);
				}

				//添加人
				lm.insert(beforeAttackSprite, 1);
				//添加魔法
				lm.insert(fireSkill.sprite, 0);
			}

		}

	}
	
	
	/**
	 * 获取主角的真实攻击力
	 * 真实攻击力=本身攻击力+武器护具增加的攻击力
	 * 或者为魔法技能攻击力
	 * @author Jagie
	 *
	 */

	public int getRealAttackPoint() {

		int result = this.attackPoint;
		if (attackStatus < 1979) {
			Property w = (Property) MyRPGGameMIDlet.mc.curLevel.properties
					.get(new Integer(curWeaponID));
			Property d = (Property) MyRPGGameMIDlet.mc.curLevel.properties
					.get(new Integer(curDefendID));

			if (w != null) {
				result += w.addAttackPoint;

			}

			if (d != null) {
				result += d.addAttackPoint;

			}
		} else {
			result += fireSkill.addAttackPoint;
		}

		return result;

	}
	
	
	/**
	 * 获取主角的真实防御力
	 * 真实防御力=本身防御力+武器护具增加的防御力
	 * 或者为魔法技能防御力
	 * @return
	 */

	public int getRealDefendPoint() {

		int result = this.defendPoint;
		if (attackStatus < 1979) {
			Property w = (Property) MyRPGGameMIDlet.mc.curLevel.properties
					.get(new Integer(curWeaponID));
			Property d = (Property) MyRPGGameMIDlet.mc.curLevel.properties
					.get(new Integer(curDefendID));

			if (w != null) {
				result += w.addDefendPoint;

			}

			if (d != null) {
				result += d.addDefendPoint;

			}
		} else {
			result += fireSkill.addDefendPoint;
		}

		return result;

	}

}

⌨️ 快捷键说明

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