コード例 #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 (currentScreen == Menu.Play)
            {
                if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
                {
                    Exit();
                }

                GamePadState  gamePadState = GamePad.GetState(PlayerIndex.One);
                KeyboardState keyState     = Keyboard.GetState();
                //ProcessMovement(keyState, gamePadState);
                //store current position to move back too if collision
                int tempx = map.ObjectGroups["Objects"].Objects["Player"].X;
                int tempy = map.ObjectGroups["Objects"].Objects["Player"].Y;
                Position = new Vector2(map.ObjectGroups["Objects"].Objects["Player"].X, map.ObjectGroups["Objects"].Objects["Player"].Y);
                ProcessMovement(keyState, gamePadState);
                //foreach(Enemy E in EnemyList)
                //{
                //E.Update(gameTime, Position);
                //}
                //MoveEnemies(Position);
                //now we have moved checkbounds
                if (CheckBounds(map.ObjectGroups["Objects"].Objects["Player"]))
                {
                    map.ObjectGroups["Objects"].Objects["Player"].X = tempx;
                    map.ObjectGroups["Objects"].Objects["Player"].Y = tempy;
                }
                var       p         = map.ObjectGroups["Objects"].Objects["Player"];
                Rectangle playerRec = new Rectangle(p.X, p.Y, p.Width, p.Height);
                CheckCoins(playerRec);
                Vector2 Test = (viewportPosition + new Vector2(0, 100) - new Vector2((graphics.PreferredBackBufferWidth / 2), (graphics.PreferredBackBufferHeight / 2)));
                foreach (var sprite in _sprites)
                {
                    sprite.Update(gameTime, Position, Test);
                }

                int i = 1;
                foreach (Enemy E in EnemyList)
                {
                    Vector2 temp = E.Position;
                    E.Update(gameTime, Position, viewportPosition, true);
                    if (CheckBounds(map.ObjectGroups["Objects"].Objects["Enemy" + i]) || CheckEnemy(map.ObjectGroups["Objects"].Objects["Enemy" + i], E))
                    {
                        E.Position = temp;
                        Console.WriteLine("Enemy collision");
                    }
                    else
                    {
                        map.ObjectGroups["Objects"].Objects["Enemy" + i].X = (int)E.Position.X;
                        map.ObjectGroups["Objects"].Objects["Enemy" + i].Y = (int)E.Position.Y;
                    }
                    i++;
                }
                elapsedTime += (int)gameTime.ElapsedGameTime.TotalMilliseconds;
                // If the elapsed time is larger than the frame time
                // we need to switch frames
                if (elapsedTime > 100)
                {
                    // Move to the next frame
                    currentFrame++;

                    // If the currentFrame is equal to frameCount reset currentFrame to zero
                    if (currentFrame == frameCount)
                    {
                        currentFrame = 0;
                    }

                    // Reset the elapsed time to zero
                    elapsedTime = 0;
                }

                // Grab the correct frame in the image strip by multiplying the currentFrame index by the Frame width

                sourceRect           = new Rectangle(currentFrame * FrameWidth, 0, FrameWidth, FrameHeight);
                _sprites[0].Position = new Vector2(map.ObjectGroups["Objects"].Objects["Player"].X, map.ObjectGroups["Objects"].Objects["Player"].Y);
                // Grab the correct frame in the image strip by multiplying the currentFrame index by the frame width
                //viewportPosition = new Vector2(map.ObjectGroups["Objects"].Objects["Player"].X - (graphics.PreferredBackBufferWidth / 2), map.ObjectGroups["Objects"].Objects["Player"].Y - (graphics.PreferredBackBufferHeight / 2));
                viewportPosition = new Vector2(map.ObjectGroups["Objects"].Objects["Player"].X, map.ObjectGroups["Objects"].Objects["Player"].Y);
                KeyboardState keys = Keyboard.GetState();
                // Takes to main menu
                if (keys.IsKeyDown(Keys.Tab))
                {
                    currentScreen = Menu.Main;
                }
                else if (keys.IsKeyDown(Keys.I))
                {
                    currentScreen = Menu.Inventory;
                }
            }
            else if (currentScreen == Menu.Main)
            {
                KeyboardState keys   = Keyboard.GetState();
                bool          change = true;

                if (!keytimer.Enabled)
                {
                    if (keys.IsKeyDown(Keys.Up))
                    {
                        current.Move(Direction.Up);
                    }

                    else if (keys.IsKeyDown(Keys.Down))
                    {
                        current.Move(Direction.Down);
                    }
                    else if (keys.IsKeyDown(Keys.Enter))
                    {
                        string test = current.GetCurrentCaption();

                        if (current == menu)
                        {
                            if (test == "Exit")
                            {
                                Exit();
                            }
                            else if (test == "Options")
                            {
                                current = options;
                            }
                            else if (test == "Continue")
                            {
                                currentScreen = Menu.Play;
                            }
                        }

                        else if (current == options)
                        {
                            if (test == "Back")
                            {
                                current = menu;
                            }
                        }
                        else if (current == options)
                        {
                            if (test == "FullScreen")
                            {
                                graphics.IsFullScreen = false;
                            }
                        }
                    }
                    else
                    {
                        change = false;
                    }

                    if (change)
                    {
                        keytimer          = new Timer();
                        keytimer.Interval = 200;
                        keytimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
                        keytimer.Enabled  = true;
                    }
                }
            }
            base.Update(gameTime);
        }
コード例 #2
0
        protected override void Update(GameTime gameTime)
        {
            if (currentScreen == Menu.Play)
            {
                if (songplaying == false)
                {
                    backsong = Content.Load <Song>("Game of Thrones");
                    MediaPlayer.Play(backsong);
                    songplaying = true;
                }
                else if (MediaPlayer.State == MediaState.Stopped)
                {
                    songplaying = false;
                }
                if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
                {
                    Exit();
                }
                clean();
                // get player input
                GamePadState  gamePadState = GamePad.GetState(PlayerIndex.One);
                KeyboardState keyState     = Keyboard.GetState();

                // record current position before movement
                int tempx = map.ObjectGroups["Objects"].Objects["Player"].X;
                int tempy = map.ObjectGroups["Objects"].Objects["Player"].Y;
                Position = new Vector2(map.ObjectGroups["Objects"].Objects["Player"].X, map.ObjectGroups["Objects"].Objects["Player"].Y);

                // process movement input move player object
                ProcessMovement(keyState, gamePadState);

                //now we have moved checkbounds
                if (CheckBounds(map.ObjectGroups["Objects"].Objects["Player"]))
                {
                    map.ObjectGroups["Objects"].Objects["Player"].X = tempx;
                    map.ObjectGroups["Objects"].Objects["Player"].Y = tempy;
                }

                // check for player interaction with coins and exits within the game
                var       p         = map.ObjectGroups["Objects"].Objects["Player"];
                Rectangle playerRec = new Rectangle(p.X, p.Y, p.Width, p.Height);
                CheckCoins(playerRec);
                CheckExits(playerRec);
                CheckKey(playerRec);
                CheckEnd(playerRec);
                CheckDoor(playerRec);

                // update player object with position and viewport
                Vector2 Test = (viewportPosition + new Vector2(0, 100) - new Vector2((graphics.PreferredBackBufferWidth / 2), (graphics.PreferredBackBufferHeight / 2)));
                foreach (var sprite in _sprites)
                {
                    sprite.Update(gameTime, Position, Test);
                    foreach (Projectile P in sprite.PlayerProjectiles)
                    {
                        Rectangle proj = new Rectangle((int)P.Position.X, (int)P.Position.Y, P.mapobj.Width, P.mapobj.Height);
                        foreach (Enemy E in EnemyList)
                        {
                            Rectangle Erec = new Rectangle((int)E.mapobj.X, (int)E.mapobj.Y, E.mapobj.Width, E.mapobj.Height);
                            if (proj.Intersects(Erec) && P.active)
                            {
                                E.Health        -= 1;
                                P.mapobj.Texture = P.Blank;
                                P.active         = false;
                                Console.WriteLine("shot: " + proj + " -- " + Erec + " -- " + E.Health);
                            }
                        }
                    }
                    sprite.Position = new Vector2(map.ObjectGroups["Objects"].Objects["Player"].X, map.ObjectGroups["Objects"].Objects["Player"].Y);
                }
                // update each enemy
                int i = 1;
                foreach (Enemy E in EnemyList)
                {
                    if (E.Health > 0)
                    {
                        // get position of enemy
                        Vector2 temp = E.Position;

                        // update the enemy to move
                        E.Update(gameTime, Position);

                        foreach (Projectile P in E.PlayerProjectiles)
                        {
                            Rectangle            proj   = new Rectangle((int)P.Position.X, (int)P.Position.Y, P.mapobj.Width, P.mapobj.Height);
                            Squared.Tiled.Object obj    = map.ObjectGroups["Objects"].Objects["Player"];
                            Rectangle            objrec = new Rectangle(
                                obj.X,
                                obj.Y,
                                obj.Width,
                                obj.Height
                                );
                            if (proj.Intersects(objrec) && P.active)
                            {
                                _sprites[0].Health -= 1;
                                P.mapobj.Texture    = P.Blank;
                                P.active            = false;
                            }
                        }

                        // check for enemy collision between the bounds and other enemies
                        if (CheckBounds(map.ObjectGroups["Objects"].Objects["Enemy" + i]) || CheckEnemy(map.ObjectGroups["Objects"].Objects["Enemy" + i], E))
                        {
                            // collision so set position back to temp value
                            E.Position = temp;
                        }
                        else
                        {
                            // no collision so update the enemy position in map
                            map.ObjectGroups["Objects"].Objects["Enemy" + i].X = (int)E.Position.X;
                            map.ObjectGroups["Objects"].Objects["Enemy" + i].Y = (int)E.Position.Y;

                            if (true)
                            {
                                Squared.Tiled.Object tempp = new Squared.Tiled.Object();
                                tempp.X       = (int)E.Position.X;
                                tempp.Y       = (int)E.Position.Y;
                                tempp.Width   = 30;
                                tempp.Height  = 30;
                                tempp.Type    = "proj";
                                tempp.Texture = projTexture;
                                if (E.Fire(E.Position, tempp, blankTexture))
                                {
                                    map.ObjectGroups["Objects"].Objects.Add("proj" + projcount, tempp);
                                    projcount++;
                                }
                            }
                        }
                        i++;
                    }
                }


                viewportPosition = new Vector2(map.ObjectGroups["Objects"].Objects["Player"].X, map.ObjectGroups["Objects"].Objects["Player"].Y);
                KeyboardState keys = Keyboard.GetState();

                if (_sprites[0].Health <= 0)
                {
                    currentScreen = Menu.Main;
                    LoadContent();
                }
                // Takes to main menu or inventory
                if (keys.IsKeyDown(Keys.Tab))
                {
                    currentScreen = Menu.Main;
                }
                else if (keys.IsKeyDown(Keys.I))
                {
                    currentScreen = Menu.Inventory;
                    string temp = "No";
                    if (key_collected)
                    {
                        temp = "Yes";
                    }
                    inventory = new SimpleTextUI(this, big, new[] { "Coins: " + coin_collected, "Key: " + temp, "Back" })
                    {
                        TextColor       = Color.Black,
                        SelectedElement = new TextElement(">", Color.White),
                        Align           = Alignment.Left
                    };
                    current = inventory;
                }
            }
            // code for the controlling menu screens
            else if (currentScreen == Menu.Main)
            {
                KeyboardState keys   = Keyboard.GetState();
                bool          change = true;

                if (!keytimer.Enabled)
                {
                    if (keys.IsKeyDown(Keys.Up))
                    {
                        current.Move(Direction.Up);
                    }

                    else if (keys.IsKeyDown(Keys.Down))
                    {
                        current.Move(Direction.Down);
                    }
                    else if (keys.IsKeyDown(Keys.Left))
                    {
                        current.Move(Direction.Left);
                        if (current.GetCurrentCaption() == "Video")
                        {
                            graphics.IsFullScreen = (current.GetCurrentValue() == "FullScreen");
                            graphics.ApplyChanges();
                        }
                    }

                    else if (keys.IsKeyDown(Keys.Right))
                    {
                        current.Move(Direction.Right);
                        if (current.GetCurrentCaption() == "Video")
                        {
                            graphics.IsFullScreen = (current.GetCurrentValue() == "FullScreen");
                            graphics.ApplyChanges();
                        }
                    }
                    else if (keys.IsKeyDown(Keys.Enter))
                    {
                        string test = current.GetCurrentCaption();

                        if (current == menu)
                        {
                            if (test == "Exit")
                            {
                                Exit();
                            }
                            else if (test == "Options")
                            {
                                current = options;
                            }
                            else if (test == "Continue")
                            {
                                currentScreen = Menu.Play;
                            }
                            else if (test == "Credits")
                            {
                                current = credits;
                            }
                        }
                        else if (current == credits)
                        {
                            if (test == "Back")
                            {
                                current = menu;
                            }
                        }
                        else if (current == options)
                        {
                            if (test == "Back")
                            {
                                current = menu;
                            }
                        }
                    }
                    else
                    {
                        change = false;
                    }

                    if (change)
                    {
                        keytimer          = new Timer();
                        keytimer.Interval = 200;
                        keytimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
                        keytimer.Enabled  = true;
                    }
                }
            }
            else if (currentScreen == Menu.Inventory)
            {
                KeyboardState keys   = Keyboard.GetState();
                bool          change = true;

                if (!keytimer.Enabled)
                {
                    if (keys.IsKeyDown(Keys.Up))
                    {
                        current.Move(Direction.Up);
                    }

                    else if (keys.IsKeyDown(Keys.Down))
                    {
                        current.Move(Direction.Down);
                    }
                    else if (keys.IsKeyDown(Keys.Enter))
                    {
                        string test = current.GetCurrentCaption();

                        if (current == inventory)
                        {
                            if (test == "Back")
                            {
                                currentScreen = Menu.Play;
                            }
                        }
                    }
                }
                else
                {
                    change = false;
                }

                if (change)
                {
                    keytimer          = new Timer();
                    keytimer.Interval = 200;
                    keytimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
                    keytimer.Enabled  = true;
                }
            }
            base.Update(gameTime);
        }