Exemple #1
0
    public void GenerateObjects(TmxObjectGroupList objectGroupList)
    {
        int objectGroupCount = objectGroupList.Count;
        for (int i = 0; i < objectGroupCount; i += 1 )
        {
            TmxObjectGroup objectGroup = objectGroupList[i];
            int objectCount = objectGroup.Objects.Count;
            PropertyDict properties = objectGroup.Properties;

            Transform layerContainer = new GameObject(objectGroup.Name).transform;
            layerContainer.rotation = Quaternion.identity;
            layerContainer.transform.SetParent(objectContainer);
            for (int j = 0; j < objectCount; j += 1)
            {
                if (!properties.ContainsKey("Type"))
                {
                    Debug.Log("ERROR: Object Layer \"" + objectGroup.Name + "\" doesn't have a Type property!");
                    continue;
                }
                string objectType = properties["Type"];
                TmxObjectGroup.TmxObject tmxObject = objectGroup.Objects[j];

                int xpos = (int)tmxObject.X / tile_width;
                int ypos = (int)tmxObject.Y / tile_height - 1;

                Vector3 worldPos = new Vector3(-xpos + 0.5f, 0.5f, ypos - 0.5f);
                GameObject spawnedObject = (GameObject)GameObject.Instantiate(Resources.Load("Objects/" + objectType), worldPos, Quaternion.identity);
                spawnedObject.name = tmxObject.ToString();

                spawnedObject.transform.position = worldPos;
                spawnedObject.transform.parent = layerContainer;

                if (objectType == "Hero")
                {
                    hero = spawnedObject.GetComponent<Hero>();
                    hero.Init(tileMap, xpos, ypos);
                }
                if (objectType == "End")
                {
                    endX = xpos;
                    endY = ypos;
                }
                /*TmxObjectGroup.TmxObject startEnd = ObjectGroups[i].Objects[j];

                int startEndX = (int)startEnd.X / tile_width;
                int startEndY = (int)startEnd.Y / tile_height;
                Vector3 worldPos = new Vector3(-startEndX + 0.5f, 1f, startEndY - 1.5f);

                if (startEnd.Name == "Start")
                {
                    //player.Spawn(worldPos, tile_width, tile_height);
                    GameObject startObject = (GameObject)GameObject.Instantiate(Resources.Load("SpawnPoint"), worldPos, Quaternion.identity);
                }
                else if (startEnd.Name == "End")
                {
                    GameObject endObjectPrefab = (GameObject)GameObject.Instantiate(Resources.Load("LevelEndTrigger"));
                    endObjectPrefab.transform.position = worldPos;
                }*/
            }
        }
    }
Exemple #2
0
    public void GenerateObjects(TmxObjectGroupList objectGroupList)
    {
        int objectGroupCount = objectGroupList.Count;

        for (int i = 0; i < objectGroupCount; i += 1)
        {
            TmxObjectGroup objectGroup = objectGroupList[i];
            int            objectCount = objectGroup.Objects.Count;
            PropertyDict   properties  = objectGroup.Properties;

            Transform layerContainer = new GameObject(objectGroup.Name).transform;
            layerContainer.rotation = Quaternion.identity;
            layerContainer.transform.SetParent(objectContainer);
            for (int j = 0; j < objectCount; j += 1)
            {
                if (!properties.ContainsKey("Type"))
                {
                    Debug.Log("ERROR: Object Layer \"" + objectGroup.Name + "\" doesn't have a Type property!");
                    continue;
                }
                string objectType = properties["Type"];
                TmxObjectGroup.TmxObject tmxObject = objectGroup.Objects[j];

                int xpos = (int)tmxObject.X / tile_width;
                int ypos = (int)tmxObject.Y / tile_height - 1;

                Vector3    worldPos      = new Vector3(-xpos + 0.5f, 0.5f, ypos - 0.5f);
                GameObject spawnedObject = (GameObject)GameObject.Instantiate(Resources.Load("Objects/" + objectType), worldPos, Quaternion.identity);
                spawnedObject.name = tmxObject.ToString();

                spawnedObject.transform.position = worldPos;
                spawnedObject.transform.parent   = layerContainer;

                if (objectType == "Hero")
                {
                    hero = spawnedObject.GetComponent <Hero>();
                    hero.Init(tileMap, xpos, ypos);
                }
                if (objectType == "End")
                {
                    endX = xpos;
                    endY = ypos;
                }

                /*TmxObjectGroup.TmxObject startEnd = ObjectGroups[i].Objects[j];
                 *
                 * int startEndX = (int)startEnd.X / tile_width;
                 * int startEndY = (int)startEnd.Y / tile_height;
                 * Vector3 worldPos = new Vector3(-startEndX + 0.5f, 1f, startEndY - 1.5f);
                 *
                 * if (startEnd.Name == "Start")
                 * {
                 *  //player.Spawn(worldPos, tile_width, tile_height);
                 *  GameObject startObject = (GameObject)GameObject.Instantiate(Resources.Load("SpawnPoint"), worldPos, Quaternion.identity);
                 * }
                 * else if (startEnd.Name == "End")
                 * {
                 *  GameObject endObjectPrefab = (GameObject)GameObject.Instantiate(Resources.Load("LevelEndTrigger"));
                 *  endObjectPrefab.transform.position = worldPos;
                 * }*/
            }
        }
    }