/// <summary> /// Assembles a component following the specifications in the blueprint /// </summary> /// <param name="blueprint">A string describing the component in PhantomComponentNotion format</param> /// <returns></returns> public static Component AssembleComponent(PCNComponent blueprint) { if (!components.ContainsKey(blueprint.Name)) { throw new Exception("Trying to create unknown Component of type '" + blueprint.Name + "'"); } Type componentType = components[blueprint.Name]; Component component; //pass all members as parameters List <Type> types = new List <Type>(); List <Object> parameters = new List <object>(); AddParameters(blueprint, types, parameters); if (types.Count > 0) { ConstructorInfo cinfo = componentType.GetConstructor(types.ToArray()); component = (Component)cinfo.Invoke(parameters.ToArray()); } else { component = (Component)Activator.CreateInstance(componentType); } for (int i = 0; i < blueprint.Components.Count; i++) { component.AddComponent(AssembleComponent(blueprint.Components[i])); } return(component); }
/// <summary> /// Build an instance of a blueprint and pass extra information to it through its properties /// </summary> /// <param name="blueprint">The blueprint describing how the instance should be build</param> /// <param name="description">PCN components that represent additional parameters</param> /// <param name="blueprintName">The blueprint name for future reference</param> /// <returns></returns> public static Entity BuildInstance(PCNComponent blueprint, PCNComponent description, string blueprintName) { Entity entity = AssembleEntity(blueprint, blueprintName); for (int i = 0; i < description.Members.Count; i++) { if (description.Members[i].Name == PROPERTY_NAME_POSITION && description.Members[i].Value is Vector2) { entity.Position = (Vector2)description.Members[i].Value; } else if (description.Members[i].Name == PROPERTY_NAME_ORIENTATION && description.Members[i].Value is float) { entity.Orientation = MathHelper.ToRadians((float)description.Members[i].Value); } else if (description.Members[i].Value is int) { entity.Properties.SetInt(description.Members[i].Name, (int)description.Members[i].Value); } else if (description.Members[i].Value is float) { entity.Properties.SetFloat(description.Members[i].Name, (float)description.Members[i].Value); } else { entity.Properties.SetObject(description.Members[i].Name, description.Members[i].Value); } } return(entity); }
public static void OpenEntityList(string listname, string filename) { string data = null; if (System.IO.File.Exists(filename)) { data = System.IO.File.ReadAllText(filename); } if (data != null) { Dictionary <string, PCNComponent> entities = new Dictionary <string, PCNComponent>(); string[] lines = data.Split('\n'); for (int i = 0; i < lines.Length; i++) { int p = lines[i].IndexOf(' '); if (p > 0) { string name = lines[i].Substring(0, p); entities[name] = new PCNComponent(lines[i].Substring(p + 1)); } } EntityLists[listname] = entities; } else { Trace.WriteLine("WARNING List not found! " + filename); } }
private static void AddTile(int x, int y, int tileSize, PCNComponent blueprint, EntityLayer entities, string blueprintName) { Vector2 position = new Vector2((x + 0.5f) * tileSize, (y + 0.5f) * tileSize); Entity entity = EntityFactory.AssembleEntity(blueprint, blueprintName); entity.Position = position; entity.Properties.Ints["isTile"] = 1; entities.AddComponent(entity); }
/// <summary> /// Assembles an entity following the specifications in the blueprint /// </summary> /// <param name="blueprint">A string describing the entity in PhantomComponentNotion format</param> /// <returns></returns> static public Entity AssembleEntity(PCNComponent blueprint, string blueprintName) { Entity entity = (Entity)AssembleComponent(blueprint); if (blueprintName != null) { entity.Properties.SetString(PROPERTY_NAME_BLUEPRINT, blueprintName); } return(entity); }
private static void AddParameters(PCNComponent component, List <Type> types, List <object> parameters) { for (int i = 0; i < component.Members.Count; i++) { if (component.Members[i].Value != null) { types.Add(component.Members[i].Value.GetType()); parameters.Add(component.Members[i].Value); } } }
private static void ParseEntity(string description, EntityLayer entities) { string listName = entities.Properties.GetString("entityList", ""); PCNComponent instance = new PCNComponent(description); PCNComponent blueprint = EntityLists[listName][instance.Name]; Entity entity = EntityFactory.BuildInstance(blueprint, instance, instance.Name); if (entity.Properties.GetBoolean("use_blueprint", false)) { entity.Properties.SetBoolean("use_blueprint", false); } entities.AddComponent(entity); }
private static void ParseData(string data) { string[] lines = data.Split(lineSplit, StringSplitOptions.RemoveEmptyEntries); ClearState(); int i = 0; GameState state = GetState(); EntityLayer entities = null; while (i < lines.Length) { if (lines[i].StartsWith("tiles ") && entities != null) { ParseTiles(lines, ref i, entities); } else if (lines[i].StartsWith("entity ") && entities != null) { ParseEntity(lines[i].Substring(7), entities); i++; } else if (lines[i].StartsWith("component ")) { PCNComponent component = new PCNComponent(lines[i].Substring(10)); for (int j = 0; j < state.Components.Count; j++) { if (state.Components[j].Properties != null && state.Components[j].Properties.GetString("editable", "") == component.Name) { ParseProperties(state.Components[j], component, 0); if (state.Components[j] is EntityLayer) { entities = state.Components[j] as EntityLayer; } } } i++; } else { if (lines[i].Length > 0) { Trace.WriteLine("WARNING: MapLoader did not understand: " + lines[i]); } i++; } } }
private void ChangeLayer() { hoveringEntity = null; selectedEntity = null; drawingType = null; drawingEntity = null; if (currentLayer < 0) { return; } if (layers[currentLayer].Type == LayerType.Tiles) { tilesX = (int)Math.Ceiling(((EntityLayer)layers[currentLayer].Layer).Bounds.X / layers[currentLayer].TileSize); tilesY = (int)Math.Ceiling(((EntityLayer)layers[currentLayer].Layer).Bounds.Y / layers[currentLayer].TileSize); tileMap = MapLoader.ConstructTileMap((EntityLayer)layers[currentLayer].Layer); } }
private void SelectEntity(UIElement sender) { Button button = sender as Button; if (layers[currentLayer].EntityList != null) { drawingType = null; drawingEntity = null; if (layers[currentLayer].EntityList.ContainsKey(button.Name)) { drawingType = layers[currentLayer].EntityList[button.Name]; drawingEntity = EntityFactory.AssembleEntity(drawingType, button.Name); drawingEntity.Position = mousePosition; } } button.GetAncestor <Window>().Hide(); main.Hide(); }
private void MouseRightDownTiles() { EntityLayer entities = layers[currentLayer].Layer as EntityLayer; if (entities != null) { int x = (int)Math.Floor(mousePosition.X / layers[currentLayer].TileSize); int y = (int)Math.Floor(mousePosition.Y / layers[currentLayer].TileSize); int index = x + y * tilesX; Entity entity = tileMap[index]; if (entity != null) { string blueprintName = entity.Properties.GetString(EntityFactory.PROPERTY_NAME_BLUEPRINT, ""); drawingType = layers[currentLayer].EntityList[blueprintName]; drawingEntity = EntityFactory.AssembleEntity(drawingType, blueprintName); drawingEntity.Position = mousePosition; return; } drawingEntity = null; drawingType = null; } }
private static void ParseProperties(Component component, PCNComponent description, int firstValue) { for (int i = firstValue; i < description.Members.Count; i++) { if (description.Members[i].Value is int) { component.Properties.SetInt(description.Members[i].Name, (int)description.Members[i].Value); } else if (description.Members[i].Value is float) { component.Properties.SetFloat(description.Members[i].Name, (float)description.Members[i].Value); } else { component.Properties.SetObject(description.Members[i].Name, description.Members[i].Value); } } if (description.Members.Count >= firstValue) { component.HandleMessage(Messages.PropertiesChanged, null); } }
private static void ParseTiles(string[] lines, ref int index, EntityLayer entities) { string tileListName = entities.Properties.GetString("tileList", ""); Dictionary <string, PCNComponent> tileList = EntityLists[tileListName]; PCNComponent tileDefs = new PCNComponent(lines[index]); index++; int tileSize = entities.Properties.GetInt("tileSize", 0); if (tileSize <= 0) { return; } int width = (int)Math.Ceiling(entities.Bounds.X / tileSize); int height = (int)Math.Ceiling(entities.Bounds.Y / tileSize); for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { while (lines[index].Length == 0) { index++; } string tile = lines[index].Substring(x * 4, 3); int t = -1; if (int.TryParse(tile, out t) && t >= 0 && t < tileDefs.Components.Count) { AddTile(x, y, tileSize, tileList[tileDefs.Components[t].Name], entities, tileDefs.Components[t].Name); } } index++; } }
private void MouseLeftDownEntities() { if (hoveringEntity != null) //Selecting { mouseOffset = hoveringEntity.Position - mousePosition; selectedEntity = hoveringEntity; string blueprintName = selectedEntity.Properties.GetString(EntityFactory.PROPERTY_NAME_BLUEPRINT, ""); drawingType = layers[currentLayer].EntityList[blueprintName]; drawingEntity = EntityFactory.AssembleEntity(drawingType, blueprintName); drawingEntity.Position = mousePosition; CopyProperties(selectedEntity, drawingEntity); //Randomize the seed if there is any if (drawingEntity.Properties.GetInt("Seed", -1) > 0) { drawingEntity.Properties.Ints["Seed"] = PhantomGame.Randy.Next(int.MaxValue); drawingEntity.HandleMessage(Messages.PropertiesChanged, null); } } else if (drawingType != null) //Drawing { EntityLayer entities = layers[currentLayer].Layer as EntityLayer; string blueprintName = drawingEntity.Properties.GetString(EntityFactory.PROPERTY_NAME_BLUEPRINT, ""); selectedEntity = EntityFactory.AssembleEntity(drawingType, blueprintName); selectedEntity.Position = mousePosition; entities.AddComponent(selectedEntity); hoveringEntity = selectedEntity; CopyProperties(drawingEntity, selectedEntity); mouseOffset *= 0; //Randomize the seed if there is any if (drawingEntity.Properties.GetInt("Seed", -1) > 0) { drawingEntity.Properties.Ints["Seed"] = PhantomGame.Randy.Next(int.MaxValue); drawingEntity.HandleMessage(Messages.PropertiesChanged, null); } } }