Esempio n. 1
0
        public void Save()
        {
            TagMaster tagMaster = new TagMaster();

            tagMaster.RegisterConverter(new NumberConverter <byte>());
            tagMaster.RegisterConverter(new NumberConverter <short>());
            tagMaster.RegisterConverter(new NumberConverter <int>());
            tagMaster.RegisterConverter(new NumberConverter <float>());
            tagMaster.RegisterConverter(new NumberConverter <long>());
            tagMaster.RegisterConverter(new NumberConverter <double>());
            tagMaster.RegisterConverter(new StringConverter());

            tagMaster.RegisterConverter(new ListConverter <long>());
            tagMaster.RegisterConverter(new ListConverter <int>());
            foreach (ITagConverter converter in Converters)
            {
                tagMaster.RegisterConverter(converter);
            }

            TagCompound root = new TagCompound();

            TagCompound sceneRoot = new TagCompound();

            root.AddProperty("scene", sceneRoot);

            sceneRoot.AddProperty("name", Scene.Name);

            sceneRoot.AddProperty("viewport", Scene.CurrentViewport);

            {
                TagList entities = new TagList();
                sceneRoot.AddProperty("entities", entities);
                foreach (Entity entity in Scene.Entities)
                {
                    TagCompound entityObj = new TagCompound();
                    entities.Add(entityObj);
                    entityObj.AddProperty("name", entity.Name);
                    entityObj.AddProperty("id", entity.Id);
                    entityObj.AddProperty("active", entity.Active);
                    TagCompound components = new TagCompound();
                    entityObj.AddProperty("components", components);
                    foreach (Component component in entity.Components)
                    {
                        components.AddProperty(component.ComponentName, component);
                    }
                }
            }

            {
                TagList systems = new TagList();
                sceneRoot.AddProperty("systems", systems);
                foreach (ComponentSystem system in Scene.Systems)
                {
                    if (system.ShouldSave)
                    {
                        systems.Add(system.SystemName);
                    }
                }
            }

            BinaryWriter writer = new BinaryWriter(new FileStream(Path, FileMode.Create));

            tagMaster.Write(root, writer);
            writer.Close();
            writer.Dispose();
        }
Esempio n. 2
0
        static TagIOUtils()
        {
            Master.RegisterConverter(new ListConverter <long>());
            Master.RegisterConverter(new ListConverter <int>());

            Master.RegisterConverter(new NumberConverter <byte>());
            Master.RegisterConverter(new NumberConverter <short>());
            Master.RegisterConverter(new NumberConverter <int>());
            Master.RegisterConverter(new NumberConverter <float>());
            Master.RegisterConverter(new NumberConverter <long>());
            Master.RegisterConverter(new NumberConverter <double>());

            Master.RegisterConverter(new StringConverter());
            Master.RegisterConverter(new BooleanConverter());

            Master.RegisterConverter(new CollisionBoxConverter());
            Master.RegisterConverter(new ColorConverter());
            Master.RegisterConverter(new ListConverter <CollisionBox>());
            Master.RegisterConverter(new ListConverter <Sound>());
            Master.RegisterConverter(new ListConverter <Sprite>());
            Master.RegisterConverter(new ListConverter <AnimatedSprite>());
            Master.RegisterConverter(new EnumConverter <DrawMode>());
            Master.RegisterConverter(new EnumConverter <DamageFilter>());
            Master.RegisterConverter(new BoolMapConverter());
        }
Esempio n. 3
0
        public Scene Load()
        {
            if (!File.Exists(Path))
            {
                Console.WriteLine("Tried to load scene at '" + Path + "', does not exist");
                return(null);
            }
            TagMaster tagMaster = new TagMaster();

            tagMaster.RegisterConverter(new ListConverter <long>());
            tagMaster.RegisterConverter(new ListConverter <int>());

            tagMaster.RegisterConverter(new NumberConverter <byte>());
            tagMaster.RegisterConverter(new NumberConverter <short>());
            tagMaster.RegisterConverter(new NumberConverter <int>());
            tagMaster.RegisterConverter(new NumberConverter <float>());
            tagMaster.RegisterConverter(new NumberConverter <long>());
            tagMaster.RegisterConverter(new NumberConverter <double>());

            tagMaster.RegisterConverter(new StringConverter());
            tagMaster.RegisterConverter(new BooleanConverter());
            foreach (ITagConverter converter in Converters)
            {
                tagMaster.RegisterConverter(converter);
            }

            BinaryReader reader = new BinaryReader(new FileStream(Path, FileMode.Open));

            TagCompound root = tagMaster.Read(reader);

            reader.Close();
            reader.Dispose();

            Scene scene = new Scene(Controller);

            TagCompound sceneRoot = root.Get <TagCompound>("scene");

            scene.Name = sceneRoot.Get <string>(tagMaster, "name") ?? "";

            scene.CurrentViewport = sceneRoot.Get <CameraView>(tagMaster, "viewport");

            foreach (ITag rawEntity in sceneRoot.Get <TagList>("entities"))
            {
                TagCompound obj    = rawEntity as TagCompound;
                Entity      entity = new Entity
                {
                    _id        = obj.Get <long>(tagMaster, "id"),
                    _is_id_set = true,
                    Name       = obj.Get <TagString>("name").Value,
                    Active     = obj.Get <TagBoolean>("active").Value
                };

                foreach (KeyValuePair <string, ITag> rawComponent in obj.Get <TagCompound>("components"))
                {
                    Type      componentType = Component.TypeForIdentifier(rawComponent.Key);
                    Component component     = tagMaster.ConvertFromValue(rawComponent.Value, componentType) as Component;
                    entity.Components.Add(component);
                }
                scene.Entities.Add(entity);
            }

            foreach (ITag rawName in sceneRoot.Get <TagList>("systems"))
            {
                TagString       name   = rawName as TagString;
                ComponentSystem system = ComponentSystem.TypeForIdentifier(name.Value).GetConstructor(new Type[0]).Invoke(new object[0]) as ComponentSystem;
                scene.Systems.Add(system);
            }

            return(scene);
        }