コード例 #1
0
        private Point exploreFrontier(PriorityQueue <Point> frontier, Point target, Dictionary <Point, Point> cameFrom, Dictionary <Point, double> costSoFar)
        {
            Point  closestTile = new Point();
            Point  currentTile;
            double closestDistance = double.PositiveInfinity;

            while (frontier.Count > 0)
            {
                currentTile = frontier.Dequeue();
                double currentDistance = Math.Sqrt(
                    Math.Abs(currentTile.X - target.X) +
                    Math.Abs(currentTile.Y - target.Y));

                if (currentDistance < closestDistance)
                {
                    closestDistance = currentDistance;
                    closestTile     = currentTile;
                }

                if (closestTile == target)
                {
                    break;
                }

                List <Point> neighbors = gameMap.GetValidNeighbors(currentTile);
                foreach (Point neighborTile in neighbors)
                {
                    GameTile tileObject = gameMap.game_tiles[(int)neighborTile.Y, (int)neighborTile.X];
                    if (tileObject.IsPathable())
                    {
                        double tileModifier       = 1.0;
                        double distanceFromTarget = Math.Sqrt(
                            Math.Abs(tileObject.Coordinates.X - target.X) +
                            Math.Abs(tileObject.Coordinates.Y - target.Y));
                        double newCost = (costSoFar[currentTile] + tileModifier) + distanceFromTarget;
                        if (!cameFrom.ContainsKey(neighborTile) || newCost < costSoFar[currentTile])
                        {
                            cameFrom[neighborTile]  = currentTile;
                            costSoFar[neighborTile] = newCost;
                            frontier.Enqueue(neighborTile, newCost);
                        }
                    }
                }
            }
            return(closestTile);
        }
コード例 #2
0
        public List <Point> GetPath(Point start, Point target)
        {
            List <Point> path = new List <Point>();
            Dictionary <Point, Point>  cameFrom  = new Dictionary <Point, Point>();
            Dictionary <Point, double> costSoFar = new Dictionary <Point, double>();
            Point closestTile = new Point();

            cameFrom[start]  = start;
            costSoFar[start] = 0;
            PriorityQueue <Point> frontier  = new PriorityQueue <Point>();
            List <Point>          neighbors = gameMap.GetValidNeighbors(start);

            foreach (Point tile in neighbors)
            {
                GameTile tileObject = gameMap.game_tiles[(int)tile.Y, (int)tile.X];
                if (tileObject.IsPathable())
                {
                    frontier.Enqueue(start, 0);
                }
            }
            closestTile = exploreFrontier(
                frontier, target, cameFrom, costSoFar);
            List <Point> newPathSteps = new List <Point>();

            newPathSteps.Add(closestTile);

            while (true)
            {
                Point nextTile = newPathSteps[0];
                if (nextTile == start)
                {
                    break;
                }
                newPathSteps.Insert(0, cameFrom[nextTile]);
            }
            return(newPathSteps);
        }
コード例 #3
0
ファイル: Omni.cs プロジェクト: ChadSki/omni
        /// <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)
        {
            // IO events start
            Random        random            = new Random();
            KeyboardState lastKeyboardState = KeyboardState;

            KeyboardState = Keyboard.GetState();
            MouseState lastMouseState = MouseState;

            MouseState   = Mouse.GetState();
            MousePos.X   = MouseState.X;
            MousePos.Y   = MouseState.Y;
            CursorMapPos = coordinateConverter.ScreenToMap(MousePos, DisplayShift);

            if (KeyboardState.IsKeyDown(Keys.Right))
            {
                DisplayShift.X -= 5;
            }
            if (KeyboardState.IsKeyDown(Keys.Left))
            {
                DisplayShift.X += 5;
            }
            if (KeyboardState.IsKeyDown(Keys.Up))
            {
                DisplayShift.Y += 5;
            }
            if (KeyboardState.IsKeyDown(Keys.Down))
            {
                DisplayShift.Y -= 5;
            }
            if (KeyboardState.IsKeyDown(Keys.Escape))
            {
                Exit();
            }
            if (KeyboardState.IsKeyUp(Keys.P) &&
                lastKeyboardState.IsKeyDown(Keys.P))
            {
                DisplayPaths = !DisplayPaths;
            }
            if (KeyboardState.IsKeyUp(Keys.S) &&
                lastKeyboardState.IsKeyDown(Keys.S))
            {
                DisplayScore = !DisplayScore;
            }

            if (MouseState.LeftButton == ButtonState.Released &&
                lastMouseState.LeftButton == ButtonState.Pressed)
            {
                Point mapCoords = coordinateConverter.ScreenToMap(MousePos, DisplayShift);
                if (gameMap.IsPointInside(mapCoords) &&
                    gameMap.game_tiles[(int)mapCoords.Y, (int)mapCoords.X].IsPathable())
                {
                    LumberCamp newLumberCamp = new LumberCamp(mapCoords);
                    gameMap.game_tiles[(int)mapCoords.Y, (int)mapCoords.X].Building = newLumberCamp;
                    gameMap.GetBuildings().Add(newLumberCamp);
                    List <Point> validNeighbors     = gameMap.GetValidNeighbors(mapCoords);
                    List <Point> spawnableNeighbors = new List <Point>();
                    foreach (Point validNeighbor in validNeighbors)
                    {
                        if (gameMap.game_tiles[(int)validNeighbor.Y, (int)validNeighbor.X].IsPathable())
                        {
                            spawnableNeighbors.Add(validNeighbor);
                        }
                    }
                    for (int x = 0; x < 3; x++)
                    {
                        Point   spawnTile  = spawnableNeighbors[random.Next(spawnableNeighbors.Count)];
                        Laborer newLaborer = new Laborer(spawnTile);
                        gameMap.GetUnits().Add(newLaborer);
                        GameTile theTile = gameMap.game_tiles[(int)spawnTile.Y, (int)spawnTile.X];
                        theTile.Unit = newLaborer;
                    }
                }
            }

            // IO events end


            // game logic start
            List <Entity> expiredEntities = new List <Entity>();

            foreach (Unit unitObject in gameMap.GetUnits())
            {
                unitObject.Tick(gameMap, Player1, pathfinder);
            }
            foreach (Terrain terrainObject in gameMap.GetTerrain())
            {
                terrainObject.Tick(gameMap, Player1, pathfinder);
                if (terrainObject.IsExpired())
                {
                    expiredEntities.Add(terrainObject);
                }
            }
            foreach (Entity entityObject in expiredEntities)
            {
                entityObject.OnDeath(gameMap);
                gameMap.GetTerrain().Remove(entityObject);
                gameMap.GetBuildings().Remove(entityObject);
            }


            // game logic ends


            base.Update(gameTime);
        }