示例#1
0
        /// <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)
        {
            // if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
            //   Exit();

            previousKBState = kbState;
            kbState         = Keyboard.GetState();

            previousMState = mState;
            mState         = Mouse.GetState();

            marioQuad = quadTree.GetContainingQuad(player.O_Position);

            if (!quitActive)
            {
                switch (gameState)
                {
                case GameState.Menu:
                    if (!quitActive)
                    {
                        mainMenu.Update(mState, previousMState, kbState, previousKBState);
                    }
                    break;

                case GameState.World:
                    if (reader.RoomNumber >= 31)
                    {
                        WinGame();
                    }

                    if (SingleKeyPress(Keys.P))     //Swtch to pause menu
                    {
                        PauseGame();
                    }

                    player.Update(gameTime.ElapsedGameTime.TotalSeconds, kbState, previousKBState);

                    for (int i = 0; i < reader.EnemyList.Count; i++)
                    {
                        if (player.O_Position.Intersects(reader.EnemyList[i].O_Position))
                        {
                            Battle(reader.EnemyList[i]);
                            killedEnemy = reader.EnemyList[i];
                        }
                    }

                    //Put in player collision with enemy here
                    //Player Movement
                    if (kbState.IsKeyDown(Keys.Left))
                    {
                        player.O_X -= 5;
                    }
                    else if (kbState.IsKeyDown(Keys.Right))
                    {
                        player.O_X += 5;
                    }

                    if (kbState.IsKeyDown(Keys.Up))
                    {
                        player.O_Y -= 5;
                    }
                    else if (kbState.IsKeyDown(Keys.Down))
                    {
                        player.O_Y += 5;
                    }

                    //Deals with players x directional movement
                    switch (playerXState)
                    {
                    case PlayerXState.StandRight:
                        if (kbState.IsKeyDown(Keys.D))
                        {
                            playerXState     = PlayerXState.WalkRight;
                            player.Direction = CharDirection.right;
                            player.State     = CharacterState.o_walk;
                        }

                        else if (kbState.IsKeyDown(Keys.A))
                        {
                            playerXState     = PlayerXState.WalkLeft;
                            player.Direction = CharDirection.left;
                            player.State     = CharacterState.o_walk;
                        }
                        break;

                    case PlayerXState.WalkRight:
                        player.Move(3);
                        player.UpdateDetectors();

                        if (reader.Right && player.O_X >= GraphicsDevice.Viewport.Width - reader.CurrentRoom.Tile.Width - player.O_Width)
                        {
                            player.O_X = GraphicsDevice.Viewport.Width - reader.CurrentRoom.Tile.Width - player.O_Width;
                        }

                        for (int i = 0; i < reader.RectList.Count; i++)
                        {
                            if (player.Right.Intersects(reader.RectList[i]))
                            {
                                player.O_X = reader.RectList[i].X - player.O_Width - 1;
                                break;
                            }
                        }

                        if (kbState.IsKeyUp(Keys.D))
                        {
                            playerXState         = PlayerXState.StandRight;
                            player.State         = CharacterState.o_idle;
                            player.XAcceleration = 1;
                        }
                        break;

                    case PlayerXState.StandLeft:
                        if (kbState.IsKeyDown(Keys.A))
                        {
                            playerXState     = PlayerXState.WalkLeft;
                            player.Direction = CharDirection.left;
                            player.State     = CharacterState.o_walk;
                        }

                        else if (kbState.IsKeyDown(Keys.D))
                        {
                            playerXState     = PlayerXState.WalkRight;
                            player.Direction = CharDirection.right;
                            player.State     = CharacterState.o_walk;
                        }
                        break;

                    case PlayerXState.WalkLeft:
                        player.Move(-3);
                        player.UpdateDetectors();
                        //player.State = CharacterState.o_walk;

                        if (reader.Left && player.O_X <= reader.CurrentRoom.Tile.Width)
                        {
                            player.O_X = reader.CurrentRoom.Tile.Width;
                        }

                        for (int i = 0; i < reader.RectList.Count; i++)
                        {
                            if (player.Left.Intersects(reader.RectList[i]))
                            {
                                player.O_X = reader.RectList[i].X + player.O_Width + 1;
                                break;
                            }
                        }

                        if (kbState.IsKeyUp(Keys.A))
                        {
                            playerXState         = PlayerXState.StandLeft;
                            player.State         = CharacterState.o_idle;
                            player.XAcceleration = 1;
                        }
                        break;
                    }

                    //Deals with player's y directional movement
                    switch (playerYState)
                    {
                    case PlayerYState.Ground:
                        bool onPlatform = false;

                        for (int i = 0; i < reader.RectList.Count; i++)
                        {
                            if (player.Below.Intersects(reader.RectList[i]) || player.O_LocY >= GraphicsDevice.Viewport.Height - player.O_Height - reader.CurrentRoom.Tile.Height)
                            {
                                onPlatform = true;
                                break;
                            }
                        }

                        for (int i = 0; i < reader.ItemList.Count; i++)
                        {
                            if (player.O_Position.Intersects(reader.ItemList[i].Rect))
                            {
                                reader.ItemList[i].Collect(player);
                            }
                        }

                        if (player.O_LocY >= GraphicsDevice.Viewport.Height - player.O_Height)
                        {
                            onPlatform = true;
                        }

                        if (!onPlatform)
                        {
                            player.JumpAcceleration = -2;
                            playerYState            = PlayerYState.Jump;
                            player.State            = CharacterState.o_jump;
                        }

                        if (kbState.IsKeyDown(Keys.Space))
                        {
                            if (previousKBState.IsKeyUp(Keys.Space))
                            {
                                playerYState = PlayerYState.Jump;
                                player.State = CharacterState.o_jump;
                            }
                        }
                        break;



                    case PlayerYState.Jump:
                        player.Jump();
                        player.UpdateDetectors();

                        for (int i = 0; i < reader.RectList.Count; i++)
                        {
                            if (player.Above.Intersects(reader.RectList[i]))
                            {
                                player.JumpAcceleration = -2;
                                break;
                            }

                            else if (player.O_Position.Intersects(reader.RectList[i]))
                            {
                                player.O_Y = reader.RectList[i].Y - player.O_Height;
                                player.JumpAcceleration = 17;
                                playerYState            = PlayerYState.Ground;

                                if (kbState.IsKeyUp(Keys.A) && kbState.IsKeyUp(Keys.D))
                                {
                                    player.State = CharacterState.o_idle;
                                }

                                else
                                {
                                    if (kbState.IsKeyUp(Keys.A) && kbState.IsKeyDown(Keys.D))
                                    {
                                        player.State = CharacterState.o_walk;
                                        player.Move(5);
                                    }
                                    else if (kbState.IsKeyUp(Keys.D) && kbState.IsKeyDown(Keys.A))
                                    {
                                        player.State = CharacterState.o_walk;
                                        player.Move(-5);
                                    }
                                }

                                break;
                            }
                        }

                        if (reader.Up)
                        {
                            if (player.O_Y <= reader.CurrentRoom.Tile.Height)
                            {
                                player.JumpAcceleration = -2;
                            }
                        }

                        if (reader.Down)
                        {
                            if (player.O_Y >= GraphicsDevice.Viewport.Height - player.O_Height - reader.CurrentRoom.Tile.Height)
                            {
                                player.O_Y   = GraphicsDevice.Viewport.Height - player.O_Height - reader.CurrentRoom.Tile.Height;
                                playerYState = PlayerYState.Ground;
                                // IDLE HERE@@@@@@@@@@@@@@@@@@@
                                player.JumpAcceleration = 17;
                            }
                        }

                        else if (player.O_Y >= GraphicsDevice.Viewport.Height - player.O_Height)
                        {
                            player.O_Y   = GraphicsDevice.Viewport.Height - player.O_Height;
                            playerYState = PlayerYState.Ground;
                            // IDLE HERE@@@@@@@@@@@@@@@@@@@
                            player.JumpAcceleration = 17;
                        }
                        break;
                    }

                    break;


                case GameState.Pause:
                    if (!quitActive)
                    {
                        pauseMenu.Update(mState, previousMState, kbState,
                                         previousKBState);

                        if (SingleKeyPress(Keys.P))
                        {
                            UnpauseGame();
                        }
                    }
                    break;


                case GameState.Battle:
                    if (SingleKeyPress(Keys.P))     //Swtch to pause menu
                    {
                        PauseGame();
                    }

                    BattleManager.Update(gameTime.ElapsedGameTime.TotalSeconds, mState, previousMState, kbState, previousKBState);
                    break;

                case GameState.GameOver:
                    gameOverMenu.Update(mState, previousMState, kbState,
                                        previousKBState);
                    break;

                case GameState.Instructions:
                    instructionsMenu.Update(mState, previousMState, kbState, previousKBState);
                    break;

                case GameState.GameWon:
                    gameWinMenu.Update(mState, previousMState, kbState, previousKBState);
                    break;
                }
            }

            if (quitActive)
            {
                quitMenu.Update(mState, previousMState, kbState, previousKBState);
            }
            if (reader.SwitchRoom(player))
            {
                reader.ReadMap("../../../Content/Rooms/room" + reader.RoomNumber + ".txt", quadTree, collectibleTexture, enemyTexture);
                if (reader.RoomNumber > 10 && reader.RoomNumber < 21)
                {
                    int temp = rand.Next(2);
                    if (temp == 0)
                    {
                        sewerBG.Tex = sewerTexture;
                    }
                    else
                    {
                        sewerBG.Tex = sewerTexture2;
                    }
                }
            }
            base.Update(gameTime);
        }
示例#2
0
        /// <summary>
        /// pure voodoo magic. might be dangerous
        /// </summary>
        /// <param name="name">name of the text file</param>
        /// <param name="plat">platform object for this particular room</param>
        /// <param name="wa">wall object for this particular room</param>
        /// <param name="i">arrray of items that can potentially be found in this room</param>
        /// <param name="batch">spritebatch</param>
        public void ReadMap(string name, QuadTreeNode qt, Texture2D collectText, Texture2D enemyText)
        {
            quadtree = qt;
            Stream       instream;
            StreamReader input = null;

            roomNumber++;
            count = 0;

            previousTransitionRect = transitionRect;


            try
            {
                instream = File.OpenRead(name);
                input    = new StreamReader(instream);

                //reads the first line, parses the info to the correct variables
                line = input.ReadLine();
                string[] newLine = line.Split(',');
                x             = int.Parse(newLine[0]);
                y             = int.Parse(newLine[1]);
                charList      = newLine[2].ToCharArray();
                up            = (charList[0].Equals('1'));
                left          = (charList[1].Equals('1'));
                down          = (charList[2].Equals('1'));
                right         = (charList[3].Equals('1'));
                transRectChar = (newLine[3].ToCharArray()[0]);
                tileArray     = new char[x, y];
                //sets up a new room with the input
                room      = new Room(x, y, up, down, left, right);
                rectList  = new List <Rectangle>();
                objList   = new List <MapObject>();
                itemList  = new List <Collectible>();
                enemyList = new List <Enemy>();
                Rectangle leftrect  = new Rectangle(0, 0, 50, 800);
                Rectangle rightrect = new Rectangle(1150, 0, 50, 800);
                Rectangle uprect    = new Rectangle(0, 0, 1200, 50);
                Rectangle downrect  = new Rectangle(0, 750, 1200, 50);

                //finds out where to go to load the next map
                if (transRectChar == 'd')
                {
                    transitionRect = downrect;
                }
                else if (transRectChar == 'u')
                {
                    transitionRect = uprect;
                }
                else if (transRectChar == 'r')
                {
                    transitionRect = rightrect;
                }

                //saves the actual room into the character array
                while ((line = input.ReadLine()) != null)
                {
                    charList = line.ToCharArray();

                    for (int n = 0; n < y; n++)
                    {
                        tileArray[count, n] = charList[n];

                        if (tileArray[count, n] == '#')
                        {
                            rectList.Add(new Rectangle(count * 25, n * 25, 25, 25));
                        }
                        if (tileArray[count, n] == '*')
                        {
                            itemList.Add(new Collectible(count * 25, n * 25, 25, 25, collectText, "item"));
                        }
                        if (tileArray [count, n] == 'e')
                        {
                            //need stats on enemies
                            enemyList.Add(new Zombie(
                                              new Vector2(count * 25, n * 25),
                                              healthBarBackground,
                                              healthBarOverlay,
                                              Zombie.ZOM_HEALTH + rando.Next(3)));
                        }
                    }
                    count++;
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            finally
            {
                if (input != null)
                {
                    input.Close();
                }
            }
        }