计划用16进制做两个东西,一个是map(地图),另一个是btl(战役,征服)

由于地块格子数,决定btl和map分开,用btl中的mapid来关联map的信息,计划用16进制储存

已知16进制有几种长度

1 writeByte 范围:0-15

2 writeShort 范围:0-255

4 writeInt 范围:0-65535

8 writeLong 范围:0-4294967295

分别用他们来储存信息

此为地图map的格式

然后就是把信息放入一个excel中,加载这个excel,获取数据

然后映射到上面的类中

然后按照格式转为16进制保存

private static void saveBinByWirteByte(FBMapBinDAO binFile,String saveBinName) {
       try{
           FileOutputStream fs_out=new FileOutputStream("D://"+saveBinName+".bin");
           DataOutputStream out =new DataOutputStream(fs_out);
           out.writeShort(binFile.mapVersion);//4
           out.writeInt(binFile.mapWidth);//8
           out.writeInt(binFile.mapHeight);//8
           for(int i=0;i<binFile.getMapbin().size();i++) {
               out.writeByte(binFile.getMapbin().get(i).getBlockType());//2
               out.writeByte(binFile.getMapbin().get(i).getBackTile());//2
               out.writeByte(binFile.getMapbin().get(i).getBackIdx());//2
               out.writeByte(binFile.getMapbin().get(i).getBackRefX());//2
               out.writeByte(binFile.getMapbin().get(i).getBackRefY());//2
               out.writeByte(binFile.getMapbin().get(i).getForeTile());//2
               out.writeByte(binFile.getMapbin().get(i).getForeIdx());//2
               out.writeByte(binFile.getMapbin().get(i).getForeRefX());//2
               out.writeByte(binFile.getMapbin().get(i).getForeRefY());//2
               out.writeByte(binFile.getMapbin().get(i).getWaterPass());//2
               out.writeByte(binFile.getMapbin().get(i).getLandPass());//2
               out.writeInt(binFile.getMapbin().get(i).getRegionId());//8
               out.writeByte(binFile.getMapbin().get(i).getClimateId());//2
           }
           out.close();
       }catch(FileNotFoundException fe){
           System.err.println(fe);
       }catch(IOException ioe){
           System.err.println(ioe);
       }

       System.out.println("Ok");
       
   }

读取的话

//读取地图
   private static FBMapBinDAO getDaoByBin(String Index) throws IOException {
       
       FBMapBinDAO fs=new FBMapBinDAO();
       List<MapBin> mps=new ArrayList<MapBin>();
       
       //定位bin位置
       byte[] bt = ReadBin.readFile("D:\\test1.bin");
       //新建对象
       StringBuilder buf=new StringBuilder() ;
       int line=0 ;//十六进制标记
       for(byte d:bt){
            if(line%1==0) {
                buf.append(String.format("%02x", d)) ;
                //System.out.println(String.format("%02x", d));
                //buf.append(",") ;
                line++;
            }
       }
       //解析并赋值
       int bufTag=0;
       //System.out.println(buf.substring(bufTag, bufTag+4));
       fs.setMapVersion(Integer.parseInt(buf.substring(bufTag, bufTag+4),16));
       bufTag=bufTag+4;
       

       //System.out.println(buf.substring(bufTag, bufTag+8));
       int width=Integer.parseInt(buf.substring(bufTag, bufTag+8),16);
       bufTag=bufTag+8;
       //System.out.println(buf.substring(bufTag, bufTag+8));
       int height=Integer.parseInt(buf.substring(bufTag, bufTag+8),16);
       bufTag=bufTag+8;
       fs.setMapWidth(width);
       fs.setMapHeight(height);
       int totalCount=width*height;
       
       //System.out.println(fs.mapVersion);
       //System.out.println(width);
       //System.out.println(height);
       //System.out.println(totalCount);
       
       for(int i=0;i<totalCount;i++) {
           MapBin mp=new MapBin();
           mp.setBlockType(Integer.parseInt(buf.substring(bufTag, bufTag+2),16));
           bufTag=bufTag+2;
           mp.setBackTile(Integer.parseInt(buf.substring(bufTag, bufTag+2),16));
           bufTag=bufTag+2;
           mp.setBackIdx(Integer.parseInt(buf.substring(bufTag, bufTag+2),16));
           bufTag=bufTag+2;
           mp.setBackRefX(Integer.parseInt(buf.substring(bufTag, bufTag+2),16));
           bufTag=bufTag+2;
           mp.setBackRefY(Integer.parseInt(buf.substring(bufTag, bufTag+2),16));
           bufTag=bufTag+2;
           mp.setForeTile(Integer.parseInt(buf.substring(bufTag, bufTag+2),16));
           bufTag=bufTag+2;
           mp.setForeIdx(Integer.parseInt(buf.substring(bufTag, bufTag+2),16));
           bufTag=bufTag+2;
           mp.setForeRefX(Integer.parseInt(buf.substring(bufTag, bufTag+2),16));
           bufTag=bufTag+2;
           mp.setForeRefY(Integer.parseInt(buf.substring(bufTag, bufTag+2),16));
           bufTag=bufTag+2;
           mp.setWaterPass(Integer.parseInt(buf.substring(bufTag, bufTag+2),16));
           bufTag=bufTag+2;
           mp.setLandPass(Integer.parseInt(buf.substring(bufTag, bufTag+2),16));
           bufTag=bufTag+2;
           mp.setRegionId(Integer.parseInt(buf.substring(bufTag, bufTag+8),16));
           bufTag=bufTag+8;
           mp.setClimateId(Integer.parseInt(buf.substring(bufTag, bufTag+2),16));
           bufTag=bufTag+2;
           mps.add(mp);
       }
       fs.setMapbin(mps);
       
       return fs;
   }

通过这样来读取信息.

(ps:没用tmx方式加载地图,因为觉得无法满足我的需要吧)

 

posted on 2019-03-03 13:15  黑狱  阅读(757)  评论(0编辑  收藏  举报