public static bool LoadFile(string filename, EntityManager entityManager) { bool success = false; try { XmlDocument file = new XmlDocument(); file.Load(filename); XmlNode topNode = file.FirstChild; for (int i = 0; i < topNode.ChildNodes.Count; i += 1) { XmlNode child = topNode.ChildNodes[i]; if (child.Name == "Shared") { LoadShared(topNode.Name, child, entityManager); } else if (child.Name == "Entity") { Entity entity = LoadEntity(child); entityManager.AddEntityToDictionary(topNode.Name, entity); } } } catch (Exception e) { //load failed Debug.WriteLine(e); } return(success); }
private void CreateWall() { Vector3 position = camera.Position; Vector3 direction = camera.Direction.Normalized() * 1; position += direction; Debug.WriteLine("Position: " + position); position.X = (float)Math.Floor(position.X + 0.4f); position.Y = 1f; position.Z = (float)Math.Ceiling(position.Z - 0.4f); Entity newWall = new Entity("Wall_"); newWall.AddComponent(new ComponentTransform(position, new Vector3(1, 1, 1), new Vector3(0, 0, 0))); newWall.AddComponent(new ComponentGeometry("Geometry/CubeGeometry.txt")); newWall.AddComponent(new ComponentTexture("Textures/wall.png")); newWall.AddComponent(new ComponentCollisionBox(new Vector3(-0.5f, -0.5f, -0.5f), new Vector3(0.5f, 0.5f, 0.5f), true)); entityManager.AddEntityToDictionary(name, newWall); }
static void LoadShared(string name, XmlNode node, EntityManager entityManager) { XmlNode sharedComponentsNode = node.FirstChild; List <IComponent> sharedComponents = new List <IComponent>(); //load shared components for use for (int j = 0; j < sharedComponentsNode.ChildNodes.Count; j += 1) { XmlNode sharedComponentNode = sharedComponentsNode.ChildNodes[j]; IComponent newComponent = LoadComponent(sharedComponentNode); if (newComponent != null) { sharedComponents.Add(newComponent); } } //create the new entities for (int i = 1; i < node.ChildNodes.Count; i += 1) //for all the entities { XmlNode entityNode = node.ChildNodes[i]; string id = entityNode.FirstChild.InnerText; Entity newEntity = new Entity(id); //add all of the shared components to the entity BUG: not a new instance of the object, same object reference added to all for (int j = 0; j < sharedComponents.Count; j += 1) { newEntity.AddComponent(sharedComponents[j].Copy()); } //load the unique components and add them to the entity XmlNode componentsNode = entityNode.FirstChild.NextSibling; for (int j = 0; j < componentsNode.ChildNodes.Count; j += 1) { //pointing wrong place XmlNode componentNode = componentsNode.ChildNodes[j]; IComponent newComponent = LoadComponent(componentNode); newEntity.AddComponent(newComponent); } entityManager.AddEntityToDictionary(name, newEntity); } }