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

📄 stuff.java

📁 手机游戏《紫色精灵》源代码
💻 JAVA
字号:
import java.util.Random;

import javax.microedition.lcdui.game.Sprite;
import javax.microedition.lcdui.Graphics;

/**
 * <p>Title: 物体</p>
 *
 * <p>Description: 描述物体,包括石头、炸弹、风扇、激光器以及镜子</p>
 *
 * <p>Copyright: Copyright (c) 2005</p>
 *
 * <p>Company: wodead</p>
 *
 * @author wodead
 * @version 1.0
 */
public class Stuff {
    //定义物体的状态:平常态
    public static final int STATUS_NORMAL = 0;
    //点燃状态
    public static final int STATUS_FIRING = 1;
    //定义物体的状态:死亡态
    public static final int STATUS_DEAD = 2;
    //定义物体的状态:正在死亡态
    public static final int STATUS_DYING = 3;
    //当前物体状态
    public int status;
    //物体的类型
    public int type;
    //物体在地图上行数
    public int row; 
    //物体在地图上列数
    public int column;
    //动画
    public Sprite animation;
    //炸弹点燃时的序列
    private static int[] bombSeq = new int[]{0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1};
    //爆炸效果的序列
    private static int[] explodeSeq = new int[]{0,1,2,2,3,3,2,1,0};
    //风扇动画序列
    private static int[][] fanSeq = new int[][]{{0,1,2,3},{4,5,6},{7,8,9,10},{4,5,6}};
    //镜子打碎时动画序列
    private static int[] brokingSeq = new int[]{0,1,2,3,3};
    //激光器的动画序列
    private static int[][] laserSeq = new int[][]{{0,1},{2,3},{4,5},{2,3}};
    //风扇和激光器被激光穿越时的动画序列
    private int firingduration = 12;
    //产生随机数
    static Random random = new Random();
    //位置左上角
    int x;
    //位置左上角
    int y;
    
    /**
     * 创建动态的物体
     * @param image Image
     * @param frameWidth int
     * @param frameHeight int
     */
    public Stuff(int type, int row, int column) {
        this.type = type;
        this.row = row;
        this.column = column;
        x = column << 4;
        y = row << 4;
        if (type == Main.STUFF_BOMB) {
            animation = new Sprite(Main.image_bomb, 16, 16);
            animation.setFrameSequence(bombSeq);
            animation.setPosition(x, y);
        } else if (type >= Main.STUFF_FAN_UP && type <= Main.STUFF_FAN_LEFT) {
            animation = new Sprite(Main.image_fan, 18, 21);
            int d = type - Main.STUFF_FAN_UP;
            animation.setFrameSequence(fanSeq[d]);
            if (d == 3)
                animation.setTransform(Sprite.TRANS_MIRROR);
            animation.setPosition(x - 1, y - 8);
        } else if (type >= Main.STUFF_LASER_UP && type <= Main.STUFF_LASER_LEFT) {
            animation = new Sprite(Main.image_laser, 24, 20);
            int d = type - Main.STUFF_LASER_UP;
            animation.setFrameSequence(laserSeq[d]);
            if (d == 3)
                animation.setTransform(Sprite.TRANS_MIRROR);
            animation.setPosition(x - 4, y - 4);
        }
    }
    
    public boolean apply(int tool) {
        //石头对任何工具都无效
        if (type == Main.STUFF_STONE)
            return false;

        switch (tool) {
        case Main.TOOL_HAMMER:
            //锤头将破害炸弹、风扇、激光器、镜子
            if (status >= STATUS_DYING)
                return false;
            
            status = STATUS_DYING;
            if (type == Main.STUFF_MIRROR_CCW || type == Main.STUFF_MIRROR_CW) {
                animation = new Sprite(Main.image_broking, 24, 24);
                if (type == Main.STUFF_MIRROR_CW )
                    animation.setTransform(Sprite.TRANS_MIRROR);
                animation.setPosition(x - 4, y - 8);
                animation.setFrameSequence(brokingSeq);
            } else {
                animation = new Sprite(Main.image_explode, 20, 20);
                animation.setFrameSequence(explodeSeq);
                animation.setPosition(x - 2, y - 2);
                if (type == Main.STUFF_BOMB) {
                    int dweepX = Main.dweep.getRefPixelX();
                    int dweepY = Main.dweep.getRefPixelY();
                    // 假如dweep在火力范围内,则杀死它
                    if (dweepY < y + 24 && dweepY > y - 24 && dweepX < x + 24
                            && dweepX > x - 24
                            && Main.dweep.status != Dweep.STATUS_HOT)
                        Main.dweep.die();
                }
            }
            
            return true;
        case Main.TOOL_TORCH:
            // 火把将点燃处于常态的炸弹
            if (type == Main.STUFF_BOMB && status == STATUS_NORMAL) {
                status = STATUS_FIRING;
                return true;
            }
            break;
        case Main.TOOL_BUCKET:
            //水桶将熄灭点燃状态的炸弹
            if (type == Main.STUFF_BOMB && status == STATUS_FIRING) {
                status = STATUS_NORMAL;
                animation.setFrame(0);
                return true;
            }
            break;
        case Main.TOOL_WRENCH_CW:
        case Main.TOOL_WRENCH_CCW:
            //扳手将旋转镜子、风扇、激光器
            if (type >= Main.STUFF_FAN_UP && type <= Main.STUFF_FAN_LEFT
                    && status == STATUS_NORMAL) {
                int d = type - Main.STUFF_FAN_UP;
                d = (tool == Main.TOOL_WRENCH_CW) ? ++d % 4 : (--d + 4) % 4;
                animation.setFrameSequence(fanSeq[d]);
                animation.setTransform((d == 3) ? Sprite.TRANS_MIRROR
                        : Sprite.TRANS_NONE);
                type = Main.STUFF_FAN_UP + d;
                animation.setPosition(x - 1, y - 8);
                return true;
            }
            
            if (type >= Main.STUFF_LASER_UP && type <= Main.STUFF_LASER_LEFT
                    && status == STATUS_NORMAL) {
                int d = type - Main.STUFF_LASER_UP;
                d = (tool == Main.TOOL_WRENCH_CW) ? ++d % 4 : (--d + 4) % 4;
                animation.setFrameSequence(laserSeq[d]);
                animation.setTransform((d == 3) ? Sprite.TRANS_MIRROR
                        : Sprite.TRANS_NONE);
                type = Main.STUFF_LASER_UP + d;
                animation.setPosition(x - 4, y - 4);
                return true;
            }
            
            if (type <= Main.STUFF_MIRROR_CCW
                    && type >= Main.STUFF_MIRROR_CW
                    && status == STATUS_NORMAL) {
                type = (type == Main.STUFF_MIRROR_CCW) ? Main.STUFF_MIRROR_CW
                        : Main.STUFF_MIRROR_CCW;
                return true;
            }
           break;
        case Main.LASER:
           if (status == STATUS_NORMAL)
                status = STATUS_FIRING;
        }

        return false;
    }

    /**
     * 
     * @param g
     */
    public void draw(Graphics g) {
        switch(status) {
        case STATUS_NORMAL:
            if (type == Main.STUFF_STONE) {
                //绘制石头
                g.drawRegion(Main.image_stone, Main.floor * 16, 0, 16, 23,
                        Sprite.TRANS_NONE, x, y - 7, 20);
            } else if ((type >= Main.STUFF_FAN_UP && type <= Main.STUFF_FAN_LEFT)
                    || (type >= Main.STUFF_LASER_UP && type <= Main.STUFF_LASER_LEFT)) {
                animation.nextFrame();
                animation.paint(g);
            } else if (type == Main.STUFF_MIRROR_CCW ||  type == Main.STUFF_MIRROR_CW) {
                g.drawRegion(Main.image_mirror, 0, 0, 16, 24,
                        (type == Main.STUFF_MIRROR_CCW) ? Sprite.TRANS_NONE
                                : Sprite.TRANS_MIRROR, x, y - 8, 20);
            } else {
                animation.paint(g);
            }
                
            break;
        case STATUS_FIRING:
            animation.nextFrame();
            animation.paint(g);
            if (type == Main.STUFF_BOMB) {
                if (isLastFrame())
                    apply(Main.TOOL_HAMMER);
            } else {
                int x1 = x + 3;
                int y1 = y + 3;
                g.drawImage(Main.image_firing, x1 + (random.nextInt() % 5), y1
                        + (random.nextInt() % 5), 20);
                g.drawImage(Main.image_firing, 
                        x1 + (random.nextInt() % 5), 
                        y1 + (random.nextInt() % 5), 20);
                if (--firingduration == 0)
                    apply(Main.TOOL_HAMMER);
            }
            break;
        case STATUS_DYING:
            animation.nextFrame();
            if (type == Main.STUFF_BOMB) {
                int sRow = (row > 1) ? row - 1 : 0;
                int sColumn = (column > 1) ? column - 1 : 0;
                int eRow = (row < 10) ? row + 1 : 11;
                int eColumn = (column < 8) ? column + 1 : 9;

                for (int i = sRow; i <= eRow; i++) {
                    for (int j = sColumn; j <= eColumn; j++) {
                        animation.setPosition(j * 16 - 2, i * 16 - 2);
                        animation.paint(g);

                        if (animation.getFrame() == 3) {
                            Stuff stuff = Main.stuffs[i][j];
                            if (stuff != null)
                                stuff.apply(Main.TOOL_HAMMER);
                        }
                    }
                }
            } else {
                animation.paint(g);
            }
            
            if (isLastFrame()) status = STATUS_DEAD;
            break;
            
        }
    }
    
    public boolean isLastFrame() {
        return animation.getFrame() == animation.getFrameSequenceLength() - 1;
    }
}

⌨️ 快捷键说明

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