Exemple #1
0
        //pg. 256
        static private List <PathNode> findAdjacentNodes(
            PathNode currentNode,
            PathNode endNode)
        {
            List <PathNode> adjacentNodes = new List <PathNode>();

            int X = currentNode.GridX;
            int Y = currentNode.GridY;

            bool upLeft    = true;
            bool upRight   = true;
            bool downLeft  = true;
            bool downRight = true;

            if ((X > 0) && (!TileMap.IsWallTile(X - 1, Y)))
            {
                adjacentNodes.Add(new PathNode(
                                      currentNode,
                                      endNode,
                                      new Vector2(X - 1, Y),
                                      CostStraight + currentNode.DirectCost));
            }
            else
            {
                upLeft   = false;
                downLeft = false;
            }
            if ((X < 49) && (!TileMap.IsWallTile(X, Y - 1)))
            {
                adjacentNodes.Add(new PathNode(
                                      currentNode,
                                      endNode,
                                      new Vector2(X, Y - 1),
                                      CostStraight + currentNode.DirectCost));
            }
            else
            {
                upLeft  = false;
                upRight = false;
            }
            if ((Y < 49) && (!TileMap.IsWallTile(X, Y + 1)))
            {
                adjacentNodes.Add(new PathNode(
                                      currentNode,
                                      endNode,
                                      new Vector2(X, Y + 1),
                                      CostStraight + currentNode.DirectCost));
            }
            else
            {
                downLeft  = false;
                downRight = false;
            }
            if ((upLeft) && (!TileMap.IsWallTile(X - 1, Y - 1)))
            {
                adjacentNodes.Add(new PathNode(
                                      currentNode,
                                      endNode,
                                      new Vector2(X - 1, Y - 1),
                                      CostDiagonal + currentNode.DirectCost));
            }
            if ((upRight) && (!TileMap.IsWallTile(X + 1, Y - 1)))
            {
                adjacentNodes.Add(new PathNode(
                                      currentNode,
                                      endNode,
                                      new Vector2(X + 1, Y - 1),
                                      CostDiagonal + currentNode.DirectCost));
            }
            if ((downLeft) && (!TileMap.IsWallTile(X - 1, Y + 1)))
            {
                adjacentNodes.Add(new PathNode(
                                      currentNode,
                                      endNode,
                                      new Vector2(X - 1, Y + 1),
                                      CostDiagonal + currentNode.DirectCost));
            }
            if ((downRight) && (!TileMap.IsWallTile(X + 1, Y + 1)))
            {
                adjacentNodes.Add(new PathNode(
                                      currentNode,
                                      endNode,
                                      new Vector2(X + 1, Y + 1),
                                      CostDiagonal + currentNode.DirectCost));
            }
            return(adjacentNodes);
        }
Exemple #2
0
        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            // TODO: use this.Content to load your game content here
            //pg. 170
            spriteSheet = Content.Load <Texture2D>(@"Textures\SpriteSheet");
            titleScreen = Content.Load <Texture2D>(@"Textures\TitleScreen");
            pericles14  = Content.Load <SpriteFont>(@"Fonts\Pericles14");
            //pg. 174
            Camera.WorldRectangle = new Rectangle(0, 0, 1600, 1600);
            Camera.ViewPortWidth  = 800;
            Camera.ViewPortHeight = 600;

            //pg. 184
            //temp code
            //tempSprite = new Sprite(
            //    new Vector2(100, 100),
            //    spriteSheet,
            //    new Rectangle(0, 64, 32, 32),
            //    Vector2.Zero);

            //tempSprite2 = new Sprite(
            //    new Vector2(200, 200),
            //    spriteSheet,
            //    new Rectangle(0, 160, 32, 32),
            //    Vector2.Zero);

            //temp end

            //pg. 196
            TileMap.Initialize(spriteSheet);

            //pg. 203
            Player.Initialize(
                spriteSheet,
                new Rectangle(0, 64, 32, 32),
                6,
                new Rectangle(0, 96, 32, 32),
                1,
                //pg. 270
                new Vector2(32, 32));


            //pg. 227
            EffectsManager.Initialize(
                spriteSheet,
                new Rectangle(0, 288, 2, 2),
                new Rectangle(0, 256, 32, 32),
                3);

            //pg. 231
            WeaponManager.Texture = spriteSheet;

            //pg. 270
            GoalManager.Initialize(
                spriteSheet,
                new Rectangle(0, 7 * 32, 32, 32),
                new Rectangle(3 * 32, 7 * 32, 32, 32),
                3,
                1);
            //pg. 286
            //GoalManager.GenerateComputers(10);

            //pg. 279
            EnemyManager.Initialize(
                spriteSheet,
                new Rectangle(0, 160, 32, 32));
        }
Exemple #3
0
        //pg. 253
        #region Public Methods
        static public List <Vector2> FindPath(
            Vector2 startTile,
            Vector2 endTile)
        {
            if (TileMap.IsWallTile(endTile) ||
                TileMap.IsWallTile(startTile))
            {
                return(null);
            }
            openList.Clear();
            nodeCosts.Clear();
            nodeStatus.Clear();

            PathNode startNode;
            PathNode endNode;

            endNode   = new PathNode(null, null, endTile, 0);
            startNode = new PathNode(null, endNode, startTile, 0);
            addNodeToOpenList(startNode);

            while (openList.Count > 0)
            {
                PathNode currentNode = openList[openList.Count - 1];
                if (currentNode.IsEqualToNode(endNode))
                {
                    List <Vector2> bestPath = new List <Vector2>();
                    while (currentNode != null)
                    {
                        bestPath.Insert(0, currentNode.GridLocation);
                        currentNode = currentNode.ParentNode;
                    }
                    return(bestPath);
                }
                openList.Remove(currentNode);
                nodeCosts.Remove(currentNode.GridLocation);

                foreach (
                    PathNode possibleNode in
                    findAdjacentNodes(currentNode, endNode))
                {
                    if (nodeStatus.ContainsKey(possibleNode.GridLocation))
                    {
                        if (nodeStatus[possibleNode.GridLocation] ==
                            NodeStatus.Closed)
                        {
                            continue;
                        }

                        if (
                            nodeStatus[possibleNode.GridLocation] ==
                            NodeStatus.Open)
                        {
                            if (possibleNode.TotalCost >=
                                nodeCosts[possibleNode.GridLocation])
                            {
                                continue;
                            }
                        }
                    }
                    addNodeToOpenList(possibleNode);
                }
                nodeStatus[currentNode.GridLocation] = NodeStatus.Closed;
            }
            return(null);
        }
Exemple #4
0
        // TODO: Add your update logic here

        //pg. 185
        //temp code
        //Vector2 spriteMove = Vector2.Zero;
        //Vector2 cameraMove = Vector2.Zero;

        //if (Keyboard.GetState().IsKeyDown(Keys.A))
        //    spriteMove.X = -1;

        //if (Keyboard.GetState().IsKeyDown(Keys.D))
        //    spriteMove.X = 1;

        //if (Keyboard.GetState().IsKeyDown(Keys.W))
        //    spriteMove.Y = -1;

        //if (Keyboard.GetState().IsKeyDown(Keys.S))
        //    spriteMove.Y = 1;

        //if (Keyboard.GetState().IsKeyDown(Keys.Left))
        //    cameraMove.X = -1;

        //if (Keyboard.GetState().IsKeyDown(Keys.Right))
        //    cameraMove.X = 1;

        //if (Keyboard.GetState().IsKeyDown(Keys.Up))
        //    cameraMove.Y = -1;

        //if (Keyboard.GetState().IsKeyDown(Keys.Down))
        //    cameraMove.Y = 1;

        //Camera.Move(cameraMove);
        //tempSprite.Velocity = spriteMove * 60;

        //tempSprite.Update(gameTime);
        //tempSprite2.Update(gameTime);
        //temp end



        //base.Update(gameTime);
        // }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            spriteBatch.Begin();

            if (gameState == GameStates.TitleScreen)
            {
                spriteBatch.Draw(
                    titleScreen,
                    new Rectangle(0, 0, 800, 600),
                    Color.White);
            }
            if ((gameState == GameStates.Playing) ||
                (gameState == GameStates.WaveComplete) ||
                (gameState == GameStates.GameOver))
            {
                TileMap.Draw(spriteBatch);
                WeaponManager.Draw(spriteBatch);
                Player.Draw(spriteBatch);
                EnemyManager.Draw(spriteBatch);
                EffectsManager.Draw(spriteBatch);
                GoalManager.Draw(spriteBatch);

                checkPlayerDeath();

                spriteBatch.DrawString(
                    pericles14,
                    "Score: " + GameManager.Score.ToString(),
                    new Vector2(30, 5),
                    Color.White);

                spriteBatch.DrawString(
                    pericles14,
                    "Terminals Remaining: " +
                    GoalManager.ActiveTerminals,
                    new Vector2(520, 5),
                    Color.White);
            }
            if (gameState == GameStates.WaveComplete)
            {
                spriteBatch.DrawString(
                    pericles14,
                    "Beginning Wave " +
                    (GameManager.CurrentWave + 1).ToString(),
                    new Vector2(300, 300),
                    Color.White);
            }
            if (gameState == GameStates.GameOver)
            {
                spriteBatch.DrawString(
                    pericles14,
                    "G A M E  O V E R!",
                    new Vector2(300, 300),
                    Color.White);
            }
            spriteBatch.End();

            //TileMap.Draw(spriteBatch);
            ////pg. 231
            //WeaponManager.Draw(spriteBatch);
            //Player.Draw(spriteBatch);
            //        //pg. 280
            //EnemyManager.Draw(spriteBatch);
            //        //pg.227
            //EffectsManager.Draw(spriteBatch);
            //    //pg. 271
            //GoalManager.Draw(spriteBatch);


            //Temp code
            //Vector2 mouseLocation = new Vector2(
            //    Mouse.GetState().X, Mouse.GetState().Y);

            //mouseLocation += Camera.Position;

            //List<Vector2> path = PathFinder.FindPath(
            //    TileMap.GetSquareAtPixel(mouseLocation),
            //    TileMap.GetSquareAtPixel(Player.BaseSprite.WorldCenter));

            //if (!(path == null))
            //{
            //    foreach (Vector2 node in path)
            //    {
            //        spriteBatch.Draw(
            //            spriteSheet,
            //            TileMap.SquareScreenRectangle((int)node.X,
            //            (int)node.Y),
            //            new Rectangle(0, 288, 32, 32),
            //            new Color(128, 0, 0, 80));
            //    }
            //}

            //End Temporary code



            //spriteBatch.End();

            base.Draw(gameTime);
//}

            // TODO: Add your drawing code here
            //pg. 184
            //spriteBatch.Begin();
            ////pg.197
            //TileMap.Draw(spriteBatch);
            //tempSprite.Draw(spriteBatch);
            //tempSprite2.Draw(spriteBatch);
            //spriteBatch.End();
            ////temp end
            //base.Draw(gameTime);
        }