/// <summary> /// Generates a single mesh collider from Tiled data. /// Only relevant faces are computed. /// Impprovement notes : /// - Meshes could be separated if the max vertice count is reached (65536) /// - Could use triangle strips instead of individual triangles /// - Have more shapes available (only boxes are supported) /// </summary> /// <param name='tiledMap'>Tiled map.</param> public void BuildMeshCollider(TiledMap tiledMap) { // Index Tiled data TiledMap.Layer layer = tiledMap.layers [COLLISION_LAYER]; TiledMap.Tileset tileset = tiledMap.tilesets [COLLISION_TILESET]; int[] grid = new int[layer.grid.Length]; // Reverse grid Y-axis System.Array.Copy(layer.grid, grid, grid.Length); ConvertGridToYUp(grid, layer.width, layer.height); // Build collider BuildMeshCollider(grid, layer.width, layer.height, tileset.firstgid); }
public void BuildTiles(TiledMap tiledMap, string tilesetName) { // Index Tiled data TiledMap.Layer layer = tiledMap.layers[BACKGROUND_LAYER]; TiledMap.Tileset tileset = tiledMap.tilesets[tilesetName]; // Reverse grid Y-axis int[] grid = new int[layer.grid.Length]; System.Array.Copy(layer.grid, grid, grid.Length); ConvertGridToYUp(grid, layer.width, layer.height); // Build tiles BuildTiles(grid, layer.width, layer.height, tileset.firstgid, tileset.tileWidth, tileset.tileHeight); }
private void LoadTilemap(string name) { // Build background // Load map data TiledMap tiledMap = new TiledMap(); tiledMap.LoadFromJSON(name); width = tiledMap.width; // Build visible tilemap with collisions normalTilemap.collisionMapping = new int[] { 1, // [0] block 0, // [1] duck 2, // [2] spike up 3, // [3] coin 4, // ... 0, 0, 0, 0 }; normalTilemap.Build(tiledMap, "background"); normalTilemap.transform.position = new Vector3(offsetX, 0); // Build duckland tilemap without colliders duckWorldTilemap.Build(tiledMap, "background", false); duckWorldTilemap.SetLayer(LayerMask.NameToLayer("DuckWorld")); duckWorldTilemap.transform.position = new Vector3(offsetX, 0); TiledMap.Layer bgLayer = tiledMap.layers["background"]; // Build special colliders for (int y = 0; y < bgLayer.height; ++y) { for (int x = 0; x < bgLayer.width; ++x) { int t = bgLayer.AtYup(x, y); // Note: t starts at index 1 Vector3 pos = new Vector3(offsetX + x + 0.5f, y + 0.5f); if (t == 3) { Instantiate(spikePrefab, pos, Quaternion.identity); } } } // Build collectibles TiledMap.Layer objectLayer = tiledMap.layers["objects"]; for (int y = 0; y < objectLayer.height; ++y) { for (int x = 0; x < objectLayer.width; ++x) { int t = objectLayer.AtYup(x, y); if (t == 4) { Instantiate(collectiblePrefab, new Vector3(offsetX + x + 0.5f, y + 0.5f), Quaternion.identity); } else if (t == 5) { Instantiate(cancerPrefab, new Vector3(offsetX + x + 0.5f, y + 0.5f), Quaternion.identity); } else if (t == 6) { Instantiate(goalPrefab, new Vector3(offsetX + x + 0.5f, y + 0.5f), Quaternion.identity); } else if (t == 7) { Instantiate(heartPrefab, new Vector3(offsetX + x + 0.5f, y + 0.5f), Quaternion.identity); } } } }