Exemple #1
0
        /// <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);
        }
        private void stopListeningToCells(PointF oldPoint)
        {
            // Get the Correct Bounds.
            int startX = getStartX();
            int endX   = getEndX();
            int startY = getStartX();
            int endY   = getEndY();

            // Get the Map object for this game.
            ModelComponent gameWorldComponent = location;

            while (!(gameWorldComponent is Gameworld))
            {
                gameWorldComponent = gameWorldComponent.Parent;
            }
            Map map = ((Gameworld)(gameWorldComponent)).GetMap();

            for (int i = startX; i < endX; i++)
            {
                for (int j = startY; j < endY; j++)
                {
                    CellComponent cell = map.GetCellAt(i, j);
                    cell.UnitAddedEvent -= new EntityInCellChangedHandler(handleUnitAddedToCell);
                }
            }
        }
        private void listenToCellsWithinVisibilityRange()
        {
            // Get the Correct Bounds.
            int startX = getStartX();
            int endX   = getEndX();
            int startY = getStartY();
            int endY   = getEndY();

            // Get the Map object.
            ModelComponent gameWorldComponent = location;

            while (!(gameWorldComponent is Gameworld))
            {
                gameWorldComponent = gameWorldComponent.Parent;
            }
            Map map = ((Gameworld)(gameWorldComponent)).GetMap();

            for (int i = startX; i <= endX; i++)
            {
                for (int j = startY; j <= endY; j++)
                {
                    CellComponent cell = map.GetCellAt(i, j);
                    cell.UnitAddedEvent += new EntityInCellChangedHandler(handleUnitAddedToCell);
                }
            }
        }
        /*
         * public functions
         */
        /// <summary>
        /// The core function; calls all valid auxiliary functions and returns the path (advanced features can be toggled)
        /// Includes a boolean toggle for use of advanced pathfinding functions
        /// </summary>
        /// <param name="map">The Map</param>
        /// <param name="start">The starting Cell</param>
        /// <param name="end">The ending Cell</param>
        /// <param name="advanced"> A boolean toggle for advanced functions</param>
        /// <returns>The path as a list of waypoints</returns>
        public static List<CellComponent> between(Map map, CellComponent start, CellComponent end, bool advanced)
        {
            // begin timing the operation
            DateTime startTime = DateTime.Now;

            // convert the given Cell-based data to Node-based data
            NodeMap nodeMap = new NodeMap(map);
            Node nodeStart = nodeMap.getNode(start.X, start.Y);
            Node nodeEnd = nodeMap.getNode(end.X, end.Y);

            // perform advanced pre-calculation tasks
            if (advanced)
            {
                // if the end Node is invalid, replace it with the nearest valid Node
                if (!nodeEnd.isValid)
                {
                    nodeEnd = Advanced.nearestValidEnd(nodeMap, nodeStart, nodeEnd);
                }
            }

            // find the path
            List<Node> nodePath = Basic.findPath(nodeMap, nodeStart, nodeEnd);

            // convert the path from List<Node> format back to List<Cell> format
            List<CellComponent> path = new List<CellComponent>(nodePath.Count);
            for (int i = 0; i < nodePath.Count; i++)
                path.Add(map.GetCellAt(nodePath[i].X, nodePath[i].Y));

            // grab and print path data
            float dist = (float)(nodePath[nodePath.Count - 1].Gscore);
            span = DateTime.Now - startTime;
            //printPath(path, dist);

            return path;
        }
Exemple #5
0
        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;
        }
Exemple #6
0
        /// <summary>
        /// Variation of the takeStep method. The unit will move to the center of the targetCell. (x + 0.5f, y + 0.5f)
        /// </summary>
        /// <returns>true if at the end of the path, false otherwise.</returns>
        private bool takeStepMiddle(UnitComponent unit)
        {
            bool completed = false;

            // Check if we are at the center of the target Cell
            if (unit.PointLocation.X == path[0].X + CENTER && unit.PointLocation.Y == path[0].Y + CENTER)
            {
                // Remove the next Cell in the path.  If that was the last Cell, we're done
                path.RemoveAt(0);
                if (path.Count == 0)
                {
                    completed = true;
                }
            }

            // If we aren't at the center of the next Cell, and that Cell is vacant, move towards it
            else if (isNextCellVacant(unit))
            {
                if (waiting)
                {
                    waiting      = false;
                    ticksWaiting = 0;
                }
                moveMiddle(unit);
            }

            // Otherwise, the next Cell in our path is not vacant; wait for a while or compute a new path
            else
            {
                waiting = true;
                ticksWaiting++;

                // We've been waiting long enough, compute a new path.
                if (ticksWaiting % WAIT_TICKS == 0)
                {
                    path = FindPath.between(map, map.GetCellAt((int)unit.PointLocation.X, (int)unit.PointLocation.Y), map.GetCellAt((int)targetX, (int)targetY));
                }
            }

            return(completed);
        }
Exemple #7
0
        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 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();
            }
        }