Example #1
0
        public ITag ToJson(TagMaster json, object rawBox)
        {
            CollisionBox box = (CollisionBox)rawBox;
            TagCompound  obj = new TagCompound();

            TagList rect = new TagList();

            obj.AddProperty("bounds", rect);
            rect.Add(box.X);
            rect.Add(box.Y);
            rect.Add(box.Width);
            rect.Add(box.Height);
            TagList faces = new TagList();

            obj.AddProperty("faces", faces);
            foreach (CollisionFaceProperties face in box.GetFaceProperties())
            {
                TagCompound faceObj = new TagCompound();
                faces.Add(faceObj);
                faceObj.AddProperty("enabled", face.Enabled);
                faceObj.AddProperty("friction", face.Friction);
                faceObj.AddProperty("snap", face.Snap);
            }
            return(obj);
        }
Example #2
0
        public static void SavePrefab(Entity entity, string name)
        {
            FileInfo file = new FileInfo(Path.Combine(PrefabDirectory, name + ".prefab"));

            try
            {
                TagCompound obj = new TagCompound();
                obj.AddProperty("name", entity.Name);
                obj.AddProperty("active", entity.Active);
                TagCompound componentRoot = new TagCompound();
                obj.AddProperty("components", componentRoot);
                foreach (Component component in entity.Components)
                {
                    componentRoot.AddProperty(component.ComponentName, component);
                }

                using (BinaryWriter writer = new BinaryWriter(new FileStream(file.FullName, FileMode.Create)))
                {
                    Master.Write(obj, writer);
                }
                Refresh();
            }
            catch (FormatException x)
            {
                Console.WriteLine(x.Message);
            }
        }
Example #3
0
        public static Entity CloneEntity(Entity entity)
        {
            TagCompound componentRoot = new TagCompound();

            foreach (Component component in entity.Components)
            {
                componentRoot.AddProperty(component.ComponentName, component);
            }

            componentRoot.Resolve(Master);

            Entity copy = new Entity
            {
                Name   = entity.Name,
                Active = entity.Active
            };

            foreach (KeyValuePair <string, ITag> rawComponent in componentRoot)
            {
                Type      componentType = Component.TypeForIdentifier(rawComponent.Key);
                Component component     = Master.ConvertFromValue(rawComponent.Value, componentType) as Component;
                copy.Components.Add(component);
            }
            return(copy);
        }
Example #4
0
        public ITag ToJson(TagMaster json, object raw)
        {
            TagCompound obj = new TagCompound();

            foreach (PropertyInfo property in raw.GetType().GetProperties())
            {
                PersistentPropertyAttribute attr = property.GetCustomAttribute(typeof(PersistentPropertyAttribute), false) as PersistentPropertyAttribute;
                if (attr == null)
                {
                    continue;
                }
                string name  = attr.KeyName ?? property.Name.ToSnakeCase();
                object value = property.GetValue(raw);
                if (value != null)
                {
                    obj.AddProperty(name, value);
                }
            }

            foreach (FieldInfo field in raw.GetType().GetFields())
            {
                PersistentPropertyAttribute attr = field.GetCustomAttribute(typeof(PersistentPropertyAttribute), false) as PersistentPropertyAttribute;
                if (attr == null)
                {
                    continue;
                }
                string name  = attr.KeyName ?? field.Name.ToSnakeCase();
                object value = field.GetValue(raw);
                if (value != null)
                {
                    obj.AddProperty(name, value);
                }
            }

            return(obj);
        }
Example #5
0
        public ITag ToJson(TagMaster json, object obj)
        {
            bool[,] map = (bool[, ])obj;
            TagCompound wrapper = new TagCompound();
            int         width   = map.GetLength(0);
            int         height  = map.GetLength(1);

            wrapper.AddProperty("width", width);
            wrapper.AddProperty("height", height);
            TagList outer = new TagList();

            wrapper.AddProperty("map", outer);

            for (int i = 0; i < width; i++)
            {
                TagList inner = new TagList();
                outer.Add(inner);
                for (int j = 0; j < height; j++)
                {
                    inner.Add(map[i, j]);
                }
            }
            return(wrapper);
        }
Example #6
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();
        }