public void render() { TileFactory tf = TileFactory.Instance; Bitmap pg = new Bitmap(800, 600); Graphics gr = Graphics.FromImage(pg); ZRTSModel.Map map = gameworld.GetMap(); // TODO: Change to include the scrolling model. for (int x = 0; x < map.GetWidth(); x++) { for (int y = 0; y < map.GetHeight(); y++) { ZRTSModel.Tile tile = map.GetCellAt(x, y).GetTile(); if (tile != null) { gr.DrawImage(tf.getBitmapImproved(tile), x * 16, y * 16, 16, 16); } //if (map.cells[x, y] != null && map.cells[x, y].tile != null && map.cells[x, y].tile.tileType != null) //gr.DrawImage(tf.getBitmap(map.cells[x, y].tile.tileType), x * 16, y * 16, 16, 16); else { gr.DrawRectangle(new Pen(Color.Black), x * 16, y * 16, 16, 16); } } } pictureBox1.Image = pg; }
/// <summary> /// Initialize necessary components /// </summary> private void initialize() { game = new XnaUITestGame(); ZRTSController controller = new ZRTSController(game); game.Components.Add(controller); model = new GameModel(); ScenarioComponent scenario = new ScenarioComponent(50, 50); player1 = new PlayerComponent(); player2 = new PlayerComponent(); player1.Name = "Nate"; player2.Name = "Smith"; // Add sand cells at each cell. ZRTSModel.Map map = scenario.GetGameWorld().GetMap(); for (int i = 0; i < map.GetWidth(); i++) { for (int j = 0; j < map.GetHeight(); j++) { CellComponent cell = new CellComponent(); cell.AddChild(new Sand()); cell.X = i; cell.Y = j; map.AddChild(cell); } } model.AddChild(scenario); game.Model = model; game.Model.PlayerInContext = player1; // Set a main Player //Create two players and set them to be enemies. game.Model.GetScenario().GetGameWorld().GetPlayerList().AddChild(player1); game.Model.GetScenario().GetGameWorld().GetPlayerList().AddChild(player2); // Set target enemy player1.EnemyList.Add(player2); player2.EnemyList.Add(player1); game.Controller = controller; // Add worker to player unitList = new List <ModelComponent>(); unitList.Add(new UnitComponent()); ((UnitComponent)unitList[0]).CanBuild = true; ((UnitComponent)unitList[0]).Type = "worker"; ((UnitComponent)unitList[0]).PointLocation = new PointF(20, 20); //(1) Get Player#1 currentPlayer1 = (PlayerComponent)((XnaUITestGame)game).Model.GetScenario().GetGameWorld().GetPlayerList().GetChildren()[0]; // check if fetch the correct player Assert.AreEqual("Nate", currentPlayer1.Name); // (2) Add the actual player's unit list currentPlayer1.GetUnitList().AddChild(unitList[0]); }
private void setupModel() { model = new GameModel(); ScenarioComponent scenario = new ScenarioComponent(20, 20); // 20 x 20 Gameworld. // Add grass cells at each cell. ZRTSModel.Map map = scenario.GetGameWorld().GetMap(); for (int i = 0; i < map.GetWidth(); i++) { for (int j = 0; j < map.GetHeight(); j++) { CellComponent cell = new CellComponent(); cell.AddChild(new Sand()); cell.X = i; cell.Y = j; map.AddChild(cell); } } model.AddChild(scenario); //Create two players and set them to be enemies. model.GetScenario().GetGameWorld().GetPlayerList().AddChild(new PlayerComponent()); model.GetScenario().GetGameWorld().GetPlayerList().AddChild(new PlayerComponent()); PlayerComponent player1 = (PlayerComponent)model.GetScenario().GetGameWorld().GetPlayerList().GetChildren()[0]; PlayerComponent player2 = (PlayerComponent)model.GetScenario().GetGameWorld().GetPlayerList().GetChildren()[1]; player1.EnemyList.Add(player2); player2.EnemyList.Add(player1); }
/// <summary> /// Creates a CreateNewScenario dialog and uses it to determine the name and size of the new scenario. Then, generates a /// new scenario of the appropriate size. /// </summary> public void createNewScenario() { if (model.GetScenario() != null) { model.GetScenario().RemoveChild(model.GetScenario().GetGameWorld()); // TODO: Ask if the user wants to discard the current scenario or save it. } CreateNewScenarioDialog dialog = new CreateNewScenarioDialog(); dialog.ShowDialog(); if (dialog.ExitWithCreate) { // Create a scenario with a map of the appropriate size ScenarioComponent scenario = new ScenarioComponent(dialog.ScenarioWidth, dialog.ScenarioHeight); // Add grass cells at each cell. ZRTSModel.Map map = scenario.GetGameWorld().GetMap(); for (int i = 0; i < map.GetWidth(); i++) { for (int j = 0; j < map.GetHeight(); j++) { CellComponent cell = new CellComponent(); cell.AddChild(new Grass()); cell.X = i; cell.Y = j; map.AddChild(cell); } } // TODO: Update SaveInfo model to change filename and UpToDate flag. // Automatically discards old scenario, by overloaded AddChild function. model.AddChild(scenario); // Empty the command queue model.GetCommandStack().EmptyStacks(); // We may have just destroyed a large scenario, so collect that garbage. // Commented out - only used for testing purposes. The C# garbage collector takes a LONG time to be activated if this call is not made, // but if the call is made, it disrupts UI. // GC.Collect(); } }
protected override void onDraw(XnaDrawArgs e) { // Determine all of the cells in view ZRTSModel.Map map = ((XnaUITestGame)Game).Model.GetScenario().GetGameWorld().GetMap(); Point upperLeftCell = new Point(ScrollX / CellDimension, ScrollY / CellDimension); Point lowerRightCell = new Point(Math.Min((ScrollX + DrawBox.Width) / CellDimension, map.GetWidth() - 1), Math.Min((ScrollY + DrawBox.Height) / CellDimension, map.GetHeight() - 1)); Point offset = new Point(ScrollX % CellDimension, ScrollY % CellDimension); // Draw all of the tiles for (int x = upperLeftCell.X; x <= lowerRightCell.X; x++) { for (int y = upperLeftCell.Y; y <= lowerRightCell.Y; y++) { CellComponent cell = map.GetCellAt(x, y); Tile tile = cell.GetTile(); DrawTileVisitor drawer = new DrawTileVisitor(e.SpriteBatch, ((XnaUITestGame)Game).SpriteSheet, new Rectangle((x - upperLeftCell.X) * CellDimension - offset.X, (y - upperLeftCell.Y) * CellDimension - offset.Y, CellDimension, CellDimension)); tile.Accept(drawer); } } }
private void setUpModel() { model = new GameModel(); ScenarioComponent scenario = new ScenarioComponent(20, 20); // 20 x 20 Gameworld. Building obstruction = new Building(); // Add grass cells at each cell. ZRTSModel.Map map = scenario.GetGameWorld().GetMap(); for (int i = 0; i < map.GetWidth(); i++) { for (int j = 0; j < map.GetHeight(); j++) { CellComponent cell = new CellComponent(); cell.AddChild(new Sand()); cell.X = i; cell.Y = j; if (i >= 2 && i <= 10 && j >= 2 && j <= 10) { cell.AddEntity(obstruction); } if (i >= 15 && i <= 18 && j >= 15 && j <= 18) { cell.AddEntity(obstruction); } if (i == 16 && j == 16) { cell.RemoveEntity(obstruction); } map.AddChild(cell); } } model.AddChild(scenario); }
public virtual void Visit(Map map) { Visit((ModelComponent)map); }
public ScenarioComponent GenerateScenarioFromXML() { ScenarioComponent scenario = null; if (reader.Read()) { // Go to the Scenario (skip the XML line) reader.Read(); scenario = new ScenarioComponent(); ModelComponent currentComponent = scenario; while (reader.Read()) { switch (reader.NodeType) { case XmlNodeType.Element: switch (reader.Name) { case "Gameworld": Gameworld gameworld = new Gameworld(); currentComponent.AddChild(gameworld); if (!reader.IsEmptyElement) currentComponent = gameworld; break; case "Map": int width = Int32.Parse(reader.GetAttribute("Width")); int height = Int32.Parse(reader.GetAttribute("Height")); Map map = new Map(width, height); currentComponent.AddChild(map); if (!reader.IsEmptyElement) currentComponent = map; break; case "Cell": int x = Int32.Parse(reader.GetAttribute("X")); int y = Int32.Parse(reader.GetAttribute("Y")); CellComponent cell = new CellComponent(); cell.X = x; cell.Y = y; currentComponent.AddChild(cell); if (!reader.IsEmptyElement) currentComponent = cell; break; case "PlayerList": PlayerList playerList = new PlayerList(); currentComponent.AddChild(playerList); if (!reader.IsEmptyElement) currentComponent = playerList; break; case "Player": PlayerComponent player = new PlayerComponent(); player.Name = reader.GetAttribute("Name"); player.Race = reader.GetAttribute("Race"); player.Gold = Int32.Parse(reader.GetAttribute("Gold")); player.Metal = Int32.Parse(reader.GetAttribute("Metal")); player.Wood = Int32.Parse(reader.GetAttribute("Wood")); currentComponent.AddChild(player); if (!reader.IsEmptyElement) currentComponent = player; break; case "BuildingList": if (!reader.IsEmptyElement) currentComponent = ((PlayerComponent)currentComponent).BuildingList; break; case "UnitList": if (!reader.IsEmptyElement) currentComponent = ((PlayerComponent)currentComponent).GetUnitList(); break; case "Sand": Sand sand = new Sand(); currentComponent.AddChild(sand); if (!reader.IsEmptyElement) currentComponent = sand; break; case "Mountain": Mountain mountain = new Mountain(); currentComponent.AddChild(mountain); if (!reader.IsEmptyElement) currentComponent = mountain; break; case "Grass": Grass grass = new Grass(); currentComponent.AddChild(grass); if (!reader.IsEmptyElement) currentComponent = grass; break; case "Unit": UnitComponent unit = new UnitComponent(); currentComponent.AddChild(unit); float unitX = float.Parse(reader.GetAttribute("X")); float unitY = float.Parse(reader.GetAttribute("Y")); unit.PointLocation = new PointF(unitX, unitY); unit.Type = reader.GetAttribute("Type"); unit.MaxHealth = short.Parse(reader.GetAttribute("MaxHealth")); unit.CurrentHealth = short.Parse(reader.GetAttribute("CurrentHealth")); unit.CanHarvest = bool.Parse(reader.GetAttribute("CanHarvest")); unit.CanAttack = bool.Parse(reader.GetAttribute("CanAttack")); unit.Attack = short.Parse(reader.GetAttribute("Attack")); unit.AttackRange = float.Parse(reader.GetAttribute("AttackRange")); unit.AttackTicks = byte.Parse(reader.GetAttribute("AttackTicks")); unit.CanBuild = bool.Parse(reader.GetAttribute("CanBuild")); unit.BuildSpeed = byte.Parse(reader.GetAttribute("BuildSpeed")); unit.Speed = float.Parse(reader.GetAttribute("Speed")); /* * Type="zombie" CanAttack="True" * Attack="20" AttackRange="4" AttackTicks="10" * BuildSpeed="30" CanBuild="True" CanHarvest="False" * CurrentHealth="100" MaxHealth="100" X="12" Y="13" * Speed="0.1" */ if (!reader.IsEmptyElement) currentComponent = unit; break; case "Building": Building building = new Building(); currentComponent.AddChild(building); building.Width = Int32.Parse(reader.GetAttribute("Width")); building.Height = Int32.Parse(reader.GetAttribute("Height")); building.PointLocation = new PointF(float.Parse(reader.GetAttribute("X")), float.Parse(reader.GetAttribute("Y"))); building.Type = reader.GetAttribute("Type"); building.CanProduce = bool.Parse(reader.GetAttribute("CanProduce")); if (!reader.IsEmptyElement) currentComponent = building; break; default: break; } break; case XmlNodeType.EndElement: if (currentComponent != null) currentComponent = currentComponent.Parent; break; } } Console.WriteLine("XmlTextReader Properties Test"); Console.WriteLine("==================="); // Read this element's properties and display them on console Console.WriteLine("Name:" + reader.Name); Console.WriteLine("Base URI:" + reader.BaseURI); Console.WriteLine("Local Name:" + reader.LocalName); Console.WriteLine("Attribute Count:" + reader.AttributeCount.ToString()); Console.WriteLine("Depth:" + reader.Depth.ToString()); Console.WriteLine("Node Type:" + reader.NodeType.ToString()); Console.WriteLine("Attribute Count:" + reader.Value.ToString()); } return scenario; }
public void Visit(Map map) { output.WriteStartElement("Map"); output.WriteAttributeString("Width", map.GetWidth().ToString()); output.WriteAttributeString("Height", map.GetHeight().ToString()); VisitChildren(map); output.WriteEndElement(); }
public void Visit(Map map) { if (MapVisitor != null) map.Accept(MapVisitor); }
/// <summary> /// Called when the scenario has changed. Unregisters the map view with all pieces of model it was associated with, and registers with /// the new scenario. /// </summary> /// <param name="scenario"></param> public void SetScenario(ScenarioComponent scenario) { context = scenario; // Empty the view. this.mapPanel.Hide(); foreach (Control c in mapPanel.Controls) { c.AllowDrop = false; if (c is TileUI) { ((TileUI)c).UnregisterFromEvents(); } } mapPanel.Controls.Clear(); realizedComponents.Clear(); this.mapPanel.Show(); if (scenario != null) { ZRTSModel.Map map = scenario.GetGameWorld().GetMap(); this.mapPanel.Size = new System.Drawing.Size(map.GetWidth() * PIXELS_PER_COORDINATE, map.GetHeight() * PIXELS_PER_COORDINATE); // Location of the map panel int xLoc, yLoc; if (this.mapPanel.Size.Width > this.Size.Width) { xLoc = 0; } else { // Place the panel in the center. xLoc = (this.Size.Width - this.mapPanel.Size.Width) / 2; } if (this.mapPanel.Size.Height > this.Size.Height) { yLoc = 0; } else { // Place the panel in the center. yLoc = (this.Size.Height - this.mapPanel.Size.Height) / 2; } this.mapPanel.Location = new System.Drawing.Point(xLoc, yLoc); this.RealizeView(); if (scenario.GetGameWorld().GetPlayerList() != null) { scenario.GetGameWorld().GetPlayerList().PlayerAddedEvent += this.PlayerAdded; scenario.GetGameWorld().GetPlayerList().PlayerRemovedEvent += this.PlayerRemoved; foreach (PlayerComponent p in scenario.GetGameWorld().GetPlayerList().GetChildren()) { p.BuildingList.BuildingAddedEventHandlers += this.BuildingAdded; p.BuildingList.BuildingRemovedEventHandlers += this.BuildingRemoved; } } } }
private void RealizeView() { // Get the indices of the upper left hand cell that is visible. int xPos = HorizontalScroll.Value / PIXELS_PER_COORDINATE; int yPos = this.VerticalScroll.Value / PIXELS_PER_COORDINATE; HashSet <Object> componentsToVirtualize = new HashSet <Object>(); foreach (Object o in realizedComponents.Keys) { componentsToVirtualize.Add(o); } mapPanel.SuspendLayout(); if (context != null) { ZRTSModel.Map map = context.GetGameWorld().GetMap(); int maxXPos = xPos + (Width / PIXELS_PER_COORDINATE) + 1; int mapMaxX = map.GetWidth(); int maxX = Math.Min(maxXPos, mapMaxX); int maxYPos = yPos + (Height / PIXELS_PER_COORDINATE) + 1; int mapMaxY = map.GetHeight(); int maxY = Math.Min(maxYPos, mapMaxY); List <Control> controlsToAdd = new List <Control>(); for (int i = xPos; i < maxX; i++) { for (int j = yPos; j < maxY; j++) { CellComponent cell = map.GetCellAt(i, j); if (!realizedComponents.Contains(cell)) { TileUI ui = new TileUI(controller, cell); realizedComponents.Add(cell, ui); ui.Location = new Point(cell.X * PIXELS_PER_COORDINATE, cell.Y * PIXELS_PER_COORDINATE); ui.Size = new Size(PIXELS_PER_COORDINATE, PIXELS_PER_COORDINATE); controlsToAdd.Add(ui); } else { componentsToVirtualize.Remove(cell); } foreach (ModelComponent m in cell.EntitiesContainedWithin) { if (m is Building) { Building building = (Building)m; if (!realizedComponents.Contains(m)) { BuildingUI b = new BuildingUI(controller, building); b.Location = new Point((int)building.PointLocation.X * PIXELS_PER_COORDINATE, (int)building.PointLocation.Y * PIXELS_PER_COORDINATE); b.Size = new Size(building.Width * PIXELS_PER_COORDINATE, building.Height * PIXELS_PER_COORDINATE); realizedComponents.Add(building, b); // Don't bundle the buildings or else BringToFront won't work mapPanel.Controls.Add(b); b.BringToFront(); } else { componentsToVirtualize.Remove(m); } } } } } mapPanel.Controls.AddRange(controlsToAdd.ToArray()); foreach (Object o in componentsToVirtualize) { Control c = (Control)realizedComponents[o]; if (c is TileUI) { // TileUIs are set up to allow drop - turn this off so that the handle to it is destroyed and so the tileUI can be destroyed by the GC. c.AllowDrop = false; ((TileUI)c).UnregisterFromEvents(); } mapPanel.Controls.Remove(c); realizedComponents.Remove(o); } mapPanel.ResumeLayout(); } }