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

📄 map.java

📁 这是一个毕业设计的泡泡堂源代码
💻 JAVA
字号:
/*
 * 创建日期 2007-3-18 @author caipiz
 */
package com.cpiz.poptang;

import java.util.Random;

import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.TiledLayer;

public class Map
{
    /**
     * 炸弹与道具层
     */
    private static TiledLayer itemsLayer = null;
    
    /**
     * 地面层
     */
    private static TiledLayer floorLayer = null;
    
    /**
     * 建筑层
     */
    private TiledLayer buildingsLayer = null;
    
    /**
     * 地图数据 第一层为地表贴图 第二层为地面物体 第三层为地面物体属性 第四层为是否被踩过
     */
    public byte[][][] tiles;

    private Random rand = new Random();
    Image imageSource = null;
    
    /**
     * 获得地面层
     * @return 地面层
     */
    public TiledLayer getFloorLayer()
    {
        if(floorLayer == null)
        {
            Image imgFloor = null;
            try
            {
                imgFloor = Image.createImage(PopTang.IMAGE_SRC_FLOOR);
            }
            catch (Exception ex)
            {
                ex.printStackTrace();
            }
            
            floorLayer = new TiledLayer(PopTang.MAP_COLS, PopTang.MAP_ROWS,
                    imgFloor, PopTang.CELL_WIDTH, PopTang.CELL_HEIGHT);
        }
        return floorLayer;
    }
    
    /**
     * 获得炸弹与道具层
     * @return 炸弹与道具层
     */
    public TiledLayer getBombsAndToolsLayer()
    {
        if(itemsLayer == null)
        {
            Image imgBomb = null;
            try
            {
                imgBomb = Image.createImage(PopTang.IMAGE_SRC_ITEMS);
            }
            catch (Exception ex)
            {
                ex.printStackTrace();
            }
            
            itemsLayer = new TiledLayer(PopTang.MAP_COLS, PopTang.MAP_ROWS, imgBomb, PopTang.CELL_WIDTH,
                    PopTang.CELL_HEIGHT);
        }
        return itemsLayer;
    }
    
    /**
     * 获得建筑层
     * @return 建筑层
     */
    public TiledLayer getBuildingsLayer()
    {
        return buildingsLayer;
    }

    /**
     * 创建建筑层
     * @param stage 当前关卡号
     * @return 建筑层
     */
    private TiledLayer createBuildingsLayer(int stage)
    {
        Image imgBuilding = null;
        
        try
        {
            imgBuilding = Image.createImage(PopTang.IMAGE_DIR + stage + ".png");
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
        }
        
        return new TiledLayer(PopTang.MAP_COLS, PopTang.MAP_ROWS, imgBuilding, PopTang.CELL_WIDTH,
                PopTang.CELL_HEIGHT);
    }
    
    /**
     * 构造地图
     * @param stage 关卡号
     * @param imgTiles 地图源图像
     */
    public Map(Stage stage)
    {
        buildingsLayer = createBuildingsLayer(stage.imgIndex);
        tiles = stage.mapData;
        
        itemsLayer = getBombsAndToolsLayer();
        floorLayer = getFloorLayer();
        
        initLayer(Map.LAYER_FLOOR);
        initLayer(Map.LAYER_BUILDING);
        clearLayer(itemsLayer);
    }
    
    /**
     * 使用地图数据填充指定层
     * @param layerIndex 指定层索引 Map.LAYER_FLOOR 或者 Map.LAYER_BUILDING
     */
    private void initLayer(int layerIndex)
    {
        TiledLayer layer = null;
        switch(layerIndex)
        {
        case Map.LAYER_FLOOR:
            layer = getFloorLayer();
            break;
        case Map.LAYER_BUILDING:
            layer = getBuildingsLayer();
            break;
        default:
            break;
        }
        
        for(int i = 0; i < PopTang.MAP_ROWS; i++)
        {
            for(int j =0; j < PopTang.MAP_COLS; j++)
            {
                layer.setCell(j, i, tiles[layerIndex][i][j]);
            }
        }
    }
    
    /**
     * 清空层指定层
     * @param layer 要清空的层
     */
    private void clearLayer(TiledLayer layer)
    {
        for(int i = 0; i < PopTang.MAP_ROWS; i++)
        {
            for(int j =0; j < PopTang.MAP_COLS; j++)
            {
                layer.setCell(j, i, 0);
            }
        }
    }

    /**
     * 更新地图<br>
     * 主要是更新建筑摧毁效果
     */
    public void update()
    {
        for (int i = 0; i < tiles[0].length; i++)
        {
            for (int j = 0; j < tiles[0][i].length; j++)
            {
                /*
                 * 摧毁效果运算
                 * 
                 * 规定贴图小于Map.ANIMATION_START为静态图
                 * 当该位置为静态图或者为动态图首帧(稳定态帧)时直接绘制
                 * 当该位置为动态图为尾帧时,爆炸结束
                 * 当该位置为动态图非首帧且非尾帧时自动递加
                 */

                if (tiles[Map.LAYER_BUILDING][i][j] <= Map.ANIMATION_START
                        || tiles[Map.LAYER_BUILDING][i][j]
                                % Map.ANIMATION_FRAMS == 1)
                {
                    // 不作任何改变
                } // 静态图
                else
                {
                    if (tiles[Map.LAYER_BUILDING][i][j] % Map.ANIMATION_FRAMS == 0)
                    {
                        // 换为地表砖贴图值及特性值
                        tiles[Map.LAYER_BUILDING][i][j] = 0;
                        
                        // 准予通行
                        addFeature(i, j, Map.CAN_PASS);
                        
                        // 随机生成道具
                        itemsLayer.setCell(j, i, randomTool());
                    }// 动态尾帧
                    else
                    {
                        tiles[Map.LAYER_BUILDING][i][j]++;
                    }
                    buildingsLayer.setCell(j, i, tiles[Map.LAYER_BUILDING][i][j]);
                }
            }
        }
    }
    
    /*
     * 产生随机道具
     */ 
    private int randomTool()
    {
        int probability = rand.nextInt(100);
        if(probability >= PopTang.TOOL_PROBABILITY)
        {
            return 0;
        }
        
        int rst = rand.nextInt(4);
        rst += Map.TOOL_BOMB;
        return rst;
    }

    /**
     * 摧毁建筑
     * 
     * @param row
     * @param col
     * @return 摧毁成功返回true,失败返回false
     */
    public boolean destroy(int row, int col)
    {
        // 消除可摧毁特性
        if (hasFeature(row, col, Map.CAN_DESTROY))
        {
            removeFeature(row, col, Map.CAN_DESTROY);
        }
        else
        {
            return false;// 因建筑不可摧毁,摧毁失败
        }

        if (tiles[Map.LAYER_BUILDING][row][col] > Map.ANIMATION_FRAMS
                && tiles[Map.LAYER_BUILDING][row][col] % Map.ANIMATION_FRAMS == 1)
        {
            tiles[Map.LAYER_BUILDING][row][col]++;
        }

        return true;
    }

    /**
     * 检查属性层指定单元格中是否存在指定属性
     * 
     * @param row 单元格所在行
     * @param col 单元格所在列
     * @param feature 要检测的属性
     */
    public boolean hasFeature(int row, int col, int feature)
    {
        return ((tiles[Map.LAYER_FEATURE][row][col] & feature) != 0);
    }
    
    /**
     * 给地图属性层的指定单元格增加指定属性<br>
     * 如果原属性已存在指定属性,则不作修改,否则增加指定属性
     * 
     * @param row 单元格所在行
     * @param col 单元格所在列
     * @param feature 要增加的属性
     */
    public void addFeature(int row, int col, int feature)
    {
        tiles[Map.LAYER_FEATURE][row][col] |= feature;
    }
    
    /**
     * 给地图属性层的指定单元格消去指定属性<br>
     * 如果原属性已存在指定属性,则消去,否则不作修改
     * 
     * @param row 单元格所在行
     * @param col 单元格所在列
     * @param feature 要消去的属性
     */
    public void removeFeature(int row, int col, int feature)
    {
        tiles[Map.LAYER_FEATURE][row][col] = 
            (byte)((tiles[Map.LAYER_FEATURE][row][col] | feature) - feature);
    }

    /**
     * 地砖贴图层
     */
    public static final int LAYER_FLOOR = 0;

    /**
     * 建筑贴图层
     */
    public static final int LAYER_BUILDING = 1;

    /**
     * 特性层
     */
    public static final int LAYER_FEATURE = 2;

    /**
     * 动画帧开始位置(第10帧以后为建筑摧毁动画)
     */
    public static final int ANIMATION_START = 10;
    
    /**
     * 物体破碎动画效果帧长
     */
    public static final int ANIMATION_FRAMS = 5;

    /**
     * 单元格可通行
     */
    public static final int CAN_PASS = 1;

    /**
     * 单元格可破坏
     */
    public static final int CAN_DESTROY = 2;

    /**
     * 单元格可推动
     */
    public static final int CAN_PUSH = 4;

    /**
     * 单元格致命
     */
    public static final int DEADLY = 8;

    /**
     * 单元格存在炸弹
     */
    public static final int BOMB = 16;

    /**
     * 炸弹
     */
    public static final int TOOL_BOMB = 29;
    
    /**
     * 能量药水
     */
    public static final int TOOL_POWER = 30;
    
    /**
     * 速度鞋
     */
    public static final int TOOL_SPEED = 31;
    
    /**
     * 金币
     */
    public static final int TOOL_GOLD_COIN = 32;
}

⌨️ 快捷键说明

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