/// <summary> /// Allows the game to run logic such as updating the world, /// checking for collisions, gathering input, and playing audio. /// </summary> /// <param name="gameTime">Provides a snapshot of timing values.</param> protected override void Update(GameTime gameTime) { MouseState ms = Mouse.GetState(); KeyboardState ks = Keyboard.GetState(); updateScrolling(ms, ks); if (ks.IsKeyDown(Keys.Escape)) { targetMode = false; } //mouseRect.Center = new Point(ms.X,ms.Y); if (ms.RightButton == ButtonState.Pressed && cooldown == 0) { currentTool = (currentTool + 1) % NUM_TILES; cooldown = 10; } if (cooldown != 0) cooldown--; if (ms.LeftButton == ButtonState.Pressed && !dragging) { if (ms.Y < toolbar.position.Y + TOOLBAR_HEIGHT) { currentTool = toolbar.parseClick(ms, currentTool); //If target was selected turn on target mode if (currentTool == TARGET) { targetMode = true; currentButton = null; } } else if (targetMode) { VectorInt click = getTileIndex(ms.X, ms.Y); if (tiles[click.X, click.Y] == BUTTON && currentButton == null) { currentButton = getButtonAt(click); } else if (currentButton != null && currentTool == TARGET && cooldown == 0) { cooldown = 20; if (ks.IsKeyDown(Keys.N)) { currentButton.addUse(); } else { currentButton.nextUse(); } } else if (currentButton != null && currentTool == DELETE) { currentButton.getCurrentUse().deleteTarget(click.X, click.Y); } else if (currentButton != null && currentTool != TARGET) { currentButton.getCurrentUse().addTarget(click.X, click.Y, currentTool); } } else { dragging = true; dragStart.X = ms.X; dragStart.Y = ms.Y; } } if(dragging && ms.LeftButton == ButtonState.Released){ dragging = false; VectorInt click = getTileIndex(ms.X, ms.Y); int x = click.X; int y = click.Y; dragStart = getTileIndex(dragStart.X, dragStart.Y); //So you can drag in any direction if (x < dragStart.X) { int tmp = x; x = dragStart.X; dragStart.X = tmp; } if (y < dragStart.Y) { int tmp = y; y = dragStart.Y; dragStart.Y = tmp; } for (int m = dragStart.X; m <= x; m++) { for (int n = dragStart.Y; n <= y; n++) { if (m >= 0 && m < LEVELW && n >= 0 && n < LEVELH) { if(currentTool < NUM_TILES) tiles[m, n] = currentTool; if (currentTool == DELETE) { deleteButton(m, n); deleteEnemy(m, n); tiles[m, n] = BLANK; } if (currentTool == BUTTON) createButton(m ,n); if (currentTool == ENEMY) enemies.AddLast(new Enemy(m, n, enemies.Count)); if (currentTool == START) { start.X = m; start.Y = n; } else if (currentTool == END) { end.X = m; end.Y = n; } } } } } if (ks.IsKeyDown(Keys.Space) && cooldown == 0) { save(); cooldown = 10; } base.Update(gameTime); }
public Enemy getEnemyAt(VectorInt index) { foreach (Enemy e in enemies) { if (e.x == index.X && e.y == index.Y) { return e; } } return null; }
/// <summary> /// Allows the game to perform any initialization it needs to before starting to run. /// This is where it can query for any required services and load any non-graphic /// related content. Calling base.Initialize will enumerate through any components /// and initialize them as well. /// </summary> protected override void Initialize() { // TODO: Add your initialization logic here graphics.PreferredBackBufferHeight = SCREENH + TOOLBAR_HEIGHT; graphics.PreferredBackBufferWidth = SCREENW; graphics.ApplyChanges(); tileHeight = (SCREENH - TOOLBAR_HEIGHT)/VISIBLE_TILES_Y; tileWidth = SCREENW / VISIBLE_TILES_X; origin = new VectorInt(0,0); dragStart = new VectorInt(0,0); start = new VectorInt(1, 1); end = new VectorInt(2, 2); buttons = new LinkedList<Button>(); enemies = new LinkedList<Enemy>(); //mouseRect = new Rectangle(0, 0, GAMETILESX * tileSize, GAMETILESY * tileSize); //this.IsMouseVisible = true; FileStream file = new FileStream(FILENAME + LEVEL_NUMBER + ".level", FileMode.OpenOrCreate, FileAccess.Read); StreamReader fileIn = new StreamReader(file); //Throws away the dimension values. This could certainly be changed to improve the editor. if (!fileIn.EndOfStream) { LEVELW = int.Parse(fileIn.ReadLine()); LEVELH = int.Parse(fileIn.ReadLine()); } tiles = new int[LEVELW, LEVELH]; if (!fileIn.EndOfStream) { for (int y = 0; y < LEVELH; y++) { String line = fileIn.ReadLine(); String[] buffer = line.Split(new char[] { ' ' }); for (int x = 0; x < LEVELW; x++) { int type = int.Parse(buffer[x]); tiles[x, y] = type; } } XmlTextReader xml = new XmlTextReader(FILENAME + LEVEL_NUMBER + ".xml"); while (xml.Read()) { if (xml.Name == "level" && xml.HasAttributes) { int x = int.Parse(xml.GetAttribute("endX")); int y = int.Parse(xml.GetAttribute("endY")); end = new VectorInt(x, y); x = int.Parse(xml.GetAttribute("startX")); y = int.Parse(xml.GetAttribute("startY")); start = new VectorInt(x, y); } //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>(); 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]; LinkedList<Vector3>[] targets = new LinkedList<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)); } /*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(); } }*/ xml.Read(); } targets[number] = useTargets; } xml.Read(); } Button b = new Button(buttonX, buttonY); //b.useActions = useActions; //b.enemyParams = enemyFields; foreach (LinkedList<Vector3> l in targets) { b.addUse(l); } buttons.AddLast(b); } 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(); } enemies.AddLast(createEnemyWithParams(fields)); } } } xml.Close(); } else { for (int x = 0; x < LEVELW; x++) { for (int y = 0; y < LEVELH; y++) { tiles[x, y] = 0; if (x == 0 || y == 0 || x == LEVELW - 1 || y == LEVELH - 1) tiles[x,y] = 1; } } } fileIn.Close(); file.Close(); base.Initialize(); }
//returns the button with the specified tile index public Button getButtonAt(VectorInt index) { foreach(Button b in buttons){ if(b.x == index.X && b.y == index.Y){ return b; } } return null; }