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

📄 mapcreator.java

📁 《神州》RPG游戏引擎
💻 JAVA
字号:
//获取地图TiledLayer对象

import java.io.*;
import java.util.*;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;

/*
 * Created on 2005-4-28
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */

/**
 * @author Administrator
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class MapCreator {
	private Image mapImg;
	private String mapFileName;
	private int mw,mh;
	private int cw,ch;
    private int layerCount;
    private int layerNameLength;
    private int width,height;
    public static int FRONT_LAYER=1;
    public static int BACK_LAYER=0;
    private Vector collisionVC;
    
	public MapCreator()
	{		
	}
	public MapCreator(String imgUrl,String map)
	{
		setImg(imgUrl);
		setMap(map);
	}
	
	public void setImg(String imgUrl)
	{
		try {
			mapImg = Image.createImage(imgUrl);
		} catch (IOException e) {			
			e.printStackTrace();
		}
	}
	public void setMap(String map)
	{
		mapFileName=map;
	}
	
	public TiledLayer getLayer(int layerType)
	{
		//System.out.println("开始获取地图Layer:"+layerType);
        InputStream is=this.getClass().getResourceAsStream(mapFileName);
        if(is==null)
        {
            System.out.println("未取到地图数据文件:"+mapFileName);
            return null;
        }
            
	    int r;
	    ByteArrayOutputStream baos=new ByteArrayOutputStream(); 
	    try
	    {
			while((r=is.read())!=-1)
			{
				baos.write(r);
			}
			byte[] map=baos.toByteArray();
			mw=map[0];
			mh=map[1];
			cw=map[2];
			ch=map[3];
            width=mw*cw;
            height=mh*ch;
            /*
            System.out.println("Map宽="+mw);
            System.out.println("Map高="+mh);
            System.out.println("Cell宽="+cw);
            System.out.println("Cell高="+ch);
            */
            //System.out.println("mapLength="+map.length);
			int indexFrom=4+layerType;
			GameTiledLayer mapLayer=new GameTiledLayer(mw, mh, mapImg,cw,ch);
			for(int j=0;j<mh;j++)
			{
				for(int i=0;i<mw;i++)
				{
                    if(map[indexFrom]<0)
                    {
                        mapLayer.setCell(i,j,Math.abs(map[indexFrom]));
                        mapLayer.setWalkTag(i,j,false);
                        if(collisionVC==null)collisionVC=new Vector();
                        /*
                        Image blankImg=Image.createImage(cw,ch);
                        Sprite collisionSprite=new Sprite(blankImg);
                        collisionSprite.setPosition(i*cw,j*ch);
                        */
                        Box box=new Box();
                        box.x=i*cw;
                        box.y=j*ch;
                        box.w=cw;
                        box.h=ch;
                        collisionVC.addElement(box);
                    }
                    else
                    {
                        mapLayer.setCell(i,j,map[indexFrom]);
                        mapLayer.setWalkTag(i,j,true);
                    }                    
					//System.out.println(map[indexFrom]);
					indexFrom+=2;
				}	
			}
            //System.out.println("获取地图成功:"+layerType);
			return mapLayer;
		}
	    catch (IOException e)
		{		
			e.printStackTrace();
		}
        System.out.println("获取地图失败:"+layerType);
	    
	    return null;
	}
    //按层取地图,支持任意层数
    public TiledLayer getLayerByIndex(int layerIndex)
    {
        //System.out.println("开始获取地图Layer:"+layerType);
        //System.out.println("取第"+layerIndex+"层地图");
        //System.out.println("读取"+mapFileName+"地图文件");
        InputStream is=this.getClass().getResourceAsStream(mapFileName);
        if(is==null)
        {
            System.out.println("未取到地图数据文件:"+mapFileName);
            return null;
        }
            
        int r;
        ByteArrayOutputStream baos=new ByteArrayOutputStream(); 
        try
        {
            while((r=is.read())!=-1)
            {
                baos.write(r);
            }
            byte[] map=baos.toByteArray();
            mw=map[0];
            mh=map[1];
            cw=map[2];
            ch=map[3];
            layerCount=map[4];
            layerNameLength=map[5];
            width=mw*cw;
            height=mh*ch;
            if(layerIndex+1>layerCount)return null;
            /*
            System.out.println("Map宽="+mw);
            System.out.println("Map高="+mh);
            System.out.println("Cell宽="+cw);
            System.out.println("Cell高="+ch);
            */
            //System.out.println("mapLength="+map.length);
            int indexFrom=6+layerNameLength+layerIndex;
            //System.out.println("layerNameLength="+layerNameLength);
            //System.out.println("layerIndex="+layerIndex);
            //System.out.println("indexFrom="+indexFrom);
            GameTiledLayer mapLayer=new GameTiledLayer(mw, mh, mapImg,cw,ch);
            for(int j=0;j<mh;j++)
            {
                for(int i=0;i<mw;i++)
                {
                    //System.out.println("map["+indexFrom+"]="+map[indexFrom]);
                    if(map[indexFrom]<0)
                    {
                        mapLayer.setCell(i,j,Math.abs(map[indexFrom]));
                        mapLayer.setWalkTag(i,j,false);
                        if(collisionVC==null)collisionVC=new Vector();
                        /*
                        Image blankImg=Image.createImage(cw,ch);
                        Sprite collisionSprite=new Sprite(blankImg);
                        collisionSprite.setPosition(i*cw,j*ch);
                        */
                        Box box=new Box();
                        box.x=i*cw;
                        box.y=j*ch;
                        box.w=cw;
                        box.h=ch;
                        collisionVC.addElement(box);
                    }
                    else
                    {
                        mapLayer.setCell(i,j,map[indexFrom]);
                        mapLayer.setWalkTag(i,j,true);
                    }                    
                    //System.out.println(map[indexFrom]);
                    indexFrom+=layerCount;
                }   
            }
            //System.out.println("获取地图成功:"+layerType);
            return mapLayer;
        }
        catch (IOException e)
        {       
            e.printStackTrace();
        }
        System.out.println("获取地图失败:"+layerIndex);
        
        return null;
    }
    public Vector getCollisionVC()
    {
        return collisionVC;
    }
    

    public int getHeight() {
        return height;
    }
    public int getWidth() {
        return width;
    }
}

⌨️ 快捷键说明

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