1. 程式人生 > >2.自定義配置映射

2.自定義配置映射

game inf bre eight 1.0 stand type() 實例 ati

計劃配置文件(除戰役征服地圖外)主要使用xml格式來保存,然後通過java的映射機制,作為類保存

xml配置示例:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<!--type -1固定 1海洋 0底圖通用  2沙漠 3雪地 4草地 5泥地 6外星 7熱帶草地-->
<pts>
    <pt id="1" name="海洋" image="pt0.png" type="1"/>
    <pt id="2" name="底圖" image="pt1.png" type
="0"/> <pt id="3" name="淺草" image="pt2.png" type="4"/> <pt id="4" name="淺沙" image="pt3.png" type="2"/> <pt id="5" name="雪地" image="pt4.png" type="3"/> <pt id="6" name="月球" image="pt5.png" type="6"/> <pt id="7" name="淺泥" image="pt6.png" type="5"/> <
pt id="8" name="深沙" image="pt7.png" type="2"/> <pt id="9" name="深泥" image="pt8.png" type="5"/> <pt id="10" name="深草" image="pt9.png" type="7"/> <pt id="11" name="砂石" image="pt10.png" type="5"/> </pts>

類配置示例:

技術分享圖片

然後添加如下方法:

//泛型方法 根據傳入的類型,返回list類型
    public static
<T> List<T> getDaoListByClass(T item,String path) throws Exception{ List<T> ts=new ArrayList<T>(); Class clazz=item.getClass(); XmlReader reader = new XmlReader(); Element root = reader.parse(Gdx.files.internal(path)); Array<Element> elements = root.getChildrenByNameRecursively(root.getChild(0).getName()); //獲得條目屬性的數量,然後通過反射,把值給了類 for (Element element : elements) { Field[] fieldName = clazz.getDeclaredFields(); item = (T) clazz.newInstance(); Class clazs=item.getClass(); for(int i=0;i<fieldName.length;i++) { // 創建實例 Field f = clazs.getDeclaredField(fieldName[i].getName()); f.setAccessible(true); if (f.getType().getName().equals(String.class.getName())) { String str=element.get(fieldName[i].getName()); f.set(item, str); }else if (f.getType().getName().equals(int.class.getName())) { int str=element.getInt(fieldName[i].getName()); f.set(item, str); }else if (f.getType().getName().equals(float.class.getName())) { float str=element.getFloat(fieldName[i].getName()); f.set(item, str); }else if (f.getType().getName().equals(boolean.class.getName())) { boolean str=element.getBoolean(fieldName[i].getName()); f.set(item, str); } } ts.add(item); //ts.add(tempItem); } return ts; }

調用示例:

   
  //此示例功能為:用某種圖案填充該pixmap到滿
  //
使用前必須初始化 pixmap // ptId-->type -1固定 1海洋 0底圖通用 2沙漠 3雪地 4草地 5泥地 6外星 7熱帶草地 public static Pixmap coverImgByPtimgId(Pixmap pixmap, int ptId) { // 通過配置文件獲取讀取的pt DefPt defPt = new DefPt(); List<DefPt> defPts = new ArrayList<DefPt>(); try { defPts = GameUtils.getDaoListByClass(defPt, "config/def_pt.xml"); } catch (Exception e) { e.printStackTrace(); } String imgPtStr = null; for (int i = 0; i < defPts.size(); i++) { if (defPts.get(i).getId() == ptId) { imgPtStr = defPts.get(i).getImage(); break; } } if (imgPtStr != null && pixmap != null) { // 獲取圖片資源 Pixmap imgPt = new Pixmap(Gdx.files.internal("pixmap/pts/" + imgPtStr)); // 獲取目標長寬,並循環繪制 int w = (int) ((pixmap.getWidth() / imgPt.getWidth()) + 1); int h = (int) ((pixmap.getHeight() / imgPt.getHeight()) + 1); int x = 0; int y = 0; int x_px; int y_px; int coverCount = w * h; // Gdx.app.log("地圖底圖開始構建", "w:" + w + " h:" + h ); for (int i = 0; i < coverCount; i++) { x = i % w; y = (int) (i / w); x_px = x * imgPt.getWidth(); y_px = y * imgPt.getHeight(); // Gdx.app.log("地圖底圖開始構建", " id:" + i + " x:" + x + " y:" + y+ " // x_px:" + x_px + " y_px:" + y_px); pixmap.drawPixmap(imgPt, x_px, y_px, 0, 0, imgPt.getWidth(), imgPt.getHeight()); } // 清除資源 imgPt.dispose(); } return pixmap; }

所有的配置文件都可以用該泛型方法處理為配置類,到遊戲中使用

2.自定義配置映射