Exemple #1
0
    /*        int wallCount = map.ObjectGroups[2].Objects.Count;
        CreateWall(tileSheet);

        for (int i = 0; i < wallCount; i++)
        {
            TmxObjectGroup.TmxObject wall = map.ObjectGroups[2].Objects[i];
            //Debug.Log("object at [" + enemy.X + ", " + enemy.Y + "]");
            int startEndX = (int)wall.X / tile_width;
            int startEndY = (int)wall.Y / tile_height;
            Vector3 worldPos = new Vector3(-startEndX, 0.5f, startEndY);

            GameObject wallObject = (GameObject)GameObject.Instantiate(wallPref, worldPos, Quaternion.identity);
            //wallObject.transform.parent = wallContainer.transform;
            wallObject.transform.localPosition = worldPos;
        }
        if (Application.isPlaying)
        {
            GameObject.Destroy(wallPref);
        }*/
    /*
        int objectCount = map.ObjectGroups[3].Objects.Count;

        for (int i = 0; i < objectCount; i++)
        {
            TmxObjectGroup.TmxObject genericObject = map.ObjectGroups[3].Objects[i];
            //Debug.Log("object at [" + enemy.X + ", " + enemy.Y + "]");
            int startEndX = (int)genericObject.X / tile_width;
            int startEndY = (int)genericObject.Y / tile_height;
            Vector3 worldPos = new Vector3(-startEndX + 0.5f, 0.5f, startEndY -1.5f);

        }*/
    public void GenerateTiles(TmxLayerList layerList)
    {
        int layerCount = layerList.Count;
        for (int i = 0; i < layerCount; i += 1)
        {
            TmxLayer layer = layerList[i];
            int tileCount = layer.Tiles.Count;
            PropertyDict properties = layer.Properties;

            string tileType = "Ground";
            if (properties.ContainsKey("Type"))
            {
                tileType = properties["Type"];
            }
            if (tileType == "Ground")
            {
                tiles = new MapSquare[layer.Tiles.Count];
            }
            Transform layerContainer = new GameObject(layer.Name).transform;
            layerContainer.rotation = Quaternion.identity;
            layerContainer.transform.SetParent(tileContainer);

            //Debug.Log(tileCount);

            for (int j = 0; j < tileCount; j += 1)
            {
                TmxLayerTile tmxTile = layer.Tiles[j];
                int tileSetId = FindTileSetId(tmxTile.Gid);

                if (tileSetId == -1)
                {
                    continue;
                }
                if (tileType == "Wall")
                {
                    if (tmxTile.Gid == 0)
                    {
                        continue;
                    }
                    tileMap.AddNode(tmxTile.X, tmxTile.Y, false);

                    int xpos = (int)tmxTile.X;
                    int ypos = (int)tmxTile.Y;
                    Vector3 worldPos = new Vector3(-xpos + 0.5f, 0.5f, ypos-0.5f);
                    //Debug.Log("[" + tmxTile.X + ", " + tmxTile.Y + "]");
                    GameObject spawnedTile;
                    if (wallPref == null) {
                        CreateWall();
                    }
                    spawnedTile = (GameObject)Instantiate(wallPref, worldPos, Quaternion.identity);
                    spawnedTile.name = "Wall_" + tmxTile.Gid;
                    spawnedTile.transform.position = worldPos;
                    spawnedTile.transform.parent = layerContainer;
                }
                if (tileType == "Ground")
                {
                    if (tmxTile.Gid != 0) {
                        tileMap.AddNode(tmxTile.X, tmxTile.Y);
                    }
                    tiles[j] = new MapSquare(tmxTile);

                }

            }

        }
    }
Exemple #2
0
/*        int wallCount = map.ObjectGroups[2].Objects.Count;
 *      CreateWall(tileSheet);
 *
 *      for (int i = 0; i < wallCount; i++)
 *      {
 *          TmxObjectGroup.TmxObject wall = map.ObjectGroups[2].Objects[i];
 *          //Debug.Log("object at [" + enemy.X + ", " + enemy.Y + "]");
 *          int startEndX = (int)wall.X / tile_width;
 *          int startEndY = (int)wall.Y / tile_height;
 *          Vector3 worldPos = new Vector3(-startEndX, 0.5f, startEndY);
 *
 *          GameObject wallObject = (GameObject)GameObject.Instantiate(wallPref, worldPos, Quaternion.identity);
 *          //wallObject.transform.parent = wallContainer.transform;
 *          wallObject.transform.localPosition = worldPos;
 *      }
 *      if (Application.isPlaying)
 *      {
 *          GameObject.Destroy(wallPref);
 *      }*/

    /*
     * int objectCount = map.ObjectGroups[3].Objects.Count;
     *
     * for (int i = 0; i < objectCount; i++)
     * {
     *  TmxObjectGroup.TmxObject genericObject = map.ObjectGroups[3].Objects[i];
     *  //Debug.Log("object at [" + enemy.X + ", " + enemy.Y + "]");
     *  int startEndX = (int)genericObject.X / tile_width;
     *  int startEndY = (int)genericObject.Y / tile_height;
     *  Vector3 worldPos = new Vector3(-startEndX + 0.5f, 0.5f, startEndY -1.5f);
     *
     * }*/


    public void GenerateTiles(TmxLayerList layerList)
    {
        int layerCount = layerList.Count;

        for (int i = 0; i < layerCount; i += 1)
        {
            TmxLayer     layer      = layerList[i];
            int          tileCount  = layer.Tiles.Count;
            PropertyDict properties = layer.Properties;

            string tileType = "Ground";
            if (properties.ContainsKey("Type"))
            {
                tileType = properties["Type"];
            }
            if (tileType == "Ground")
            {
                tiles = new MapSquare[layer.Tiles.Count];
            }
            Transform layerContainer = new GameObject(layer.Name).transform;
            layerContainer.rotation = Quaternion.identity;
            layerContainer.transform.SetParent(tileContainer);

            //Debug.Log(tileCount);

            for (int j = 0; j < tileCount; j += 1)
            {
                TmxLayerTile tmxTile   = layer.Tiles[j];
                int          tileSetId = FindTileSetId(tmxTile.Gid);

                if (tileSetId == -1)
                {
                    continue;
                }
                if (tileType == "Wall")
                {
                    if (tmxTile.Gid == 0)
                    {
                        continue;
                    }
                    tileMap.AddNode(tmxTile.X, tmxTile.Y, false);

                    int     xpos     = (int)tmxTile.X;
                    int     ypos     = (int)tmxTile.Y;
                    Vector3 worldPos = new Vector3(-xpos + 0.5f, 0.5f, ypos - 0.5f);
                    //Debug.Log("[" + tmxTile.X + ", " + tmxTile.Y + "]");
                    GameObject spawnedTile;
                    if (wallPref == null)
                    {
                        CreateWall();
                    }
                    spawnedTile      = (GameObject)Instantiate(wallPref, worldPos, Quaternion.identity);
                    spawnedTile.name = "Wall_" + tmxTile.Gid;
                    spawnedTile.transform.position = worldPos;
                    spawnedTile.transform.parent   = layerContainer;
                }
                if (tileType == "Ground")
                {
                    if (tmxTile.Gid != 0)
                    {
                        tileMap.AddNode(tmxTile.X, tmxTile.Y);
                    }
                    tiles[j] = new MapSquare(tmxTile);
                }
            }
        }
    }