1.自定义游戏地图格式的设定,加载,保存

本文探讨了使用16进制存储地图(map)与战役(btl)数据的方法,通过不同长度的16进制数值类型如writeByte、writeShort、writeInt、writeLong来储存游戏地图的各种属性,包括地块类型、背景瓷砖、区域ID等,并介绍了如何将这些数据写入和读取二进制文件的过程。
摘要由CSDN通过智能技术生成

计划用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方式加载地图,因为觉得无法满足我的需要吧)

 

转载于:https://www.cnblogs.com/tysk/p/10464896.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值