//make the button do something public void useButton(Button button) { //change each tile to its new type string action = button.getAction(); if (action == "enemy") { buttonNoise = true; enemies.AddLast(FileIOManager.createEnemyWithParams(button.enemyParams)); Enemy e = enemies.Last.Value; e.spriteTexture = contentHolder.Texture; e.exlcamationPoint = contentHolder.exlcamationPoint; e.lazerBeam = contentHolder.lazerBeam; e.Source = new Rectangle(0, 0, e.spriteTexture.Width, e.spriteTexture.Height); e.origin = new Vector2(e.spriteTexture.Width / 2, e.spriteTexture.Height / 2); } else if (action == "changeTiles") { buttonNoise = true; foreach (Vector3 point in button.getTileArrayForUse()) { layout[(int)point.X, (int)point.Y].assignBooleans((int)point.Z); } } else if (action == "displayMessage") { MoonPrison.displayMessage(button.messages.First.Value); if (button.cycles) { button.messages.AddLast(button.messages.First.Value); } button.messages.RemoveFirst(); } }
public static Level loadLevel(string fileName,ContentManager Content) { Level level = new Level(); level.levelName = fileName.Split("\\".ToCharArray()).Last(); //read in the level layout FileStream file = new FileStream(fileName + ".level", FileMode.OpenOrCreate, FileAccess.Read); StreamReader fileIn = new StreamReader(file); Vector2 endpos = Vector2.Zero; level.levelW = int.Parse(fileIn.ReadLine()); level.levelH = int.Parse(fileIn.ReadLine()); level.layout = new Tile[level.levelW, level.levelH]; for (int y = 0; y < level.levelH; y++) { String line = fileIn.ReadLine(); String[] buffer = line.Split(new char[] { ' ' }); for (int x = 0; x < level.levelW; x++) { level.layout[x, y] = new Tile(int.Parse(buffer[x])); Vector2 position = new Vector2(); position.X = x * Level.tileWidth + Level.tileWidth / 2; position.Y = y * Level.tileHeight + Level.tileHeight / 2; level.layout[x, y].position = position; level.layout[x, y].setWidth(Level.tileWidth); level.layout[x, y].setHeight(Level.tileHeight); level.layout[x, y].updateBound(); } } fileIn.Close(); file.Close(); //read the associated XML file XmlTextReader xml = new XmlTextReader(fileName + ".xml"); while (xml.Read()) { if (xml.Name == "level" && xml.HasAttributes) { int x = int.Parse(xml.GetAttribute("endX")); int y = int.Parse(xml.GetAttribute("endY")); level.layout[x, y].assignBooleans(6); int px = int.Parse(xml.GetAttribute("startX")); int py = int.Parse(xml.GetAttribute("startY")); level.player.position = new Vector2(px * Level.tileWidth + Level.tileWidth / 2, py * Level.tileHeight); } //if the node is a tag if (xml.NodeType == XmlNodeType.Element) { //if the tag is a button if (xml.Name == "button") { Dictionary<string, string> enemyFields = new Dictionary<string, string>(); Dictionary<string, string> buttonFields = new Dictionary<string, string>(); LinkedList<string> messages = new LinkedList<string>(); int buttonX = int.Parse(xml.GetAttribute("x")); int buttonY = int.Parse(xml.GetAttribute("y")); int numUses = int.Parse(xml.GetAttribute("uses")); string[] useActions = new string[numUses]; Vector3[][] targets = new Vector3[numUses][]; xml.MoveToAttribute(0); for (int i = 0; i < xml.AttributeCount; i++) { buttonFields.Add(xml.Name, xml.Value); xml.MoveToNextAttribute(); } //while we haven't gotten to the end of the button xml.Read(); while (xml.Name != "button") { if (xml.NodeType == XmlNodeType.Element && xml.Name.StartsWith("use")) { LinkedList<Vector3> useTargets = new LinkedList<Vector3>(); int number = int.Parse(xml.Name.Substring(3)); string variety = xml.GetAttribute("type"); useActions[number] = variety; xml.Read(); while (!xml.Name.StartsWith("use")) { //read in nodes, which are all targets //each target has a point corresponding to a tile in the layout and a type to change that tile to if (xml.NodeType == XmlNodeType.Element && xml.Name == "target") { int x = int.Parse(xml.GetAttribute("x")); int y = int.Parse(xml.GetAttribute("y")); int type = int.Parse(xml.GetAttribute("type")); useTargets.AddLast(new Vector3(x, y, type)); level.layout[x, y].isChangedbyButton = true; } else if (xml.NodeType == XmlNodeType.Element && xml.Name == "enemy") { xml.MoveToAttribute(0); for (int i = 0; i < xml.AttributeCount; i++) { enemyFields.Add(xml.Name, xml.Value); xml.MoveToNextAttribute(); } } else if (xml.NodeType == XmlNodeType.Element && xml.Name == "display") { messages.AddLast(xml.GetAttribute("message")); } xml.Read(); } targets[number] = useTargets.ToArray(); } xml.Read(); } Button b = new Button(); b.useActions = useActions; b.enemyParams = enemyFields; b.messages = messages; b.tiles = targets.ToArray(); foreach (KeyValuePair<string, string> entry in buttonFields) { if (entry.Key == "uses") { b.totalUses = int.Parse(entry.Value); b.isMultipleUse = (b.totalUses > 1); } else if (entry.Key == "cycles") { b.cycles = Boolean.Parse(entry.Value); } else if (entry.Key == "sensor") { b.isMotionSensor = Boolean.Parse(entry.Value); } } if (b.isMotionSensor) { b.assignBooleans(7); } level.layout[buttonX, buttonY] = b; Vector2 position = new Vector2(); position.X = buttonX * Level.tileWidth + Level.tileWidth/2; position.Y = buttonY * Level.tileHeight+Level.tileHeight/2; level.layout[buttonX, buttonY].position = position; level.layout[buttonX, buttonY].setWidth(Level.tileWidth); level.layout[buttonX, buttonY].setHeight(Level.tileHeight); level.layout[buttonX, buttonY].origin = new Vector2(Level.tileWidth, Level.tileHeight) * .05f; level.layout[buttonX, buttonY].updateBound(); } //the player xml name sets player initial position else if (xml.Name == "player") { int playerX = int.Parse(xml.GetAttribute("x")); int playerY = int.Parse(xml.GetAttribute("y")); level.player.position = new Vector2(playerX * Level.tileWidth, playerY * Level.tileHeight); } //enemy expects an initial position else if (xml.Name == "enemy") { Dictionary<string,string> fields = new Dictionary<string,string>(); xml.MoveToAttribute(0); for (int i = 0; i < xml.AttributeCount; i++) { fields.Add(xml.Name,xml.Value); xml.MoveToNextAttribute(); } level.enemies.AddLast(FileIOManager.createEnemyWithParams(fields)); } } } xml.Close(); return level; }