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

📄 entity.java

📁 一个老外写的一个j2me环境的rpg引擎
💻 JAVA
字号:
package mobileRPG.client;

import javax.microedition.lcdui.*;
import mobileRPG.*;

public class Entity {
	
	static public int DIR_UP = -1;
	static public int DIR_DOWN = -2;
	static public int DIR_LEFT = -3;
	static public int DIR_RIGHT = -4;
	
	private int xSize, ySize;
	private int xPosition, yPosition;
	private EntityPath path;
	private Image[] image;
	private int imageIndex;
	
	// Used in run
	private int xVelocity, yVelocity;
	private int xTarget, yTarget;
	private int distanceCount;
	private MapCanvas map;
	
	public Entity(int xSize, int ySize, int xPosition, int yPosition, ImageSet imageset, String file, MapCanvas map) {
		this.xSize = xSize;
		this.ySize = ySize;
		this.map = map;
		setXPosition(xPosition);
		setYPosition(yPosition);
		
		loadImages(imageset, file);
		path = null;
		
		// Used in run
		xVelocity = 0;
		yVelocity = 0;
		xTarget = xPosition;
		yTarget = yPosition;
		distanceCount = 0;

	}
	
	public int getXSize() {return xSize;}
	public int getYSize() {return ySize;}
	
	public int getXPosition() {return xPosition;}
	public int getYPosition() {return yPosition;}
	
	public void setXPosition(int xPosition) {this.xPosition = xPosition;}
	public void setYPosition(int yPosition) {this.yPosition = yPosition;}
	
	public boolean loadImages(ImageSet imageset, String file) {
		ConfigFile cf = new ConfigFile(file);
		image = new Image[8];
		for (int n = 0; n < image.length; n++) {
			image[n] = imageset.getImage(cf.readIntLine());
		}
		imageIndex = 0;
		return true;
	}
	
	public void paint(Graphics g) {
		g.drawImage(image[imageIndex], xPosition, yPosition, Graphics.TOP|Graphics.LEFT);
	}
	
	public void addEntityPath(int x, int y) {
		path = new EntityPath(x, y, path);
	}
	
	public synchronized void move(int dirCode) {
		EntityPath e = path;
		int xVector = 0;
		int yVector = 0;
		
		if (dirCode == DIR_UP) {yVector = -1 * ySize;}
		else if (dirCode == DIR_DOWN) {yVector = ySize;}
		else if (dirCode == DIR_LEFT) {xVector = -1 * xSize;}
		else if (dirCode == DIR_RIGHT) {xVector = xSize;}
		
		if (e == null) {
			addEntityPath(xPosition + xVector, yPosition + yVector);
		} else {
			addEntityPath(e.getXTarget() + xVector, e.getYTarget() + yVector);
		}

	}
	
	public void run() {
		int x, y;
		
		if (xTarget == xPosition && yTarget == yPosition) {
			xVelocity = 0;
			yVelocity = 0;
			distanceCount = xSize / 4;

			EntityPath e = path;
			if (e != null) {e = e.getLast();}
			
			if (e != null && !e.isCompleted()) {
				e.setCompleted();
				xTarget = e.getXTarget();
				yTarget = e.getYTarget();
				
				if (yTarget < yPosition) {
					yVelocity = -1;
					imageIndex = 2;
				} else if (yTarget > yPosition) {
					yVelocity = 1;
					imageIndex = 0;
				} else if (xTarget < xPosition) {
					xVelocity = -1;
					imageIndex = 1;
				} else if (xTarget > xPosition) {
					xVelocity = 1;
					imageIndex = 3;
				}
			}
		}
		
		x = xPosition / 16;
		y = yPosition / 16;
		map.setNeedsRefresh(x, y);
		map.setNeedsRefresh(x + Math.abs(xVelocity), y + Math.abs(yVelocity));
		
		xPosition += xVelocity;
		yPosition += yVelocity;
		
		distanceCount += Math.abs(xVelocity) + Math.abs(yVelocity);
		if (distanceCount >= xSize / 2) {
			if (imageIndex > 3) {imageIndex -= 4;}
			else {imageIndex += 4;}
			distanceCount = 0;
		}

	}
}

⌨️ 快捷键说明

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