示例#1
0
    private IEnumerator LoadCoroutine()
    {
        yield return(null);

        try
        {
            ReadWorldFile.Read(SelectedWorld.GetLoadStream(),
                               null, GetComponent <VoxelArray>(), false);
        }
        catch (MapReadException e)
        {
            var dialog = loadingGUI.gameObject.AddComponent <DialogGUI>();
            dialog.message          = e.FullMessage;
            dialog.yesButtonText    = "Close";
            dialog.yesButtonHandler = () =>
            {
                Close(Scenes.MENU);
            };
            Debug.LogError(e.InnerException);
            yield break;
        }
        finally
        {
            Destroy(loadingGUI);
        }
    }
示例#2
0
    private IEnumerator LoadWorldCoroutine(string path = null, System.IO.Stream stream = null)
    {
        loadingWorld = true;
        errorMessage = null;
        yield return(null);

        yield return(null);

        try
        {
            if (stream != null)
            {
                dataList = ReadWorldFile.ReadEmbeddedData(stream, type);
            }
            else
            {
                dataList = ReadWorldFile.ReadEmbeddedData(path, type);
            }
        }
        catch (MapReadException e)
        {
            errorMessage = e.Message;
            Debug.LogError(e.InnerException);
        }
        finally
        {
            loadingWorld = false;
            if (stream != null)
            {
                stream.Close();
                ShareMap.ClearFileWaitingToImport();
            }
        }
    }
示例#3
0
    private Material ReadMaterial(JSONObject matObject)
    {
        string name;
        Color  color    = Color.white;
        bool   setColor = false;

        if (matObject["name"] != null)
        {
            name = matObject["name"];
        }
        else if (matObject["mode"] != null)
        {
            name = matObject["mode"];
            if (matObject["color"] != null)
            {
                color    = ReadColor(matObject["color"].AsArray);
                setColor = true;
                bool overlay = color.a != 1;
                if (matObject["alpha"] != null)
                {
                    overlay = matObject["alpha"].AsBool; // new with version 4
                }
                if (overlay)
                {
                    name += "_overlay";
                }
            }
        }
        else
        {
            warnings.Add("Error reading material");
            return(ReadWorldFile.MissingMaterial(false));
        }

        Material mat = ResourcesDirectory.FindMaterial(name, editor);

        if (mat == null)
        {
            warnings.Add("Unrecognized material: " + name);
            return(ReadWorldFile.MissingMaterial(false));
        }
        if (setColor)
        {
            string colorProp = ResourcesDirectory.MaterialColorProperty(mat);
            if (colorProp != null)
            {
                mat = ResourcesDirectory.InstantiateMaterial(mat);
                mat.SetColor(colorProp, color);
            }
        }
        return(mat);
    }
示例#4
0
    private Material ReadCustomTexture(MessagePackObjectDictionary texDict,
                                       Dictionary <string, Material> customTextureNames, bool overlay)
    {
        CustomTexture customTex = new CustomTexture(
            new Material(ReadWorldFile.MissingMaterial(overlay).shader), overlay);

        ReadPropertiesObject(texDict, customTex);
        Material mat = customTex.material;

        if (texDict.ContainsKey(FileKeys.CUSTOM_MATERIAL_NAME))
        {
            mat.name = texDict[FileKeys.CUSTOM_MATERIAL_NAME].AsString();
            customTextureNames[mat.name] = mat;
        }
        return(mat);
    }
示例#5
0
    public List <string> BuildWorld(Transform cameraPivot, VoxelArray voxelArray, bool editor)
    {
        var warnings = ReadWorldFile.Read(Resources.Load <TextAsset>("Templates/indoor"),
                                          cameraPivot, voxelArray, editor);

        foreach (var obj in voxelArray.IterateObjects())
        {
            if (obj is PlayerObject)
            {
                var behavior = new SoundBehavior();
                behavior.SetProperty("dat", data);
                obj.behaviors.Add(behavior);
                break;
            }
        }
        return(warnings);
    }
示例#6
0
    private IEnumerator LoadCoroutine()
    {
        yield return(null);

        var guiGameObject = loadingGUI.gameObject;

        List <string> warnings;

        try
        {
            warnings = ReadWorldFile.Read(SelectedWorld.GetLoadStream(),
                                          cameraPivot, voxelArray, true);
        }
        catch (MapReadException e)
        {
            var dialog = guiGameObject.AddComponent <DialogGUI>();
            dialog.message          = e.Message;
            dialog.yesButtonText    = "Close";
            dialog.yesButtonHandler = () =>
            {
                voxelArray.unsavedChanges = false;
                Close();
            };
            Destroy(loadingGUI);
            Debug.LogError(e);
            yield break;
        }
        // reading the file creates new voxels which sets the unsavedChanges flag
        // and clears existing voxels which sets the selectionChanged flag
        voxelArray.unsavedChanges   = false;
        voxelArray.selectionChanged = false;

        Destroy(loadingGUI);
        foreach (MonoBehaviour b in enableOnLoad)
        {
            b.enabled = true;
        }

        if (PlayerPrefs.HasKey("last_editScene_version"))
        {
            string lastVersion = PlayerPrefs.GetString("last_editScene_version");
            if (CompareVersions(lastVersion, "1.2.0") == -1)
            {
                var dialog = guiGameObject.AddComponent <DialogGUI>();
                dialog.message          = "N-Space has been updated with the ability to bevel edges! Would you like a tutorial?";
                dialog.yesButtonText    = "Yes";
                dialog.noButtonText     = "No";
                dialog.yesButtonHandler = () =>
                {
                    TutorialGUI.StartTutorial(Tutorials.BEVEL_TUTORIAL, guiGameObject, voxelArray, touchListener);
                };
            }
            else if (CompareVersions(lastVersion, "1.3.0") == -1)
            {
                LargeMessageGUI.ShowLargeMessageDialog(guiGameObject, "<b>Version 1.3.0 update</b>\n\n"
                                                       + "N-Space has been updated with a new behavior for sound effects and music. Try it out!\n\n"
                                                       + "Also, check the main menu for links to video tutorials and a subreddit.");
            }
        }
        PlayerPrefs.SetString("last_editScene_version", Application.version);

        if (warnings.Count > 0)
        {
            // avoids a bug where two dialogs created on the same frame will put the unfocused one on top
            // for some reason it's necessary to wait two frames
            yield return(null);

            yield return(null);

            string message = "There were some issues with reading the world:\n\n  •  " +
                             string.Join("\n  •  ", warnings.ToArray());
            LargeMessageGUI.ShowLargeMessageDialog(guiGameObject, message);
        }
    }
示例#7
0
    private Material ReadMaterial(MessagePackObjectDictionary matDict, bool forceOverlay,
                                  Dictionary <string, Material> customTextureNames)
    {
        string name;

        if (matDict.ContainsKey(FileKeys.MATERIAL_NAME))
        {
            name = matDict[FileKeys.MATERIAL_NAME].AsString();
        }
        else if (matDict.ContainsKey(FileKeys.MATERIAL_MODE))  // version 9 and earlier
        {
            name = matDict[FileKeys.MATERIAL_MODE].AsString();
            // ignore MATERIAL_ALPHA key, it's usually wrong
            if (matDict.ContainsKey(FileKeys.MATERIAL_COLOR))
            {
                if (ReadColor(matDict[FileKeys.MATERIAL_COLOR]).a < 1)
                {
                    forceOverlay = true;
                }
            }
            if (forceOverlay)
            {
                name += "_overlay";
            }
        }
        else
        {
            warnings.Add("Error reading material");
            return(ReadWorldFile.MissingMaterial(forceOverlay));
        }

        Material mat;
        bool     isCustom = customTextureNames != null && customTextureNames.ContainsKey(name);

        if (isCustom)
        {
            mat = customTextureNames[name];
        }
        else
        {
            mat = ResourcesDirectory.FindMaterial(name, editor);
        }
        if (mat == null)
        {
            warnings.Add("Unrecognized material: " + name);
            return(ReadWorldFile.MissingMaterial(forceOverlay));
        }
        if (!isCustom && matDict.ContainsKey(FileKeys.MATERIAL_COLOR))
        {
            // custom textures can't have colors
            string colorProp = ResourcesDirectory.MaterialColorProperty(mat);
            if (colorProp != null)
            {
                Color color    = ReadColor(matDict[FileKeys.MATERIAL_COLOR]);
                bool  setColor = color != mat.GetColor(colorProp);

                var colorStyle = ResourcesDirectory.ColorStyle.TINT;
                if (matDict.ContainsKey(FileKeys.MATERIAL_COLOR_STYLE))
                {
                    Enum.TryParse(matDict[FileKeys.MATERIAL_COLOR_STYLE].AsString(), out colorStyle);
                }
                bool setStyle = colorStyle == ResourcesDirectory.ColorStyle.PAINT &&
                                ResourcesDirectory.GetMaterialColorStyle(mat) != ResourcesDirectory.ColorStyle.PAINT;

                if (setColor || setStyle)
                {
                    mat = ResourcesDirectory.InstantiateMaterial(mat);
                }
                if (setColor)
                {
                    mat.SetColor(colorProp, color);
                }
                if (setStyle)
                {
                    ResourcesDirectory.SetMaterialColorStyle(mat, colorStyle);
                }
            }
        }
        return(mat);
    }
示例#8
0
    private void ReadWorld(JSONObject world, VoxelArray voxelArray)
    {
        var materials = new List <Material>();

        if (world["materials"] != null)
        {
            foreach (JSONNode matNode in world["materials"].AsArray)
            {
                JSONObject matObject = matNode.AsObject;
                materials.Add(ReadMaterial(matObject));
            }
        }

        var substances = new List <Substance>();

        if (world["substances"] != null)
        {
            foreach (JSONNode subNode in world["substances"].AsArray)
            {
                Substance s = new Substance();
                ReadEntity(subNode.AsObject, s);
                substances.Add(s);
            }
        }

        if (world["global"] != null)
        {
            ReadPropertiesObject(world["global"].AsObject, voxelArray.world);
        }
        if (fileWriterVersion <= 2 && world["sky"] != null)
        {
            Material sky = materials[world["sky"].AsInt];
            if (sky != ReadWorldFile.MissingMaterial(false)) // default skybox is null
            {
                voxelArray.world.SetSky(sky);
            }
        }
        if (world["map"] != null)
        {
            ReadMap(world["map"].AsObject, voxelArray, materials, substances);
        }
        if (fileWriterVersion <= 2 && world["player"] != null)
        {
            PlayerObject player = new PlayerObject();
            ReadObjectEntity(world["player"].AsObject, player);
            voxelArray.AddObject(player);
        }
        if (world["objects"] != null)
        {
            foreach (JSONNode objNode in world["objects"].AsArray)
            {
                JSONObject objObject = objNode.AsObject;
                string     typeName  = objObject["name"];
                var        objType   = GameScripts.FindTypeWithName(GameScripts.objects, typeName);
                if (objType == null)
                {
                    warnings.Add("Unrecognized object type: " + typeName);
                    continue;
                }
                ObjectEntity obj = (ObjectEntity)objType.Create();
                ReadObjectEntity(objObject, obj);
                voxelArray.AddObject(obj);
            }
        }

        if (!editor)
        {
            // start the game
            foreach (Substance s in substances)
            {
                s.InitEntityGameObject(voxelArray);
            }
            foreach (ObjectEntity obj in voxelArray.IterateObjects())
            {
                obj.InitEntityGameObject(voxelArray);
            }
        }
        else // editor
        {
            foreach (ObjectEntity obj in voxelArray.IterateObjects())
            {
                obj.InitObjectMarker((VoxelArrayEditor)voxelArray);
            }
        }
    }