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

📄 zonsprite.java

📁 基于J2ME 开发的冒险类手机游戏
💻 JAVA
字号:
package zonja;

import java.io.DataInputStream;
import java.io.DataOutputStream;

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

public abstract class ZonSprite extends ZonLayer {
	
	//game related final
	public static final int DIRECTION_HORIZONTAL = 0;
	public static final int DIRECTION_VERTICAL = 1;
	
	public static final int MOVEDIRECTION_LEFT = 0;
	public static final int MOVEDIRECTION_RIGHT = 1;
	
	public static final int LIFTDIRECTION_UP = 2;
	public static final int LIFTDIRECTION_DOWN = 3;
	
	public static final int COLLIDES_NOTHING = 0;
	public static final int COLLIDES_HORIZONTAL = 1;
	public static final int COLLIDES_VERTICAL = 2;
	public static final int COLLIDES_DEAD = 4;


	

	
	// Game related variable
	protected int hVelocity = 0;
	protected int vVelocity = 0;
	protected int hVelTemp = 0;
	protected int vVelTemp = 0;
	protected int moveDirection = MOVEDIRECTION_RIGHT;
	protected boolean isVisible = true;
	
	public int realWidth; 
	public int realHeight; 
	
	public ZonRect rect = new ZonRect();
	
	public boolean isStanding = true;
	protected Image sourceImage = null;
	protected Image reverseImage = null;
	
	protected ZonTileMap map;
	
	protected int frameWidth = 0;
	protected int frameHeight = 0;
	protected int frameIndex = 0;
	protected int forceFrameIndex = -1;    //for life is dead?
	protected int actionFirstIndex = 0;
	protected int actionLastIndex = 0;
	
	protected int worldOffsetX = 0;
	protected int worldOffsetY = 0;

	abstract void mainAction();
	
	public ZonSprite(int scrWidth, int scrHeight) {
		super(scrWidth, scrHeight);
	}
	
	public boolean saveData(DataOutputStream dos) {
		try {
			dos.writeByte(hVelocity);
			dos.writeByte(vVelocity);
			dos.writeByte(moveDirection);
			dos.writeBoolean(isVisible);
			dos.writeBoolean(isStanding);
			dos.writeInt(offsetX);
			dos.writeInt(offsetY);
			dos.writeInt(worldOffsetX);
			dos.writeInt(worldOffsetY);
			dos.writeInt(actionFirstIndex);
			dos.writeInt(actionLastIndex);
			dos.writeInt(frameIndex);
		}
		catch(Exception e) {
			return false;
		}
		
		return true;
	}

	public boolean loadData(DataInputStream dis) {
		try {
			hVelocity = dis.readByte();
			vVelocity = dis.readByte();
			moveDirection = dis.readByte();
			isVisible = dis.readBoolean();
			isStanding = dis.readBoolean();
			offsetX = dis.readInt();
			offsetY = dis.readInt();
			worldOffsetX = dis.readInt();
			worldOffsetY = dis.readInt();
			actionFirstIndex = dis.readInt();
			actionLastIndex = dis.readInt();
			frameIndex = dis.readInt();
		}
		catch(Exception e) {
			return false;
		}
		
		return true;
	}


	protected void setMap(ZonTileMap map){
		this.map = map;	
		worldOffsetX = offsetX - map.getOffsetX();
		worldOffsetY = offsetY - map.getOffsetY();
	}
	
	protected void setWorldOffset(int x, int y){
		worldOffsetX = x;
		worldOffsetY = y;
	}
	protected void changeWorldOffset(int dx, int dy){
		worldOffsetX += dx;
		worldOffsetY += dy;
	}
	
	protected int collidesWith(ZonTileMap map, boolean pixelLevel){
		
		int tileHeight = map.getTileHeight();
		int tileWidth = map.getTileWidth();		
		int res = 0;
		//COLLIDES_VERTICAL
		if(hVelocity > 0){
			int vCollidesX = worldOffsetX + frameWidth + hVelocity -2;
			int vCollidesY1 = worldOffsetY + vVelocity + 2;
			int vCollidesY2 = worldOffsetY + vVelocity + 28;
			
			int arrayIndexY1 = vCollidesY1 / tileHeight;
			int arrayIndexY2 = vCollidesY2 / tileHeight;
			int arrayIndexX = vCollidesX / tileWidth;
			
			int tileIndex1 = map.getTileData(arrayIndexX, arrayIndexY1);
			int tileIndex2 = map.getTileData(arrayIndexX, arrayIndexY2);
			
			int tileFlag1 = (tileIndex1 & 0xF000) >> 12;
			int tileFlag2 = (tileIndex2 & 0xF000) >> 12;
			
			if(tileFlag2 == 0x0001 || tileFlag2 == 0x0002 || tileFlag2 == 0x0003 || tileFlag2 == 0x0005
				|| tileFlag1 == 0x0001 ){
			//	if(!(tileFlag1 == 0x0001 && tileFlag2 == 0x0001)){
					res |= COLLIDES_HORIZONTAL;
					//hVelTemp = arrayIndexX * tileWidth - (worldOffsetX + frameWidth - 4);
					hVelTemp = 0;
			
			//		}
			}

		}else{
			int vCollidesX = worldOffsetX + hVelocity + 2;
			int vCollidesY1 = worldOffsetY + vVelocity + 2;
			int vCollidesY2 = worldOffsetY + vVelocity + 28;
			
			int arrayIndexY1 = vCollidesY1 / tileHeight;
			int arrayIndexY2 = vCollidesY2 / tileHeight;
			int arrayIndexX = vCollidesX / tileWidth;
			
			int tileIndex1 = map.getTileData(arrayIndexX, arrayIndexY1);
			int tileIndex2 = map.getTileData(arrayIndexX, arrayIndexY2);
			
			int tileFlag1 = (tileIndex1 & 0xF000) >> 12;
			int tileFlag2 = (tileIndex2 & 0xF000) >> 12;
			
			if(tileFlag2 == 0x0001 || tileFlag2 == 0x0002 || tileFlag2 == 0x0003 || tileFlag2 == 0x0005
				|| tileFlag1 == 0x0001){
	//			if(!(tileFlag1 == 0x0001 && tileFlag2 == 0x0001)){
					res |= COLLIDES_HORIZONTAL;
					//hVelTemp = arrayIndexX * tileWidth - (worldOffsetX + frameWidth - 4);
					hVelTemp = 0;
				//}
			}
		
		}
		
		if(vVelocity > 0){
			//check 2 part
			int vCollidesX1 = worldOffsetX + 11;
			int vCollidesX2 = worldOffsetX + 22;
			int vCollidesY = (worldOffsetY + vVelocity + frameHeight);
	
			int arrayIndexX1 = vCollidesX1 / tileWidth;
			int arrayIndexX2 = vCollidesX2 / tileWidth;
			int arrayIndexY = vCollidesY / tileHeight;
			
			//get tile index by array index;
			int tileIndex1 = map.getTileData(arrayIndexX1, arrayIndexY);
			int tileIndex2 = map.getTileData(arrayIndexX2, arrayIndexY);
			
			int tileFlag1 = (tileIndex1 & 0xF000) >> 12;
			int tileFlag2 = (tileIndex2 & 0xF000) >> 12;
			if(tileFlag1 == 0x0005 || tileFlag1 == 0x0002 || tileFlag1 == 0x0003 ||
					tileFlag2 == 0x0005 || tileFlag2 == 0x0002 || tileFlag2 == 0x0003 ){
				res |= COLLIDES_VERTICAL;
				vVelTemp = arrayIndexY * tileHeight - worldOffsetY - frameHeight + 1; //modified

				if(vVelTemp < 0){
					vVelTemp = vVelocity;
						isStanding = false;
				}else 
					isStanding = true;
			}
/*			if((moveDirection == MOVEDIRECTION_RIGHT && tileFlag1 == 0x0004) 
					|| (moveDirection == MOVEDIRECTION_LEFT && tileFlag2 == 0x0004)){
				res |= COLLIDES_DEAD;*/
			if(tileFlag1 == 0x0004 && tileFlag2 == 0x0004){
				res |= COLLIDES_DEAD;
			}
		}else{
			int vCollidesX1 = worldOffsetX + 11;
			int vCollidesX2 = worldOffsetX + 22;
			int vCollidesY = (worldOffsetY + vVelocity);
			
			int arrayIndexX1 = vCollidesX1 / tileWidth;
			int arrayIndexX2 = vCollidesX2 / tileWidth;
			int arrayIndexY = vCollidesY / tileHeight;
			
			//get tile index by array index;
			int tileIndex1 = map.getTileData(arrayIndexX1, arrayIndexY);
			int tileIndex2 = map.getTileData(arrayIndexX2, arrayIndexY);
			
			int tileFlag1 = (tileIndex1 & 0xF000) >> 12;
			int tileFlag2 = (tileIndex2 & 0xF000) >> 12;
			
			if(ZonCanvas.gameStage == ZonCanvas.GAMESTAGE_ONE){
				if(tileFlag1 == 0x0005 || tileFlag2 == 0x0005 ){
					res |= COLLIDES_VERTICAL;
					if((arrayIndexY + 1)* tileHeight - worldOffsetY > 0)
						vVelTemp = worldOffsetY - (arrayIndexY + 1) * tileHeight;
				}		
			}
		
		}		
		
		if(res != 0)
			return res;
		else
			return COLLIDES_NOTHING;
	}
	

	public boolean collidesWith(ZonSprite sprite, boolean pixelLevel){
        sprite.rect.setRect(sprite.offsetX + (sprite.frameWidth - sprite.realWidth) / 2, 
    			sprite.offsetY + (sprite.frameHeight - sprite.realHeight) / 2, sprite.realWidth, sprite.realHeight);

        if(moveDirection == MOVEDIRECTION_RIGHT){    	
            rect.setRect(offsetX + frameWidth / 2, 
            		offsetY + (frameHeight - realHeight) / 2, realWidth / 2, realHeight);
     
        	if(ZonRect.checkRectCollisdes(rect, sprite.rect)){
        		return true;
        	}
        	else{
        		return false;
        	}

        }else{
            rect.setRect(offsetX + (frameWidth - realWidth) / 2, 
            		offsetY + (frameHeight - realHeight) / 2, realWidth / 2, realHeight);	
        	if(ZonRect.checkRectCollisdes(rect, sprite.rect)){
        		return true;
        	}
        	else{
        		return false;
        	}
        }
        
	}

	public boolean initSprite(String picName, int spriteWidth, int spriteHeight, 
			boolean buildReverse) {
		sourceImage = findImage(picName);
		if(sourceImage == null) 
			return false;
		
		setSpriteSize(spriteWidth, spriteHeight);
		setSpriteIndex(0);

		return true;
	}
	
	public void setSpriteSize(int width, int height) {
		frameWidth = width;
		frameHeight = height;
	}
	
	public int getFrameWidth() {
		return frameWidth;
	}

	public int getFrameHeight() {
		return frameHeight;
	}
	
	public void setSpriteIndex(int spriteIndex) {
		this.frameIndex = spriteIndex;
	}

	public int getFrame(){
		return frameIndex;
	}


	public void setFrameSequence(int firstIndex, int lastIndex) {
		if (firstIndex > lastIndex)
			return;

		actionFirstIndex = firstIndex;
		actionLastIndex = lastIndex;
		frameIndex = firstIndex;
	}
	public void nextFrame() {
		frameIndex++;
		if (frameIndex > actionLastIndex)
			frameIndex = actionFirstIndex;
	}

	public void prevFrame() {
		frameIndex--;
		if (frameIndex < actionFirstIndex)
			frameIndex = actionLastIndex;
	}

	public int getActionFirstIndex() {
		return actionFirstIndex;
	}

	public int getActionLastIndex() {
		return actionLastIndex;
	}

	public void setMoveDirection(int direction) {
		if(direction == MOVEDIRECTION_LEFT || direction == MOVEDIRECTION_RIGHT)
			moveDirection = direction;
	}
	
	public int getMoveDirection() {
		return moveDirection;
	}
	
	public void setVelocity(int direction, int velocity) {
		switch(direction) {
		case DIRECTION_HORIZONTAL:
			hVelocity = velocity;
			break;
		case DIRECTION_VERTICAL:
			vVelocity = velocity;
			break;
		}
	}
	
	public int getVelocity(int direction) {
		switch(direction) {
		case DIRECTION_HORIZONTAL:
			return hVelocity;
		case DIRECTION_VERTICAL:
			return vVelocity;
		}
		return 0;
	}
	
	public void setVisible(boolean visible){
		isVisible = visible;
	}
	
	public void setRealSize(int dx, int dy){
		realWidth = dx;
		realHeight = dy;
	}
	
	public int getRealHeight(){
		return realHeight;
	}
	
	public int getRealWidth(){
		return realWidth;
	}
/*	void paint(Graphics g, boolean bReverse, boolean bWrap) {
		// TODO Auto-generated method stub
		g.setClip(offsetX, offsetY, frameWidth, frameHeight);

		int x = 0, y = 0, index = (forceFrameIndex == -1) ? frameIndex : forceFrameIndex;
		x = offsetX;
		y = offsetY - index * frameHeight;
		
		g.drawImage(bReverse ? reverseImage : sourceImage, x, y, Graphics.TOP
				| Graphics.LEFT);
	}*/
	
	void paint(Graphics g, boolean bReverse, boolean bWrap) {
		// TODO Auto-generated method stub
		if(isVisible){
			g.setClip(offsetX, offsetY, frameWidth, frameHeight);
	
			int x = 0, y = 0, index = (forceFrameIndex == -1) ? frameIndex : forceFrameIndex;
			x = offsetX;
			y = offsetY - index * frameHeight;

			g.drawImage(bReverse ? reverseImage : sourceImage, x, y, Graphics.TOP
					| Graphics.LEFT);	
		}

	}

	public int getWorldOffsetX() {
		return worldOffsetX;
	}

	public void setWorldOffsetX(int worldOffsetX) {
		this.worldOffsetX = worldOffsetX;
	}

	public int getWorldOffsetY() {
		return worldOffsetY;
	}

	public void setWorldOffsetY(int worldOffsetY) {
		this.worldOffsetY = worldOffsetY;
	}

	public int getVVelTemp() {
		return vVelTemp;
	}
	
	public void setVVelTemp(int velTemp) {
		vVelTemp = velTemp;
	}
	
	public void setHVelTemp(int helTemp) {
		hVelTemp = helTemp;
	}
	
	public int getHVelTemp() {
		return hVelTemp;
	}


}



⌨️ 快捷键说明

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