public Map Parse(string text)
        {
            JSONNode json = JSON.Parse(text);

            Map map = new Map(createReferences(json));
            map.Layers = createLayers(json["layers"]);

            map.Height = json["height"].AsInt;
            map.Width = json["width"].AsInt;

            return map;
        }
Ejemplo n.º 2
0
        private void createTiles(Map map)
        {
            foreach (Layer layer in map.Layers) {

                Debug.Log("Rendering layer: " + layer.Name);

                GameObject holder = new GameObject();
                holder.name = layer.Name;
                holder.transform.parent = tileHolder.transform;

                int x = 0, y = -map.Height;

                foreach (int d in layer.Data) {

                    if (d != 0) {

                        GameObject prefab = tilePrefab;

                        //  set a custom prefab if tile requires one
                        if (map.ObjectReferences.ContainsKey(d)) {
                            prefab = map.ObjectReferences[d];
                        }

                        GameObject t = (GameObject)GameObject.Instantiate(
                            prefab,
                            new Vector3(x * tileSize.x, -y * tileSize.y, layer.Height),
                            layer.Rotation);

                        t.name = x + ", " + y + ": " + tilePrefab.name;

                        //  only set the custom tile if using the tile prefab
                        if (prefab == tilePrefab) {
                            SpriteRenderer renderer = t.GetComponentInChildren<SpriteRenderer>();
                            if (renderer != null) {
                                renderer.sprite = spriteSheet.sprites[d - 1];
                            }
                        }

                        t.transform.parent = holder.transform;
                    }

                    x++;

                    if (x >= map.Width) {
                        x = 0;
                        y++;
                    }
                }
            }
        }
Ejemplo n.º 3
0
        private void initialize()
        {
            if (map == null) {
                Debug.Log("Loading map data");
                map = new TiledMapLoader(
                    new JSONMapParser())
                    .Load(mapJson);

                MapContainer.Map = map;
            }

            tileHolder = GameObject.Find(tileHolderName);

            if (tileHolder != null) {
                return;
            }

            tileHolder = new GameObject();
            tileHolder.name = tileHolderName;

            spriteSheet = new SpriteSheet(spriteResource);

            createTiles(map);
        }