Example #1
0
    bool LoadCell(float cellZ, float cellX, XElement xCell, TileMap.Row virtualRow)
    {
        TileMap.Cell loadedCell = new TileMap.Cell();
        loadedCell.tiles = new List <CTile>();

        var xTiles = xCell.Elements(XMLFields.TILE);

        //Generate base ground below each cell, if enabled
        if (baseGroundPrefab)
        {
            UnityEngine.Object.Instantiate(baseGroundPrefab, new Vector3(cellX, mapProperties.startingPosition.y - mapProperties.tileHeight, cellZ)
                                           , Quaternion.identity);
        }

        float   tileY        = mapProperties.startingPosition.y;
        Vector3 tilePosition = new Vector3(cellX, tileY, cellZ);

        foreach (var xTile in xTiles)
        {
            if (!LoadTile(tilePosition, xTile, loadedCell))
            {
                Debug.Log("Couldn't load tile at " + tilePosition.ToString());
                return(false);
            }

            tilePosition.y += mapProperties.tileHeight;
        }

        virtualRow.cells.Add(loadedCell);
        return(true);
    }
Example #2
0
    bool LoadTile(Vector3 position, XElement xTile, TileMap.Cell virtualCell)
    {
        string tileIDStr = xTile.Element(XMLFields.ID).Value;

        int tileID;

        if (!int.TryParse(tileIDStr, out tileID))
        {
            Debug.LogError("Tile ID \"" + tileIDStr + "\" is not an int!");
            return(false);
        }

        GameObject tileObject = GetTilePrefabForID(tileID);

        if (tileObject == null)
        {
            Debug.Log("Couldn't find tile prefab with ID " + tileIDStr);
            return(false);
        }

        string tileYRotationStr = xTile.Element(XMLFields.Y_ROTATION).Value;
        float  tileYRotation;

        if (!float.TryParse(tileYRotationStr, out tileYRotation))
        {
            Debug.LogError("Tile rotation " + tileYRotationStr + " isn't a float!");
            return(false);
        }
        Quaternion tileSpawnRotation = Quaternion.identity;

        tileSpawnRotation.eulerAngles = new Vector3(0.0f, tileYRotation, 0.0f);

        GameObject loadedTile = null;

        if (parentTransform != null)
        {
            loadedTile = UnityEngine.Object.Instantiate(tileObject, position, tileSpawnRotation, parentTransform);
        }
        else
        {
            loadedTile = UnityEngine.Object.Instantiate(tileObject, position, tileSpawnRotation);
        }

        if (!loadedTile)
        {
            Debug.Log("Couldn't instantiate prefab " + tileObject.name);
            return(false);
        }

        virtualCell.tiles.Add(loadedTile.GetComponent <CTile>());
        return(true);
    }