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

📄 entity.java

📁 一个很不错的j2me的rpg游戏的源码
💻 JAVA
字号:
package mobileRPG.client;

import javax.microedition.lcdui.*;

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 int xTileStart, yTileStart;
	public int xTileTarget, yTileTarget;
	
	public boolean kicking;
	
	public Entity(int xSize, int ySize, int xPosition, int yPosition, Main main, String file, MapCanvas map) {
		this.xSize = xSize;
		this.ySize = ySize;
		this.map = map;
		setXPosition(xPosition);
		setYPosition(yPosition);
		
		imageIndex = 0;
		loadImages(main, file);
		path = null;
		
		// Used in run
		xVelocity = 0;
		yVelocity = 0;
		xTarget = xPosition;
		yTarget = yPosition;
		
		xTileStart = getXTile();
		yTileStart = getYTile();
		xTileTarget = xTarget/16;
		yTileTarget = yTarget/16;
		
		distanceCount = 0;

	}
	
	public Image getCurrentImage() {
		if (kicking) {
			return image[imageIndex+4];
		}
		return image[imageIndex];
	}
	
	public void toggleKick() {
		kicking = !kicking;
	}
	
	private int getXTile() {return xPosition / 16;}
	private int getYTile() {return yPosition / 16;}
	
	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 isMoving() {
		if (path != null) {
			EntityPath ep = path.getLast();
			if (ep == null) {
				return false;
			} else if (ep.isCompleted()) {
				return false;
			}
			return true;
		}
		return false;
	}
	
	public void loadImages(Main main, String file) {
		ConfigFile cf = new ConfigFile(file);
		
		// First get the image set
		ImageSet imageset = main.imageSetLoader.loadImages(cf, "Character", false);
		
		// Next put the images in their place		
		image = new Image[8];
		for (int n = 0; n < image.length; n++) {
			image[n] = imageset.getImage(cf.readIntLine());
		}
		
		cf.close();
		
	}
		
	public synchronized boolean move(int dirCode) {
		EntityPath e = path;
		int xVector = 0;
		int yVector = 0;
		
		int xPrev, yPrev;
		int xTarget_new, yTarget_new;
		int xTileTarget_new, yTileTarget_new;
		
		     if (dirCode == DIR_UP)    {yVector = ySize * -1;}
		else if (dirCode == DIR_DOWN)  {yVector = ySize;}
		else if (dirCode == DIR_LEFT)  {xVector = xSize * -1;}
		else if (dirCode == DIR_RIGHT) {xVector = xSize;}
		
		if (e == null) {
			xPrev = xPosition;
			yPrev = yPosition;
		} else {
			xPrev = e.getXTarget();
			yPrev = e.getYTarget();
		}
		
		xTarget_new = xPrev + xVector;
		yTarget_new = yPrev + yVector;
		
		// Check to see if this is a valid target
		xTileTarget_new = xTarget_new / 16;
		yTileTarget_new = yTarget_new / 16;
		
		if (map.isPassable(xTileTarget_new, yTileTarget_new)) {
			// target is valid, procede with adding
			addEntityPath(xTarget_new, yTarget_new);
			return true;
		} else {
			// Target is blocked, input a push
			addEntityPush(xPrev, yPrev, xTarget_new, yTarget_new);
		}
		
		return false;

	}
	
	public void addEntityPath(int x, int y) {
		// Add this path onto the front a linked list with path info in it.
		path = new EntityPath(x, y, path);
	}
	
	public void addEntityPush(int x, int y, int xPush, int yPush) {
		// Add this path onto the front a linked list with path info in it.
		path = new EntityPush(x, y, path, xPush, yPush);
	}
	
	public void run() {
		int x, y;
		
		onTime();
		processActiveScripts();
		
		// If the entity is at its target position AND it has pathes queued up
		if (path != null && xTarget == xPosition && yTarget == yPosition) {
			
			// Set the current tile
			xTileStart = getXTile();
			yTileStart = getYTile();
			
			// Reset the velocity to 0
			xVelocity = 0;
			yVelocity = 0;
			distanceCount = xSize / 4;
			
			// get the top of the linked list
			EntityPath ep = path.getLast();
			
			// Complete the last path if it is active
			// the entity has arrived at the desired spot
			if (ep != null && ep.isActive()) {
				ep.setCompleted();
				ep.setActive(false);
				ep = path.getLast();
			}
			
			// If there is a queued path that hasn't been completed, run it
			if (ep != null) {
				if (!ep.isCompleted()) {
					if (ep.isPush()) {
						setActiveEntityPush((EntityPush)ep);
					} else {
						setActiveEntityPath(ep);
					}
				}
			}
			
		}
		
		// Determine which tiles need to be refreshed, get the previous tile and the current tile
		x = xPosition / 16;
		y = yPosition / 16;
		if (!map.isScrollWithCharacter()) {
			map.setNeedsRefresh(x, y);
			map.setNeedsRefresh(x + Math.abs(xVelocity), y + Math.abs(yVelocity));
		}
		
		// Add the velocity on to the position
		xPosition += xVelocity;
		yPosition += yVelocity;
		
		// Determine if the character image needs to change to the other foot.
		distanceCount += Math.abs(xVelocity) + Math.abs(yVelocity);
		if (distanceCount >= xSize / 2) {
			if (imageIndex > 3) {imageIndex -= 4;}
			else {imageIndex += 4;}
			distanceCount = 0;
		}
		
		map.checkForScrolling();

	}
	
	private void setActiveEntityPush(EntityPush ep) {
		ep.setActive(true);
		
		// Make these local variables
		int xTarget = ep.xPushTarget;
		int yTarget = ep.yPushTarget;
		
		if (yTarget < yPosition) {
			imageIndex = 2;
		} else if (yTarget > yPosition) {
			imageIndex = 0;
		} else if (xTarget < xPosition) {
			imageIndex = 1;
		} else if (xTarget > xPosition) {
			imageIndex = 3;
		}
		
		// Push code here.
		// ***************
		// ***************
		// ***************
	}
	
	private void setActiveEntityPath(EntityPath ep) {
		ep.setActive(true);
		int speed = 2;
		
		xTarget = ep.getXTarget();
		yTarget = ep.getYTarget();
		
		xTileTarget = xTarget/16;
		yTileTarget = yTarget/16;
		
		if (yTarget < yPosition) {
			yVelocity = speed * -1;
			imageIndex = 2;
		} else if (yTarget > yPosition) {
			yVelocity = speed;
			imageIndex = 0;
		} else if (xTarget < xPosition) {
			xVelocity = speed * -1;
			imageIndex = 1;
		} else if (xTarget > xPosition) {
			xVelocity = speed;
			imageIndex = 3;
		}
	}
	
	public void onTime() {}
	
	public void processActiveScripts() {
		
	}
	
}

⌨️ 快捷键说明

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