/Code/Layer.cs

https://bitbucket.org/zeroeth/unitmx · C# · 178 lines · 145 code · 11 blank · 22 comment · 21 complexity · 5cdcdc377a6b8d8e8a0f0f24a427601d MD5 · raw file

  1. /*!
  2. * UniTMX: A tiled map editor file importer for Unity3d
  3. * https://bitbucket.org/PolCPP/unitmx
  4. *
  5. * Copyright 2012 Pol Cámara
  6. * Released under the MIT license
  7. * Check LICENSE.MIT for more details.
  8. */
  9. using System;
  10. using UnityEngine;
  11. using System.Collections.Generic;
  12. // Manages all the magic. It builds both the tile mesh with it's uv's
  13. // And collision layer mesh
  14. public class Layer
  15. {
  16. TileSet _tileset;
  17. string[] _data;
  18. int _currentLayerID;
  19. int _width;
  20. int _height;
  21. int _vertexCount = 0;
  22. public int vertexCount {
  23. get {
  24. return this._vertexCount;
  25. }
  26. }
  27. public Layer (TileSet tileset, string data, int currentLayerID, int width, int height)
  28. {
  29. this._tileset = tileset;
  30. this._data = data.Split (',');
  31. this._currentLayerID = currentLayerID;
  32. this._width = width;
  33. this._height = height;
  34. }
  35. // Renders the tile vertices.
  36. // Basically, it reads the entire CSV file cells, and creates a
  37. // 4 vertexes (forming a rectangle or square according to settings)
  38. // when a value different than 0 is found
  39. public List<Vector3> renderVertices ()
  40. {
  41. int dataIndex = 0;
  42. //_currentLayerID = _currentLayerID * 10;
  43. float z = _currentLayerID * -10;
  44. List<Vector3> vertices = new List<Vector3> ();
  45. for (int i = 1; i <= _height; i++) {
  46. for (int j = 1; j <= _width; j++) {
  47. string dataValue = _data [dataIndex].ToString ().Trim ();
  48. if (dataValue != "0") {
  49. vertices.AddRange (new Vector3[] {
  50. new Vector3 (_tileset.width * (j + 1), _tileset.height * (-i + 1), z),
  51. new Vector3 (_tileset.width * (j + 1), _tileset.height * -i, z),
  52. new Vector3 (_tileset.width * j, _tileset.height * (-i + 1), z),
  53. new Vector3 (_tileset.width * j, _tileset.height * -i, z),
  54. });
  55. _vertexCount += 4;
  56. }
  57. dataIndex++;
  58. }
  59. }
  60. return vertices;
  61. }
  62. // Renders the collision vertices.
  63. // Basically, it works the same way as renderVertices but in this case
  64. // it checks the value to see what kind of collision mesh we need to draw.
  65. public List<Vector3> renderColVertices ()
  66. {
  67. int dataIndex = 0;
  68. float z2 = _currentLayerID * -10;
  69. float z = _currentLayerID * -10 + 5; // - _tileset.height;
  70. //_currentLayerID = _currentLayerID * 10;
  71. TileSet.CollisionFormat collision = TileSet.CollisionFormat.none;
  72. List<Vector3> vertices = new List<Vector3> ();
  73. for (int i = 1; i <= _height; i++) {
  74. for (int j = 1; j <= _width; j++) {
  75. string dataValue = _data [dataIndex].ToString ().Trim ();
  76. if (dataValue != "0") {
  77. collision = _tileset.GetCollision (int.Parse (dataValue));
  78. if (collision != TileSet.CollisionFormat.none) {
  79. if ((collision & TileSet.CollisionFormat.top) != 0) {
  80. vertices.AddRange (new Vector3[] {
  81. new Vector3 (_tileset.width * (j + 1), _tileset.height * (-i + 1), z),
  82. new Vector3 (_tileset.width * (j + 1), _tileset.height * (-i + 1), z2),
  83. new Vector3 (_tileset.width * j, _tileset.height * (-i + 1), z),
  84. new Vector3 (_tileset.width * j, _tileset.height * (-i + 1), z2),
  85. });
  86. _vertexCount += 4;
  87. }
  88. if ((collision & TileSet.CollisionFormat.bottom) != 0) {
  89. vertices.AddRange (new Vector3[] {
  90. new Vector3 (_tileset.width * j, _tileset.height * -i, z),
  91. new Vector3 (_tileset.width * j, _tileset.height * -i, z2),
  92. new Vector3 (_tileset.width * (j + 1), _tileset.height * -i, z),
  93. new Vector3 (_tileset.width * (j + 1), _tileset.height * -i, z2),
  94. });
  95. _vertexCount += 4;
  96. }
  97. if ((collision & TileSet.CollisionFormat.left) != 0) {
  98. vertices.AddRange (new Vector3[] {
  99. new Vector3 (_tileset.width * j, _tileset.height * (-i + 1), z),
  100. new Vector3 (_tileset.width * j, _tileset.height * (-i + 1), z2),
  101. new Vector3 (_tileset.width * j, _tileset.height * (-i + 0), z),
  102. new Vector3 (_tileset.width * j, _tileset.height * (-i + 0), z2),
  103. });
  104. _vertexCount += 4;
  105. }
  106. if ((collision & TileSet.CollisionFormat.right) != 0) {
  107. vertices.AddRange (new Vector3[] {
  108. new Vector3 (_tileset.width * (j + 1), _tileset.height * (-i + 0), z),
  109. new Vector3 (_tileset.width * (j + 1), _tileset.height * (-i + 0), z2),
  110. new Vector3 (_tileset.width * (j + 1), _tileset.height * (-i + 1), z),
  111. new Vector3 (_tileset.width * (j + 1), _tileset.height * (-i + 1), z2),
  112. });
  113. _vertexCount += 4;
  114. }
  115. }
  116. }
  117. dataIndex++;
  118. }
  119. }
  120. // _vertexCount = vertices.Length();
  121. return vertices;
  122. }
  123. // Creates the Face UV for the faces according to the tile represented on the TMX.
  124. public List<Vector2> renderUv ()
  125. {
  126. List<Vector2> uv = new List<Vector2> ();
  127. int horizontalCellCount = _tileset.materialWidth / (_tileset.width + _tileset.tileBorder);
  128. int verticalCellCount = _tileset.materialHeight / (_tileset.height + _tileset.tileBorder);
  129. float cellWidth = ((float)_tileset.width / _tileset.materialWidth);
  130. float cellHeight = ((float)_tileset.height / _tileset.materialHeight);
  131. float borderWidth = ((float)_tileset.tileBorder / _tileset.materialWidth);
  132. float borderHeight = ((float)_tileset.tileBorder / _tileset.materialHeight);
  133. int totalCells = _width * _height;
  134. int dataValue;
  135. for (int i = 0; i < totalCells; i++) {
  136. dataValue = int.Parse(_data [i].ToString ().Trim ());
  137. if (dataValue != 0) {
  138. dataValue = dataValue - _tileset.firstGID;
  139. int posY = dataValue / verticalCellCount;
  140. int posX = dataValue % horizontalCellCount;
  141. float u = ((cellWidth + borderWidth) * posX) + borderWidth/2;
  142. float v = 1.0f - ((cellHeight + borderHeight) * posY) - borderHeight/2;
  143. uv.AddRange (new Vector2[] {
  144. new Vector2 (u + cellWidth, v),
  145. new Vector2 (u + cellWidth, v - cellHeight),
  146. new Vector2 (u, v),
  147. new Vector2 (u, v - cellHeight),
  148. });
  149. }
  150. }
  151. return uv;
  152. }
  153. // Creates the triangles given the ammount of the Used Vertices until now (including other layers).
  154. public List<int> renderTriangles (int start, int end)
  155. {
  156. List<int> triangles = new List<int> ();
  157. int currentTri = start;
  158. while(currentTri < end) {
  159. triangles.AddRange (new int[] {
  160. currentTri, currentTri + 1, currentTri + 2,
  161. currentTri + 2, currentTri + 1, currentTri + 3,
  162. });
  163. currentTri += 4;
  164. }
  165. return triangles;
  166. }
  167. }