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> /// find the closest cell from the specified destination /// </summary> /// <param name="point">Destination</param> /// <returns>Closest cell</returns> private CellComponent findClosestCell(PointF point) { double distanceSquared = Math.Pow(map.GetWidth(), 2.0) + Math.Pow(map.GetHeight(), 2.0); CellComponent cell = null; for (int i = Math.Max((int)building.PointLocation.X - 1, 0); i <= building.PointLocation.X + building.Width + 1; i++) { for (int j = Math.Max((int)building.PointLocation.Y - 1, 0); j <= building.PointLocation.Y + building.Height + 1; j++) { // Ignore cases in the middle of the proposed building site. if (!(i >= (int)building.PointLocation.X && i < (int)building.PointLocation.X + building.Width && j >= (int)building.PointLocation.Y && j < (int)building.PointLocation.Y + building.Height)) { double calculatedDistanceSquared = findDistanceSquared(point.X, point.Y, i, j); if (calculatedDistanceSquared <= distanceSquared) { if (map.GetCellAt(i, j) != null) { cell = map.GetCellAt(i, j); distanceSquared = calculatedDistanceSquared; } } } } } return(cell); }
public void Visit(Map map) { output.WriteStartElement("Map"); output.WriteAttributeString("Width", map.GetWidth().ToString()); output.WriteAttributeString("Height", map.GetHeight().ToString()); VisitChildren(map); output.WriteEndElement(); }
private int getEndX() { int endX = (int)pointLocation.X + (int)visibilityRange; ModelComponent gameWorldComponent = location; while (!(gameWorldComponent is Gameworld)) { gameWorldComponent = gameWorldComponent.Parent; } Map map = ((Gameworld)(gameWorldComponent)).GetMap(); if (endX >= map.GetWidth()) { endX = map.GetWidth() - 1; } return(endX); }
/// <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(); } }
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); }
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); } } }
/// <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(); } }