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

📄 dweep.java

📁 这是用J2ME开发的手机游戏
💻 JAVA
字号:
import java.util.Vector;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.game.Sprite;

/**
 * <p>Title: 游戏的主角dweep</p>
 *
 * <p>Description:游戏的主角dweep需要通过使用工具,来闯过任务关卡</p>
 *
 * <p>Copyright: Copyright (c) wodead 2005</p>
 *
 * <p>Company: wodead</p>
 *
 * @author wodead
 * @version 1.0
 */
public class Dweep extends Sprite {
    public static final int STATUS_NORMAL = 0;
    public static final int STATUS_WET = 1;
    public static final int STATUS_FREEZED = 2;
    public static final int STATUS_HOT = 3;
    public static final int STATUS_DYING = 4;
    public static final int STATUS_DEAD = 5;
    //定义dweep的目的地x坐标
    private int destinationX;
    //定义dweep的目的地y坐标
    private int destinationY;
    //定义正常序列
    private final static int[] liveSeq = {0, 1, 2, 3, 2, 1};
    //定义死亡序列
    private final static int[] dyingSeq = {4, 5, 6, 7, 8, 9, 10, 11, 12, 11};
    //定义冰冻序列
    private final static int[] freezedSeq = {17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 18, 18};
    //定义湿序列
    private final static int[] wetSeq = {13, 14, 15, 16, 15, 14};
    //定义湿序列
    private final static int[] hotSeq = { 19, 19, 20, 20, 19, 19, 20, 20, 19, 19, 20, 20, 19, 19, 20, 20, 19, 19, 21, 21};
    //定义路线
    private Vector path = new Vector();
    //定义dweep上一次处于的位置:行
    private int lastRow;
    //定义dweep上一次处于的位置:列
    private int lastColumn;
    //Dweep的状态
    public int status;
    //寻径算法
    private AAsterisk aasterisk = new AAsterisk();
    
    /**
     * 构造一个dweep
     */
    protected Dweep() {
        super(Main.image_dweep, 18, 27);
        setFrameSequence(liveSeq);
        defineReferencePixel(1, 12);
    }
    
    /**
     * 设置dweep的行进路线
     * @param path Vector
     */
    public void setPath(int targetRow, int targetColumn) {
        if (status == STATUS_NORMAL || status == STATUS_WET) {
            if (path.isEmpty()) {
                // 当路径列表为空时dweep处于不动状态,则从dweep当前位置开始寻路。
                aasterisk.find(getRefPixelY() >> 4, getRefPixelX() >> 4, targetRow,
                        targetColumn, path);
            } else {
                // 否则,则从路径的第一个节点重新开始寻路,该节点是目前dweep正在移向的节点。
                BlockNode node = (BlockNode) path.firstElement();
                aasterisk.find(node.row, node.column, targetRow,
                        targetColumn, path);
            }
        }
    }

    /**
     * 淋湿dweep
     */
    public void wet() {
        status = STATUS_WET;
        setFrameSequence(wetSeq);
    }
    
    /**
     * 重设dweep状态为正常状态
     */
    public void reset() {
        status = STATUS_NORMAL;
        setFrameSequence(liveSeq);
    }
    
    /**
     * 激光射中Dweep
     */
    public void laser() {
        if (status != STATUS_HOT) {
            if (status == STATUS_WET)
                reset();
            else
                die();
        }
    }
    
    /**
	 * 杀死dweep
	 */
    public void die() {
        if (status <= STATUS_DYING) {
            status = STATUS_DYING;
            setFrameSequence(dyingSeq);
            path.removeAllElements();
            MainMIDlet.vibrate(1000);
        }
    }

    /**
     * 对Dweep应用工具
     * @param type 工具类型
     * @return 是否成功
     */
    public boolean apply(int type) {
        switch(type) {
        case Main.TOOL_HAMMER:
            if (status == STATUS_FREEZED) {
                wet();
                return true;
            } else if (status == STATUS_HOT) {
                reset();
                setLocation((getRefPixelY() + 8)/16, (getRefPixelX() + 8) /16);
                return true;
            }
            break;
        case Main.TOOL_BUCKET:
            if (status == STATUS_NORMAL) {
                wet();
                return true;
            } else if (status == STATUS_HOT) {
                wet();
                setLocation((getRefPixelY() + 8)/16, (getRefPixelX() + 8) /16);
                return true;
            }
            break;
        case Main.TOOL_TORCH:
            switch (status) {
            case STATUS_NORMAL:
                die();
                return true;
            case STATUS_FREEZED:
                wet();
                return true;
            case STATUS_WET:
                reset();
                return true;
            }
            break;
        }
        
        return false;
    }
    
    /**
     * 冰冻dweep
     */
    public void freeze() {
        status = STATUS_FREEZED;
        path.removeAllElements();
        setFrameSequence(freezedSeq);
    }
    
    /**
     * 加热dweep
     */
    public void hot() {
        status = STATUS_HOT;
        path.removeAllElements();
        setFrameSequence(hotSeq);
    }
    
    /**
     * 设置dweep的位置
     * @param row 行位置
     * @param column 列位置
     */
    public void setLocation(int row, int column) {
    	destinationX = column << 4;
    	destinationY = row << 4;
        lastRow = row;
        lastColumn = column;
        setRefPixelPosition(destinationX, destinationY);
    }
    
    /**
     * 更新位置
     *
     */
    private void updatePostion() {
        int currentX = getRefPixelX();
        int currentY = getRefPixelY();
        // 更新当前位置
        if (currentX < destinationX && currentX < 159)
            currentX += 2;
        else if (currentX > destinationX && currentX > 0)
            currentX -= 2;
        if (currentY < destinationY && currentY < 191)
            currentY += 2;
        else if (currentY > destinationY && currentY > 0)
            currentY -= 2;

        //设置新位置
        setRefPixelPosition(currentX, currentY);
    }
    
    /**
     * 更新dweep的画面
     */
    public void draw(Graphics g) {
        int currentX = getRefPixelX();
        int currentY = getRefPixelY();
    	int dweepRow = currentY >> 4;
		int dweepColumn = currentX >> 4;
        //假如已经死亡则返回
        switch (status){
        case STATUS_DEAD:
        	return;
        case STATUS_NORMAL:
        case STATUS_WET:
            
            //dweep正好完全处于一个格子中间,
            if ((currentX & 0xF) == 0 && (currentY & 0xF) == 0) {

                int t = Main.base[dweepRow][dweepColumn];
                if (t > 3 && t < 20 && Main.addTool(t))
                        Main.base[dweepRow][dweepColumn] = 0;
                
                // dweep第一次进入该格
                if (lastRow != dweepRow || lastColumn != dweepColumn) {
                    switch(t) {
                    case Main.FLOOR_HOT:
                        if (status == STATUS_NORMAL) hot(); else reset();
                        break;
                    case Main.FLOOR_ICE:
                        freeze();
                        break;
                    case Main.FLOOR_TARGET:
                        Main.setStatus(Main.MISSION_COMPLETED);
                        break;
                    }
                }
                
                //更新目标位置
            	if (!path.isEmpty()) {
    				BlockNode firstNode = (BlockNode) path.firstElement();
    				int nextRow = firstNode.row;
    				int nextColumn = firstNode.column;
    				if (nextRow == dweepRow && nextColumn == dweepColumn) {
    					path.removeElementAt(0);
    					if (!path.isEmpty()) {
    						firstNode = ((BlockNode) path.firstElement());
    						nextRow = firstNode.row;
    						nextColumn = firstNode.column;
    					}
    				}
    				
    				destinationX = nextColumn * 16;
    				destinationY = nextRow * 16;
            	}
                //保存位置
                lastRow = dweepRow;
                lastColumn = dweepColumn;
    				
            }
                
            updatePostion();
            break;
        case STATUS_HOT:
			if ((currentX & 0xF) == 0 && (currentY & 0xF) == 0) {
                //保存位置
                lastRow = dweepRow;
                lastColumn = dweepColumn;
                // 判断周围是否有风扇
                int offsetR = 0, offsetL = 0;
                int offsetU = 0, offsetD = 0;

                Stuff stuff1 = Main.getStuff(dweepRow - 1, dweepColumn);
                Stuff stuff2 = Main.getStuff(dweepRow - 2, dweepColumn);
                if (stuffIsFan(stuff1, Main.STUFF_FAN_DOWN))
                    offsetD = 32;
                else if (stuffIsFan(stuff2, Main.STUFF_FAN_DOWN))
                    offsetD = 16;

                stuff1 = Main.getStuff(dweepRow + 1, dweepColumn);
                stuff2 = Main.getStuff(dweepRow + 2, dweepColumn);
                if (stuffIsFan(stuff1, Main.STUFF_FAN_UP))
                    offsetU = 32;
                else if (stuffIsFan(stuff2, Main.STUFF_FAN_UP))
                    offsetU = 16;

                stuff1 = Main.getStuff(dweepRow, dweepColumn - 1);
                stuff2 = Main.getStuff(dweepRow, dweepColumn - 2);
                if (stuffIsFan(stuff1, Main.STUFF_FAN_RIGHT))
                    offsetR = 32;
                else if (stuffIsFan(stuff2, Main.STUFF_FAN_RIGHT))
                    offsetR = 16;

                stuff1 = Main.getStuff(dweepRow, dweepColumn + 1);
                stuff2 = Main.getStuff(dweepRow, dweepColumn + 2);
                if (stuffIsFan(stuff1, Main.STUFF_FAN_LEFT))
                    offsetL = 32;
                else if (stuffIsFan(stuff2, Main.STUFF_FAN_LEFT))
                    offsetL = 16;

                destinationY = (offsetU != 0 && offsetD != 0) ? currentY
                        : currentY + offsetD - offsetU;
                destinationX = (offsetR != 0 && offsetL != 0) ? currentX
                        : currentX + offsetR - offsetL;

                if (currentX != destinationX && currentY != destinationY) {
                    destinationX = currentX
                            + ((offsetR - offsetL > 0) ? 16 : -16);
                    destinationY = currentY
                            + ((offsetD - offsetU > 0) ? 16 : -16);
                }
            }

            updatePostion();
            break;
        case STATUS_DYING:
        	if (getFrame() >= getFrameSequenceLength() - 1) {
	            status = STATUS_DEAD;
	            // 呜呼!dweep死了,那么整个任务就失败了!
                Main.setStatus(Main.MISSION_FAIL);
	            return;
        	}
		}
        
		nextFrame();
		paint(g);
    }
    
    /**
     * 判断物体是不是风扇
     * @param stuff
     * @param type
     * @return
     */
    private boolean stuffIsFan(Stuff stuff, int type) {
		return stuff != null && stuff.type == type;
	}
}

⌨️ 快捷键说明

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