/android/Android(LGame-0.3.2&LAE-1.1)/LAE-1.1(Canvas)/src/core/org/loon/framework/android/game/action/map/tmx/TMXTileSet.java

http://loon-simple.googlecode.com/ · Java · 191 lines · 128 code · 40 blank · 23 comment · 19 complexity · 7acb8778d60a489144e657b45954e2e0 MD5 · raw file

  1. package org.loon.framework.android.game.action.map.tmx;
  2. import java.io.InputStream;
  3. import java.util.HashMap;
  4. import javax.xml.parsers.DocumentBuilder;
  5. import javax.xml.parsers.DocumentBuilderFactory;
  6. import org.loon.framework.android.game.action.sprite.SpriteSheet;
  7. import org.loon.framework.android.game.core.graphics.LImage;
  8. import org.loon.framework.android.game.core.resource.Resources;
  9. import org.w3c.dom.Document;
  10. import org.w3c.dom.Element;
  11. import org.w3c.dom.NodeList;
  12. /**
  13. *
  14. * Copyright 2008 - 2011
  15. *
  16. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  17. * use this file except in compliance with the License. You may obtain a copy of
  18. * the License at
  19. *
  20. * http://www.apache.org/licenses/LICENSE-2.0
  21. *
  22. * Unless required by applicable law or agreed to in writing, software
  23. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  24. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  25. * License for the specific language governing permissions and limitations under
  26. * the License.
  27. *
  28. * @project loonframework
  29. * @author chenpeng
  30. * @email ceponline@yahoo.com.cn
  31. * @version 0.1.0
  32. */
  33. public class TMXTileSet {
  34. // ????
  35. private final TMXTiledMap map;
  36. // ????
  37. public int index;
  38. public String name;
  39. public int firstGID;
  40. public int lastGID = Integer.MAX_VALUE;
  41. public int tileWidth;
  42. public int tileHeight;
  43. public SpriteSheet tiles;
  44. public int tilesAcross;
  45. public int tilesDown;
  46. private HashMap<Integer, TMXProperty> props = new HashMap<Integer, TMXProperty>();
  47. protected int tileSpacing = 0;
  48. protected int tileMargin = 0;
  49. public TMXTileSet(TMXTiledMap map, Element element, boolean loadImage)
  50. throws RuntimeException {
  51. this.map = map;
  52. name = element.getAttribute("name");
  53. firstGID = Integer.parseInt(element.getAttribute("firstgid"));
  54. String source = element.getAttribute("source");
  55. if ((source != null) && (!source.equals(""))) {
  56. try {
  57. InputStream in = Resources.getResourceAsStream(map
  58. .getTilesLocation()
  59. + "/" + source);
  60. DocumentBuilder builder = DocumentBuilderFactory.newInstance()
  61. .newDocumentBuilder();
  62. Document doc = builder.parse(in);
  63. Element docElement = doc.getDocumentElement();
  64. element = docElement;
  65. } catch (Exception e) {
  66. throw new RuntimeException(this.map.tilesLocation + "/"
  67. + source);
  68. }
  69. }
  70. String tileWidthString = element.getAttribute("tilewidth");
  71. String tileHeightString = element.getAttribute("tileheight");
  72. if (tileWidthString.length() == 0 || tileHeightString.length() == 0) {
  73. throw new RuntimeException(
  74. "tileWidthString.length == 0 || tileHeightString.length == 0");
  75. }
  76. tileWidth = Integer.parseInt(tileWidthString);
  77. tileHeight = Integer.parseInt(tileHeightString);
  78. String sv = element.getAttribute("spacing");
  79. if ((sv != null) && (!"".equals(sv))) {
  80. tileSpacing = Integer.parseInt(sv);
  81. }
  82. String mv = element.getAttribute("margin");
  83. if ((mv != null) && (!"".equals(mv))) {
  84. tileMargin = Integer.parseInt(mv);
  85. }
  86. NodeList list = element.getElementsByTagName("image");
  87. Element imageNode = (Element) list.item(0);
  88. String fileName = imageNode.getAttribute("source");
  89. if (loadImage) {
  90. LImage image = new LImage(map.getTilesLocation() + "/" + fileName);
  91. setTileSetImage(image);
  92. }
  93. NodeList pElements = element.getElementsByTagName("tile");
  94. for (int i = 0; i < pElements.getLength(); i++) {
  95. Element tileElement = (Element) pElements.item(i);
  96. int id = Integer.parseInt(tileElement.getAttribute("id"));
  97. id += firstGID;
  98. TMXProperty tileProps = new TMXProperty();
  99. Element propsElement = (Element) tileElement.getElementsByTagName(
  100. "properties").item(0);
  101. NodeList properties = propsElement.getElementsByTagName("property");
  102. for (int p = 0; p < properties.getLength(); p++) {
  103. Element propElement = (Element) properties.item(p);
  104. String name = propElement.getAttribute("name");
  105. String value = propElement.getAttribute("value");
  106. tileProps.setProperty(name, value);
  107. }
  108. props.put(new Integer(id), tileProps);
  109. }
  110. }
  111. public int getTileWidth() {
  112. return tileWidth;
  113. }
  114. public int getTileHeight() {
  115. return tileHeight;
  116. }
  117. public int getTileSpacing() {
  118. return tileSpacing;
  119. }
  120. public int getTileMargin() {
  121. return tileMargin;
  122. }
  123. public void setTileSetImage(LImage image) {
  124. tiles = new SpriteSheet(image, tileWidth, tileHeight, tileSpacing,
  125. tileMargin);
  126. tilesAcross = tiles.getHorizontalCount();
  127. tilesDown = tiles.getVerticalCount();
  128. if (tilesAcross <= 0) {
  129. tilesAcross = 1;
  130. }
  131. if (tilesDown <= 0) {
  132. tilesDown = 1;
  133. }
  134. lastGID = (tilesAcross * tilesDown) + firstGID - 1;
  135. }
  136. public TMXProperty getProperties(int globalID) {
  137. return (TMXProperty) props.get(new Integer(globalID));
  138. }
  139. public int getTileX(int id) {
  140. return id % tilesAcross;
  141. }
  142. public int getTileY(int id) {
  143. return id / tilesAcross;
  144. }
  145. public void setLimit(int limit) {
  146. lastGID = limit;
  147. }
  148. public boolean contains(int gid) {
  149. return (gid >= firstGID) && (gid <= lastGID);
  150. }
  151. }