经过构思,游戏将分为两部分,

1,预览图,只负责展示世界形势

2,根据预览图生成的战役项

现在要记录的是我制作预览图的部分

1.预览图只有实际地图的1/4,首先生成地图(建议不要缩放以前地图,由于误差原因会导致地图色绘制不准),由于方法只支持int值,1/4导致一些误差(海洋中有些尖刺),不得不手动修复

然后处理为4024*2024大小通过分割工具分割为几部分

再通过xml记录绘制位置,在游戏中合成大图

  1. <SmallMaps><!--存放位置为img下的name文件夹,id与mapId一一对应-->
  2. <Maps id="1" name="world" width="3308" height="1536" >
  3. <map n="1.png" x="0" y="0"/>
  4. <map n="2.png" x="1000" y="0"/>
  5. <map n="3.png" x="2000" y="0"/>
  6. <map n="4.png" x="3000" y="0"/>
  7. <map n="5.png" x="0" y="1000"/>
  8. <map n="6.png" x="1000" y="1000"/>
  9. <map n="7.png" x="2000" y="1000"/>
  10. <map n="8.png" x="3000" y="1000"/>
  11. </Maps>
  12. </SmallMaps>

经过考虑,将把预览图内容放入actor中

首先实现根据玩家动作缩放,移动预览图

然后是地图循环

先定义ifLoop,drawLoop状态,在actor中的draw方法里

如果是drawLoop状态,则在上次绘制的右边追加一次绘制

  1. {
  2. Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
  3. //一次性绘制
  4. oldColor=batch.getColor();
  5. batch.draw(spriteMain, getX(), getY(), getOriginX(), getOriginY(), getWidth(), getHeight(),
  6. getZoom(), getZoom(), getRotation());
  7. batch.draw(spriteCountry, getX(), getY(), getOriginX(), getOriginY(), getWidth(), getHeight(),
  8. getZoom(), getZoom(), getRotation());
  9.  
  10. //batch.setColor(Color.RED);
  11. if(drawLoop){
  12. batch.draw(spriteMain, getX()+mapW_px*this.zoom, getY(), getOriginX(), getOriginY(), getWidth(), getHeight(),
  13. getZoom(), getZoom(), getRotation());
  14. batch.draw(spriteCountry, getX()+mapW_px*this.zoom, getY(), getOriginX(), getOriginY(), getWidth(), getHeight(),
  15. getZoom(), getZoom(), getRotation());
  16. }
  17. //batch.setColor(oldColor);
  18.  
  19. }

draw方法修改

然后是通过x坐标判断drawLoop状态

  1. public void setX(float x) {
  2. if(ifLoop){////判断当前坐标是否需要循环显示右边
  3. if(x<=xMin&&x >= (-xMax * zoom + vw)){
  4. drawLoop=false;
  5. }else if(x>xMin){//从起点向左
  6. drawLoop=true;
  7. x=(-xMax -vw)+x;
  8. }else if(x < (-xMax + vw)&&x>(-xMax )){//划入右边,接近循环
  9. drawLoop=true;
  10. }else if(x<=(-xMax -vw)) {//划出并还原
  11. drawLoop=false;
  12. x = x + xMax +vw;
  13. }/**/
  14. }else {
  15. if (x > xMin) {
  16. x = xMin;
  17. } else if (x < -(xMax)) {
  18. x = -(xMax);
  19. }
  20. }
  21. this.x = x;
  22. }

判断x坐标位置实现循环

然后就可以实现循环了.

以下为地图色思路:

地图色这里使用的是区块色,前几天花了两天时间重绘了地图色

首先说思路:

首先绘制区块色(缩放1/4)

然后与预览图一起加载这里会完美的盖到一起(如果是用相同坐标绘制是不会有偏差的)

然后是区块变色,这里参考了libgdx 涂口红的原理,使用 texture.draw(pixmap,0,0);

变色前:

变色后:

  1. package com.zhfy.game.screen;
  2.  
  3. import java.util.HashMap;
  4. import java.util.List;
  5. import java.util.Map;
  6.  
  7. import com.badlogic.gdx.Gdx;
  8. import com.badlogic.gdx.Input;
  9. import com.badlogic.gdx.InputMultiplexer;
  10. import com.badlogic.gdx.InputProcessor;
  11. import com.badlogic.gdx.ScreenAdapter;
  12. import com.badlogic.gdx.graphics.Texture;
  13. import com.badlogic.gdx.graphics.g2d.BitmapFont;
  14. import com.badlogic.gdx.graphics.profiling.GLProfiler;
  15. import com.badlogic.gdx.input.GestureDetector;
  16. import com.badlogic.gdx.input.GestureDetector.GestureListener;
  17. import com.badlogic.gdx.math.Vector2;
  18. import com.badlogic.gdx.scenes.scene2d.Group;
  19. import com.badlogic.gdx.scenes.scene2d.InputEvent;
  20. import com.badlogic.gdx.scenes.scene2d.Stage;
  21. import com.badlogic.gdx.scenes.scene2d.ui.ImageButton;
  22. import com.badlogic.gdx.scenes.scene2d.ui.Label;
  23. import com.badlogic.gdx.scenes.scene2d.ui.Label.LabelStyle;
  24. import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
  25. import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;
  26. import com.badlogic.gdx.utils.XmlReader;
  27. import com.badlogic.gdx.utils.XmlReader.Element;
  28. import com.badlogic.gdx.utils.viewport.StretchViewport;
  29. import com.zhfy.game.MainGame;
  30. import com.zhfy.game.config.ResConfig;
  31. import com.zhfy.game.framework.GameLayout;
  32. import com.zhfy.game.framework.GameMap;
  33. import com.zhfy.game.framework.GameUtil;
  34. import com.zhfy.game.model.framework.TextureRegionListDAO;
  35. import com.zhfy.game.screen.actor.MapActor;
  36. import com.zhfy.game.screen.actor.SMapActor;
  37.  
  38. /**
  39. * 主游戏场景(游戏主界面), 实现 Screen 接口 或者 继承 ScreenAdapter 类 <br/>
  40. * 这里就展示一张图片代表游戏主界面
  41. */
  42. public class GameScreen extends ScreenAdapter {
  43.  
  44. private MainGame game;
  45.  
  46. private Stage gameStage;
  47.  
  48. private TextureRegionListDAO imgLists;
  49.  
  50. private TextureRegionListDAO imgUpList;
  51.  
  52. private TextureRegionListDAO imgDownList;
  53.  
  54. private ImageButton button;
  55. //使用场景
  56. private int screenId=81;
  57.  
  58. private int stageId;
  59.  
  60. InputProcessorEvent processorEvent;
  61. MapListener mapListener;
  62. InputMultiplexer multiplexer;
  63.  
  64. boolean isTouching;
  65. float touchBaseX;
  66. float touchBaseY;
  67. float touch_X;
  68. float touch_Y;
  69. float moveX;
  70. float moveY;
  71. Label label;
  72. //中心点位置
  73. float cx;
  74. float cy;
  75. float javaHeap;
  76. float javaMaxHeap;
  77. boolean ifLoop;
  78.  
  79. //游戏舞台的基本宽高
  80. static float GAMESTAGE_WIDTH;
  81. static float GAMESTAGE_HEIGHT;
  82. //GLProfile gp = new GrayFilter();
  83. GLProfiler gp;
  84.  
  85. //ui
  86. private Element uiR;
  87. private List<Element> ui;
  88. private XmlReader reader ;
  89. private String bgTexture;
  90. private float tempX,tempY,tempW,tempH;
  91. com.badlogic.gdx.utils.Array<Element> buttonEs;
  92. private Map tempMap;
  93.  
  94. private int i;//function的计数标志,从1开始
  95. private SMapActor mapActor;
  96.  
  97. private float mapX;
  98. private float mapY;
  99. private float mapXMax;
  100. private float mapYMax;
  101. private float mapScaleX;
  102. private float mapScaleY;
  103.  
  104. public GameScreen(MainGame mainGame) {
  105.  
  106. this.game=mainGame;
  107.  
  108. GAMESTAGE_WIDTH=game.getWorldWidth();
  109. GAMESTAGE_HEIGHT=game.getWorldHeight();
  110.  
  111. //ui
  112. {
  113. reader = ResConfig.reader;
  114. uiR=GameLayout.getXmlERootByScreenId(screenId);
  115. ui=GameUtil.getXmlEByRootE(uiR);
  116. }
  117.  
  118. setStageId(mainGame.getStageId());
  119.  
  120. //1根据mapid读取地图DAO
  121. //2加载地图图片,生成图片作为背景
  122. /*textureMap=framework.getMapByMapId(mapId);
  123. defMap=framework.getDefMapByMapId(mapId);
  124. mapW=defMap.getWidth();
  125. mapH=defMap.getHeight();
  126. mapSprite = new Sprite(textureMap);*/
  127.  
  128. //3增加拖动等功能,可以拖动地图
  129. //4替换地图dao的地图素材
  130. //5保存地图
  131.  
  132. gp =new GLProfiler(Gdx. graphics);
  133. gp.enable();
  134.  
  135. isTouching=false;
  136. multiplexer = new InputMultiplexer();
  137.  
  138. // 使用伸展视口创建舞台
  139. gameStage = new Stage(new StretchViewport(GAMESTAGE_WIDTH, GAMESTAGE_HEIGHT));
  140. // 将输入处理设置到舞台(必须设置, 否则点击按钮没效果)
  141.  
  142. multiplexer.addProcessor(gameStage);
  143. // 创建游戏人物演员
  144. //mapActor = new mapActor(new TextureRegion(mapSprite),0,0,mapW,mapH,GAMESTAGE_WIDTH, GAMESTAGE_HEIGHT);
  145.  
  146. //mapActor = new MapActor(stageId,GAMESTAGE_WIDTH, GAMESTAGE_HEIGHT,mainGame.getAssetManager(),screenId);
  147.  
  148. // 添加演员到舞台
  149. // gameStage.addActor(mapActor);
  150. //stageId不对,应从中获取对应的mapId,ifLoop也是 TODO
  151. ifLoop=true;
  152. mapActor = new SMapActor(stageId,GAMESTAGE_WIDTH, GAMESTAGE_HEIGHT,mainGame.getAssetManager(),screenId);
  153. gameStage.addActor(mapActor);
  154. //将来以玩家选择国家首都为位置标准 TODO
  155.  
  156. //根据screenId初始化资源
  157. //获取对应图片
  158. imgLists=GameUtil.getTextureReigonByScreenId( screenId,mainGame.getAssetManager());
  159.  
  160. i=1;
  161. for (Element window:ui) {
  162. tempX=window.getInt("x");tempY=window.getInt("y");tempW=window.getInt("w");tempH=window.getInt("h");
  163.  
  164. // 添加演员到舞台
  165. imgUpList=new TextureRegionListDAO();
  166. imgDownList=new TextureRegionListDAO();
  167. //遍历window的buttons按钮
  168. buttonEs = window.getChildByName("buttons").getChildrenByNameRecursively("button"); // 递归遍历,否则的话返回null
  169. for (Element buttonE : buttonEs) {
  170. //Gdx.app.log("ui测试", button.get("remark"));
  171. imgUpList.add(imgLists.getTextureByName(buttonE.get("imgUpName")));
  172. imgDownList.add(imgLists.getTextureByName(buttonE.get("imgDownName")));
  173.  
  174. button = new ImageButton(new TextureRegionDrawable(imgLists.getTextureByName(buttonE.get("imgUpName")).getTextureRegion()),new TextureRegionDrawable(imgLists.getTextureByName(buttonE.get("imgDownName")).getTextureRegion()),new TextureRegionDrawable(imgLists.getTextureByName(buttonE.get("imgDownName")).getTextureRegion()));
  175. button.setSize(buttonE.getInt("w")==0?imgLists.getTextureByName(buttonE.get("imgUpName")).getTextureRegion().getRegionWidth():buttonE.getInt("w")*imgLists.getTextureByName(buttonE.get("imgUpName")).getTextureRegion().getRegionWidth()/100, buttonE.getInt("h")==0?imgLists.getTextureByName(buttonE.get("imgUpName")).getTextureRegion().getRegionHeight():buttonE.getInt("h")*imgLists.getTextureByName(buttonE.get("imgUpName")).getTextureRegion().getRegionHeight()/100);
  176. button.setPosition(
  177. buttonE.getInt("x")*gameStage.getWidth()/100+button.getWidth()/2>gameStage.getWidth()?gameStage.getWidth()-button.getWidth():buttonE.getInt("x")*gameStage.getWidth()/100-button.getWidth()/2<0?0:buttonE.getInt("x")*gameStage.getWidth()/100-button.getWidth()/2,
  178. buttonE.getInt("y")*gameStage.getWidth()/100+button.getHeight()/2>gameStage.getHeight()?gameStage.getHeight()-button.getHeight():buttonE.getInt("y")*gameStage.getHeight()/100-button.getHeight()/2<0?0:buttonE.getInt("y")*gameStage.getHeight()/100-button.getHeight()/2);
  179. tempMap=new HashMap();
  180. tempMap.put("FUNCTION_ID", buttonE.get("functionId"));
  181. tempMap.put("ID", i);
  182. i++;
  183. function(tempMap);
  184. gameStage.addActor(button);
  185. Gdx.input.setInputProcessor(gameStage);
  186. }
  187. }
  188. //文字示例
  189. label=new Label("FPS:"+Gdx.graphics.getFramesPerSecond(),new LabelStyle(new BitmapFont(), null));
  190.  
  191. label.setWidth(200);//设置每行的宽度
  192. label.setWrap(true);//开启换行
  193. label.setPosition(20, 40);
  194. gameStage.addActor(label);
  195.  
  196. processorEvent = new InputProcessorEvent();
  197. mapListener=new MapListener();
  198.  
  199. Gdx.app.log("平台", Gdx.app.getType()+"");
  200. switch (Gdx.app.getType()) {
  201. case Desktop:// Code for Desktop applicationbreak;
  202. multiplexer.addProcessor(processorEvent);
  203. case Android:// Code for Android applicationbreak;
  204. multiplexer.addProcessor(new GestureDetector(mapListener));
  205. case WebGL:// Code for WebGL applicationbreak;
  206. multiplexer.addProcessor(processorEvent);
  207. default:// Unhandled (new?) platform applicationbreak;
  208. multiplexer.addProcessor(processorEvent);
  209. multiplexer.addProcessor(new GestureDetector(mapListener));
  210. }
  211. Gdx.input.setInputProcessor(multiplexer);
  212.  
  213. }
  214.  
  215. @Override
  216. public void render(float delta) {
  217.  
  218. handleInput();
  219. gameStage.draw();
  220. gameStage.act();
  221. //Gdx.app.log("FPS:", Gdx.graphics.getFramesPerSecond()+"");
  222.  
  223. javaHeap=(Math.round(Gdx.app.getJavaHeap()/1024/1024*10)/10);
  224. if(javaHeap>javaMaxHeap) {
  225. javaMaxHeap=javaHeap;
  226. }
  227.  
  228. label.setText("FPS:"+Gdx.graphics.getFramesPerSecond());
  229. label.getText().appendLine("");
  230. label.getText().appendLine("javaHeap:"+javaHeap+"m/"+javaMaxHeap+"m");
  231. label.getText().appendLine("drawCalls:"+gp.getDrawCalls());
  232. label.getText().appendLine("nativeHeap:"+Math.round(Gdx.app.getNativeHeap()/1024/1024*10)/10+"m");
  233. //label.getText().appendLine("inputKeyMode:"+mapActor.getKeyMode());//快捷键模式
  234. gp.reset();
  235. }
  236.  
  237. public void dispose() {
  238. super.dispose();
  239. // 场景被销毁时释放资源
  240. if (gameStage != null) {
  241. gameStage.dispose();
  242. gameStage=null;
  243. }
  244. if(mapActor!=null){
  245. mapActor.dispose();
  246. mapActor=null;
  247. }
  248.  
  249. }
  250.  
  251. //实现的功能
  252. public void function(Map map){
  253. int i=Integer.parseInt(map.get("FUNCTION_ID").toString());
  254. switch(i) {
  255. case 0://返回
  256. button.addListener(new ClickListener() {
  257. public void clicked(InputEvent event, float x, float y) {
  258. //Gdx.app.log("点击了第1个按钮", "x:" + x+" y:" + y);
  259. if(game!=null) {
  260. game.showGameScreen(screenId,1);
  261. }
  262. }
  263. });
  264. break;
  265. case 1://加载
  266. button.addListener(new ClickListener() {
  267. public void clicked(InputEvent event, float x, float y) {
  268. Gdx.app.log("点击了第2个按钮1", "x:" + x+" y:" + y+" stageId:"+stageId);
  269.  
  270. }
  271. });
  272. break;
  273. case 2://保存bin
  274. button.addListener(new ClickListener() {
  275. public void clicked(InputEvent event, float x, float y) {
  276. Gdx.app.log("点击了第3个按钮2", "x:" + x+" y:" + y+" stageId:"+stageId);
  277.  
  278. }
  279. });
  280. break;
  281. case 3://随机装饰(除特殊装饰外)
  282. button.addListener(new ClickListener() {
  283. public void clicked(InputEvent event, float x, float y) {
  284. Gdx.app.log("点击了第4个按钮", "x:" + x+" y:" + y+" stageId:"+stageId);
  285.  
  286. }
  287. });
  288. break;
  289. case 4:
  290. button.addListener(new ClickListener() {
  291. public void clicked(InputEvent event, float x, float y) {
  292. Gdx.app.log("点击了第4个按钮", "x:" + x+" y:" + y+" stageId:"+stageId);
  293.  
  294. }
  295. });
  296. break;
  297. case 5:
  298. button.addListener(new ClickListener() {
  299. public void clicked(InputEvent event, float x, float y) {
  300. Gdx.app.log("点击了第4个按钮", "x:" + x+" y:" + y+" stageId:"+stageId);
  301.  
  302. }
  303. });
  304. break;
  305. case 6:
  306. button.addListener(new ClickListener() {
  307. public void clicked(InputEvent event, float x, float y) {
  308. Gdx.app.log("点击了第5个按钮", "x:" + x+" y:" + y+" stageId:"+stageId);
  309.  
  310. }
  311. });
  312. break;
  313. default://查询绘制省区
  314. button.addListener(new ClickListener() {
  315. public void clicked(InputEvent event, float x, float y) {
  316. Gdx.app.log("点击了默认按钮", "x:" + x+" y:" + y);
  317. }
  318. });
  319. break;
  320. }
  321. }
  322.  
  323. public void resize(int width, int height) {
  324. // use true here to center the camera
  325. // that's what you probably want in case of a UI
  326. gameStage.getViewport().update(width, height, false);
  327. }
  328.  
  329. //电脑
  330. class InputProcessorEvent implements InputProcessor {
  331.  
  332. @Override
  333. public boolean keyDown(int keycode) {
  334. /*if (keycode == Keys.BACK) {
  335. // 处理返回事件
  336. } else if (keycode == Keys.MENU) {
  337. // 处理菜单事件
  338. }*/
  339. return true; // 如果此处设置为false那么不会执行key up
  340. }
  341.  
  342. @Override
  343. public boolean keyUp(int keycode) {
  344. return true;
  345. }
  346. @Override
  347. public boolean keyTyped(char character) { // 可以输出按键的字母和数字,不过貌似不好使
  348. return true;
  349. }
  350.  
  351. @Override
  352. public boolean touchDown(int screenX, int screenY, int pointer, int button) {
  353. return true;
  354. }
  355.  
  356. @Override
  357. public boolean touchUp(int screenX, int screenY, int pointer, int button) {
  358. return true;
  359. }
  360.  
  361. @Override
  362. public boolean touchDragged(int screenX, int screenY, int pointer) {
  363. return true;
  364. }
  365.  
  366. @Override
  367. public boolean mouseMoved(int screenX, int screenY) {
  368. cx=screenX;
  369. cy=screenY;
  370. return true;
  371. }
  372.  
  373. @Override
  374. public boolean scrolled(int amount) {
  375.  
  376. if(amount>0) {
  377. mapActor.setZoom(mapActor.getZoom()+0.01f,cx,cy);
  378. }else {
  379. mapActor.setZoom(mapActor.getZoom()-0.01f,cx,cy);
  380. }
  381. //Gdx.app.log("", mapActor.getZoom()+"");
  382. return true;
  383. }
  384.  
  385. }
  386. //触摸屏
  387.  
  388. class MapListener implements GestureListener{
  389.  
  390. @Override
  391. public boolean touchDown(float x, float y, int pointer, int button) {
  392. Gdx.app.log("touchDown", "x:" + x+" y:" + y);
  393. return false;
  394. }
  395.  
  396. @Override
  397. public boolean tap(float x, float y, int count, int button) {
  398. Gdx.app.log("tap", "x:" + x+" y:" + y);
  399. return false;
  400. }
  401.  
  402. @Override
  403. public boolean longPress(float x, float y) {
  404. Gdx.app.log("longPress", "x:" + x+" y:" + y);
  405. return false;
  406. }
  407.  
  408. @Override
  409. public boolean fling(float velocityX, float velocityY, int button) {
  410. Gdx.app.log("fling", "velocityX:" + velocityX+" velocityY:" + velocityY);
  411. return false;
  412. }
  413.  
  414. @Override
  415. public boolean pan(float x, float y, float deltaX, float deltaY) {
  416. Gdx.app.log("touchDown", "x:" + x+" y:" + y);
  417. return false;
  418. }
  419.  
  420. @Override
  421. public boolean panStop(float x, float y, int pointer, int button) {
  422. Gdx.app.log("touchDown", "x:" + x+" y:" + y);
  423. return false;
  424. }
  425.  
  426. @Override
  427. public boolean zoom (float originalDistance, float currentDistance){
  428. Gdx.app.log("zoom", "originalDistance:" + originalDistance+" currentDistance:" + currentDistance);
  429. //TODO 触摸缩放事件
  430. return false;
  431. }
  432.  
  433. @Override
  434. public boolean pinch (Vector2 initialFirstPointer, Vector2 initialSecondPointer, Vector2 firstPointer, Vector2 secondPointer){
  435. Gdx.app.log("pinch", "");
  436. return false;
  437. }
  438. @Override
  439. public void pinchStop () {
  440. Gdx.app.log("pinchStop", "");
  441. }
  442.  
  443. }
  444.  
  445. private void handleInput() {
  446.  
  447. // justTouched 是开始按下手指的第一个点。
  448. if (Gdx.input.justTouched() && isTouching == false) {
  449. isTouching = true;
  450. touchBaseX = Gdx.input.getX(0);
  451. touchBaseY = Gdx.input.getY(0);
  452. //touchBaseX += cam.position.x - GAMESTAGE_WIDTH / 2;
  453. //Gdx.app.log("触摸", "1");
  454.  
  455. // isTouched 是结束时,手指按下的点。
  456. } else if (Gdx.input.isTouched(0) && isTouching == true) {
  457. touch_X = Gdx.input.getX(0);
  458. touch_Y = Gdx.input.getY(0);
  459. moveX=(touchBaseX-touch_X)/20;
  460. moveY=(touch_Y-touchBaseY)/20;
  461. if(moveX>50) {
  462. moveX=50;
  463. }
  464. if(moveX<-50) {
  465. moveX=-50;
  466. }
  467. if(moveY>50) {
  468. moveY=50;
  469. }
  470. if(moveY<-50) {
  471. moveY=-50;
  472. }
  473. mapActor.setX(mapActor.getX()-moveX);
  474. mapActor.setY(mapActor.getY()-moveY);
  475.  
  476. //Gdx.app.log("移动","x:"+mapActor.getX()+" y:"+mapActor.getY());
  477. }else {
  478. isTouching =false;
  479. }
  480. /*
  481. if (Gdx.input.isKeyPressed(Input.Keys.A)) {
  482. mapActor.setZoom(mapActor.getZoom()+0.0001f,cx,cy);
  483. }
  484. if (Gdx.input.isKeyPressed(Input.Keys.Q)) {
  485. mapActor.setZoom(mapActor.getZoom()-0.0001f,cx,cy);
  486. }*/
  487.  
  488. if (Gdx.input.isKeyPressed(Input.Keys.LEFT)) {
  489. mapActor.setX(mapActor.getX()+1);
  490. }
  491. if (Gdx.input.isKeyPressed(Input.Keys.RIGHT)) {
  492. mapActor.setX(mapActor.getX()-1);
  493. }
  494. if (Gdx.input.isKeyPressed(Input.Keys.DOWN)) {
  495. mapActor.setY(mapActor.getY()+1);
  496. }
  497. if (Gdx.input.isKeyPressed(Input.Keys.UP)) {
  498. mapActor.setY(mapActor.getY()-1);
  499. }
  500. }
  501.  
  502. public int getStageId() {
  503. return stageId;
  504. }
  505.  
  506. public void setStageId(int stageId) {
  507. this.stageId = stageId;
  508. }
  509. }

GameScreen

  1. package com.zhfy.game.screen.actor;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5.  
  6. import com.badlogic.gdx.Gdx;
  7. import com.badlogic.gdx.assets.AssetManager;
  8. import com.badlogic.gdx.graphics.Color;
  9. import com.badlogic.gdx.graphics.GL20;
  10. import com.badlogic.gdx.graphics.Pixmap;
  11. import com.badlogic.gdx.graphics.PixmapIO;
  12. import com.badlogic.gdx.graphics.Texture;
  13. import com.badlogic.gdx.graphics.g2d.Batch;
  14. import com.badlogic.gdx.graphics.g2d.BitmapFont;
  15. import com.badlogic.gdx.graphics.g2d.Sprite;
  16. import com.badlogic.gdx.graphics.g2d.TextureRegion;
  17. import com.badlogic.gdx.scenes.scene2d.Actor;
  18. import com.badlogic.gdx.scenes.scene2d.InputEvent;
  19. import com.badlogic.gdx.scenes.scene2d.ui.Label;
  20. import com.badlogic.gdx.scenes.scene2d.ui.Label.LabelStyle;
  21. import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
  22. import com.zhfy.game.config.ResConfig;
  23. import com.zhfy.game.config.ResConfig.Map;
  24. import com.zhfy.game.framework.ComUtil;
  25. import com.zhfy.game.framework.GameFramework;
  26. import com.zhfy.game.framework.GameMap;
  27. import com.zhfy.game.framework.GameUtil;
  28. import com.zhfy.game.model.content.BTLDAO;
  29. import com.zhfy.game.model.content.MapBinDAO;
  30. import com.zhfy.game.model.content.def.DefMap;
  31. import com.zhfy.game.model.content.def.DefPt;
  32. import com.zhfy.game.model.content.def.DefStage;
  33. import com.zhfy.game.model.framework.Coord;
  34. import com.zhfy.game.model.framework.SpriteDAO;
  35. import com.zhfy.game.model.framework.SpriteListDAO;
  36.  
  37. public class SMapActor extends Actor {
  38. //做实时绘制前的备份
  39.  
  40. // 本类用来存放背景地图,获取地图范围,移动,缩放背景地图,确定点击位置等,以及环状地图
  41. //private DefMap defMap;
  42. private Pixmap pixmap;// 临时画布
  43. private MapBinDAO mapBinDAO;
  44. private BTLDAO btlDao;
  45. private DefStage defStage;
  46.  
  47. private List<Integer> coastGrid;
  48. private List<Integer> regionGrid;
  49.  
  50. private Texture textureMain;// 主图
  51. private Texture textureCountry;
  52. private Pixmap pixmapCountry;
  53.  
  54. private Sprite spriteMain;// 主图
  55. private Sprite spriteCountry;
  56. private GameFramework gameFramework;
  57. private float x;
  58. private float y;
  59. private int w;
  60. private int h;
  61.  
  62. private int mapId;
  63. private int mapW_px;
  64. private int mapH_px;
  65. private boolean ifLoop;// 是否循环
  66. private boolean drawLoop;
  67. private float zoom;
  68. private float zoomMax;
  69. private float zoomMin;
  70. private float xMin;
  71. private float yMin;
  72. private float xMax;
  73. private float yMax;
  74.  
  75. private float vw;
  76. private float vh;
  77.  
  78. private float vx;
  79. private float vy;
  80.  
  81. private Coord coord;
  82. private Coord gridCoord;
  83. private AssetManager am;
  84. Color oldColor;
  85. private int screenId;
  86. private boolean ifNeedUnload;//是否需要卸载,在资源加载完后卸载,如果未加载完退出,在退出时顺带清理资源
  87.  
  88. public float getVx() {
  89. return vx;
  90. }
  91.  
  92. public void setVx(float vx) {
  93. this.vx = vx;
  94. }
  95.  
  96. public float getVy() {
  97. return vy;
  98. }
  99.  
  100. public void setVy(float vy) {
  101. this.vy = vy;
  102. }
  103.  
  104. // 颜色层
  105. //private Texture textureGrid;
  106. private SpriteDAO spriteGrid;
  107. private SpriteListDAO spriteGrids;
  108. private int gw;
  109. private int gh;
  110.  
  111. public SMapActor(int stageId, float vw, float vh, AssetManager am, int screenId) {
  112. super();
  113. this.am=am;
  114. ifLoop = true;//TODO
  115. drawLoop=false;
  116. ifNeedUnload=false;
  117.  
  118. this.screenId=screenId;
  119. // 获取defMap
  120.  
  121. this.gameFramework=new GameFramework();
  122. defStage = gameFramework.getDefStageByStageId(stageId);
  123. long start = System.currentTimeMillis();
  124. btlDao=gameFramework.getBtlDaoByStageId(stageId);
  125. long end = System.currentTimeMillis(); // 记录结束时间
  126. Gdx.app.log("stage加载时间", (end-start)+"");
  127. mapBinDAO = gameFramework.getMapDaoByMapId(stageId);;
  128. //mapBinDAO=new MapBinDAO(btlDao);
  129.  
  130. coastGrid=mapBinDAO.getIdsByAround(12);
  131. regionGrid=mapBinDAO.getAllRegionIds();
  132.  
  133. {
  134. // 绘制主要图
  135. textureMain= new Texture(GameMap.drawSmallMapAndClearPixmap(stageId,am));
  136. spriteMain= new Sprite(textureMain);
  137. }
  138.  
  139. {
  140. this.x = 0;// TODO 随后设计为玩家起始位置
  141. this.y = 0;
  142. this.w = mapBinDAO.getMapWidth();
  143. this.h = mapBinDAO.getMapHeight();
  144. this.vw = vw;
  145. this.vh = vh;
  146. // 实际宽高
  147. this.mapW_px = textureMain.getWidth();
  148. this.mapH_px = textureMain.getHeight();
  149. this.zoom = 1f;
  150. this.zoomMax = 1.2f;
  151. this.zoomMin = 0.5f;
  152. this.xMax = mapW_px*this.zoom-vw;;
  153. this.yMax = mapH_px*this.zoom-vh;
  154. this.yMin = 0;
  155. this.xMin = 0;
  156.  
  157. }
  158.  
  159. /**/{//绘制一个区域图
  160. // 绘制画布转为装饰
  161. long startTime = System.currentTimeMillis();
  162. pixmapCountry = GameMap.createPixmap((int)(w*ResConfig.Map.SMALLMAP_SCALE),(int)(h*ResConfig.Map.SMALLMAP_SCALE));
  163. GameMap.getPixmapByDaoForColor(mapBinDAO,pixmapCountry,ResConfig.Map.SMALLMAP_SCALE,coastGrid);
  164. //PixmapIO.writePNG(Gdx.files.external("textureColor.png"), pixmapCountry);
  165. textureCountry=new Texture(pixmapCountry);
  166. long endTime = System.currentTimeMillis(); //获取结束时间
  167. Gdx.app.log("色图构建", " 运行时间:"+(endTime - startTime) + "ms");
  168. spriteCountry= new Sprite(textureCountry);
  169. spriteCountry.setSize(textureMain.getWidth(),textureMain.getHeight());
  170. }
  171.  
  172. /*{
  173. // 绘制画布转为装饰
  174. pixmap = GameMap.createPixmap((int)(w*ResConfig.Map.SMALLMAP_SCALE),(int)(h*ResConfig.Map.SMALLMAP_SCALE));
  175. pixmap=GameMap.coverImgByPtimgId(pixmap, 2);
  176. gameFramework.getPixmapDecorateByDao(pixmap, mapBinDAO,am,ResConfig.Map.SMALLMAP_SCALE);
  177. PixmapIO.writePNG(Gdx.files.external("textureDecorate.png"), pixmap);
  178. pixmap.dispose();
  179. }*/
  180.  
  181. setSize(spriteMain.getWidth(), spriteMain.getHeight());
  182.  
  183. addListener(new ClickListener() {
  184. @Override
  185. public void clicked(InputEvent event, float x, float y) {//TODO 点击生成任务区域
  186. // 点击图片,图片隐藏![这里写图片描述](https://img-blog.csdn.net/20160425220710777)
  187. // super.clicked(event, x, y);
  188. // // Gdx.app.log("点击的精灵位置1", "x:" + (getX()/getZoom()+x)+" y:"
  189. // // +(getY()/getZoom()+ y));
  190. //
  191. // // 由于要在actor等中使用坐标绘图,所以采用libgdx的actor坐标系
  192. // // 左下为0,0,向右上方向负数扩展
  193. //
  194. // // Gdx.app.log("点击背景位置1", "bgx:" + getX()+" bgy:" + getY()+"
  195. // // zoom:" + getZoom()+" x:" + x+" y:" + y+" rgw:" +
  196. // // getRegion().getRegionWidth()+" rgh:" +
  197. // // getRegion().getRegionHeight());
  198. // // Gdx.app.log("点击背景位置2", "actX:" + (getX()-x)/getZoom()+"
  199. // // actY:"+ (getY()-y)/getZoom());
  200.  
  201. //coord = GameMap.getHotCell(getImgXCoordByActX((getX() - x) / getZoom()), getImgYCoordByActY((getY() - y) / getZoom()),zoom);
  202.  
  203. //Gdx.app.log("点击背景位置3", "imgX:" + getImgXCoordByActX((getX() - x) / getZoom()) + " imgY:" + getImgYCoordByActY((getY() - y) / getZoom()));
  204. //Gdx.app.log("点击背景位置4", "imgX:" + coord.getX() + " imgY:" + coord.getY());
  205. /*pixmapCountry.drawLine(200, (int)x, 50, (int)y);
  206. textureCountry.draw(pixmapCountry, 0, 0);*/
  207. // MapBinDAO mapBinDAO, Pixmap pixmap,float scale,int regionId,Color color,List<Integer> coastGrid
  208.  
  209. pixmapCountry=GameMap.updateColorByRegion(mapBinDAO,pixmapCountry, Map.SMALLMAP_SCALE,regionGrid.get(ComUtil.getOneByListRand(regionGrid)),GameUtil.getColorByNum((int)(Math.random()*60000)),coastGrid);
  210. textureCountry.draw(pixmapCountry,0,0);
  211. }
  212. });
  213.  
  214. }
  215.  
  216. @Override
  217. public void act(float delta) {
  218. super.act(delta);
  219. }
  220.  
  221. @Override
  222. public void draw(Batch batch, float parentAlpha) {
  223. super.draw(batch, parentAlpha);
  224. if (spriteMain == null || !isVisible()) {
  225. return;
  226. }
  227.  
  228. {
  229. Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
  230. //一次性绘制
  231. //oldColor=batch.getColor();
  232. batch.draw(spriteMain, getX(), getY(), getOriginX(), getOriginY(), getWidth(), getHeight(),
  233. getZoom(), getZoom(), getRotation());
  234. batch.draw(spriteCountry, getX(), getY(), getOriginX(), getOriginY(), getWidth(), getHeight(),
  235. getZoom(), getZoom(), getRotation());
  236.  
  237. //batch.setColor(Color.RED);
  238. if(drawLoop){
  239. batch.draw(spriteMain, getX()+mapW_px*this.zoom, getY(), getOriginX(), getOriginY(), getWidth(), getHeight(),
  240. getZoom(), getZoom(), getRotation());
  241. batch.draw(spriteCountry, getX()+mapW_px*this.zoom, getY(), getOriginX(), getOriginY(), getWidth(), getHeight(),
  242. getZoom(), getZoom(), getRotation());
  243. }
  244. //batch.setColor(oldColor);
  245.  
  246. }
  247.  
  248. }
  249. public float getX() {
  250. return x;
  251. }
  252. public void setX(float x) {
  253. if(ifLoop){////判断当前坐标是否需要循环显示右边
  254. if(x<=xMin&&x >= (-xMax * zoom + vw)){
  255. drawLoop=false;
  256. }else if(x>xMin){//从起点向左
  257. drawLoop=true;
  258. x=(-xMax -vw)+x;
  259. }else if(x < (-xMax + vw)&&x>(-xMax )){//划入右边,接近循环
  260. drawLoop=true;
  261. }else if(x<=(-xMax -vw)) {//划出并还原
  262. drawLoop=false;
  263. x = x + xMax +vw;
  264. }/**/
  265. }else {
  266. if (x > xMin) {
  267. x = xMin;
  268. } else if (x < -(xMax)) {
  269. x = -(xMax);
  270. }
  271. }
  272. this.x = x;
  273. }
  274.  
  275. public float getY() {
  276. return y;
  277. }
  278. public void setY(float y) {
  279. if (y > yMin) {
  280. y = yMin;
  281. } else if (y < -yMax) {
  282. y = -yMax;
  283. }
  284. this.y = y;
  285. }
  286.  
  287. public int getW() {
  288. return w;
  289. }
  290.  
  291. public void setW(int w) {
  292. this.w = w;
  293. }
  294.  
  295. public int getH() {
  296. return h;
  297. }
  298.  
  299. public void setH(int h) {
  300. this.h = h;
  301. }
  302.  
  303. public float getVw() {
  304. return vw;
  305. }
  306.  
  307. public void setVw(float vw) {
  308. this.vw = vw;
  309. }
  310.  
  311. public float getVh() {
  312. return vh;
  313. }
  314.  
  315. public void setVh(float vh) {
  316. this.vh = vh;
  317. }
  318.  
  319. public float getZoom() {
  320. return zoom;
  321. }
  322.  
  323. public void setZoom(float zoom,float cx,float cy) {
  324. this.xMax = mapW_px*zoom-vw;;
  325. this.yMax = mapH_px*zoom-vh;
  326. this.xMin = 0;
  327. this.yMin = 0;
  328.  
  329. if (zoom > zoomMax) {
  330. zoom = zoomMax;
  331. } else if (zoom < zoomMin) {
  332. zoom = zoomMin;
  333. } else {
  334. setX( x + ((x - cx) * (zoom-this.zoom)) / zoom);
  335. setY( y + ((y - (vh-cy)) * (zoom-this.zoom)) / zoom);
  336. this.zoom = zoom;
  337. }
  338.  
  339. }
  340.  
  341. // 将↓→坐标(左上角为0,0,越向右下越大)转为←↓act坐标(左下角为0,0,越向右上越小)
  342. public float getActXCoordByImgX(float print_x) {
  343. return (-print_x);
  344. }
  345.  
  346. public float getActYCoordByImgY(float print_y) {
  347. return (print_y - spriteMain.getHeight());
  348. }
  349.  
  350. // 将←↓act坐标(左下角为0,0,越向右上越小)转化为↓→坐标(左上角为0,0,越向右下越大)
  351. // 将↓→坐标(左上角为0,0,越向右下越大)转为←↓act坐标(左下角为0,0,越向右上越小)
  352. public float getImgXCoordByActX(float print_x) {
  353. if(print_x!=0f) {
  354. return (-print_x);
  355. }else {
  356. return 0f;
  357. }
  358. }
  359.  
  360. public float getImgYCoordByActY(float print_y) {
  361. return (spriteMain.getHeight() + print_y);
  362. }
  363.  
  364. // 清理5张图层
  365. public void dispose() {
  366. if(pixmapCountry!=null){
  367. pixmapCountry.dispose();
  368. pixmapCountry=null;
  369. }
  370. if (textureMain != null) {
  371. textureMain.dispose();
  372. textureMain=null;
  373. }
  374. if (textureCountry!=null){
  375. textureCountry.dispose();
  376. textureCountry=null;
  377. }
  378. if (ifNeedUnload) {
  379. GameUtil.unloadSingleResByScreenId(am, screenId);
  380. }
  381.  
  382. }
  383.  
  384. public Pixmap getPixmap() {
  385. return pixmap;
  386. }
  387.  
  388. public void setPixmap(Pixmap pixmap) {
  389. this.pixmap = pixmap;
  390. }
  391.  
  392. public MapBinDAO getMapBinDAO() {
  393. return mapBinDAO;
  394. }
  395.  
  396. public void setMapBinDAO(MapBinDAO mapBinDAO) {
  397. this.mapBinDAO = mapBinDAO;
  398. }
  399.  
  400. public Texture gettextureMain() {
  401. return textureMain;
  402. }
  403.  
  404. public void settextureMain(Texture textureMain) {
  405. this.textureMain = textureMain;
  406. }
  407.  
  408. public Sprite getspriteMain() {
  409. return spriteMain;
  410. }
  411.  
  412. public void setspriteMain(Sprite spriteMain) {
  413. this.spriteMain = spriteMain;
  414. }
  415.  
  416. public SpriteDAO getSpriteGrid() {
  417. return spriteGrid;
  418. }
  419.  
  420. public void setSpriteGrid(SpriteDAO spriteGrid) {
  421. this.spriteGrid = spriteGrid;
  422. }
  423.  
  424. public float getZoomMax() {
  425. return zoomMax;
  426. }
  427.  
  428. public void setZoomMax(float zoomMax) {
  429. this.zoomMax = zoomMax;
  430. }
  431.  
  432. public int getGw() {
  433. return gw;
  434. }
  435.  
  436. public void setGw(int gw) {
  437. this.gw = gw;
  438. }
  439.  
  440. public int getGh() {
  441. return gh;
  442. }
  443.  
  444. public void setGh(int gh) {
  445. this.gh = gh;
  446. }
  447.  
  448. public SpriteListDAO getSpriteGrids() {
  449. return spriteGrids;
  450. }
  451.  
  452. public void setSpriteGrids(SpriteListDAO spriteGrids) {
  453. this.spriteGrids = spriteGrids;
  454. }
  455. }

SMapActor

  1. package com.zhfy.game.framework;
  2.  
  3. import java.io.BufferedInputStream;
  4. import java.io.File;
  5. import java.io.FileInputStream;
  6. import java.io.IOException;
  7. import java.io.InputStream;
  8. import java.lang.reflect.Field;
  9. import java.util.ArrayList;
  10. import java.util.List;
  11. import java.util.Properties;
  12.  
  13. import com.badlogic.gdx.Gdx;
  14. import com.badlogic.gdx.assets.AssetManager;
  15. import com.badlogic.gdx.files.FileHandle;
  16. import com.badlogic.gdx.graphics.Pixmap;
  17. import com.badlogic.gdx.graphics.Pixmap.Format;
  18. import com.badlogic.gdx.graphics.PixmapIO;
  19. import com.badlogic.gdx.graphics.Texture;
  20. import com.badlogic.gdx.graphics.TextureData;
  21. import com.badlogic.gdx.graphics.g2d.TextureRegion;
  22. import com.badlogic.gdx.scenes.scene2d.Stage;
  23. import com.badlogic.gdx.utils.Array;
  24. import com.badlogic.gdx.utils.XmlReader;
  25. import com.badlogic.gdx.utils.XmlReader.Element;
  26. import com.badlogic.gdx.utils.viewport.StretchViewport;
  27. import com.zhfy.game.MainGame;
  28. import com.zhfy.game.config.ResConfig;
  29. import com.zhfy.game.framework.tool.BTLTooL;
  30. import com.zhfy.game.framework.tool.FSearchTool;
  31. import com.zhfy.game.model.content.BTLDAO;
  32. import com.zhfy.game.model.content.MapBinDAO;
  33. import com.zhfy.game.model.content.def.DefMap;
  34. import com.zhfy.game.model.content.def.DefPt;
  35. import com.zhfy.game.model.content.def.DefStage;
  36. import com.zhfy.game.model.content.def.DefTerrainimg;
  37. import com.zhfy.game.model.framework.PixmapDAO;
  38. import com.zhfy.game.model.framework.PixmapListDAO;
  39. import com.zhfy.game.model.framework.TextureDAO;
  40. import com.zhfy.game.model.framework.TextureListDAO;
  41. import com.zhfy.game.model.framework.TextureRegionDAO;
  42. import com.zhfy.game.model.framework.TextureRegionListDAO;
  43. import com.zhfy.game.screen.actor.base.BaseActor;
  44.  
  45. public class GameFramework {
  46.  
  47. //private TextureListDAO imgLists;
  48.  
  49. //private Texture imgBg;
  50.  
  51. //private List<Stage> stages;
  52.  
  53. private byte[] bt;// 等待清空
  54.  
  55. //private MapBinDAO mapBinDao;// 等待清空
  56.  
  57. //private Pixmap pixmap;// 使用完就清空
  58.  
  59. //private PixmapListDAO pixmapLists;// 使用完就清空
  60.  
  61. private FSearchTool tool ;//哪个用到哪个初始化
  62.  
  63. /*//TODO
  64. // 通过使用场景来清理图片
  65. public void textureDispose(int beforeScreenId,int nextScreenId) {
  66. Gdx.app.log("执行了内存销毁 " ," screenId:"+beforeScreenId);
  67. //获取当前场景,下一个场景要加载的资源图
  68. //比对资源,把不用的资源清除掉
  69. // 通过使用场景id获取图片名集合
  70. List<String> nameList = getTextureNameByScreenId(beforeScreenId);
  71. // 根据图片名集合清理资源
  72. imgLists.clearTexture(nameList);
  73. // 清理背景图
  74. if (imgBg != null) {
  75. imgBg.dispose();
  76. imgBg = null;
  77. }
  78. }*/
  79.  
  80. // 通过screenId获取要读取的图片资源
  81. public List<String> getTextureNameByScreenId(int screenId) {
  82. List<String> strs = new ArrayList<String>();
  83. XmlReader reader = ResConfig.reader;
  84. Element root = ResConfig.ConfigScreen;
  85. int childNum = root.getChildCount();
  86. for (int i = 0; i < childNum; i++) {
  87. if (root.getChild(i).getInt("id") == screenId) {
  88. Array<Element> xmlFiles = root.getChild(i)
  89. .getChildrenByNameRecursively("xmlFile");
  90. for (Element xmlFile : xmlFiles) {
  91. strs.add(xmlFile.get("name"));
  92. }
  93. break;
  94. }
  95. }
  96. return strs;
  97. }
  98.  
  99. /*// 根据screenid,获取要加载的资源
  100. public TextureRegionListDAO getTextureReigonByScreenId(int screenId) {
  101. // 验证imgLists是否有东西
  102. List<String> nameList = getTextureNameByScreenId(screenId);
  103. TextureRegionListDAO imgList = new TextureRegionListDAO();
  104. for (int i = 0; i < nameList.size(); i++) {
  105. XmlReader reader = ResConfig.reader;
  106. *//**
  107. * root是整个xml文件从根节点算起,此处是指<Credits> </Credits>
  108. *//*
  109. Element root = reader.parse(
  110. Gdx.files.internal("image/" + nameList.get(i) + ".xml"));
  111. String imgFile = root.getAttribute("imagePath");
  112. // 每个图片添加的时候都要加使用场景,多场景用;分割screenid="1"
  113. Array<Element> images = root.getChildrenByNameRecursively("sprite");
  114. for (Element image : images) {
  115. TextureRegion imgRegion = new TextureRegion(
  116. imgLists.getName(nameList.get(i)).getTexture(),
  117. image.getInt("x"), image.getInt("y"), image.getInt("w"),
  118. image.getInt("h"));
  119. TextureRegionDAO imgRegionDAO = new TextureRegionDAO();
  120. imgRegionDAO.setTextureRegion(imgRegion);
  121. imgRegionDAO.setRefx(image.getInt("refx"));
  122. imgRegionDAO.setRefy(image.getInt("refy"));
  123. imgRegionDAO.setName(image.get("n").replace(".png", ""));
  124. imgList.add(imgRegionDAO);
  125. // Gdx.app.log("getImgName",image.get("n").replace(".png", "")+"
  126. // x:"+image.getInt("refx")+" y:"+image.getInt("refy"));
  127. }
  128. }
  129. imgList.check();
  130. return imgList;
  131. }*/
  132.  
  133. /*// 根据imgName,获取要加载的资源
  134. public TextureRegionListDAO getTextureReigonByImgFileName(
  135. String imgFileName) {
  136. // 验证imgLists是否有东西
  137. TextureRegionListDAO imgList = new TextureRegionListDAO();
  138. XmlReader reader = ResConfig.reader;
  139. Element root = reader
  140. .parse(Gdx.files.internal("image/" + imgFileName + ".xml"));
  141. String imgFile = root.getAttribute("imagePath");
  142. // 每个图片添加的时候都要加使用场景,多场景用;分割screenid="1"
  143. Array<Element> images = root.getChildrenByNameRecursively("sprite");
  144. for (Element image : images) {
  145. TextureRegion imgRegion = new TextureRegion(
  146. imgLists.getName(imgFileName).getTexture(),
  147. image.getInt("x"), image.getInt("y"), image.getInt("w"),
  148. image.getInt("h"));
  149. TextureRegionDAO imgRegionDAO = new TextureRegionDAO();
  150. imgRegionDAO.setTextureRegion(imgRegion);
  151. imgRegionDAO.setRefx(image.getInt("refx"));
  152. imgRegionDAO.setRefy(image.getInt("refy"));
  153. imgRegionDAO.setName(image.get("n").replace(".png", ""));
  154. imgList.add(imgRegionDAO);
  155. // Gdx.app.log("getImgName",image.get("n").replace(".png", "")+"
  156. // x:"+image.getInt("refx")+" y:"+image.getInt("refy"));
  157. }
  158. imgList.check();
  159. return imgList;
  160. }*/
  161.  
  162. // TODO 未验证
  163. // 根据imgName,获取要加载的资源
  164. /*public PixmapListDAO getPixmapByImgFileName(String fileName) {
  165. PixmapListDAO pixmapLists=new PixmapListDAO();
  166. // 验证pixmapLists是否有东西
  167. XmlReader reader = ResConfig.reader;
  168. Element root = reader
  169. .parse(Gdx.files.internal("pixmap/" + fileName + ".xml"));
  170. // 每个图片添加的时候都要加使用场景,多场景用;分割screenid="1"
  171. Array<Element> images = root.getChildrenByNameRecursively("sprite");
  172. for (Element image : images) {
  173. PixmapDAO pixmapDAO = new PixmapDAO();
  174. pixmapDAO.setPixmap( new Pixmap(Gdx.files.internal("pixmap/"+image.get("n"))));;
  175. pixmapDAO.setRefx(image.getInt("refx"));
  176. pixmapDAO.setRefy(image.getInt("refy"));
  177. pixmapDAO.setName(image.get("n").replace(".png", ""));
  178. pixmapLists.add(pixmapDAO);
  179. }
  180. pixmapLists.check();
  181. return pixmapLists;
  182. }*/
  183.  
  184. /* // 根据screenId获取加载的背景图名称
  185. public Texture getBgTextureByScreenId(int screenId,AssetManager as) {
  186. XmlReader reader = ResConfig.reader;
  187. String str = "";
  188. Element root = ResConfig.ConfigScreen;
  189. int childNum = root.getChildCount();
  190. for (int i = 0; i < childNum; i++) {
  191. if (root.getChild(i).getInt("id") == screenId) {
  192. str = root.getChild(i).get("bgImg");
  193. //imgBg = new Texture(Gdx.files.internal("image/" + str + ".png"));
  194. imgBg =as.get("image/" + str + ".png", Texture.class);
  195. return imgBg;
  196. }
  197. }
  198. return null;
  199. }*/
  200.  
  201. // 根据screenId来把资源存入 getResourceByScreentId
  202. /*public void init(int screenId) {
  203. imgLists = new TextureListDAO();
  204. List<String> nameList = getTextureNameByScreenId(screenId);
  205. //stages = new ArrayList<Stage>();
  206.  
  207. for (int i = 0; i < nameList.size(); i++) {
  208. XmlReader reader = ResConfig.reader;
  209. *//**
  210. * root是整个xml文件从根节点算起,此处是指<Credits> </Credits>
  211. *//*
  212. Element root = reader.parse(
  213. Gdx.files.internal("image/" + nameList.get(i) + ".xml"));
  214. String imgFile = root.getAttribute("imagePath");
  215. // 加入临时文件
  216. TextureDAO tempImgDao = new TextureDAO();
  217. tempImgDao.setName(imgFile.toString().substring(0,
  218. imgFile.toString().lastIndexOf(".")));
  219. tempImgDao.setTexture(
  220. new Texture(Gdx.files.internal("image/" + imgFile)));
  221. imgLists.add(tempImgDao);
  222. // Gdx.app.log( "setImgName", imgFile.toString().substring(0,
  223. // imgFile.toString().lastIndexOf(".")));
  224. }
  225. }*/
  226.  
  227. // 根据配置文件生成相关stage对象并返回
  228. // stage相当于一个窗口,一个游戏场景由一个或多个窗口组成
  229. // 暂时废弃
  230. /* public List<Stage> getStagesByScreenId(int screenId) {
  231. Stage stage = new Stage(new StretchViewport(ResConfig.FIX_WORLD_WIDTH,
  232. Gdx.graphics.getHeight() * ResConfig.FIX_WORLD_WIDTH / Gdx.graphics.getWidth()));
  233. GameFramework framework = new GameFramework();
  234. // 根据screenId获取下级元素
  235. XmlReader reader = ResConfig.reader;
  236. Element root = reader
  237. .parse(Gdx.files.internal("config/config_layout.xml"));
  238. int childNum = root.getChildCount();
  239. for (int i = 0; i < childNum; i++) {
  240. if (root.getChild(i).getInt("screenId") == screenId) {
  241. // 绘制背景图
  242. imgBg = new Texture(Gdx.files.internal(
  243. "image/" + root.getChild(i).get("bg") + ".png"));
  244. BaseActor actor = new BaseActor(new TextureRegion(imgBg));
  245. stage.addActor(actor);
  246. // 如果defaultWinId不为空,则查找对应win
  247. if (!root.getChild(i).get("defaultWinId").equals("")) {
  248. int childNum1 = root.getChild(i).getChildCount();
  249.  
  250. for (int i1 = 0; i1 < childNum1; i1++) {
  251. // 生成win下的各个组件 pixmap 拼合图 textures 装饰图 buttons 按钮
  252. Element pixmaps = root.getChild(i).getChild(i1)
  253. .getChildByName("pixmaps");
  254. // Gdx.app.log("imgs",imgs.size+"");
  255. // 临时-拼合图
  256. {
  257. Array<Element> pixmapList = pixmaps
  258. .getChildrenByNameRecursively("pixmap");
  259. for (Element pixmap : pixmapList) {
  260. Array<Element> imgs = pixmap
  261. .getChildrenByNameRecursively("img");
  262. // Gdx.app.log("imgs",imgs.size+"");
  263. for (Element img : imgs) {
  264.  
  265. }
  266. }
  267. }
  268. Element textures = root.getChild(i).getChild(i1)
  269. .getChildByName("textures");
  270. // 装饰图
  271. {
  272.  
  273. }
  274.  
  275. Element buttons = root.getChild(i).getChild(i1)
  276. .getChildByName("buttons");
  277. // 按钮
  278. {
  279.  
  280. }
  281.  
  282. if (stage != null) {
  283. //stages.add(stage);
  284. }
  285. // 打开默认窗口
  286. }
  287. }
  288. }
  289. }
  290. return null;
  291. }*/
  292.  
  293. // 根据mapid生成MapBinDAO类 画全图
  294. public MapBinDAO getMapDaoByMapId(int mapId) {
  295. XmlReader reader = ResConfig.reader;
  296. String str = "";
  297. Element root = reader.parse(Gdx.files.internal("config/def_map.xml"));
  298. int childNum = root.getChildCount();
  299. MapBinDAO mapBinDao=null;
  300. for (int i = 0; i < childNum; i++) {
  301. if (root.getChild(i).getInt("id") == mapId) {
  302. str = root.getChild(i).get("name");
  303. //TODO 安卓不能加载,会闪退
  304. bt = Gdx.files.local("bin/" + str + ".bin").readBytes();
  305. //bt = Gdx.files.internal("bin/" + str + ".bin").readBytes();
  306. try {
  307. mapBinDao = GameMap.saveBin(bt);
  308. } catch (IOException e) {
  309. e.printStackTrace();
  310. }
  311. return mapBinDao;
  312. }
  313. }
  314. return mapBinDao;
  315. }
  316.  
  317. public BTLDAO getBtlDaoByStageId(int stageId) {
  318. XmlReader reader = ResConfig.reader;
  319. String str = "";
  320. // FileHandle file = Gdx.files.internal("rule/rule_fb1_btl.xml");
  321. Element root = reader.parse(Gdx.files.internal("config/def_stage.xml"));
  322. int childNum = root.getChildCount();
  323. BTLDAO btlDao=null;
  324.  
  325. for (int i = 0; i < childNum; i++) {
  326. if (root.getChild(i).getInt("id") == stageId) {
  327. str = root.getChild(i).get("name");
  328. bt = Gdx.files.internal("stage/" + str + ".btl").readBytes();
  329. try {
  330. btlDao = BTLTooL.LoadBtl("rule/rule_fb1_btl.xml",bt);
  331. } catch (Exception e) {
  332. e.printStackTrace();
  333. }
  334. return btlDao;
  335. }
  336. }
  337. return btlDao;
  338. }
  339.  
  340. //根据传入的地图编号保存地图
  341. public void saveMapDaoByMapId(MapBinDAO mapbin,int mapId) {
  342. XmlReader reader = ResConfig.reader;
  343. String str = "";
  344. Element root = reader.parse(Gdx.files.internal("config/def_map.xml"));
  345. int childNum = root.getChildCount();
  346. for (int i = 0; i < childNum; i++) {
  347. if (root.getChild(i).getInt("id") == mapId) {
  348. str = root.getChild(i).get("name");
  349. String path="bin/" + str + ".bin";
  350. GameMap.saveMapBin(mapbin,path);
  351. }
  352. }
  353. }
  354.  
  355. //根据MapId获得地图属性
  356. public DefMap getDefMapByMapId(int id) {
  357. DefMap defMap = new DefMap();
  358. List<DefMap> defMaps = new ArrayList<DefMap>();
  359. try {
  360. defMaps = GameUtil.getDaoListByClass(defMap, "config/def_map.xml");
  361. } catch (Exception e) {
  362. e.printStackTrace();
  363. }
  364. if (defMaps != null) {
  365. try {
  366. tool= new FSearchTool(defMaps, "id");
  367. return (DefMap) tool.searchTask((id+""));
  368. } catch (Exception e) {
  369. // TODO Auto-generated catch block
  370. e.printStackTrace();
  371. }
  372.  
  373. }
  374. return null;
  375. }
  376.  
  377. //根据MapId获得地图属性
  378. public DefStage getDefStageByStageId(int id) {
  379. DefStage defStage = new DefStage();
  380. List<DefStage> defStages = new ArrayList<DefStage>();
  381. try {
  382. defStages = GameUtil.getDaoListByClass(defStage, "config/def_stage.xml");
  383. } catch (Exception e) {
  384. e.printStackTrace();
  385. }
  386. if (defStages != null) {
  387. try {
  388. tool= new FSearchTool(defStages, "id");
  389. return (DefStage) tool.searchTask((id+""));
  390. } catch (Exception e) {
  391. // TODO Auto-generated catch block
  392. e.printStackTrace();
  393. }
  394.  
  395. }
  396. return null;
  397. }
  398.  
  399. // 依据id画全图
  400. public Texture getMapByMapId(int id,AssetManager am,float scale) {
  401. // 2.1加载图片资源 一次性使用 用完就删
  402. //TextureRegionListDAO mapRegionList = getTextureReigonByImgFileName("bt_tiles");
  403. PixmapListDAO pixmapLists=getPixmapListByFileName("pm_tiles",am);
  404.  
  405. MapBinDAO mapBinDao=null;
  406.  
  407. // 2.2加载地图属性
  408. DefTerrainimg defTerrainimg = new DefTerrainimg();
  409. DefMap defMap = new DefMap();
  410. List<DefTerrainimg> defTerrainimgs = new ArrayList<DefTerrainimg>();
  411. List<DefMap> defMaps = new ArrayList<DefMap>();
  412. boolean state=false;
  413.  
  414. try {
  415. defTerrainimgs = GameUtil.getDaoListByClass(defTerrainimg,
  416. "config/def_terrainimg.xml");
  417. defMaps = GameUtil.getDaoListByClass(defMap, "config/def_map.xml");
  418. } catch (Exception e) {
  419. e.printStackTrace();
  420. }
  421. // 2.3加载bin
  422. mapBinDao = getMapDaoByMapId(id);
  423.  
  424. // 2.4读取map.xml获取地图属性
  425. if (defMaps != null) {
  426. /*for (int i = 0; i < defMaps.size(); i++) {
  427. if (defMaps.get(i).getId() == id) {
  428. defMap = defMaps.get(i);
  429. state=true;
  430. break;
  431. }
  432. }*/
  433.  
  434. try {
  435. tool= new FSearchTool(defMaps, "id");
  436. } catch (Exception e) {
  437. // TODO Auto-generated catch block
  438. e.printStackTrace();
  439. }
  440. defMap =(DefMap) tool.searchTask((id+""));
  441. if(defMap!=null) {
  442. state=true;
  443. }
  444. }
  445.  
  446. if(state) {
  447. Gdx.app.log("存在地图:", "继续");
  448. }else {
  449. Gdx.app.log("不存在地图:", "终止");
  450. return null;
  451. }
  452.  
  453. // 2.5生成Pixmap
  454. Pixmap pixmap=GameMap.createPixmapByDefMap(defMap);
  455. //绘制底纹
  456. pixmap=GameMap.coverImgByPtimgId(pixmap,2);
  457.  
  458. long startTime = System.currentTimeMillis(); //获取开始时间
  459. //绘图
  460. pixmap=GameMap.getPixmapByDao(mapBinDao,pixmap,pixmapLists,defTerrainimgs,0,mapBinDao.getMapbin().size(),scale);
  461. long endTime = System.currentTimeMillis(); //获取结束时间
  462.  
  463. Gdx.app.log("地图构建", " 运行时间:"+(endTime - startTime) + "ms");
  464.  
  465. // 再由新的 Pixmap 对象生成一个新的 Texture 对象
  466. Texture textureMap = new Texture(pixmap, Format.RGBA8888,false);
  467. if(pixmap!=null) {
  468. //这里输出截图进行测试
  469. //PixmapIO.writePNG(Gdx.files.external(defMap.getName()+".png"), pixmap);
  470. pixmap.dispose();
  471. //Gdx.app.log("地图图片保存", "成功");
  472. }
  473. //clearPixmapListByFileName("pm_tiles");
  474. return textureMap;
  475. }
  476.  
  477. // 根据条件画图
  478. public Pixmap getPixmapDecorateByDao(Pixmap pixmap,MapBinDAO mapBinDao,AssetManager am,float scale) {
  479. // 2.1加载图片资源 一次性使用 用完就删
  480. //TextureRegionListDAO mapRegionList = getTextureReigonByImgFileName("bt_tiles");
  481. PixmapListDAO pixmapLists=getPixmapListByFileName("pm_tiles",am);
  482.  
  483. // 2.2加载地图属性
  484. DefTerrainimg defTerrainimg = new DefTerrainimg();
  485. List<DefTerrainimg> defTerrainimgs = new ArrayList<DefTerrainimg>();
  486.  
  487. try {
  488. defTerrainimgs = GameUtil.getDaoListByClass(defTerrainimg,
  489. "config/def_terrainimg.xml");
  490. } catch (Exception e) {
  491. e.printStackTrace();
  492. }
  493.  
  494. long startTime = System.currentTimeMillis(); //获取开始时间
  495. //绘图
  496. pixmap=GameMap.getPixmapByDao(mapBinDao,pixmap,pixmapLists,defTerrainimgs,0,mapBinDao.getMapbin().size(),scale);
  497. long endTime = System.currentTimeMillis(); //获取结束时间
  498. Gdx.app.log("地图构建", " 运行时间:"+(endTime - startTime) + "ms");
  499.  
  500. //clearPixmapListByFileName("pm_tiles");
  501. return pixmap;
  502. }
  503.  
  504. //根据条件给陆地打孔 废弃
  505. /* public Pixmap getLandByDAO(MapBinDAO mapBinDAO, Pixmap pixmap, int mapx, int mapy,int mapw,int maph) {
  506. //加载打孔图片
  507. PixmapListDAO pixmapLists=getPixmapListByFileName("pm_shadow");
  508. pixmap=GameMap.getLandByDAO(mapBinDAO,pixmap,pixmapLists,mapx,mapy,mapw,maph);
  509. //clearPixmapListByFileName("pm_shadow");
  510. return pixmap;
  511. }*/
  512.  
  513. //加载内存图片PixmapListDAO pixmapLists 目标pm_tiles 则传入pm_tiles TODO
  514. public static PixmapListDAO getPixmapListByFileName(String fileName,AssetManager am) {
  515. //1读取路径下说明文件
  516. XmlReader reader = ResConfig.reader;
  517. Element root = reader
  518. .parse(Gdx.files.internal("pixmap/"+fileName.substring(3)+"/" + fileName + ".xml"));
  519. // 每个图片添加的时候都要加使用场景,多场景用;分割screenid="1"
  520. Array<Element> images = root.getChildrenByNameRecursively("sprite");
  521. //验证pixmapLists是否有东西
  522. PixmapListDAO pixmapLists=new PixmapListDAO();
  523. //2根据说明文件添加到pixmapLists
  524. for (Element image : images) {
  525. PixmapDAO pixmapDAO = new PixmapDAO();
  526. //pixmapDAO.setPixmap( new Pixmap(Gdx.files.internal("pixmap/"+fileName.substring(3)+"/" +image.get("n"))));;
  527. pixmapDAO.setPixmap(am.get("pixmap/"+fileName.substring(3)+"/" +image.get("n"),Pixmap.class));
  528.  
  529. pixmapDAO.setRefx(image.getInt("refx"));
  530. pixmapDAO.setRefy(image.getInt("refy"));
  531. pixmapDAO.setName(image.get("n").replace(".png", ""));
  532. pixmapLists.add(pixmapDAO);
  533. }
  534. pixmapLists.check();
  535. return pixmapLists;
  536. }
  537. //清除内存图片PixmapListDAO pixmapLists
  538. /*public void clearPixmapListByFileName(String fileName) {
  539. //1读取路径下说明文件
  540. //2根据说明文件移除pixmapLists的内容,并depose
  541. XmlReader reader = ResConfig.reader;
  542. Element root = reader
  543. .parse(Gdx.files.internal("pixmap/"+fileName.substring(3)+"/" + fileName + ".xml"));
  544. // 每个图片添加的时候都要加使用场景,多场景用;分割screenid="1"
  545. Array<Element> images = root.getChildrenByNameRecursively("sprite");
  546. for (Element image : images) {
  547. pixmapLists.remove(image.get("n").replace(".png", ""));
  548. }
  549. pixmapLists.check();
  550. }*/
  551.  
  552. }

GameFramework

package com.zhfy.game.model.content;

import java.io.BufferedInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.lang.reflect.Field;
import java.lang.reflect.Type;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream; import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Pixmap;
import com.zhfy.game.framework.ComUtil;
import com.zhfy.game.framework.GameMap;
import com.zhfy.game.framework.GameUtil;
import com.zhfy.game.model.content.btl.BtlModule1;
import com.zhfy.game.model.content.def.DefMap;
import com.zhfy.game.model.content.def.DefTerrainimg;
import com.zhfy.game.model.framework.Coord; public class MapBinDAO { public int mapVersion;// 2-地图版本
public int mapWidth;// 4-长
public int mapHeight;// 4-宽
public List<MapBin> MapBin;// //写死了
public MapBinDAO(BTLDAO btlDao){
if(btlDao!=null) {
mapVersion=1;
mapWidth=Integer.parseInt(btlDao.getBm0().getBm0_13());
mapHeight=Integer.parseInt(btlDao.getBm0().getBm0_14());
MapBin=new ArrayList<MapBin>();
MapBin mapBin;
for(BtlModule1 bm1:btlDao.getBm1()) {
mapBin=new MapBin();
mapBin.setBlockType(Integer.parseInt(bm1.getBm1_1()));
mapBin.setBackTile(Integer.parseInt(bm1.getBm1_2()));
mapBin.setBackIdx(Integer.parseInt(bm1.getBm1_3()));
mapBin.setBackRefX(Integer.parseInt(bm1.getBm1_4()));
mapBin.setBackRefY(Integer.parseInt(bm1.getBm1_5()));
mapBin.setWaterPass(Integer.parseInt(bm1.getBm1_6()));
mapBin.setLandPass(Integer.parseInt(bm1.getBm1_7()));
mapBin.setRegionId(Integer.parseInt(bm1.getBm1_8()));
mapBin.setClimateId(Integer.parseInt(bm1.getBm1_9()));
mapBin.setBuildId(Integer.parseInt(bm1.getBm1_10()));
mapBin.setBuildLv(Integer.parseInt(bm1.getBm1_11()));
mapBin.setFacility(Integer.parseInt(bm1.getBm1_12()));
mapBin.setAreaId(Integer.parseInt(bm1.getBm1_13()));
MapBin.add(mapBin);
}
}
} public int getMapVersion() {
return mapVersion;
} public void setMapVersion(int mapVersion) {
this.mapVersion = mapVersion;
} public int getMapWidth() {
return mapWidth;
} public void setMapWidth(int mapWidth) {
this.mapWidth = mapWidth;
} public int getMapHeight() {
return mapHeight;
} public void setMapHeight(int mapHeight) {
this.mapHeight = mapHeight;
} public List<MapBin> getMapbin() {
return MapBin;
} public void setMapbin(List<MapBin> MapBin) {
this.MapBin = MapBin;
} public void initRegionId() {
int i, iMax;
iMax = MapBin.size();
for (i = 0; i < iMax; i++) {
MapBin.get(i).setRegionId(i);
}
} //随机装饰(除特殊装饰,以及ref有值的外)
public void randomAllDecoration() {
Random rand = new Random();
//储存地块最高范围
Map map = GameMap.getDecorateRandMaxMap(); //Map(id_type+"_min",idx_min) Map(id_type+"_max",idx_max)
//循环遍历所有地块
int i;
int iLength = MapBin.size();
int vMin, vMax;
for (i = 0; i < iLength; i++) {
/*
* if(MapBin.get(i).getBackTile()==5) { Gdx.app.log("MapBin.i:",
* "imin:"+(MapBin.get(i).getBackTile()+"_"+MapBin.get(i).
* getBlockType()+"_min")+" imax:"+(MapBin.get(i).getBackTile()+"_"+
* MapBin.get(i).getBlockType()+"_max")); }
*/
if (map.containsKey(MapBin.get(i).getBackTile() + "_" + MapBin.get(i).getBlockType() + "_min") && map.containsKey(MapBin.get(i).getBackTile() + "_" + MapBin.get(i).getBlockType() + "_max") && MapBin.get(i).getBackRefX() == 0 && MapBin.get(i).getBackRefY() == 0) { vMin = (Integer) map.get(MapBin.get(i).getBackTile() + "_" + MapBin.get(i).getBlockType() + "_min");
vMax = (Integer) map.get(MapBin.get(i).getBackTile() + "_" + MapBin.get(i).getBlockType() + "_max");
//Gdx.app.log("", " backTile:"+MapBin.get(i).getBackTile()+" type:"+MapBin.get(i).getBackTile()+" max:"+map.get(MapBin.get(i).getBackTile()+"_"+MapBin.get(i).getBlockType()+"_max")+" min:"+map.get(MapBin.get(i).getBackTile()+"_"+MapBin.get(i).getBlockType()+"_min")+" backIdx:"+backIdx);
MapBin.get(i).setBackIdx(rand.nextInt(vMax - vMin + 1) + vMin);
}
if (map.containsKey(MapBin.get(i).getForeTile() + "_" + MapBin.get(i).getBlockType() + "_min") && map.containsKey(MapBin.get(i).getForeTile() + "_" + MapBin.get(i).getBlockType() + "_max") && MapBin.get(i).getForeRefX() == 0 && MapBin.get(i).getForeRefY() == 0) {
vMin = (Integer) map.get(MapBin.get(i).getForeTile() + "_" + MapBin.get(i).getBlockType() + "_min");
vMax = (Integer) map.get(MapBin.get(i).getForeTile() + "_" + MapBin.get(i).getBlockType() + "_max");
//Gdx.app.log("", " backTile:"+MapBin.get(i).getBackTile()+" type:"+MapBin.get(i).getBackTile()+" max:"+map.get(MapBin.get(i).getBackTile()+"_"+MapBin.get(i).getBlockType()+"_max")+" min:"+map.get(MapBin.get(i).getBackTile()+"_"+MapBin.get(i).getBlockType()+"_min")+" backIdx:"+backIdx);
MapBin.get(i).setForeIdx(rand.nextInt(vMax - vMin + 1) + vMin);
}
}
} //随机增加2层装饰
public void randomForeDecoration() {
int foreId, vMin, vMax;
Random rand = new Random();
//储存地块最高范围
Map map = GameMap.getDecorateRandMaxMap();
//循环遍历所有地块
int i;
int iLength = MapBin.size();
Object o1; for (i = 0; i < iLength; i++) {
//配对规则
//4丘陵 4 5 6
//5山地 4 5 6
//6森林 4 5 6
if (rand.nextInt(100) < 31 && map.containsKey(MapBin.get(i).getBackTile() + "_" + MapBin.get(i).getBlockType() + "_min") && map.containsKey(MapBin.get(i).getBackTile() + "_" + MapBin.get(i).getBlockType() + "_max") && MapBin.get(i).getBackTile() > 3 && MapBin.get(i).getBackTile() < 7) {
foreId = 4 + rand.nextInt(3);
MapBin.get(i).setForeTile(foreId);
o1 = map.get(foreId + "_" + MapBin.get(i).getBlockType() + "_min");
if (o1 != null && MapBin.get(i).getForeRefX() == 0 && MapBin.get(i).getForeRefY() == 0) {
vMin = (Integer) map.get(foreId + "_" + MapBin.get(i).getBlockType() + "_min");
vMax = (Integer) map.get(foreId + "_" + MapBin.get(i).getBlockType() + "_max");
//Gdx.app.log("", " backTile:"+MapBin.get(i).getBackTile()+" type:"+MapBin.get(i).getBackTile()+" max:"+map.get(MapBin.get(i).getBackTile()+"_"+MapBin.get(i).getBlockType()+"_max")+" min:"+map.get(MapBin.get(i).getBackTile()+"_"+MapBin.get(i).getBlockType()+"_min")+" backIdx:"+backIdx);
MapBin.get(i).setForeIdx(rand.nextInt(vMax - vMin + 1) + vMin);
}
} else {
MapBin.get(i).setForeTile(0);
MapBin.get(i).setForeIdx(0);
}
}
} //规则 有建筑或有地名的区块=id 否则为0
public void resetRegionId() {
int i;
int iMax = MapBin.size();
for (i = 0; i < iMax; i++) {
if (MapBin.get(i).getAreaId() == 0 && MapBin.get(i).getBuildId() == 0) {
MapBin.get(i).setRegionId(0);
} else {
MapBin.get(i).setRegionId(i);
}
}
} //根据纬度生成随机气候 TODO
public void randomClimate() { } //随机海岸河流 TODO
public void randomCoastRever() { } //完全随机地图 TODO
public void randomAll(int i) { } //重新切割省区 TODO
public void cutRegion() {
Random rand = new Random();
int tempId, exct = 0, i = 0, iMax = 0, j, jMax, rsct, riSize;
List<Integer> regions = null;
List<Integer> temps = null;
Map regionCountMap = new HashMap();
Boolean ifSea =false; for (j = 0; j < 5; j++) {
exct=0;
regions = getRegions(j);
Gdx.app.log("阶段1", "核心地块数" + regions.size()+"获得地块类:"+j); //循环 挨个对核心地块进行循环
while (exct < 20-j && regions.size() != 0) {
for (i = regions.size() - 1; i >= 0; i--) {
tempId = getRegionId(regions.get(i));
if(!ifSea){//判断海洋,移除
if(tempId>=0&&MapBin.get(tempId).getBlockType()==1){
tempId=-1;
}
}
if (tempId >= 0) {
riSize = getIdByRegion(tempId).size();
if (riSize > (13 + Math.random() * 5)) {
regions.remove(i);
} else {
MapBin.get(tempId).setRegionId(regions.get(i));
}
} else {
regions.remove(i);
}
}
Gdx.app.log("阶段2", "核心地块数" + regions.size() + " 循环次数:" + exct);
exct++;
}
}
//循环2 自定义随机地块 // 随机一定条件取核心值
//循环挨个核心地块循环 //如果核心地块周围有非核心地块的空地且面积不超过一定值,则染色(条件可放宽),否则移除,直到移除全部地块
{
regions = getRegionIdsByChance((int) 5,ifSea);
rsct = getRegionForIdIs0();
Gdx.app.log("阶段3", "剩余绘制数" + rsct);
for (Integer rg : regions) {
if(!ifSea&&MapBin.get(rg).getBlockType()==1){ }else{
MapBin.get(rg).setRegionId(rg);
} }
} int rgc = 0;//可用地块
while (rsct > 0) {//空白地块不能等于0
jMax = regions.size();
if(jMax==0) {
break;
}
for (j = jMax - 1; j >= 0; j--) {//regions.get(j)为当前地块值
//定义当前地块为特定地块 //循环填充周围地块
iMax=(int)(10 + Math.random() * 10);
for (i = 0; i < iMax; i++) {
rgc = getRegionId(regions.get(j));
if (rgc == -1) {
regions.remove(regions.get(j));
break;
} else {
MapBin.get(rgc).setRegionId(regions.get(j));
}
}
}
rsct = getRegionForIdIs0();
Gdx.app.log("阶段3", "剩余绘制数" + rsct + " 核心地块数:" + regions.size());
} /* 自检,合并不规范地块 */
{
//0查看所有region为0的地块并合并到周围地块
{
regions = getIdsForBlankRegion();
for (Integer region : regions) {//rsCoords.size() rand.nextInt(
temps = getAroundIdById(region, 9);
if (temps.size() != 0) {
MapBin.get(region).setRegionId(temps.get(rand.nextInt(temps.size())));
} else {
//此时剩下的应该是碎小的岛屿或水洞
temps = getAroundIdById(region, 0);
if (temps.size() != 0) {
MapBin.get(region).setRegionId(temps.get(rand.nextInt(temps.size())));
} else {
Gdx.app.log("警告:有空白地块无法合并到周围地块:", region + "");
} }
}
}
//对所有region数量进行检查
//Map.Entry entry;
int rgct, argId;
List<Integer> argIds, regionCounts;
{
checkRegion(); regionCountMap = getRegionCountMap();
regionCounts = ComUtil.getKeyByMap(regionCountMap);
for (Integer rgId : regionCounts) {
if (regionCountMap.containsKey(rgId) && regionCountMap.get(rgId) != null) {
rgct = Integer.parseInt(regionCountMap.get(rgId).toString());
if (rgct < 7) {
if (MapBin.get(rgId).getBuildId() == 1) {
//0:低于5的城市合并周围的最低地块普通或建筑地块(小于5)
argIds = getAroundRegionId(rgId);//获取周围地块
for (Integer tarId : argIds) {
if (Integer.parseInt(regionCountMap.get(tarId).toString()) < 6 && MapBin.get(tarId).getBuildId() != 1 && MapBin.get(tarId).getBlockType() != 1) {
updateRegionIds(rgId, tarId);
regionCountMap.put(rgId, (Integer.parseInt(regionCountMap.get(tarId).toString()) + Integer.parseInt(regionCountMap.get(rgId).toString())));
regionCountMap.remove(tarId);
break;
}
}
} else if (MapBin.get(rgId).getBuildId() == 4) {
if(ifSea) {//如果容许海洋,则合并
//1:低于5的海港合并周围的最低海洋地块(小于5)
argIds = getAroundRegionId(rgId);//获取周围地块
for (Integer tarId : argIds) {
if (Integer.parseInt(regionCountMap.get(tarId).toString()) < 6 && MapBin.get(tarId).getBuildId() != 4 && MapBin.get(tarId).getBlockType() == 1) {
updateRegionIds(rgId, tarId);
regionCountMap.put(rgId, (Integer.parseInt(regionCountMap.get(tarId).toString()) + Integer.parseInt(regionCountMap.get(rgId).toString())));
regionCountMap.remove(tarId);
break;
}
}
}else{
//否则把海港归到最近的地块
updHabourForE();
}
} else if (MapBin.get(rgId).getBlockType() != 1 && MapBin.get(rgId).getBuildId() == 0) {
//2:低于5的陆地地块合并到周围地块数量最低的陆地地块
argIds = getAroundRegionId(rgId);//获取周围地块
for (Integer tarId : argIds) {
if (Integer.parseInt(regionCountMap.get(tarId).toString()) < 6 && MapBin.get(tarId).getBuildId() != 4 && MapBin.get(tarId).getBlockType() != 1) {
updateRegionIds(rgId, tarId);
regionCountMap.put(rgId, (Integer.parseInt(regionCountMap.get(tarId).toString()) + Integer.parseInt(regionCountMap.get(rgId).toString())));
regionCountMap.remove(tarId);
break;
}
} } else if (MapBin.get(rgId).getBlockType() == 1&&ifSea) {
//3:低于5的海洋地块合并到周围地块数量最低的海洋地块 argIds = getAroundRegionId(rgId);//获取周围地块
for (Integer tarId : argIds) {
if (Integer.parseInt(regionCountMap.get(tarId).toString()) < 6 && MapBin.get(tarId).getBuildId() != 4 && MapBin.get(tarId).getBlockType() == 1) {
updateRegionIds(rgId, tarId);
regionCountMap.put(rgId, (Integer.parseInt(regionCountMap.get(tarId).toString()) + Integer.parseInt(regionCountMap.get(rgId).toString())));
regionCountMap.remove(tarId);
break;
}
}
} else if (MapBin.get(rgId).getBlockType() != 1 && MapBin.get(rgId).getBuildId() != 1) {
//4:低于5的非城市建筑陆地地块合并到最近的城市地块 argIds = getAroundRegionId(rgId);//获取周围地块
for (Integer tarId : argIds) {
if (Integer.parseInt(regionCountMap.get(tarId).toString()) < 6 && MapBin.get(tarId).getBuildId() != 4 && MapBin.get(tarId).getBlockType() != 1) {
updateRegionIds(rgId, tarId);
regionCountMap.put(rgId, (Integer.parseInt(regionCountMap.get(tarId).toString()) + Integer.parseInt(regionCountMap.get(rgId).toString())));
regionCountMap.remove(tarId);
break;
}
} } else {
Gdx.app.log("警告:未检测到的地块:", rgId + "");
}
}
}
}
} //0查看所有孤岛类地块并合并到周围地块
{
//自动合并孤岛类地块
mergeIslandGridByRegion();
regions = getIdsForBlankRegion();
for (Integer region : regions) {//rsCoords.size() rand.nextInt(
if (MapBin.get(region).getBlockType() == 1) {
temps = getAroundIdById(region, 3);
} else {
temps = getAroundIdById(region, 4);
}
if (temps.size() != 0) {
MapBin.get(region).setRegionId(temps.get(rand.nextInt(temps.size())));
} else {
Gdx.app.log("警告:有空白地块无法合并到周围地块:", region + "");
}
}
}
{//如果不需要海则清除
if(!ifSea){
for(MapBin mapbin:MapBin){
if(mapbin.getBlockType()==1&&mapbin.getBuildId()!=4){
mapbin.setRegionId(0);
}
}
}
}
Gdx.app.log("执行完成", "");
}
} //重新切割省区
public void cutRegionForE(int maxC) {
Random rand = new Random();
int tempId, exct = 0, i = 0, iMax = 0, j, jMax, rsct, riSize;
List<Integer> regions = null;
List<Integer> temps = null;
Map regionCountMap = new HashMap(); {
exct=0;
regions = getRegions(1);
Gdx.app.log("阶段1", "核心地块数" + regions.size()+"获得地块类:"+1); //循环 挨个对核心地块进行循环
while (exct < maxC && regions.size() != 0) {
for (i = regions.size() - 1; i >= 0; i--) {
tempId = getRegionIdForE(regions.get(i));
if (tempId >= 0) {
MapBin.get(tempId).setRegionId(regions.get(i));
} else {
regions.remove(i);
}
}
Gdx.app.log("阶段2", "核心地块数" + regions.size() + " 循环次数:" + exct);
exct++;
}
} //打印空白陆地地块和海洋有港口的地块
// logBlankGrid();
} //把region是target的修改为 rgId
private void updateRegionIds(Integer rgId, Integer targetId) {
//交换前检查合不合法
if(MapBin.get(MapBin.get(rgId).getRegionId()).getRegionId()!=MapBin.get(rgId).getRegionId()) {
Gdx.app.log("警告:不合法,修正前", "rgId:"+rgId+" targetId:"+targetId);
rgId=MapBin.get(MapBin.get(rgId).getRegionId()).getRegionId();
Gdx.app.log("警告:不合法,修正后", "rgId:"+rgId+" targetId:"+targetId);
}
for (MapBin mapBin : MapBin) {
if (mapBin.getRegionId() == targetId) {
mapBin.setRegionId(rgId);
}
} } public List<Integer> getAroundRegionId(int rgId) {
List<Integer> regions = new ArrayList<Integer>();
List<Integer> tempId;
int i, iMax = MapBin.size();
int blockType = MapBin.get(rgId).getBlockType();
for (i = 0; i < iMax; i++) {
if (MapBin.get(i).getRegionId() == rgId) {
if (blockType != 1) {
//陆地
tempId = getAroundIdById(i, 4);
} else {
//海洋
tempId = getAroundIdById(i, 3);
}
for (Integer id : tempId) {
if (MapBin.get(id).getRegionId() != rgId) {
regions.add(MapBin.get(id).getRegionId());
}
}
}
}
tempId = new ArrayList<Integer>();//去重
for (Integer in : regions) {
if (!tempId.contains(in)) {
tempId.add(in);
}
}
return tempId;
} //根据条件获得核心地块
private List<Integer> getRegions(int i) { //0 全部核心地块 只限城市和海港
//1 只限于陆地的核心地块 只限城市
//2 只限于陆地的核心地块 只限工厂且未被覆盖
//3 只限于陆地的核心地块 只限机场且未被覆盖
//4 只限于陆地的核心地块 只限油库且未被覆盖
//5 只限于海洋的核心地块 只限海港
//6 全部核心地块,不限类型
int m;
int mMax = MapBin.size();
List<Integer> regions = new ArrayList<Integer>();
if (i == 0) {
for (m = 0; m < mMax; m++) {
if (MapBin.get(m).getAreaId() != 0 || (MapBin.get(m).getBuildId() == 1 || MapBin.get(m).getBuildId() == 4)) {
regions.add(m);
}
}
} else if (i == 1) {
for (m = 0; m < mMax; m++) {
if (MapBin.get(m).getBlockType() != 1 && (MapBin.get(m).getAreaId() != 0 || (MapBin.get(m).getBuildId() == 1))) {
regions.add(m);
}
}
} else if (i == 2) {
for (m = 0; m < mMax; m++) {
if (MapBin.get(m).getBlockType() != 1 && (MapBin.get(m).getRegionId() == m || MapBin.get(m).getRegionId() == 0) && (MapBin.get(m).getAreaId() != 0 || (MapBin.get(m).getBuildId() == 2))) {
regions.add(m);
}
}
} else if (i == 3) {
for (m = 0; m < mMax; m++) {
if (MapBin.get(m).getBlockType() != 1 && (MapBin.get(m).getRegionId() == m || MapBin.get(m).getRegionId() == 0) && (MapBin.get(m).getAreaId() != 0 || (MapBin.get(m).getBuildId() == 3))) {
regions.add(m);
}
}
} else if (i == 4) {
for (m = 0; m < mMax; m++) {
if (MapBin.get(m).getBlockType() != 1 && (MapBin.get(m).getRegionId() == m || MapBin.get(m).getRegionId() == 0) && (MapBin.get(m).getAreaId() != 0 || (MapBin.get(m).getBuildId() == 5))) {
regions.add(m);
}
}
} else if (i == 5) {
for (m = 0; m < mMax; m++) {
if (MapBin.get(m).getBlockType() == 1 && (MapBin.get(m).getAreaId() != 0 || (MapBin.get(m).getBuildId() == 4))) {
regions.add(m);
}
}
} else if (i == 6) {
for (m = 0; m < mMax; m++) {
if(!regions.contains(MapBin.get(m).getRegionId())) {
regions.add(m);
}
}
}
return regions;
} //根据id获得周围6边的地块id
/*
* type 0全部 1获取region为0的地块 3获取周围的有region的海洋地块 4获取周围的有region的陆地地块
* 5只获取region为0的海洋地块 6只获取region为0的陆地地块 7只获取region为0的陆地平原地块
* 8只获取region为0的陆地非平原地块 9根据id区分海洋陆地,获取region为0的地块
* 10 获取周围是陆地的地块 11获取周围是海洋的地块
* 12获取沿海陆地地块
*/
public List<Integer> getAroundIdById(int id, int type) {
List<Integer> ids = new ArrayList<Integer>();
List<Integer> rsIds = new ArrayList<Integer>();
boolean top = false;
boolean foot = false;
boolean left = false;
boolean right = false;
//判断处于哪个边
int y = (int) id / mapWidth;
int x = id - y * mapWidth;
int t1, t2, t3, t4, t5, t6; if ((x&1) == 1) {
t1 = id - 1;
t2 = id - mapWidth;
t3 = id + 1;
t4 = id + mapWidth - 1;
t5 = id + mapWidth;
t6 = id + mapWidth + 1;
} else {
t1 = id - mapWidth - 1;
t2 = id - mapWidth;
t3 = id - mapWidth + 1;
t4 = id - 1;
t5 = id + mapWidth;
t6 = id + 1;
}
if (x == 0) {
left = true;
}
if (x == mapWidth - 1) {
right = true;
}
if (y == 0) {
top = true;
}
if (y == mapHeight - 1) {
foot = true;
} if (!top && !left) {
ids.add(t1);
}
if (!top) {
ids.add(t2);
}
if (!top && !right) {
ids.add(t3);
}
if (!foot && !left) {
ids.add(t4);
}
if (!foot) {
ids.add(t5);
}
if (!foot && !right) {
ids.add(t6);
} //type 0全部 1只读取region为0的值 5只获取region为0的海洋地块 6只获取region为0的陆地地块
//7只获取region为0的陆地平原地块 8只获取region为0的陆地非平原地块
//10 获取周围是陆地的地块 11获取周围是海洋的地块
if (type == 0) {
return ids;
} else {
for (Integer id2 : ids) {
if (type == 1 && MapBin.get(id2).getRegionId() == 0) {
rsIds.add(id2);
} else if (type == 6 && MapBin.get(id2).getRegionId() == 0 && MapBin.get(id2).getBlockType() != 1) {
rsIds.add(id2);
} else if (type == 5 && MapBin.get(id2).getRegionId() == 0 && (MapBin.get(id2).getBlockType() == 1 || MapBin.get(id2).getBackTile() == 1)) {
rsIds.add(id2);
} else if (type == 7 && MapBin.get(id2).getRegionId() == 0 && MapBin.get(id2).getBlockType() != 1 && MapBin.get(id2).getBackTile() == 0) {
rsIds.add(id2);
} else if (type == 8 && MapBin.get(id2).getRegionId() == 0 && MapBin.get(id2).getBlockType() != 1 && MapBin.get(id2).getBackTile() != 0) {
rsIds.add(id2);
} else if (type == 3 && MapBin.get(id2).getRegionId() != 0 && (MapBin.get(id2).getBlockType() == 1)) {
rsIds.add(id2);
} else if (type == 4 && MapBin.get(id2).getRegionId() != 0 && (MapBin.get(id2).getBlockType() != 1)) {
rsIds.add(id2);
} else if (type == 9 && ((MapBin.get(id).getBlockType()!=1) ==(MapBin.get(id2).getBlockType() != 1))) {
rsIds.add(id2);
} else if (type == 10 && (MapBin.get(id2).getBlockType() != 1)) {
rsIds.add(id2);
} else if (type == 11 && (MapBin.get(id2).getBlockType() == 1)) {
rsIds.add(id2);
}else if (type == 12 && (MapBin.get(id2).getBlockType() == 1)&& (MapBin.get(id).getBlockType() != 1)) {
rsIds.add(id2);
}
}
return rsIds;
}
} public List<Integer> getAroundIdByIds(List<Integer> ids, int type) {
List<Integer> rss = new ArrayList<Integer>();
for (Integer id : ids) {
List<Integer> rs = getAroundIdById(id, type);
rss.addAll(rs);
}
return rss;
} private List<Integer> getIdByRegion(int regionId) {
int m;
int mMax = MapBin.size();
List<Integer> rs = new ArrayList<Integer>(); for (m = 0; m < mMax; m++) {
if (MapBin.get(m).getRegionId() == regionId) {
int s=m;
rs.add(s);
}
}
return rs;
} //-1找不到
//获得要扩展的id
public int getRegionId(int r) {
List<Integer> rsIds;
//1判断自身是海洋还是陆地
if (MapBin.get(r).getBlockType() == 1) {//海
//获取周边的海洋地块
rsIds = getAroundIdById(r, 5);
if (rsIds.size() == 0) {
rsIds = getAroundIdByIds(getIdByRegion(r), 5);
if (rsIds == null || rsIds.size() == 0) {
//如果周围没有空余地块,则使用相邻的相邻地块判断
return -1;
}
}
//根据条件随机获取其中一个地块id
//return(rsIds.get(rand.nextInt(rsIds.size())));
return getShortAroundId(rsIds, MapBin.get(r).getRegionId());
} else {//陆
//获取周边陆地地块
rsIds = getAroundIdById(r, 7);
if (rsIds.size() != 0&&ComUtil.ifGet(30)) {
return getShortAroundId(rsIds, MapBin.get(r).getRegionId());
} else {
rsIds = getAroundIdById(r, 8);
if (rsIds.size() != 0&&ComUtil.ifGet(30)) {
return getShortAroundId(rsIds, MapBin.get(r).getRegionId());
} else {
rsIds = getAroundIdById(r, 6);
if (rsIds.size() != 0&&ComUtil.ifGet(30)) {
return getShortAroundId(rsIds, MapBin.get(r).getRegionId());
} else {
rsIds = getAroundIdByIds(getIdByRegion(r), 6);
if (rsIds != null && rsIds.size() == 0&&ComUtil.ifGet(50)) {
//如果周围没有空余地块,则使用相邻的相邻地块判断
rsIds = getAroundIdByIds(getIdByRegion(r), 7);
if (rsIds != null && rsIds.size() == 0) {
//如果周围没有空余地块,则使用相邻的相邻地块判断
rsIds = getAroundIdByIds(getIdByRegion(r), 8);
if (rsIds != null && rsIds.size() == 0) {
return -1;
}
}
}
if (rsIds == null) {
return -1;
}
return getShortAroundId(rsIds, MapBin.get(r).getRegionId());
}
}
}
}
} //-1找不到
//获得要扩展的id
public int getRegionIdForE(int r) {
List<Integer> rsIds;
List<Integer> tempIds;
//1判断自身是海洋还是陆地
if (MapBin.get(r).getBlockType() == 1) {//海
//获取周边的海洋地块
rsIds = getAroundIdById(r, 5);
if (rsIds.size() == 0) {
rsIds = getAroundIdByIds(getIdByRegion(r), 5);
if (rsIds == null || rsIds.size() == 0) {
//如果周围没有空余地块,则使用相邻的相邻地块判断
return -1;
}
}
//根据条件随机获取其中一个地块id
//return(rsIds.get(rand.nextInt(rsIds.size())));
return getShortAroundId(rsIds, MapBin.get(r).getRegionId());
} else {//陆
//获取周边陆地地块
rsIds = getAroundIdById(r, 6);
if (rsIds.size() == 0) {
tempIds= getIdByRegion(r);
rsIds = getAroundIdByIds(tempIds, 6);
if (rsIds == null || rsIds.size() <3) {
//如果周围没有空余地块,则使用相邻的相邻地块判断
//Gdx.app.log("清除核心", r+"");
return -1;
}
}
int id= getShortAroundId(rsIds, MapBin.get(r).getRegionId());
return id;
}
} //chance 最高获取概率,最低默认为10 如果不容许有海洋地块,则多抽0.1
private List<Integer> getRegionIdsByChance(int chance,boolean ifSea) {
if(!ifSea){
chance= (int) (chance*1.2);
} int cutSide = 5;
Random rand = new Random(); int i, j, m,rsMax,rpMax;
int mMax = MapBin.size();//chance<(int)(Math.random()*(100)
List<Integer> rs = new ArrayList<Integer>();
List<Integer> result = new ArrayList<Integer>();
//获取全部为空的地块
for (m = 0; m < mMax; m++) {
if (MapBin.get(m).getRegionId() == 0) {
if(ifSea){
rs.add(m);
}else if(!ifSea&&MapBin.get(m).getBlockType()!=1){
rs.add(m);
}
}
}
if (rs.size() == 0) {
return rs;
}
rsMax=rs.size();
rpMax=rsMax*chance/100;
/*List<Coord> coords = converCoords(rs);
List<Coord> rsCoords = new ArrayList<Coord>();
rs = new ArrayList<Integer>();
for (i = mapWidth; i >= 0; i = i - cutSide) {
for (j = mapHeight; j >= 0; j = j - cutSide) {
for (m = coords.size() - 1; m >= 0; m--) {
if (coords.get(m).getX() > i && coords.get(m).getY() > j) {
rsCoords.add(coords.get(m));
coords.remove(m);
}
if (rsCoords.size()>0) {
rs.add(rsCoords.get(rand.nextInt(rsCoords.size())).getId());
rsCoords.clear();
rs=ComUtil.getNewList(rs);
}
if(rs.size()>rsMax*chance/100){
break;
}
if (coords.size() == 0) {
break;
}
}
}
}*/
int tempId;
for(i = 0;i<rpMax;i++){
//显示数字并将其从列表中删除,从而实现不重复.
tempId=new Random().nextInt(rs.size());
rs.remove(tempId);
result.add(tempId);
} Gdx.app.log("获得随机核心", rsMax+":"+result.size());
return result;
} //获取空白值
private int getRegionForIdIs0() {
int ct = 0, m, mMax = MapBin.size();
for (m = 0; m < mMax; m++) {
if (MapBin.get(m).getRegionId() == 0) {
ct++;
}
}
return ct;
} //获得空白地块
private List<Integer> getIdsForBlankRegion() {
int m;
int mMax = MapBin.size();
List<Integer> rs = new ArrayList<Integer>();
for (m = 0; m < mMax; m++) {
if (MapBin.get(m).getRegionId() == 0) {
rs.add(m);
}
}
return rs;
} //将id转为coord(x,y,id,regionId)
private Coord converCoord(int id) {
int y = (int) id / mapWidth;
int x = id - y * mapWidth;
return new Coord(x, y, id, MapBin.get(id).getRegionId());
} //将id转为coord
private List<Coord> converCoords(List<Integer> ids) {
int m;
int mMax = ids.size();
List<Coord> rsIds = new ArrayList<Coord>();
for (m = 0; m < mMax; m++) {
int s=m;
rsIds.add(converCoord(ids.get(s)));
}
return rsIds;
} //获得最近的地块
public int getShortAroundId(List<Integer> ids, int regionId) {
// Gdx.app.log("求最近的地块,regionId", regionId+"");
List<Coord> coords = converCoords(ids);
int i, j, iMax = coords.size(), tempId = -1;double jl,jl2;
Coord coord = converCoord(regionId);
for (i = 0; i < iMax; i++) {
for (j = 0; j < iMax; j++) {
jl=Math.pow((coords.get(i).getX() - coord.getX()),2) + Math.pow((coords.get(i).getY() - coord.getY()),2);
jl2=(Math.pow((coords.get(j).getX() - coord.getX()),2) + Math.pow((coords.get(j).getY() - coord.getY()),2));
//Gdx.app.log("求最近的地块,交换前", "i:"+coords.get(i).getId()+" 距离系数:"+jl);
// Gdx.app.log("求最近的地块,交换前", "j:"+coords.get(i).getId()+" 距离系数:"+jl2); if (jl<jl2) { // 交换两数的位置
//Gdx.app.log("求最近的地块,交换前", "i:"+coords.get(i).getId()+" j:"+coords.get(j).getId());
ComUtil.swap(coords, i, j);
//Gdx.app.log("求最近的地块,交换后", "i:"+coords.get(i).getId()+" j:"+coords.get(j).getId());
}
}
}
//Gdx.app.log("求最近的地块,最前", coords.get(0).getId()+"");
//Gdx.app.log("求最近的地块,最后", coords.get(coords.size()-1).getId()+"");
Random rand = new Random();
{//去除非同一势力 TODO
for (i = coords.size() - 1; i >= 0; i--) {
if(!ifComLegion(coords.get(i).getId(),coord.getId())) {
coords.remove(i);
}
}
} if (coords.size() > 2) {
tempId = coords.get(rand.nextInt(2)).getId();
} else if (coords.size() == 0) {
return -1;
} else {
tempId = coords.get(0).getId();
}
// 1.8的写法 取出List中的对象的属性值
//List<Integer> xs = coords.stream().map(Coord::getX).collect(Collectors.toList());
//List<Integer> ys = coords.stream().map(Coord::getY).collect(Collectors.toList()); List<Integer> xs=new ArrayList<Integer>();
List<Integer> ys=new ArrayList<Integer>(); for(Coord cd:coords){
xs.add(cd.getX());
ys.add(cd.getY());
} int maxX=Collections.max(xs);
int minX=Collections.min(xs);
int maxY=Collections.max(ys);
int minY=Collections.min(ys); if((maxX-minX)>3||(maxY-minY)>3) {
if((maxY-minY)!=0&&(maxX-minX)!=0&&((maxX-minX)/(maxY-minY)>2||(maxY-minY)/(maxX-minX)>2)||getRegionCountByRegionId(regionId)<((maxX-minX)*(maxY-minY)/3)) {
return -1;
}
}
/*if(getRegionCountByRegionId(regionId)>(10 + Math.random() * 5)) {
return -1;
}*/
return tempId;
} //获取各region的数量记录为map
private Map getRegionCountMap() {
Map rsMap = new HashMap();
for (MapBin mapBin : MapBin) {
if (!rsMap.containsKey(mapBin.getRegionId())) {
rsMap.put(mapBin.getRegionId(), 1);
} else {
rsMap.put(mapBin.getRegionId(), Integer.parseInt(rsMap.get(mapBin.getRegionId()).toString()) + 1);
}
}
return rsMap;
} //获取通过region获取region的数量
private int getRegionCountByRegionId(int regionId) {
int c=0;
for (MapBin mapBin : MapBin) {
if (mapBin.getRegionId()==regionId) {
c++;
}
}
return c;
} //通过region获取ids
public List<Integer> getIdsByRegionId(int regionId){
List<Integer> rs= new ArrayList<Integer>();
int c=0;
for (MapBin mapBin : MapBin) {
if (mapBin.getRegionId()==regionId) {
rs.add(c);
}
c++;
}
return rs;
}
//获取所有region
public List<Integer> getAllRegionIds(){
List<Integer> rs = new ArrayList<Integer>();
for (MapBin mapBin : MapBin) {
if (mapBin.getRegionId()!=0&&!rs.contains(mapBin.getRegionId())) {
rs.add(mapBin.getRegionId());
}
}
return rs;
} //合并孤岛类地块
private void mergeIslandGridByRegion() {
int i,iMax=MapBin.size(),rsI=0;
String rz;
List<Integer> rs;
int tempRegionId;
for (i=0;i<iMax;i++) {
//获得结果值
rs= getAroundIdById(i,9);
Collections.sort(rs);
rsI=0;
//判断是否是孤岛
for(Integer id:rs) {
if(MapBin.get(id).getRegionId()!=MapBin.get(i).getRegionId()) {
rsI++;
}
}
if(rsI>4) {
rz= ComUtil.getListMostRepeatData(rs);
//updateRegionIds(MapBin.get(Integer.parseInt(rz)).getRegionId(),i);
MapBin.get(i).setRegionId(MapBin.get(Integer.parseInt(rz)).getRegionId());
} }
} public static void main(String[] args) {
int i; for (i = 0; i < 30; i++) {
System.out.println("sj:" + (int) (10 + Math.random() * 15));
} } //检查所属的区块是不是合法的region,如果不是给周围的地块 TODO测试
private void checkRegion() {
int i,iMax=MapBin.size();String rsI;
List<Integer> rs;
for (i=0;i<iMax;i++) {
//获得结果值
rs= getAroundIdById(i,9);
Collections.sort(rs);
if(MapBin.get(MapBin.get(i).getRegionId()).getRegionId()!=MapBin.get(i).getRegionId()) {
rsI=ComUtil.getListMostRepeatData(rs);
if(rsI!=null&&rsI!="") {
//Gdx.app.log("所属区域region不合法:", "i:"+i);
updateRegionIds(MapBin.get(Integer.parseInt(rsI)).getRegionId(),MapBin.get(i).getRegionId());
}
}
}
} //打印空白的非海洋地块(包括港口)
private void logBlankGrid() {
int i=0;
for (MapBin mapBin : MapBin) {
if (mapBin.getRegionId()==0&&(mapBin.getBackTile()!=1&&mapBin.getBackTile()!=2)) {
Gdx.app.log("空白地块", i+"");
}
i++;
}
} //验证势力是否一致 r源id i目标id
private boolean ifComLegion(int r,int i) {
int sourceLegion=MapBin.get(r).getFacility();
int regionLegion=MapBin.get(i).getFacility();
if(sourceLegion==regionLegion) {
return true;
}else if(sourceLegion==255) {
return true;
}else {
return false;
} } //将海港城市归为最近的陆地城市
private void updHabourRegionForE() {
List<Integer> habour= getRegions(5);
List<Integer> rsIds,tempIds;int id;
Random rand = new Random();
for(Integer r:habour) {
rsIds = getAroundIdById(r, 4);
if (rsIds.size() == 0) {
Gdx.app.log("警告", r+":海港周围为空");
}else {
id= rsIds.get(rand.nextInt(rsIds.size()));
if(id!=-1&&MapBin.get(r).getRegionId()==r) {
MapBin.get(r).setRegionId(MapBin.get(id).getRegionId());
}
}
}
} public void updHabourForE() {
updHabourRegionForE();
logBlankGrid();
} //替换当前的地区的所有相同区块为一个id的所在区块
public void replaceRegionIdById(int id){
int regionId=MapBin.get(id).getRegionId();
for (MapBin mapBin : MapBin) {
if(mapBin.getRegionId()==regionId){
mapBin.setRegionId(id);
}
}
} //替换当前空白的地块以及与其相邻的地块的region
public void replaceRegionIdForFFArea(int id,int regionId){
//替换不穿海,只替换该区域的
List<Integer> rs= new ArrayList<Integer>();
List<Integer> tempI;
if(MapBin.get(id).getRegionId()!=0){
return;
}else{
List<Integer> tempIds=getAroundIdById(id, 6);
rs.addAll(tempIds);
int n=0;
do{
tempIds= (List<Integer>) ((ArrayList<Integer>) rs).clone();
for(int i:tempIds){
tempI=getAroundIdById(i, 6);
rs.removeAll(tempI);
rs.addAll(tempI);
}
n=n+1;
if(n>5){
break;
}
}while(ComUtil.ifListContainListByInteger(rs,tempIds)); for(int i:rs){
MapBin.get(i).setRegionId(regionId);
}
}
} //批量获取符合某种条件的地块,条件参看getAroundIdById
public List<Integer> getIdsByAround(int type){
List<Integer> rsGrid=new ArrayList<Integer>();
List<Integer> rs;
int id=0;
for (MapBin mapBin : MapBin) {
rs = getAroundIdById(id, type);
if(rs.size()>0){
rsGrid.add(id);
}
id=id+1;
}
return rsGrid;
} }

MapBinDAO

package com.zhfy.game.framework;

import java.awt.Graphics;
import java.awt.geom.Point2D;
import java.io.DataOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Pixmap.Blending;
import com.badlogic.gdx.graphics.Pixmap.Filter;
import com.badlogic.gdx.graphics.PixmapIO;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.utils.XmlReader;
import com.zhfy.game.config.ResConfig;
import com.zhfy.game.framework.tool.FileByte;
import com.zhfy.game.model.content.MapBin;
import com.zhfy.game.model.content.MapBinDAO;
import com.zhfy.game.model.content.def.DefMap;
import com.zhfy.game.model.content.def.DefPt;
import com.zhfy.game.model.content.def.DefTerrainimg;
import com.zhfy.game.model.framework.Coord;
import com.zhfy.game.model.framework.PixmapDAO;
import com.zhfy.game.model.framework.PixmapListDAO; public class GameMap {
// 六边形地图绘制 全部static方法 public static void main(String[] args) throws Exception { // saveBinByWirteByte(fb,"123");
// MapBinDAO fb=apBin("123");
// saveMapBin(fb,"123");
} // 写为bin数组
@SuppressWarnings("unused")
public static void saveMapBin(MapBinDAO binFile, String Path) {
try {
//FileOutputStream fs_out = new FileOutputStream(Path);//"D://test.bin"
FileByte out=new FileByte();
out.writeShort(binFile.mapVersion);//
out.writeInt(binFile.mapWidth);//
out.writeInt(binFile.mapHeight);//
for (int i = 0; i < binFile.getMapbin().size(); i++) {
out.writeByte(binFile.getMapbin().get(i).getBlockType());//
out.writeByte(binFile.getMapbin().get(i).getBackTile());//
out.writeByte(binFile.getMapbin().get(i).getBackIdx());//
out.writeByte(binFile.getMapbin().get(i).getBackRefX());//
out.writeByte(binFile.getMapbin().get(i).getBackRefY());//
out.writeByte(binFile.getMapbin().get(i).getForeTile());//
out.writeByte(binFile.getMapbin().get(i).getForeIdx());//
out.writeByte(binFile.getMapbin().get(i).getForeRefX());//
out.writeByte(binFile.getMapbin().get(i).getForeRefY());//
out.writeByte(binFile.getMapbin().get(i).getWaterPass());//
out.writeByte(binFile.getMapbin().get(i).getLandPass());//
out.writeInt(binFile.getMapbin().get(i).getRegionId());//
out.writeByte(binFile.getMapbin().get(i).getClimateId());//
out.writeByte(binFile.getMapbin().get(i).getBuildId());//
out.writeByte(binFile.getMapbin().get(i).getBuildLv());//
out.writeByte(binFile.getMapbin().get(i).getFacility());//
out.writeShort(binFile.getMapbin().get(i).getAreaId());//
}
FileHandle file = Gdx.files.local(Path);
file.writeBytes(out.getByte(), false);
//out.writeInt(i);//8位 2147483647
//out.writeShort(i2);//4位 32769
//out.writeByte(i3);//2位 127 } catch (FileNotFoundException fe) {
System.err.println(fe);
} catch (IOException ioe) {
System.err.println(ioe);
}
System.out.println("Ok");
} public static void saveMapBin2(MapBinDAO binFile, String Path) {
try {
FileOutputStream fs_out = new FileOutputStream(Path);//"D://test.bin"
DataOutputStream out = new DataOutputStream(fs_out);
out.writeShort(binFile.mapVersion);//
out.writeInt(binFile.mapWidth);//
out.writeInt(binFile.mapHeight);//
for (int i = 0; i < binFile.getMapbin().size(); i++) {
out.writeByte(binFile.getMapbin().get(i).getBlockType());//
out.writeByte(binFile.getMapbin().get(i).getBackTile());//
out.writeByte(binFile.getMapbin().get(i).getBackIdx());//
out.writeByte(binFile.getMapbin().get(i).getBackRefX());//
out.writeByte(binFile.getMapbin().get(i).getBackRefY());//
out.writeByte(binFile.getMapbin().get(i).getForeTile());//
out.writeByte(binFile.getMapbin().get(i).getForeIdx());//
out.writeByte(binFile.getMapbin().get(i).getForeRefX());//
out.writeByte(binFile.getMapbin().get(i).getForeRefY());//
out.writeByte(binFile.getMapbin().get(i).getWaterPass());//
out.writeByte(binFile.getMapbin().get(i).getLandPass());//
out.writeInt(binFile.getMapbin().get(i).getRegionId());//
out.writeByte(binFile.getMapbin().get(i).getClimateId());//
}
out.close(); //out.writeInt(i);//8位 2147483647
//out.writeShort(i2);//4位 32769
//out.writeByte(i3);//2位 127
// out.close(); } catch (FileNotFoundException fe) {
System.err.println(fe);
} catch (IOException ioe) {
System.err.println(ioe);
}
System.out.println("Ok");
}/* */ // 循环遍历 在地块图上 是海洋和海岸的位置打窟窿,海岸使用id为2的抹除相应地形
public static Pixmap getLandByDAO(MapBinDAO mapBinDAO, Pixmap pixmap,PixmapListDAO pixmapLists, int mapx, int mapy,int mapw,int maph) { int x = 0;
int y = 0;
//int x_px_circle = 0;
//int y_px_circle = 0;
int x_px = 0;
int y_px = 0;
PixmapDAO shadowPixmap;
Pixmap shadowTargetPixmap = new Pixmap(46, 40, Pixmap.Format.RGBA8888);;
String shadowName; HashMap<String, String> terrainimgMap = new HashMap<String, String>(); //六边形四个角的坐标
int hexgaon_x1=0;//左上,左下
int hexgaon_x2=0;//右上,右下
int hexgaon_x3=0;//左中
int hexgaon_x4=0;//右中
int hexgaon_y1=0;//上
int hexgaon_y2=0;//中
int hexgaon_y3=0;//下 //TODO 随后增加对地图属性的解析 int beginIndex=0;
int EndIndex=mapw*maph; int xC;
int yC; Blending blend = pixmap.getBlending(); pixmap.setBlending(Blending.None); for (int id = beginIndex; id < EndIndex; id++) {
x = (id % mapBinDAO.getMapWidth()) + 1;
y = (id / mapBinDAO.getMapWidth()) + 1;
//x_px_circle = (int) (((x - 1) * ResConfig.Map.GRID_WIDTH) * Config.Map.MAP_SCALE + Config.Map.HEXAGON_WIDTH * Config.Map.MAP_SCALE / 2);
//y_px_circle = (int) ((((x & 1) == 1 ? (y - 1) * ResConfig.Map.HEXAGON_HEIGHT : (y - 1) * ResConfig.Map.HEXAGON_HEIGHT + ResConfig.Map.HEXAGON_HEIGHT_REF)) * Config.Map.MAP_SCALE + Config.Map.HEXAGON_HEIGHT * Config.Map.MAP_SCALE / 2); x_px = (int) ((x - 1) * ResConfig.Map.GRID_WIDTH* ResConfig.Map.MAP_SCALE);
y_px = (int) (((x & 1) == 1 ? (y - 1) * ResConfig.Map.HEXAGON_HEIGHT : (y - 1) * ResConfig.Map.HEXAGON_HEIGHT + ResConfig.Map.HEXAGON_HEIGHT_REF)* ResConfig.Map.MAP_SCALE); if (mapBinDAO.getMapbin().get(id).getBackTile() == 1 ) { // 这里填充圆,或四个三角形组合的六边形
// pixmap.fillCircle(x_px_circle, y_px_circle, (int) ((Config.Map.HEXAGON_WIDTH+12) * Config.Map.MAP_SCALE / 2));
//六个点
hexgaon_x1=(int) (((x - 1) * ResConfig.Map.GRID_WIDTH) * ResConfig.Map.MAP_SCALE +35*ResConfig.Map.MAP_SCALE);//左上,左下 原来是36
hexgaon_x2=(int) (((x - 1) * ResConfig.Map.GRID_WIDTH) * ResConfig.Map.MAP_SCALE +113*ResConfig.Map.MAP_SCALE);//右上,右下 ResConfig.Map.GRID_WIDTH
hexgaon_x3=(int) (((x - 1) * ResConfig.Map.GRID_WIDTH) * ResConfig.Map.MAP_SCALE-1);//左中
hexgaon_x4=(int) (((x - 1) * ResConfig.Map.GRID_WIDTH) * ResConfig.Map.MAP_SCALE + ResConfig.Map.HEXAGON_WIDTH * ResConfig.Map.MAP_SCALE );//右中
hexgaon_y1=(int) ((((x & 1) == 1 ? (y - 1) * ResConfig.Map.HEXAGON_HEIGHT : (y - 1) * ResConfig.Map.HEXAGON_HEIGHT + ResConfig.Map.HEXAGON_HEIGHT_REF)) * ResConfig.Map.MAP_SCALE -1);//上
hexgaon_y2=(int) ((((x & 1) == 1 ? (y - 1) * ResConfig.Map.HEXAGON_HEIGHT : (y - 1) * ResConfig.Map.HEXAGON_HEIGHT + ResConfig.Map.HEXAGON_HEIGHT_REF)) * ResConfig.Map.MAP_SCALE + ResConfig.Map.HEXAGON_HEIGHT * ResConfig.Map.MAP_SCALE / 2);//中
hexgaon_y3=(int) ((((x & 1) == 1 ? (y - 1) * ResConfig.Map.HEXAGON_HEIGHT : (y - 1) * ResConfig.Map.HEXAGON_HEIGHT + ResConfig.Map.HEXAGON_HEIGHT_REF)) * ResConfig.Map.MAP_SCALE + ResConfig.Map.HEXAGON_HEIGHT * ResConfig.Map.MAP_SCALE );//下 pixmap.fillTriangle(hexgaon_x3, hexgaon_y2, hexgaon_x1, hexgaon_y1, hexgaon_x2, hexgaon_y1);
pixmap.fillTriangle(hexgaon_x3, hexgaon_y2, hexgaon_x2, hexgaon_y1, hexgaon_x4, hexgaon_y2);
pixmap.fillTriangle(hexgaon_x3, hexgaon_y2, hexgaon_x2, hexgaon_y3, hexgaon_x4, hexgaon_y2);
pixmap.fillTriangle(hexgaon_x3, hexgaon_y2, hexgaon_x1, hexgaon_y3, hexgaon_x2, hexgaon_y3); // Gdx.app.log("切除陆地", "id:" + id +" x:"+x+" y:"+ y+"
// x_px:"+x_px+" y_px:"+ y_px);
}else if(mapBinDAO.getMapbin().get(id).getBackTile() == 2) {
//pixmap.setBlending(blend);
shadowName=terrainimgMap.get(mapBinDAO.getMapbin().get(id).getBackTile() + "_" + mapBinDAO.getMapbin().get(id).getBackIdx());
if(shadowName!=null) {
shadowPixmap = pixmapLists.getPixmapByName(shadowName);
/*
pixmap.drawPixmap(shadowPixmap.getPixmap(), 0, 0, (int)(shadowPixmap.getPixmap().getWidth()), (int)(shadowPixmap.getPixmap().getHeight()), x_px,
y_px, (int) (shadowPixmap.getPixmap().getWidth() * Config.Map.MAP_SCALE), (int) (shadowPixmap.getPixmap().getHeight() * Config.Map.MAP_SCALE));
*/ //Gdx.app.log("切除海岸边缘","shadowName:"+shadowName);
/*
TODO
* 暂时未完成
*根据阴影的形状来切割掉像素,实现边界对齐
* */
//pixmap.setBlending(Blending.None);
shadowTargetPixmap.drawPixmap(shadowPixmap.getPixmap(), 0, 0, (int)(shadowPixmap.getPixmap().getWidth()), (int)(shadowPixmap.getPixmap().getHeight()), 0,
0, (int) (shadowPixmap.getPixmap().getWidth() * ResConfig.Map.MAP_SCALE), (int) (shadowPixmap.getPixmap().getHeight() * ResConfig.Map.MAP_SCALE)); for ( xC=0; xC<(int) (shadowTargetPixmap.getWidth() ); xC++) {
for ( yC=0; yC<(int) (shadowTargetPixmap.getHeight() ); yC++) {
//int color = pixmap.getPixel(xC, yC);
/*if(shadowPixmap.getPixmap().getPixel(xC, yC)>256&&shadowPixmap.getPixmap().getPixel(xC, yC)<66000){
pixmap.drawPixel(xC+x_px, yC+y_px, 0);
}*/
/*if(shadowPixmap.getName().equals("coast_58")) {
//Gdx.app.log("切除海岸边缘","color:"+shadowTargetPixmap.getPixel(xC, yC)+" x:"+xC+" y:"+yC);
Gdx.app.log("",""+shadowTargetPixmap.getPixel(xC, yC));
}*/ if(shadowTargetPixmap.getPixel(xC, yC)==65535||shadowTargetPixmap.getPixel(xC, yC)==65022){
pixmap.drawPixel(xC+x_px, yC+y_px,0); Gdx.app.log("切除海岸边缘","color:"+shadowPixmap.getPixmap().getPixel(xC, yC)+" x:"+xC+" y:"+yC); }/*else {
Gdx.app.log("切除海岸边缘","color:"+pixmap.getPixel(xC, yC)+" x:"+xC+" y:"+yC);
}*/ }
} } }
}
// 把blending设置回来
pixmap.setBlending(blend);
return pixmap;
} // 通过dao绘制图片
// pixmap要绘制的目标图片
// pixmapLists使用图片
// defTerrainimgs图片说明文件
public static Pixmap getPixmapByDao(MapBinDAO mapBinDAO, Pixmap pixmap, PixmapListDAO pixmapLists, List<DefTerrainimg> defTerrainimgs, int beginIndex, int EndIndex,float scale) {
// 解析dao
if (mapBinDAO.getMapbin() != null) { int x = 0;
int y = 0;
int x_px = 0;
int y_px = 0;
HashMap<String, String> terrainimgMap = new HashMap<String, String>();
// 将要取得的图片的id和名字放入位置
for (DefTerrainimg defTerrainimg : defTerrainimgs) {
terrainimgMap.put(defTerrainimg.getId() + "_" + defTerrainimg.getIdx(), defTerrainimg.getImage());
}
String imgBack;
String imgFore;
PixmapDAO backPixmap;
PixmapDAO forePixmap;
int refSize=0;//对水面大小补位,否则可能有空缺
//Pixmap.Filter filter;
// 单线程绘制图片
for (int id = beginIndex; id < EndIndex; id++) { x = (id % mapBinDAO.getMapWidth()) + 1;
y = (id / mapBinDAO.getMapWidth()) + 1;
// x_px=(int) ((x-1)*139.5);
// y_px=(int) ((x&1) == 1?(y-1)*161:(y-1)*161+80.5);
x_px = (int) ((x - 1) * ResConfig.Map.GRID_WIDTH);
y_px = (int) (((x & 1) == 1 ? (y - 1) * ResConfig.Map.HEXAGON_HEIGHT : (y - 1) * ResConfig.Map.HEXAGON_HEIGHT + ResConfig.Map.HEXAGON_HEIGHT_REF)); //Gdx.app.log("地图开始构建", "mapId:" + id + " x:" + x + " y:" + y);
if (mapBinDAO.getMapbin().get(id).getBackTile() != 0 /*&& mapBinDAO.getMapbin().get(id).getBackTile() != 1*/) {
if(mapBinDAO.getMapbin().get(id).getBackTile() == 1&&scale<0.5f){
refSize=1;
}else{
refSize=0;
}
// 获取该位置要画的地形 z
imgBack = terrainimgMap.get(mapBinDAO.getMapbin().get(id).getBackTile() + "_" + mapBinDAO.getMapbin().get(id).getBackIdx());
if (imgBack == null) {
//Gdx.app.log("地图层1未找到", mapBinDAO.getMapbin().get(id).getBackTile() + "_" + mapBinDAO.getMapbin().get(id).getBackIdx());
imgBack = terrainimgMap.get(mapBinDAO.getMapbin().get(id).getBackTile() + "_"+ComUtil.getRandom(1,4));
}
// 获取图像并绘制到上面
backPixmap = pixmapLists.getPixmapByName(imgBack);
if (backPixmap != null) {
pixmap.drawPixmap
(backPixmap.getPixmap(),
0,
0,
backPixmap.getPixmap().getWidth(),
backPixmap.getPixmap().getHeight(),
(int) (scale*((x_px + mapBinDAO.getMapbin().get(id).getBackRefX()+backPixmap.getRefx()) * ResConfig.Map.MAP_SCALE))-refSize*2,
(int) (scale*((y_px + mapBinDAO.getMapbin().get(id).getBackRefY()+backPixmap.getRefy()) * ResConfig.Map.MAP_SCALE))-refSize*2,
(int) (scale*backPixmap.getPixmap().getWidth() * ResConfig.Map.MAP_SCALE)+refSize*3,
(int) (scale*backPixmap.getPixmap().getHeight() * ResConfig.Map.MAP_SCALE)+refSize*3); //Gdx.app.log("地图层1构建成功", " backid:" + mapBinDAO.getMapbin().get(id).getBackTile() + "_" + mapBinDAO.getMapbin().get(id).getBackIdx() + "img:" + imgBack);
} /*
* else { Gdx.app.log("地图层1构建失败", " backid:" +
* mapBinDAO.getMapbin().get(id).getBackTile() + "_" +
* mapBinDAO.getMapbin().get(id).getBackIdx()); }
*/
} /*
* else { Gdx.app.log("地图层1忽略构建", ""); }
*/
// 忽略底图和海洋
if (mapBinDAO.getMapbin().get(id).getForeTile() != 0 && mapBinDAO.getMapbin().get(id).getForeTile() != 1) {
imgFore = terrainimgMap.get(mapBinDAO.getMapbin().get(id).getForeTile() + "_" + mapBinDAO.getMapbin().get(id).getForeIdx());
if (imgFore == null) {
Gdx.app.log("地图层2未找到",mapBinDAO.getMapbin().get(id).getForeTile() + "_" + mapBinDAO.getMapbin().get(id).getForeIdx());
imgFore = terrainimgMap.get(mapBinDAO.getMapbin().get(id).getForeTile() + "_1");
}
forePixmap = pixmapLists.getPixmapByName(imgFore);
if (forePixmap != null) {
pixmap.drawPixmap
(forePixmap.getPixmap(),
0,
0,
forePixmap.getPixmap().getWidth(),
forePixmap.getPixmap().getHeight(),
(int) (scale*((x_px + mapBinDAO.getMapbin().get(id).getForeRefX()+forePixmap.getRefx()) * ResConfig.Map.MAP_SCALE)),
(int) (scale*((y_px + mapBinDAO.getMapbin().get(id).getForeRefY()+forePixmap.getRefy()) * ResConfig.Map.MAP_SCALE)),
(int) (scale*forePixmap.getPixmap().getWidth() * ResConfig.Map.MAP_SCALE),
(int) (scale*forePixmap.getPixmap().getHeight() * ResConfig.Map.MAP_SCALE));
} /*
* else { Gdx.app.log("地图层2构建成功", " backid:" +
* mapBinDAO.getMapbin().get(id).getForeTile() + "_" +
* mapBinDAO.getMapbin().get(id).getForeIdx()); }
*/
} /*
* else { Gdx.app.log("地图层2", "忽略构建"); }
*/
}
Gdx.app.log("地图构建", "完成");
// PixmapIO.writePNG(Gdx.files.external("texture_world.png"), pixmap); return pixmap;
} else {
Gdx.app.log("地图构建", "失败");
return null;
}
} //绘制颜色区块图
public static Pixmap getPixmapByDaoForColor(MapBinDAO mapBinDAO, Pixmap pixmap,float scale,List<Integer> coastGrid) { // 解析dao
if (mapBinDAO.getMapbin() != null) { int x = 0;
int y = 0;
int x_px_circle = 0;
int y_px_circle = 0;
int hexgaon_x1;
int hexgaon_x2;
int hexgaon_x3;
int hexgaon_x4;
int hexgaon_y1;
int hexgaon_y2;
int hexgaon_y3; //沿海地块用圆形来填充
//List<Integer> coastGrid=mapBinDAO.getIdsByAround(12); int color;
int nextRegionId=0;
//先绘制海岸 //沿海地块用圆形来填充
pixmap.setBlending(Blending.None);
for(int id:coastGrid) {
if(mapBinDAO.getMapbin().get(id).getBlockType()!=1&&mapBinDAO.getMapbin().get(id).getRegionId()!=0){
x = (id % mapBinDAO.getMapWidth()) + 1;
y = (id / mapBinDAO.getMapWidth()) + 1;
x_px_circle = (int) (scale*(((x - 1) * ResConfig.Map.GRID_WIDTH) * ResConfig.Map.MAP_SCALE + ResConfig.Map.HEXAGON_WIDTH * ResConfig.Map.MAP_SCALE / 2));
y_px_circle = (int) (scale*((((x & 1) == 1 ? (y - 1) * ResConfig.Map.HEXAGON_HEIGHT : (y - 1) * ResConfig.Map.HEXAGON_HEIGHT + ResConfig.Map.HEXAGON_HEIGHT_REF)) * ResConfig.Map.MAP_SCALE + ResConfig.Map.HEXAGON_HEIGHT * ResConfig.Map.MAP_SCALE / 2)); //Gdx.app.log("海陆绘制圆", "" + id);
//沿海地块用圆形来填充
if (mapBinDAO.getMapbin().get(id).getRegionId() != nextRegionId) {
color = GameUtil.getColorByNum(mapBinDAO.getMapbin().get(id).getRegionId());
//获取颜色
pixmap.setColor(color);
nextRegionId = mapBinDAO.getMapbin().get(id).getRegionId();
//Gdx.app.log("setColor","id:"+id+" color:"+color);
}
pixmap.fillCircle(x_px_circle, y_px_circle, (int) ((ResConfig.Map.HEXAGON_WIDTH + 13) * ResConfig.Map.MAP_SCALE / 2*scale));
}
} // 单线程绘制所有图片
for (int id = 0; id < mapBinDAO.getMapbin().size(); id++) { x = (id % mapBinDAO.getMapWidth()) + 1;
y = (id / mapBinDAO.getMapWidth()) + 1; hexgaon_x1=(int) (scale*(((x - 1) * ResConfig.Map.GRID_WIDTH) * ResConfig.Map.MAP_SCALE +35*ResConfig.Map.MAP_SCALE));//左上,左下 原来是36
hexgaon_x2=(int) (scale*(((x - 1) * ResConfig.Map.GRID_WIDTH) * ResConfig.Map.MAP_SCALE +113*ResConfig.Map.MAP_SCALE));//右上,右下 ResConfig.Map.GRID_WIDTH
hexgaon_x3=(int) (scale*(((x - 1) * ResConfig.Map.GRID_WIDTH) * ResConfig.Map.MAP_SCALE-1));//左中
hexgaon_x4=(int) (scale*(((x - 1) * ResConfig.Map.GRID_WIDTH) * ResConfig.Map.MAP_SCALE + ResConfig.Map.HEXAGON_WIDTH * ResConfig.Map.MAP_SCALE ));//右中
hexgaon_y1=(int) (scale*((((x & 1) == 1 ? (y - 1) * ResConfig.Map.HEXAGON_HEIGHT : (y - 1) * ResConfig.Map.HEXAGON_HEIGHT + ResConfig.Map.HEXAGON_HEIGHT_REF)) * ResConfig.Map.MAP_SCALE -1));//上
hexgaon_y2=(int) (scale*((((x & 1) == 1 ? (y - 1) * ResConfig.Map.HEXAGON_HEIGHT : (y - 1) * ResConfig.Map.HEXAGON_HEIGHT + ResConfig.Map.HEXAGON_HEIGHT_REF)) * ResConfig.Map.MAP_SCALE + ResConfig.Map.HEXAGON_HEIGHT * ResConfig.Map.MAP_SCALE / 2));//中
hexgaon_y3=(int) (scale*((((x & 1) == 1 ? (y - 1) * ResConfig.Map.HEXAGON_HEIGHT : (y - 1) * ResConfig.Map.HEXAGON_HEIGHT + ResConfig.Map.HEXAGON_HEIGHT_REF)) * ResConfig.Map.MAP_SCALE + ResConfig.Map.HEXAGON_HEIGHT * ResConfig.Map.MAP_SCALE ));//下 if (mapBinDAO.getMapbin().get(id).getRegionId() != nextRegionId) {
color = GameUtil.getColorByNum(mapBinDAO.getMapbin().get(id).getRegionId());
//获取颜色
pixmap.setColor(color);
nextRegionId = mapBinDAO.getMapbin().get(id).getRegionId();
//Gdx.app.log("setColor","id:"+id+" color:"+color);
} //Gdx.app.log("地图开始构建", "mapId:" + id + " x:" + x + " y:" + y);
if (mapBinDAO.getMapbin().get(id).getBlockType()!=1&&mapBinDAO.getMapbin().get(id).getRegionId()!=0) { pixmap.fillTriangle(hexgaon_x3, hexgaon_y2, hexgaon_x1, hexgaon_y1, hexgaon_x2, hexgaon_y1);
pixmap.fillTriangle(hexgaon_x3, hexgaon_y2, hexgaon_x2, hexgaon_y1, hexgaon_x4, hexgaon_y2);
pixmap.fillTriangle(hexgaon_x3, hexgaon_y2, hexgaon_x2, hexgaon_y3, hexgaon_x4, hexgaon_y2);
pixmap.fillTriangle(hexgaon_x3, hexgaon_y2, hexgaon_x1, hexgaon_y3, hexgaon_x2, hexgaon_y3);
} } Gdx.app.log("色图构建", "完成");
// PixmapIO.writePNG(Gdx.files.external("texture_world.png"), pixmap); return pixmap;
} else {
Gdx.app.log("色图构建", "失败");
return null;
}
}
//修改region的颜色
public static Pixmap updateColorByRegion(MapBinDAO mapBinDAO, Pixmap pixmap,float scale,int regionId,int color,List<Integer> coastGrid){ //待绘制地块
List<Integer> ids=mapBinDAO.getIdsByRegionId(regionId);
if (mapBinDAO.getMapbin() != null&&ids.size()>0) {
pixmap.setColor(color);
int x = 0;
int y = 0;
int x_px_circle = 0;
int y_px_circle = 0;
int hexgaon_x1;
int hexgaon_x2;
int hexgaon_x3;
int hexgaon_x4;
int hexgaon_y1;
int hexgaon_y2;
int hexgaon_y3; //先绘制海岸 //沿海地块用圆形来填充
pixmap.setBlending(Blending.None);
for(int id:ids) {
if(mapBinDAO.getMapbin().get(id).getBlockType()!=1&&mapBinDAO.getMapbin().get(id).getRegionId()!=0 &&coastGrid.contains(id)){
x = (id % mapBinDAO.getMapWidth()) + 1;
y = (id / mapBinDAO.getMapWidth()) + 1;
x_px_circle = (int) (scale*(((x - 1) * ResConfig.Map.GRID_WIDTH) * ResConfig.Map.MAP_SCALE + ResConfig.Map.HEXAGON_WIDTH * ResConfig.Map.MAP_SCALE / 2));
y_px_circle = (int) (scale*((((x & 1) == 1 ? (y - 1) * ResConfig.Map.HEXAGON_HEIGHT : (y - 1) * ResConfig.Map.HEXAGON_HEIGHT + ResConfig.Map.HEXAGON_HEIGHT_REF)) * ResConfig.Map.MAP_SCALE + ResConfig.Map.HEXAGON_HEIGHT * ResConfig.Map.MAP_SCALE / 2));
//Gdx.app.log("海陆绘制圆", "" + id);
//沿海地块用圆形来填充
pixmap.fillCircle(x_px_circle, y_px_circle, (int) ((ResConfig.Map.HEXAGON_WIDTH + 13) * ResConfig.Map.MAP_SCALE / 2*scale));
}
}
// 单线程绘制所有图片
for (int id:ids) { x = (id % mapBinDAO.getMapWidth()) + 1;
y = (id / mapBinDAO.getMapWidth()) + 1; hexgaon_x1=(int) (scale*(((x - 1) * ResConfig.Map.GRID_WIDTH) * ResConfig.Map.MAP_SCALE +35*ResConfig.Map.MAP_SCALE));//左上,左下 原来是36
hexgaon_x2=(int) (scale*(((x - 1) * ResConfig.Map.GRID_WIDTH) * ResConfig.Map.MAP_SCALE +113*ResConfig.Map.MAP_SCALE));//右上,右下 ResConfig.Map.GRID_WIDTH
hexgaon_x3=(int) (scale*(((x - 1) * ResConfig.Map.GRID_WIDTH) * ResConfig.Map.MAP_SCALE-1));//左中
hexgaon_x4=(int) (scale*(((x - 1) * ResConfig.Map.GRID_WIDTH) * ResConfig.Map.MAP_SCALE + ResConfig.Map.HEXAGON_WIDTH * ResConfig.Map.MAP_SCALE ));//右中
hexgaon_y1=(int) (scale*((((x & 1) == 1 ? (y - 1) * ResConfig.Map.HEXAGON_HEIGHT : (y - 1) * ResConfig.Map.HEXAGON_HEIGHT + ResConfig.Map.HEXAGON_HEIGHT_REF)) * ResConfig.Map.MAP_SCALE -1));//上
hexgaon_y2=(int) (scale*((((x & 1) == 1 ? (y - 1) * ResConfig.Map.HEXAGON_HEIGHT : (y - 1) * ResConfig.Map.HEXAGON_HEIGHT + ResConfig.Map.HEXAGON_HEIGHT_REF)) * ResConfig.Map.MAP_SCALE + ResConfig.Map.HEXAGON_HEIGHT * ResConfig.Map.MAP_SCALE / 2));//中
hexgaon_y3=(int) (scale*((((x & 1) == 1 ? (y - 1) * ResConfig.Map.HEXAGON_HEIGHT : (y - 1) * ResConfig.Map.HEXAGON_HEIGHT + ResConfig.Map.HEXAGON_HEIGHT_REF)) * ResConfig.Map.MAP_SCALE + ResConfig.Map.HEXAGON_HEIGHT * ResConfig.Map.MAP_SCALE ));//下 //Gdx.app.log("地图开始构建", "mapId:" + id + " x:" + x + " y:" + y);
if (mapBinDAO.getMapbin().get(id).getBlockType()!=1&&mapBinDAO.getMapbin().get(id).getRegionId()!=0) { pixmap.fillTriangle(hexgaon_x3, hexgaon_y2, hexgaon_x1, hexgaon_y1, hexgaon_x2, hexgaon_y1);
pixmap.fillTriangle(hexgaon_x3, hexgaon_y2, hexgaon_x2, hexgaon_y1, hexgaon_x4, hexgaon_y2);
pixmap.fillTriangle(hexgaon_x3, hexgaon_y2, hexgaon_x2, hexgaon_y3, hexgaon_x4, hexgaon_y2);
pixmap.fillTriangle(hexgaon_x3, hexgaon_y2, hexgaon_x1, hexgaon_y3, hexgaon_x2, hexgaon_y3);
}
}
Gdx.app.log("色图构建", "完成"+" regionId:"+regionId+" color:"+color);
//PixmapIO.writePNG(Gdx.files.external("updateColor_"+regionId+".png"), pixmap);
return pixmap;
} else {
Gdx.app.log("色图构建", "失败");
return pixmap;
}
} // 绘制分区图片 512*512
// pixmap要绘制的目标图片
// pixmapLists使用图片
// defTerrainimgs图片说明文件
//beginX,beginY绘制的起始坐标
public static Pixmap getPixmapForArea(MapBinDAO mapBinDAO, Pixmap pixmap, PixmapListDAO pixmapLists, List<DefTerrainimg> defTerrainimgs, int beginX, int beginY) { int areaW=ResConfig.Map.PT_GRID_WIDTH;
int areaH=ResConfig.Map.PT_GRID_HEIGHT+1; beginX=beginX+1;
beginY=beginY+1; int mapRefX=0;
int mapRefXPx=0; //如果beginX不是1,则要从这个开始的再开始绘制
if(beginX>1) {
mapRefX=-1;
mapRefXPx=(int) ((beginX-1)*ResConfig.Atlas.PT_SIDE % (ResConfig.Map.GRID_WIDTH*ResConfig.Map.MAP_SCALE)/ResConfig.Map.MAP_SCALE);
}/**/ int areaSum=areaW*areaH;
int mapW=mapBinDAO.mapWidth; //区域偏移量 // 解析dao
if (mapBinDAO.getMapbin() != null) { int x = 0;
int y = 0;
int x_px = 0;
int y_px = 0;
HashMap<String, String> terrainimgMap = new HashMap<String, String>();
// 将要取得的图片的id和名字放入位置
for (DefTerrainimg defTerrainimg : defTerrainimgs) {
terrainimgMap.put(defTerrainimg.getId() + "_" + defTerrainimg.getIdx(), defTerrainimg.getImage());
}
String imgBack;
String imgFore;
PixmapDAO backPixmap;
PixmapDAO forePixmap;
int id; // 单线程绘制图片
for (int i = 0; i < areaSum; i++) { x = (i % areaW) + 1;
y = (i / areaW) + 1; //判断是否跨行
if(((beginX-1)*areaW+x-1)<mapW) {
id=((beginX-1)*areaW+x-1+mapRefX)+((beginY-1)*(areaH)+y-1)*mapW;//计算当前绘制的id
}else {
id=((beginX-1)*areaW+x-1+mapRefX)+((beginY-1)*(areaH)+y-1)*mapW-mapW;
} x_px = (int) ((x - 1) * ResConfig.Map.GRID_WIDTH)-mapRefXPx;
y_px = (int) (((id & 1) == 1 ? (y - 1) * ResConfig.Map.HEXAGON_HEIGHT+ ResConfig.Map.HEXAGON_HEIGHT_REF : (y - 1) * ResConfig.Map.HEXAGON_HEIGHT )); /*if(beginX==2&&beginY==1) {
Gdx.app.log("测试:", "id:"+id+" x:"+x+" y:"+y+" x_px:"+x_px+" y_px:"+y_px);
}*/ if(id>=mapBinDAO.getMapbin().size()) {
break;
}
if (mapBinDAO.getMapbin().get(id).getBackTile() != 0 /*&& mapBinDAO.getMapbin().get(id).getBackTile() != 1*/) { //Gdx.app.log("地图开始构建", "mapId:" + id + " x:" + x + " y:" + y);
// 获取该位置要画的地形 z
imgBack = terrainimgMap.get(mapBinDAO.getMapbin().get(id).getBackTile() + "_" + mapBinDAO.getMapbin().get(id).getBackIdx());
if (imgBack == null) {
//Gdx.app.log("地图层1未找到", mapBinDAO.getMapbin().get(id).getBackTile() + "_" + mapBinDAO.getMapbin().get(id).getBackIdx());
imgBack = terrainimgMap.get(mapBinDAO.getMapbin().get(id).getBackTile() + "_"+ComUtil.getRandom(1,4));
}
// 获取图像并绘制到上面
backPixmap = pixmapLists.getPixmapByName(imgBack);
if (backPixmap != null) { pixmap.drawPixmap(backPixmap.getPixmap(), 0, 0, backPixmap.getPixmap().getWidth(), backPixmap.getPixmap().getHeight(), (int) ((x_px + mapBinDAO.getMapbin().get(id).getBackRefX()) * ResConfig.Map.MAP_SCALE),
(int) ((y_px + mapBinDAO.getMapbin().get(id).getBackRefY()) * ResConfig.Map.MAP_SCALE), (int) (backPixmap.getPixmap().getWidth() * ResConfig.Map.MAP_SCALE), (int) (backPixmap.getPixmap().getHeight() * ResConfig.Map.MAP_SCALE));
}
}
// 忽略底图和海洋
if (mapBinDAO.getMapbin().get(id).getForeTile() != 0 && mapBinDAO.getMapbin().get(id).getForeTile() != 1) {
imgFore = terrainimgMap.get(mapBinDAO.getMapbin().get(id).getForeTile() + "_" + mapBinDAO.getMapbin().get(id).getForeIdx());
if (imgFore == null) {
Gdx.app.log("地图层2未找到",mapBinDAO.getMapbin().get(id).getForeTile() + "_" + mapBinDAO.getMapbin().get(id).getForeIdx());
imgFore = terrainimgMap.get(mapBinDAO.getMapbin().get(id).getForeTile() + "_1");
}
forePixmap = pixmapLists.getPixmapByName(imgFore);
if (forePixmap != null) {
pixmap.drawPixmap(forePixmap.getPixmap(), x_px + mapBinDAO.getMapbin().get(id).getForeRefX(), y_px + mapBinDAO.getMapbin().get(id).getForeRefY(), 0, 0, forePixmap.getPixmap().getWidth(), forePixmap.getPixmap().getHeight());
}
}
}
/*if(beginX==2&&beginY==1) {
PixmapIO.writePNG(Gdx.files.external("texture_"+beginY+"."+beginX+".png"), pixmap);
}*/
//PixmapIO.writePNG(Gdx.files.external("texture_"+beginY+"."+beginX+".png"), pixmap); Gdx.app.log("地图构建", "完成");
return pixmap;
} else {
Gdx.app.log("地图构建", "失败");
return null;
}
} // 读取地图 保存位置编号
public static MapBinDAO saveBin(byte[] bt) throws IOException { MapBinDAO fs = new MapBinDAO(null);
List<MapBin> mps = new ArrayList<MapBin>(); /*
* //TODO根据Index获取保存位置
*
* //定位bin位置 byte[] bt = GameUtils.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;
mp.setBuildId(Integer.parseInt(buf.substring(bufTag, bufTag+2),16));
bufTag=bufTag+2;
mp.setBuildLv(Integer.parseInt(buf.substring(bufTag, bufTag+2),16));
bufTag=bufTag+2;
mp.setFacility(Integer.parseInt(buf.substring(bufTag, bufTag+2),16));
bufTag=bufTag+2;
mp.setAreaId(Integer.parseInt(buf.substring(bufTag, bufTag+4),16));
bufTag=bufTag+4;
mps.add(mp);
}
fs.setMapbin(mps); return fs;
} public static int getWidth(int w) {
return (int) ((148 + ResConfig.Map.GRID_WIDTH * (w - 1)));
} public static int getHeight(int h) {
return (int) ((192 + ResConfig.Map.HEXAGON_HEIGHT * (h - 1)));
} // 六边形网格定位
// @param xPos 输入,所需查询的点的x坐标
// @param yPos 输入,所需查询的点的y坐标
// @param cell_x 输出,改点所在网格的x坐标
// @param cell_y 输出,改点所在网格的y坐标
public static Coord getHotCell(float xPos, float yPos,float zoom) { float GRID_WIDTH = ResConfig.Map.GRID_WIDTH;// (CELL_BORDER*1.5f)
float GRID_HEIGHT =ResConfig.Map.HEXAGON_HEIGHT;// (CELL_BORDER*0.8660254f)
float Grid_BORDER=GRID_WIDTH/1.5f; int cell_y;
int cell_x;
xPos=xPos/ResConfig.Map.MAP_SCALE;
yPos=yPos/ResConfig.Map.MAP_SCALE;
cell_x = (int) (xPos / GRID_WIDTH);
float x = xPos - cell_x * GRID_WIDTH; cell_y = (int) (yPos / GRID_HEIGHT);
float y = yPos - cell_y * GRID_HEIGHT; //if(! (Grid_BORDER-Math.abs((x-1/2*GRID_WIDTH))>Math.abs(y-1/2*GRID_HEIGHT)/Math.sqrt(3))) {
if(! (Math.abs(GRID_WIDTH/2-x)+Math.abs(GRID_HEIGHT/2-y)/Math.sqrt(3)<=Grid_BORDER*Math.sqrt(3))) {
//不在六边形内部
if(x>GRID_WIDTH/2) {
//在右边
cell_x++;
}
} /*if( cell_x % 2==1 && y<64) {
cell_y--;
}
if((cell_x&1)!=1&& y>64) {
cell_y=cell_y-1;
}
if(cell_y<0) {
cell_y=0;
}*/ Coord coord = new Coord(cell_x, cell_y);
return coord; } // 六边形网格定位
// @param xPos 输入,所需查询的点的x坐标
// @param yPos 输入,所需查询的点的y坐标,y必须转换,因为以左上开始计算
// @param cell_x 输出,改点所在网格的x坐标
// @param cell_y 输出,改点所在网格的y坐标
public static Coord getHotCell2(float xPos, float yPos) { float GRID_WIDTH =ResConfig.Map.GRID_WIDTH;
float GRID_HEIGHT =ResConfig.Map.HEXAGON_HEIGHT;// (CELL_BORDER*0.8660254f)
float GRID_W_B=(GRID_WIDTH/4);//宽-变成/2 即小长 int cell_y;
int cell_x;
float vertex_x_px;
float vertex_y_px;
xPos=xPos/ResConfig.Map.MAP_SCALE;//不知道为什么会出现偏移,所以通过这里来减少偏移
yPos=yPos/ResConfig.Map.MAP_SCALE;
cell_x = (int) (xPos / GRID_WIDTH);
float x = Math.abs(xPos - cell_x * GRID_WIDTH);
cell_y = (int) (yPos / GRID_HEIGHT);
float y = Math.abs(yPos - cell_y * GRID_HEIGHT); //先判断位置
if(x<GRID_W_B) {
if(y<GRID_HEIGHT/2) {
//在上
if(x/Math.tan(60)<y) {
cell_x++;
}
}else {
//在下
if(x/Math.tan(60)<(y-GRID_HEIGHT/2)) {
cell_x++;
}
}
//右边
}/**/ if( cell_x % 2==0 && y<64) {
cell_y--;
}else if(cell_x % 2!=0){
cell_y--;
}
/*if((cell_x&1)!=1&& y>64) {
cell_y=cell_y-1;
}
if(cell_y<0) {
cell_y=0;
}*/ vertex_x_px=cell_x*GRID_WIDTH*ResConfig.Map.MAP_SCALE;
vertex_y_px=cell_x%2!=0?(cell_y+1)*GRID_HEIGHT*ResConfig.Map.MAP_SCALE:(cell_y+0.5f)*GRID_HEIGHT*ResConfig.Map.MAP_SCALE;
Coord coord = new Coord(-cell_x, cell_y-1,vertex_x_px,vertex_y_px);
return coord;
} // TODO
// 横六边形地图类
class hexagonMap {
private int w;
private int h;
private float w_px;// 单个像素长度
private float h_px;// 单个像素宽度
private float sum_w_px;
private float sum_h_px;
} //画六边形网格
public static Pixmap drawHexagonGrid(Pixmap pixmap, MapBinDAO mapBinDAO)// 画网格
{
// 加载边框
int x = 0;
int y = 0;
int x_px = 0;
int y_px = 0;
// 将要取得的图片的id和名字放入位置
Pixmap gridPixmap = new Pixmap(Gdx.files.internal("pixmap/tiles/grid.png"));
// 绘制网格图片
for (int id = 0; id < mapBinDAO.getMapbin().size(); id++) {
x = (id % mapBinDAO.getMapWidth()) + 1;
y = (id / mapBinDAO.getMapWidth()) + 1;
// x_px=(int) ((x-1)*139.5);
// y_px=(int) ((x&1) == 1?(y-1)*161:(y-1)*161+80.5);
x_px = (int) ((x - 1) * ResConfig.Map.GRID_WIDTH);
y_px = (int) (((x & 1) == 1 ? (y - 1) * ResConfig.Map.HEXAGON_HEIGHT : (y - 1) * ResConfig.Map.HEXAGON_HEIGHT + ResConfig.Map.HEXAGON_HEIGHT_REF));
// Gdx.app.log("绘制地图网格", "mapId:" + id + " x:" + x + " y:" + y);
pixmap.drawPixmap(gridPixmap, 0, 0, gridPixmap.getWidth(), gridPixmap.getHeight(), (int) ((x_px + mapBinDAO.getMapbin().get(id).getBackRefX()) * ResConfig.Map.MAP_SCALE), (int) ((y_px + mapBinDAO.getMapbin().get(id).getBackRefY()) * ResConfig.Map.MAP_SCALE),
(int) (gridPixmap.getWidth() * ResConfig.Map.MAP_SCALE), (int) (gridPixmap.getHeight() * ResConfig.Map.MAP_SCALE));
}
// 清除边框
gridPixmap.dispose();
return pixmap;
} //画六边形网格
public static Pixmap drawHexagonGridByView(Pixmap pixmap,AssetManager am)// 画网格
{
// 加载边框
int x = 0;
int y = 0;
int x_px = 0;
int y_px = 0;
// 将要取得的图片的id和名字放入位置
//Pixmap gridPixmap = new Pixmap(Gdx.files.internal("pixmap/tiles/grid.png"));
Pixmap gridPixmap = am.get(("pixmap/tiles/grid.png"),Pixmap.class); int w=(int)(pixmap.getWidth()/ResConfig.Map.GRID_WIDTH/ResConfig.Map.MAP_SCALE)+3;
int h=(int)(pixmap.getHeight()/ResConfig.Map.HEXAGON_HEIGHT/ResConfig.Map.MAP_SCALE)+3;
int size=w*h; // 绘制网格图片
for (int id = 0; id < size; id++) {
x = (id % w) + 1;
y = (id / w) + 1;
// x_px=(int) ((x-1)*139.5);
// y_px=(int) ((x&1) == 1?(y-1)*161:(y-1)*161+80.5);
x_px = (int) ((x - 1) * ResConfig.Map.GRID_WIDTH);
y_px = (int) (((x & 1) == 1 ? (y - 1) * ResConfig.Map.HEXAGON_HEIGHT : (y - 1) * ResConfig.Map.HEXAGON_HEIGHT + ResConfig.Map.HEXAGON_HEIGHT_REF));
//Gdx.app.log("绘制地图网格", "mapId:" + id + " x:" + x + " y:" + y+ " x_px:" + x_px + " y_px:" + y_px);
pixmap.drawPixmap(gridPixmap, 0, 0, gridPixmap.getWidth(), gridPixmap.getHeight(), (int) ((x_px)* ResConfig.Map.MAP_SCALE), (int) ((y_px) * ResConfig.Map.MAP_SCALE),
(int) (gridPixmap.getWidth() * ResConfig.Map.MAP_SCALE), (int) (gridPixmap.getHeight() * ResConfig.Map.MAP_SCALE));
}
// 清除边框
//gridPixmap.dispose();
return pixmap;
} // Pixmap大小计算
// w=186+139.5*(x-1) h=242+161*(y-1)
// int w = (int) (186 + 139.5 * (defMap.getWidth() - 1));
// int h = 242 + 161 * (defMap.getHeight() - 1);
// int w = (int) ((148 + ResConfig.Map.GRID_WIDTH * (defMap.getWidth() - 1)));
// int h = (int) ((191 + ResConfig.Map.HEXAGON_HEIGHT * (defMap.getHeight() - 1)));
public static Pixmap createPixmapByDefMap(DefMap defMap) {
int w = GameMap.getWidth(defMap.getWidth());
int h = GameMap.getHeight(defMap.getHeight()); int sum = defMap.getWidth() * defMap.getHeight();
/*
* if(sum>10000) { scale=(float) (((int)(10-(sum-10000)*0.0001))*0.06);
* }
*/
w = (int) (w * ResConfig.Map.MAP_SCALE);
h = (int) (h * ResConfig.Map.MAP_SCALE); //Gdx.app.log("地图大小", "sum:" + sum + " w:" + defMap.getWidth() + " h:" + defMap.getHeight());
//Gdx.app.log("地图图片大小", "缩放比例:" + Config.Map.MAP_SCALE + " w_px:" + w + " h_px:" + h);
return new Pixmap(w, h, Pixmap.Format.RGBA8888);
} public static Pixmap createPixmap(int mapW,int mapH) {
int w = GameMap.getWidth(mapW);
int h = GameMap.getHeight(mapH); int sum = w * h;
/*
* if(sum>10000) { scale=(float) (((int)(10-(sum-10000)*0.0001))*0.06);
* }
*/
w = (int) (w * ResConfig.Map.MAP_SCALE);
h = (int) (h * ResConfig.Map.MAP_SCALE); //Gdx.app.log("地图大小", "sum:" + sum + " w:" + defMap.getWidth() + " h:" + defMap.getHeight());
//Gdx.app.log("地图图片大小", "缩放比例:" + Config.Map.MAP_SCALE + " w_px:" + w + " h_px:" + h);
return new Pixmap(w, h, Pixmap.Format.RGBA8888);
} //创建一个当前窗口大小的画布
public static Pixmap createPixmapByView(float vw,float vh) {
int w = (int)vw;
int h = (int)vh;
//Gdx.app.log("海洋地图:"," w:" + w + " h:" +h); return new Pixmap(w, h, Pixmap.Format.RGBA8888);
} // 使用前必须初始化 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 = GameUtil.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());
}
/*if(ptId==1){
int color = pixmap.getPixel(12, 12);
//pixmap.drawPixel(xC, yC, color);
Gdx.app.log("海色","color:"+color);
}*/ // 清除资源
imgPt.dispose();
}
return pixmap;
} //获得地形装饰的最高上限,最低下限为0
//储存方式为 Map(id_type+"_min",idx_min) Map(id_type+"_max",idx_max)
public static Map getDecorateRandMaxMap() {
DefTerrainimg defTerrainimg = new DefTerrainimg();
List<DefTerrainimg> defTerrainimgs = new ArrayList<DefTerrainimg>();
try {
defTerrainimgs = GameUtil.getDaoListByClass(defTerrainimg,
"config/def_terrainimg.xml");
} catch (Exception e) {
e.printStackTrace();
}
int i,iLength;
iLength=defTerrainimgs.size();
Map map=new HashMap();
for(i=0;i<iLength;i++) {//省略掉类型为-1的值
if(defTerrainimgs.get(i).getType()==-1) {
} else if(!map.containsKey((defTerrainimgs.get(i).getId()+"_"+defTerrainimgs.get(i).getType()+"_min"))) {
map.put(defTerrainimgs.get(i).getId()+"_"+defTerrainimgs.get(i).getType()+"_min", defTerrainimgs.get(i).getIdx());
} else if(!map.containsKey((defTerrainimgs.get(i).getId()+"_"+defTerrainimgs.get(i).getType()+"_max"))) {
map.put(defTerrainimgs.get(i).getId()+"_"+defTerrainimgs.get(i).getType()+"_max", defTerrainimgs.get(i).getIdx());
}else if((Integer)map.get(defTerrainimgs.get(i).getId()+"_"+defTerrainimgs.get(i).getType()+"_min")>defTerrainimgs.get(i).getIdx() ) {
map.put(defTerrainimgs.get(i).getId()+"_"+defTerrainimgs.get(i).getType()+"_min", defTerrainimgs.get(i).getIdx());
}else if((Integer)map.get(defTerrainimgs.get(i).getId()+"_"+defTerrainimgs.get(i).getType()+"_max")<defTerrainimgs.get(i).getIdx() ) {
map.put(defTerrainimgs.get(i).getId()+"_"+defTerrainimgs.get(i).getType()+"_max", defTerrainimgs.get(i).getIdx());
}
}
return map;
} //添加周围的id坐标
public static List<Coord> addCoordByIds(List<Integer> aroundIds,int w){
int cell_x,cell_y;float vertex_x_px,vertex_y_px;
List<Coord> coords = new ArrayList<Coord>(); for(Integer id:aroundIds) {
cell_x=id-(int)(id/w)*w;
cell_y=(int)(id/w)+1;
vertex_x_px=-cell_x*ResConfig.Map.GRID_WIDTH*ResConfig.Map.MAP_SCALE;
vertex_y_px=-cell_x%2!=0?(cell_y+1)*ResConfig.Map.HEXAGON_HEIGHT*ResConfig.Map.MAP_SCALE:(cell_y+0.5f)*ResConfig.Map.HEXAGON_HEIGHT*ResConfig.Map.MAP_SCALE;
coords.add(new Coord(cell_x,cell_y,vertex_x_px,vertex_y_px));
}
return coords;
} //根据smallMap文件绘制图片顺便清理图片
public static Pixmap drawSmallMapAndClearPixmap(int mapId,AssetManager am){
XmlReader reader = ResConfig.reader;
XmlReader.Element root = reader.parse(Gdx.files.internal("config/def_smallmap.xml"));
int childNum = root.getChildCount();
String imgFileName;
int w=0;
int h=0;
Pixmap pixmap = null;XmlReader.Element xmlFile;
for (int i = 0; i < childNum; i++) {
if (root.getChild(i).getInt("id")==mapId) {
imgFileName=root.getChild(i).get("name");
xmlFile=root.getChild(i);
pixmap = GameMap.createPixmapByView(xmlFile.getInt("width"), xmlFile.getInt("height"));
for(int j=0;j<xmlFile.getChildCount();j++) {
pixmap.drawPixmap(am.get(("image/"+imgFileName+"/"+xmlFile.getChild(j).get("n")),Pixmap.class),xmlFile.getChild(j).getInt("x"),xmlFile.getChild(j).getInt("y"));
am.unload("image/"+imgFileName+"/"+xmlFile.getChild(j).get("n"));
}
break;
}
}
//PixmapIO.writePNG(Gdx.files.external("smallWorld.png"), pixmap);
return pixmap;
} }

GameMap

18.libgdx制作预览图,背景移动循环,改变地图颜色的更多相关文章

  1. Android开发 获取视频中的信息(例如预览图或视频时长) MediaMetadataRetriever媒体元数据检索器

    前言 在Android里获取视频的信息主要依靠MediaMetadataRetriever实现 获取最佳视频预览图 所谓的最佳就是MediaMetadataRetriever自己计算的 /** * 获 ...

  2. HTML5-video标签-实现点击预览图播放或暂停视频

    HTML5-video标签-实现点击预览图播放或暂停视频 刚刚参加工作,开始更多的接触到一些新的知识,促使我开始了解html5和css3的新特性.这时我才真的发现到html5和css3的强大. 之前关 ...

  3. 可拖拽和带预览图的jQuery文件上传插件ssi-uploader

    插件描述:ssi-uploader是一款带预览图并且可以拖拽文件的jQuery ajax文件上传插件.该文件上传插件支持AJAX,支持多文件上传,可控制上的文件格式和文件大小,提供各种回调函数,使用非 ...

  4. 终极指南:如何为iOS8应用制作预览视频

    最近一两个月里,苹果的世界里出现了很多新东西,比如屏幕更大的iPhone 6,可穿戴设备Apple Watch,iOS8,以及旨在帮助用户更好的发现应用的App Store改版等等. 说到App St ...

  5. APP_Store - 怎样为iOS8应用制作预览视频

    关于iOS 8应用预览视频的话题,从设计.技术规范,到录屏.编辑工具,介绍的都比较详尽:建议收藏,在接下来用的到的时候作以参考.下面进入译文. 最近一两个月里,苹果的世界里出现了很多新东西,比如屏幕更 ...

  6. Images corrections preview with lensfun 不同型号镜头预览图 828张 合集

    lensfun 目前支持900多种镜头, 但是网上并没有预览图; 闲暇时间做了800多张预览图合集 下载地址 链接: https://pan.baidu.com/s/1crfhoKKZKnxntvNH ...

  7. PHP如何生成文章预览图

    PHP如何生成文章预览图 一.总结 一句话总结:php的wkhtmltox扩展,php官方文档有怎么使用,或者github,或者百度,等等等等 wkhtmltox 1.PHP如何自动生成文章预览图? ...

  8. 资源管理器总是生成 avi,mpeg的预览图

    感觉非常讨厌. 图片可以直接显示,但是视频的预览图感觉很不舒服. 查了一下 , 用ShellExView 禁用一下 MF XXXX Property Handler 就可以了. 官网最下面可以下载: ...

  9. APP图标设计小技巧:在iOS上快速获得APP图标的真实预览图

    严格来说,这并不是一篇关于前端开发的文章,因为涉及到的知识非常浅.这只是一个向设计狮们分享的小经验,只是其中用到了一些前端内容. 最近接了个私活,了解到一个初创公司正在高价悬赏Logo(主要用于APP ...

随机推荐

  1. 如何实现一个HTTP请求库——axios源码阅读与分析 JavaScript

    概述 在前端开发过程中,我们经常会遇到需要发送异步请求的情况.而使用一个功能齐全,接口完善的HTTP请求库,能够在很大程度上减少我们的开发成本,提高我们的开发效率. axios是一个在近些年来非常火的 ...

  2. Redis源码解析:30发布和订阅

    Redis的发布与订阅功能,由SUBSCRIBE,PSUBSCRIBE,UNSUBSCRIBE,PUNSUBSCRIBE,以及PUBLISH等命令实现. 通过执行SUBSCRIBE命令,客户端可以订阅 ...

  3. js构造函数+原型

    注:普通对象与函数对象 var o1 = {}; var o2 =new Object(); var o3 = new f1(); function f1(){}; var f2 = function ...

  4. poj1160 动态规划

    #include<stdio.h> #include<string.h> #define INF 999999999 #define Min(x,y) (x<y?x:y) ...

  5. 码云及git使用

    首次使用码云,将本地文件与之关联(创建仓库之后的页面截图) git -- 版本控制(协同开发软件) git add . # 将当前文件下所有内容添加到临时缓存区 git commit -m " ...

  6. 数据库----SQL基本查询

    SQL基本查询 查表 :show create table 表名(show tables); describe 表名:(desc 表名)// 模糊查询:show table like '%c%';(查 ...

  7. 【AHOI2013复仇】从一道题来看DFS及其优化的一般步骤和数组分层问题【转】

    http://www.cppblog.com/MatoNo1/archive/2012/09/23/191708.html —————————————————————————————————————— ...

  8. Spring boot随时获取ApplicationContex

    @Service public class SpringManager implements ApplicationListener<ContextRefreshedEvent> { pr ...

  9. Leetcode36.Valid Sudoku有效的数独

    判断一个 9x9 的数独是否有效.只需要根据以下规则,验证已经填入的数字是否有效即可. 数字 1-9 在每一行只能出现一次. 数字 1-9 在每一列只能出现一次. 数字 1-9 在每一个以粗实线分隔的 ...

  10. PHPStorm 批量选择,多光标同时编辑相同的内容

    一直按Alt+J