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); }
public K FromJson <K>(TagMaster json, ITag value) { TagList arr = value as TagList; List <T> list = new List <T>(); foreach (ITag element in arr) { list.Add(json.ConvertFromValue <T>(element)); } if (typeof(K).IsArray) { return((K)Convert.ChangeType(list.ToArray(), typeof(K))); } else { return((K)Convert.ChangeType(list, typeof(K))); } }
public T Get <T>(TagMaster json, string key) { if (!Properties.ContainsKey(key)) { return(default(T)); } ITag property = Properties[key]; if (property is T) { return((T)property); } else if (json != null) { return(json.ConvertFromValue <T>(property)); } else { return(default(T)); } }
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); }