Beispiel #1
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) // 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);
            }
        }
    }
    private void ReadWorld(MessagePackObjectDictionary world, Transform cameraPivot, VoxelArray voxelArray)
    {
        if (editor && cameraPivot != null && world.ContainsKey(FileKeys.WORLD_CAMERA))
        {
            ReadCamera(world[FileKeys.WORLD_CAMERA].AsDictionary(), cameraPivot);
        }

        var materials = new List <Material>();

        if (world.ContainsKey(FileKeys.WORLD_MATERIALS))
        {
            foreach (var matObj in world[FileKeys.WORLD_MATERIALS].AsList())
            {
                materials.Add(ReadMaterial(matObj.AsDictionary(), false));
            }
        }

        var overlays = new List <Material>();

        if (world.ContainsKey(FileKeys.WORLD_OVERLAYS))
        {
            foreach (var matObj in world[FileKeys.WORLD_OVERLAYS].AsList())
            {
                overlays.Add(ReadMaterial(matObj.AsDictionary(), true));
            }
        }

        var substances = new List <Substance>();

        if (world.ContainsKey(FileKeys.WORLD_SUBSTANCES))
        {
            foreach (var subObj in world[FileKeys.WORLD_SUBSTANCES].AsList())
            {
                Substance s = new Substance();
                ReadEntity(subObj.AsDictionary(), s);
                substances.Add(s);
            }
        }

        if (world.ContainsKey(FileKeys.WORLD_GLOBAL))
        {
            ReadPropertiesObject(world[FileKeys.WORLD_GLOBAL].AsDictionary(), voxelArray.world);
        }

        if (world.ContainsKey(FileKeys.WORLD_VOXELS))
        {
            foreach (var voxelObj in world[FileKeys.WORLD_VOXELS].AsList())
            {
                ReadVoxel(voxelObj, voxelArray, materials, overlays, substances);
            }
        }

        if (world.ContainsKey(FileKeys.WORLD_OBJECTS))
        {
            foreach (var objObj in world[FileKeys.WORLD_OBJECTS].AsList())
            {
                var    objDict  = objObj.AsDictionary();
                string typeName = objDict[FileKeys.PROPOBJ_NAME].AsString();
                var    objType  = GameScripts.FindTypeWithName(GameScripts.objects, typeName);
                if (objType == null)
                {
                    warnings.Add("Unrecognized object type: " + typeName);
                    continue;
                }
                ObjectEntity obj = (ObjectEntity)objType.Create();
                ReadObjectEntity(objDict, 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);
            }
        }
    }
Beispiel #3
0
    private void ReadWorld(MessagePackObjectDictionary world, Transform cameraPivot, VoxelArray voxelArray)
    {
        if (world.ContainsKey(FileKeys.WORLD_TYPE))
        {
            voxelArray.type = (VoxelArray.WorldType)world[FileKeys.WORLD_TYPE].AsInt32();
        }

        if (editor && cameraPivot != null && world.ContainsKey(FileKeys.WORLD_CAMERA))
        {
            ReadCamera(world[FileKeys.WORLD_CAMERA].AsDictionary(), cameraPivot);
        }

        var customMaterialNames = new Dictionary <string, Material>();

        voxelArray.customMaterials = new List <Material>();
        if (world.ContainsKey(FileKeys.WORLD_CUSTOM_MATERIALS))
        {
            foreach (var texObj in world[FileKeys.WORLD_CUSTOM_MATERIALS].AsList())
            {
                voxelArray.customMaterials.Add(
                    ReadCustomTexture(texObj.AsDictionary(), customMaterialNames, false));
            }
        }

        var customOverlayNames = new Dictionary <string, Material>();

        voxelArray.customOverlays = new List <Material>();
        if (world.ContainsKey(FileKeys.WORLD_CUSTOM_OVERLAYS))
        {
            foreach (var texObj in world[FileKeys.WORLD_CUSTOM_OVERLAYS].AsList())
            {
                voxelArray.customOverlays.Add(
                    ReadCustomTexture(texObj.AsDictionary(), customOverlayNames, true));
            }
        }

        var materials = new List <Material>();

        if (world.ContainsKey(FileKeys.WORLD_MATERIALS))
        {
            foreach (var matObj in world[FileKeys.WORLD_MATERIALS].AsList())
            {
                materials.Add(ReadMaterial(matObj.AsDictionary(), false, customMaterialNames));
            }
        }

        var overlays = new List <Material>();

        if (world.ContainsKey(FileKeys.WORLD_OVERLAYS))
        {
            foreach (var matObj in world[FileKeys.WORLD_OVERLAYS].AsList())
            {
                overlays.Add(ReadMaterial(matObj.AsDictionary(), true, customOverlayNames));
            }
        }

        var substances = new List <Substance>();

        if (world.ContainsKey(FileKeys.WORLD_SUBSTANCES))
        {
            foreach (var subObj in world[FileKeys.WORLD_SUBSTANCES].AsList())
            {
                Substance s = new Substance();
                ReadEntity(subObj.AsDictionary(), s);
                substances.Add(s);
            }
        }

        if (world.ContainsKey(FileKeys.WORLD_GLOBAL))
        {
            ReadPropertiesObject(world[FileKeys.WORLD_GLOBAL].AsDictionary(), voxelArray.world);
            // the new skybox shader makes ambient light for this sky a lot brighter
            if (fileWriterVersion <= 10 && RenderSettings.skybox.name == "sky5X3")
            {
                RenderSettings.ambientIntensity *= 0.67f;
            }
        }

        if (world.ContainsKey(FileKeys.WORLD_VOXELS))
        {
            foreach (var voxelObj in world[FileKeys.WORLD_VOXELS].AsList())
            {
                ReadVoxel(voxelObj, voxelArray, materials, overlays, substances);
            }
        }

        if (world.ContainsKey(FileKeys.WORLD_OBJECTS))
        {
            foreach (var objObj in world[FileKeys.WORLD_OBJECTS].AsList())
            {
                var    objDict  = objObj.AsDictionary();
                string typeName = objDict[FileKeys.PROPOBJ_NAME].AsString();
                var    objType  = GameScripts.FindTypeWithName(GameScripts.objects, typeName);
                if (objType == null)
                {
                    warnings.Add("Unrecognized object type: " + typeName);
                    continue;
                }
                ObjectEntity obj = (ObjectEntity)objType.Create();
                ReadObjectEntity(objDict, obj, materials, overlays);
                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);
            }
        }
    }