ArcadeRPG.GameEngine.Update C# (CSharp) Method

Update() public method

Allows the game to run logic such as updating the world, checking for collisions, gathering input, and playing audio.
public Update ( GameTime gameTime ) : void
gameTime GameTime Provides a snapshot of timing values.
return void
        public void Update(GameTime gameTime)
        {
            if (game_state.local_player.getHealth() <= 0)
            {
                die();
                return;
            }

            PlayerDir pot_dir = game_state.local_player.getDirection();
            int pot_x = game_state.local_player.getX();
            int pot_y = game_state.local_player.getY();

            double new_width = ((double)health_bar_width) * (double)((double)game_state.local_player.getHealth() / (double)game_state.local_player.getMaxHealth());
            health_bar_rec.Width = (int)new_width;

            TouchCollection touchCollection = TouchPanel.GetState();
            foreach (TouchLocation tl in touchCollection)
            // for each place the screen has been touched at the point of "getState"
            {

                //if the screen is pressed on an arrow, move sprite accordingly
                if (tl.State == TouchLocationState.Pressed || tl.State == TouchLocationState.Moved)
                {
                    if ((tl.Position.X >= 75) && (tl.Position.X <= 120) && (tl.Position.Y >= 310) && (tl.Position.Y <= 380)) // up arrow
                    {
                        pot_y -= game_state.local_player.getSpeed();
                        pot_dir = PlayerDir.UP;
                        // -1 = up direction (axis goes down incresingly)
                    }

                    if ((tl.Position.X >= 75) && (tl.Position.X <= 120) && (tl.Position.Y >= 420) && (tl.Position.Y <= 480)) // down arrow
                    {
                        pot_y += game_state.local_player.getSpeed();
                        pot_dir = PlayerDir.DOWN;
                    }

                    if ((tl.Position.X >= 15) && (tl.Position.X <= 70) && (tl.Position.Y >= 365) && (tl.Position.Y <= 430)) // left arrow
                    {
                        pot_x -= game_state.local_player.getSpeed();
                        pot_dir = PlayerDir.LEFT;
                    }

                    if ((tl.Position.X >= 115) && (tl.Position.X <= 170) && (tl.Position.Y >= 365) && (tl.Position.Y <= 430)) // right arrow
                    {
                        pot_x += game_state.local_player.getSpeed();
                        pot_dir = PlayerDir.RIGHT;
                    }

                    //Collision and updating
                    if (game_state.coll_engine.check_map_col(pot_x, pot_y, game_state.local_player.getWidth(), game_state.local_player.getHeight()) == false)
                    {
                        if (game_state.obj_mang.checkForGateAt(pot_x, pot_y, game_state.local_player.getWidth(), game_state.local_player.getHeight()) && !game_state.local_player.hasKey())
                        {
                            //there is a gate and the player does not have a key
                        }
                        else //no gate or the player has the key
                        {
                            game_state.local_player.setX(pot_x);
                            game_state.local_player.setY(pot_y);

                            Item item = game_state.obj_mang.getItemAt(pot_x, pot_y, game_state.local_player.getWidth(), game_state.local_player.getHeight());
                            if (item != null)
                            {
                                game_state.fx_engine.RequestSound(soundType.ITEM_PICKUP);
                                game_state.local_player.addItem(item); // item collected, set weapon type to respective type of item collected

                                if (game_state.local_player.getWeapon() == weaponType.NONE)
                                {
                                    if (item.getType() == itemType.SWORD)
                                    {
                                        game_state.local_player.setWeapon(weaponType.SWORD);
                                    }
                                    if (item.getType() == itemType.LASER)
                                    {
                                        game_state.local_player.setWeapon(weaponType.LASER);
                                    }
                                }
                            }

                            if (!game_state.local_player.moving) // player is still
                            {
                                game_state.local_player.setDirection(pot_dir);
                                game_state.local_player.moving = true;
                                character_sprite[(int)game_state.local_player.getWeapon()].StartAnimating((int)pot_dir * 3, ((int)pot_dir * 3) + 2);
                            }
                        }

                    }

                }
                else if (tl.State == TouchLocationState.Released)
                {
                    if (tl.Position.X >= backpackpos.X && tl.Position.X <= backpackpos.X + backpack.Width && tl.Position.Y >= backpackpos.Y && tl.Position.Y <= backpackpos.Y + backpack.Height)
                    {
                        // has user elected to bring up inventory?
                        backpackmenu.backpack_touched = true;
                        //^^ yes
                    }
                    if (backpackmenu.backpack_touched == false) // if they havent
                    {
                        if ((tl.Position.X >= 700) && (tl.Position.Y >= 385)) // and the Fire button is tapped
                        {

                            game_state.fx_engine.RequestRumble(200);
                            if (game_state.local_player.getWeapon() == weaponType.LASER) //if the proper weapon is selected and able to shoot bullets while the fire button is tapped

                            {
                                game_state.bullet_engine.fire(game_state.local_player.getX() + (int)character_sprite[(int)game_state.local_player.getWeapon()].size.X / 2,
                                    game_state.local_player.getY() + (int)character_sprite[(int)game_state.local_player.getWeapon()].size.Y / 2,
                                    game_state.local_player.getDirection(),
                                    bulletOwner.PLAYER,
                                    bulletType.SMALL);
                                game_state.fx_engine.RequestSound(soundType.SHOOT); // shoot a bullet
                            }
                            else if (game_state.local_player.getWeapon() == weaponType.SWORD)
                                // else swing the sword, dont shoot bullets
                            {
                                sword_swing = true;
                                game_state.fx_engine.RequestSound(soundType.SWORD);
                                int bullet_x = 0;
                                int bullet_y = 0;
                                switch (game_state.local_player.getDirection())
                                {
                                    case PlayerDir.DOWN:
                                        bullet_x = game_state.local_player.getX() - 2;
                                        bullet_y = game_state.local_player.getY() + game_state.local_player.getHeight();
                                        break;
                                    case PlayerDir.UP:
                                        bullet_x = game_state.local_player.getX() - 2;
                                        bullet_y = game_state.local_player.getY() - game_state.local_player.getHeight();
                                        break;
                                    case PlayerDir.LEFT:
                                        bullet_x = game_state.local_player.getX() - (game_state.local_player.getWidth());
                                        bullet_y = game_state.local_player.getY();
                                        break;
                                    case PlayerDir.RIGHT:
                                        bullet_x = game_state.local_player.getX() + game_state.local_player.getWidth();
                                        bullet_y = game_state.local_player.getY();
                                        break;
                                }

                                sword_bullet = game_state.bullet_engine.fire(bullet_x, bullet_y, game_state.local_player.getDirection(), bulletOwner.PLAYER, bulletType.SWORD);
                            }
                        }
                    }
                    else // bring up the inventory and display the items properly and in an orderly fashion
                    {
                        Vector2 formatpos = new Vector2(315, 170);
                        int tileSize = game_state.tile_engine.getTileSize();

                        Item toRemove = null;
                        foreach (Item i in game_state.local_player.getInventory())
                        {
                            Rectangle dest = new Rectangle((int)formatpos.X, (int)formatpos.Y, 300, 40);
                            if(dest.Contains((int)tl.Position.X,(int)tl.Position.Y))
                            {
                                //menu item Clicked and ready to be used in game
                                switch (i.getType())
                                {
                                    case itemType.LASER: game_state.local_player.setWeapon(weaponType.LASER); break;
                                    case itemType.SWORD: game_state.local_player.setWeapon(weaponType.SWORD); break;
                                    case itemType.ATT_BOOST: toRemove = i; att_boost_delay = 10000; game_state.local_player.setAttackBonus(5); break;
                                    case itemType.DEF_BOOST: toRemove = i; def_boost_delay = 10000; game_state.local_player.setDefenseBonus(5); break;
                                    case itemType.KEY:
                                    default: break;
                                }
                                backpackmenu.backpack_touched = false;  // exit from inventory now
                            }
                            formatpos.Y += 50;
                        }
                        if (toRemove != null)
                            game_state.local_player.removeItem(toRemove);

                        if (backpackExit.Contains((int)tl.Position.X, (int)tl.Position.Y)) // coordinates of the "exit" button in inventory
                        {
                            backpackmenu.backpack_touched = false; //user has exited, return to main game screen
                        }
                    }

                    character_sprite[(int)game_state.local_player.getWeapon()].StopAnimating();
                    game_state.local_player.moving = false;
                }

            }

            if (sword_swing) // swing sword
            {
                sword_delay += gameTime.ElapsedGameTime.Milliseconds;
                if (sword_delay > 200)
                {
                    sword_delay = 0;
                    sword_swing = false;
                    game_state.bullet_engine.RemoveBullet(sword_bullet);
                }
            }

            if (game_state.local_player.getAttackBonus() > 0) //add attack bonuses
            {
                att_boost_delay -= gameTime.ElapsedGameTime.Milliseconds;
                if (att_boost_delay <= 0)
                {
                    game_state.local_player.setAttackBonus(0);
                }
            }

            if (game_state.local_player.getDefenseBonus() > 0) // add defensive bonuses
            {
                def_boost_delay -= gameTime.ElapsedGameTime.Milliseconds;
                if (def_boost_delay <= 0)
                {
                    game_state.local_player.setDefenseBonus(0);
                }
            }

            List<ColToken> cols = game_state.local_player.col_tok.GetCollisions();
            for (int j = 0; j < cols.Count(); ++j)
            {
                ColToken coll = cols.ElementAt(j);
                if (coll.GetLocalType() != ColType.MAP)
                {
                    if (!game_state.local_player.hurt)
                    {
                        int damage = 0;
                        if (coll.GetLocalType() == ColType.BULLET)
                        {

                            Bullet bul = (Bullet)coll.GetParent();
                            if (bul.owner == bulletOwner.PLAYER)
                            {
                                continue;
                            }

                        }
                        else if (coll.GetLocalType() == ColType.MONSTER)
                        {
                            Enemy enem = (Enemy)coll.GetParent();
                            damage = enem.getAttack();
                        }
                        //Player gets hurt!

                        game_state.local_player.setHealth(game_state.local_player.getHealth() + game_state.local_player.getDefenseBonus() - damage);
                        game_state.local_player.hurt = true;
                        game_state.fx_engine.RequestSound(soundType.PLAYER_HURT);
                        //Get hurt by a little
                        hurt_time = 500;
                    }
                }
            }
            game_state.local_player.col_tok.ResetCollisions();
            game_state.local_player.col_tok.update(game_state.local_player.getX(), game_state.local_player.getY());

            if(game_state.local_player.hurt) {
                hurt_time -= gameTime.ElapsedGameTime.Milliseconds; // dont repeatedly get hurt, only get hurt once per attack by an enemy
                if (hurt_time <= 0)
                {
                    game_state.local_player.hurt = false; // time to get hurt has run out
                }
            }
            currTime -= gameTime.ElapsedGameTime; // start timer on actual game

            if (currTime.TotalSeconds <= 0) // time per level has run out
            {
                return;
            }

            game_state.monster_engine.Update(gameTime.ElapsedGameTime.Milliseconds);
            game_state.bullet_engine.Update();

            game_state.coll_engine.Update();

            game_state.fx_engine.Update(gameTime.ElapsedGameTime.Milliseconds);
            if (testAtExit( game_state.local_player.getX(),(game_state.local_player.getY()+game_state.local_player.getHeight()-1))||testAtExit( game_state.local_player.getX()+game_state.local_player.getWidth(),(game_state.local_player.getY()+game_state.local_player.getHeight())))
            {
                int tempn = game_state.tile_engine.getCurrentLevel() + 1;
                LoadLevel(tempn);
            }
        }

Usage Example

Esempio n. 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)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            {
                this.Exit();
            }

            if (DEV_MODE)
            {
                if (intro.isShowing())
                {
                    intro.Hide();
                }
                if (instruct.isShowing())
                {
                    instruct.Hide();
                }
                if (instruct2.isShowing())
                {
                    instruct2.Hide();
                }
            }

            if (intro.isShowing()) // if the introduction screen is showing, continue showing until introTime runs out (5 seconds)
            {
                intro.update(gameTime);
            }
            else if (instruct.isShowing()) // if instructions are showing, continue showing for 5 seconds and let it advance
            {
                instruct.update(gameTime);
            }
            else if (instruct2.isShowing()) // if second instructions are showing, continue showing
            {
                instruct2.update(gameTime);
            }
            else if (timex.isShowing()) // user ran out of time, time expired screen is up. will wait here for user to choose to play again or not
            {
                timex.update(gameTime);
                if (timex.play_again)             // user wants to play again
                {
                    timex.Hide();                 // so hide game screen and
                    timex.reset();
                    game_engine.Update(gameTime); // go to game environment
                }
                else if (!timex.isRunning())      // user did not select play again before time ran out
                {
                    timex.Hide();
                    spriteBatch.Begin();
                    gameover.Show(spriteBatch); // exit game
                    spriteBatch.End();
                }
            }
            else if (gameover.isShowing()) // game over screen is showing
            {
                gameover.update(gameTime);
                if (gameover.exit)
                {
                    this.Exit(); // end game
                }
            }
            else
            {
                game_engine.Update(gameTime);
                if (game_engine.gameEnded)
                {
                    spriteBatch.Begin();
                    gameover.Show(spriteBatch);
                    spriteBatch.End();
                }
            }


            base.Update(gameTime);
        }