Example #1
0
    /// <summary>
    /// Load the level saved in the corresponding JSON file.
    /// </summary>
    /// <param name="level">Name of the level saved in the corresponding JSON file.</param>
    public void LoadLevel(string level)
    {
        string jsonText;

        if (PlayerPrefs.GetInt("currentLevelIsCustom", 0) == 1)
        {
            jsonText = LevelManager.LoadLevel(level).ToString();
        }
        else
        {
            jsonText = LoadFile(level);
        }

        JSONObject dataAsJson = new JSONObject(jsonText);

        // Create board from file
        dataAsJson.GetField("Board", delegate(JSONObject boardData)
        {
            BoardManager.BoardSize.x = (int)boardData["Size"]["X"].i;
            BoardManager.BoardSize.y = (int)boardData["Size"]["Y"].i;

            BoardManager.CellSize   = (int)boardData["CellSize"].i;
            BoardManager.CellOffset = (int)boardData["CellOffset"].i;

            BoardManager.SpawnPoint.x = (int)boardData["SpawnPoint"]["X"].i;
            BoardManager.SpawnPoint.y = (int)boardData["SpawnPoint"]["Y"].i;

            foreach (var path in boardData["Paths"].list)
            {
                BoardManager.Paths.Add(new BoardPath((int)path["X1"].i, (int)path["Y1"].i, (int)path["X2"].i,
                                                     (int)path["Y2"].i));
            }

            BoardManager.EndPoint.x = (int)boardData["EndPoint"]["X"].i;
            BoardManager.EndPoint.y = (int)boardData["EndPoint"]["Y"].i;

            BoardManager.CreateBoard();
        }, Debug.LogError);

        // Load player inventory
        if (dataAsJson["Inventory"].HasField("Mirrors") && dataAsJson["Inventory"]["Mirrors"].i > 0)
        {
            CreateInventoryItem(MirrorInventoryItemPrefab, "mirror", (int)dataAsJson["Inventory"]["Mirrors"].i);
        }

        if (dataAsJson["Inventory"].HasField("MirrorFilters") && dataAsJson["Inventory"]["MirrorFilters"].i > 0)
        {
            CreateInventoryItem(FilterMirrorInventoryItemPrefab, "mirror-filter",
                                (int)dataAsJson["Inventory"]["MirrorFilters"].i);
        }

        if (dataAsJson["Inventory"].HasField("Prisms") && dataAsJson["Inventory"]["Prisms"].i > 0)
        {
            CreateInventoryItem(PrismInventoryItemPrefab, "prism", (int)dataAsJson["Inventory"]["Prisms"].i);
        }

        if (dataAsJson["Inventory"].HasField("Filters") && dataAsJson["Inventory"]["Filters"].i > 0)
        {
            CreateInventoryItem(FilterInventoryItemPrefab, "filter", (int)dataAsJson["Inventory"]["Filters"].i);
        }

        if (dataAsJson["Inventory"].HasField("Obstacles") && dataAsJson["Inventory"]["Obstacles"].i > 0)
        {
            CreateInventoryItem(ObstacleInventoryItemPrefab, "obstacle", (int)dataAsJson["Inventory"]["Obstacles"].i);
        }

        if (dataAsJson["Inventory"].HasField("StandardTurret") && dataAsJson["Inventory"]["StandardTurret"].i > 0)
        {
            CreateInventoryItem(StandardTurretInventoryItemPrefab, "standard-turret",
                                (int)dataAsJson["Inventory"]["StandardTurret"].i);
        }

        if (dataAsJson["Inventory"].HasField("MissileTurret") && dataAsJson["Inventory"]["MissileTurret"].i > 0)
        {
            CreateInventoryItem(MissileTurretInventoryItemPrefab, "missile-turret",
                                (int)dataAsJson["Inventory"]["MissileTurret"].i);
        }

        if (dataAsJson["Inventory"].HasField("LaserTurret") && dataAsJson["Inventory"]["LaserTurret"].i > 0)
        {
            CreateInventoryItem(LaserTurretInventoryItemPrefab, "laser-turret",
                                (int)dataAsJson["Inventory"]["LaserTurret"].i);
        }

        foreach (var jsonEntity in dataAsJson["Entities"].list)
        {
            GameObject objectInstance = null;
            switch (jsonEntity["Type"].str)
            {
            case "Mirror":
                Debug.Log("Instanciating a mirror...");
                objectInstance = Instantiate(MirrorPrefab, ItemsContainer.transform);
                Mirror mirror = objectInstance.GetComponentInChildren <Mirror>();
                mirror.Orientation = (Direction)jsonEntity["Orientation"].i;
                break;

            case "Filter":
                Debug.Log("Instanciating a filter...");
                objectInstance = Instantiate(FilterPrefab, ItemsContainer.transform);
                Filter filter = objectInstance.GetComponentInChildren <Filter>();
                filter.Color = new RayColor(
                    jsonEntity["Red"].b,
                    jsonEntity["Green"].b,
                    jsonEntity["Blue"].b,
                    RayColor.DEFAULT_ALPHA);
                break;

            case "Prism":
                Debug.Log("Instanciating a prism...");
                objectInstance = Instantiate(PrismPrefab, ItemsContainer.transform);
                // Prism prism = objectInstance.GetComponentInChildren<Prism>();
                break;

            case "Filter Mirror":
                Debug.Log("Instanciating a filter mirror...");
                objectInstance = Instantiate(FilterMirrorPrefab, ItemsContainer.transform);
                FilterMirror filterMirror = objectInstance.GetComponentInChildren <FilterMirror>();
                filterMirror.Orientation = (Direction)jsonEntity["Orientation"].i;
                filterMirror.Color       = new RayColor(
                    jsonEntity["Red"].b,
                    jsonEntity["Green"].b,
                    jsonEntity["Blue"].b,
                    RayColor.DEFAULT_ALPHA);
                break;

            case "Light Source":
                Debug.Log("Instanciating a light source...");
                objectInstance = Instantiate(LightSourcePrefab, ItemsContainer.transform);
                Laser laser = objectInstance.GetComponentInChildren <Laser>();
                foreach (var jsonRay in jsonEntity["Rays"].list)
                {
                    RayColor rayColor =
                        new RayColor(jsonRay["Red"].b, jsonRay["Green"].b, jsonRay["Blue"].b,
                                     RayColor.DEFAULT_ALPHA);
                    RaySource raySource =
                        new RaySource((Direction)jsonRay["Direction"].i, jsonRay["Enabled"].b, rayColor);
                    laser.AddSource(raySource);
                }

                break;

            case "Obstacle":
                Debug.Log("Instanciating an obstacle...");
                objectInstance = Instantiate(ObstaclePrefab, ItemsContainer.transform);
                break;

            case "Standard Turret":
                Debug.Log("Instanciating a standard turret...");
                objectInstance = Instantiate(StandardTurretPrefab, ItemsContainer.transform);
                break;

            case "Missile Turret":
                Debug.Log("Instanciating a missile turret...");
                objectInstance = Instantiate(MissileTurretPrefab, ItemsContainer.transform);
                break;

            case "Laser Turret":
                Debug.Log("Instanciating a laser turret...");
                objectInstance = Instantiate(LaserTurretPrefab, ItemsContainer.transform);
                break;

            default:
                Debug.LogError(string.Format("Object of type {0} is not supported.", jsonEntity["Type"].str));
                break;
            }

            if (objectInstance == null)
            {
                continue;
            }

            var objectInstancePosition = new Vector2Int((int)jsonEntity["X"].i, (int)jsonEntity["Y"].i);
            if (!BoardManager.AddItem(objectInstance, objectInstancePosition))
            {
                Debug.LogError(string.Format("Could not instantiate {0} at position {1}.", objectInstance,
                                             objectInstancePosition));
                Destroy(objectInstance);
                continue;
            }

            var dragAndDrop = objectInstance.GetComponentInChildren <DragAndDrop>();
            if (dragAndDrop)
            {
                dragAndDrop.IsDraggable = jsonEntity["Draggable"].b;
            }

            var raySensitive = objectInstance.GetComponentInChildren <RaySensitive>();
            if (raySensitive)
            {
                raySensitive.ColliderEnabled = true;
            }
        }

        if (dataAsJson != null)
        {
            TdManager.SetUpGame(dataAsJson);
        }
    }