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

📄 otta.java

📁 Java ME手机应用开发大全一书的配套光盘上的源码
💻 JAVA
字号:
import java.util.Vector;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.game.Sprite;

public class Otta 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;
    //定义Otta的目的地x坐标
    private int destinationX;
    //定义Otta的目的地y坐标
    private int destinationY;
    //定义正常序列
    private final static int[] liveSeq = {0, 1, 2, 3, 3, 2, 1, }; 
    //定义死亡序列
    private final static int[] dyingSeq = {4, 4, 5, 5, 6, 6, 5, 5, 
    4, 4, 4, 5, 5, 6, 6, 5, 5, 4, 4, 4, 5, 5, 6, 6, 5, 5, 4, 4};
    //定义固定序列
    private final static int[] freezedSeq = {7,8};
    //定义无敌序列
    private final static int[] wetSeq = {12, 13, 14, 15, 14, 13};
    //定义干序列
    private final static int[] hotSeq = {9, 10, 11, 10};
    //定义路线
    private Vector path = new Vector();
    //定义Otta上一次处于的位置:行
    private int lastRow;
    //定义Otta上一次处于的位置:列
    private int lastColumn;
    //Otta的状态
    public int status;
    //寻径算法
    private WaySearch aasterisk = new WaySearch();
    //构造一个otta
    protected Otta() 
    {
        super(MainCanvas.image_otta, 21, 27);
        setFrameSequence(liveSeq);
        defineReferencePixel(1, 12);
    }
    //设置Otta的行进路线
    public void setPath(int targetRow, int targetColumn) 
    {
        if (status == STATUS_NORMAL || status == STATUS_WET) 
        {
            if (path.isEmpty())
            {
                // 当路径列表为空时Otta处于不动状态,则从Otta当前位置开始寻路。
                aasterisk.find(getRefPixelY() >> 4, getRefPixelX() >> 4, targetRow,
                        targetColumn, path);
            } 
            else 
            {
                // 否则,则从路径的第一个节点重新开始寻路,该节点是目前Otta正在移向的节点。
                Node node = (Node) path.firstElement();
                aasterisk.find(node.row, node.column, targetRow,
                        targetColumn, path);
            }
        }
    }
    //无敌Otta
    public void wet()
    {
        status = STATUS_WET;
        setFrameSequence(wetSeq);
    }
    //重设Otta状态为正常状态
    public void reset() 
    {
        status = STATUS_NORMAL;
        setFrameSequence(liveSeq);
    }
    //激光射中Otta
    public void laser() 
    {
        if (status != STATUS_HOT) 
        {
            if (status == STATUS_WET)
                reset();
            else
                die();
        }
    }
    //杀死Otta
    public void die()
    {
        if (status <= STATUS_DYING)
        {
            status = STATUS_DYING;
            setFrameSequence(dyingSeq);
            path.removeAllElements();
            Start.vibrate(1000);
        }
    }
    //对Otta应用工具
    public boolean apply(int type)
    {
        switch(type) 
        {
            case MainCanvas.TOOL_POWER:
                if (status == STATUS_NORMAL) 
                {
                    wet();
                    return true;
                } 
                else if (status == STATUS_HOT)
                {
                    wet();
                    setLocation((getRefPixelY() + 8)/16, (getRefPixelX() + 8) /16);
                    return true;
                }
                break;
            case MainCanvas.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;
    }
    //冰冻Otta
    public void freeze() 
    {
        status = STATUS_FREEZED;
        path.removeAllElements();
        setFrameSequence(freezedSeq);
    }
    //加热Otta
    public void hot() 
    {
        status = STATUS_HOT;
        path.removeAllElements();
        setFrameSequence(hotSeq);
    }
    //设置Otta的位置
    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);
    }
    //更新Otta的画面
    public void draw(Graphics g) 
    {
        int currentX = getRefPixelX();
        int currentY = getRefPixelY();
    	int OttaRow = currentY >> 4;
		int OttaColumn = currentX >> 4;
        //假如已经死亡则返回
        switch (status)
        {
            case STATUS_DEAD:
                return;
            case STATUS_NORMAL:
            case STATUS_WET:            
            //Otta正好完全处于一个格子中间
            if ((currentX & 0xF) == 0 && (currentY & 0xF) == 0)
            {
                int t = MainCanvas.base[OttaRow][OttaColumn];
                if (t > 3 && t < 20 && MainCanvas.addTool(t))
                    MainCanvas.base[OttaRow][OttaColumn] = 0;
                // Otta第一次进入该格
                if (lastRow != OttaRow || lastColumn != OttaColumn)
                {
                    switch(t)
                    {
                        case MainCanvas.RIVER:
                            freeze();
                            break;
                        case MainCanvas.TARGET:
                            MainCanvas.setStatus(MainCanvas.MISSION_COMPLETED);
                            break;
                    }
                }                
                //更新目标位置
            	if (!path.isEmpty())
                {
                    Node firstNode = (Node) path.firstElement();
                    int nextRow = firstNode.row;
                    int nextColumn = firstNode.column;
                    if (nextRow == OttaRow && nextColumn == OttaColumn)
                    {
                        path.removeElementAt(0);
                        if (!path.isEmpty()) 
                        {
                            firstNode = ((Node) path.firstElement());
                            nextRow = firstNode.row;
                            nextColumn = firstNode.column;
                        }
                    }     
                    destinationX = nextColumn * 16;
                    destinationY = nextRow * 16;
                }
                //保存位置
                lastRow = OttaRow;
                lastColumn = OttaColumn;
            }                
            updatePostion();
            break;
            case STATUS_HOT:
                if ((currentX & 0xF) == 0 && (currentY & 0xF) == 0)
                {
                    //保存位置
                    lastRow = OttaRow;
                    lastColumn = OttaColumn;
                }
                updatePostion();
                break;
            case STATUS_DYING:
                if (getFrame() >= getFrameSequenceLength() - 1) 
                {
                    status = STATUS_DEAD;
                    //Otta死了,那么整个任务就失败了!
                    MainCanvas.setStatus(MainCanvas.MISSION_FAIL);
                    return;
                }
        }
        nextFrame();
        paint(g);
    }
}

⌨️ 快捷键说明

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