/// <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> public void Update(GameTime gameTime) // is the user dead? if so handle it { 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 >= 60) && (tl.Position.X <= 110) && (tl.Position.Y >= 330) && (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 >= 60) && (tl.Position.X <= 110) && (tl.Position.Y >= 420) && (tl.Position.Y <= 470)) // down arrow { pot_y += game_state.local_player.getSpeed(); pot_dir = PlayerDir.DOWN; } if ((tl.Position.X >= 15) && (tl.Position.X <= 65) && (tl.Position.Y >= 375) && (tl.Position.Y <= 425)) // left arrow { pot_x -= game_state.local_player.getSpeed(); pot_dir = PlayerDir.LEFT; } if ((tl.Position.X >= 100) && (tl.Position.X <= 150) && (tl.Position.Y >= 375) && (tl.Position.Y <= 425)) // 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); } }
/// <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> 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; //Put a nicer function here 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 >= 50) && (tl.Position.X <= 100) && (tl.Position.Y >= 345) && (tl.Position.Y <= 385)) // up arrow { //local_player.setY(local_player.getY() - 3); pot_y -= game_state.local_player.getSpeed(); pot_dir = PlayerDir.UP; // -1 = up direction (axis goes down incresingly) } if ((tl.Position.X >= 50) && (tl.Position.X <= 100) && (tl.Position.Y >= 425) && (tl.Position.Y <= 465)) // down arrow { pot_y += game_state.local_player.getSpeed(); pot_dir = PlayerDir.DOWN; // local_player.setY(local_player.getY() + 3); } if ((tl.Position.X >= 15) && (tl.Position.X <= 55) && (tl.Position.Y >= 385) && (tl.Position.Y <= 425)) // left arrow { pot_x -= game_state.local_player.getSpeed(); pot_dir = PlayerDir.LEFT; //local_player.setX(local_player.getX() - 3); // -1 = left because left to right } if ((tl.Position.X >= 90) && (tl.Position.X <= 130) && (tl.Position.Y >= 385) && (tl.Position.Y <= 425)) // right arrow { pot_x += game_state.local_player.getSpeed(); pot_dir = PlayerDir.RIGHT; //local_player.setX(local_player.getX() + 3); } //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); //System.Diagnostics.Debug.WriteLine("Stuff: {0},{1}", pot_x, 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.local_player.addItem(item); 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) { 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) { backpackmenu.backpack_touched = true; } if (backpackmenu.backpack_touched == false) { if ((tl.Position.X >= 700) && (tl.Position.Y >= 385)) // Fire button { if (game_state.local_player.getWeapon() == weaponType.LASER) { 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); } else if (game_state.local_player.getWeapon() == weaponType.SWORD) { 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(); //+ game_state.local_player.getHeight(); break; case PlayerDir.RIGHT: bullet_x = game_state.local_player.getX() + game_state.local_player.getWidth(); bullet_y = game_state.local_player.getY(); //+ game_state.local_player.getHeight(); break; } sword_bullet = game_state.bullet_engine.fire(bullet_x, bullet_y, game_state.local_player.getDirection(), bulletOwner.PLAYER, bulletType.SWORD); } } } else { 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 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) { 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) { att_boost_delay -= gameTime.ElapsedGameTime.Milliseconds; if (att_boost_delay <= 0) { game_state.local_player.setAttackBonus(0); } } if (game_state.local_player.getDefenseBonus() > 0) { 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) { //BulletEngine will deal the damage for this /* * Bullet bull = (Bullet)coll.GetParent(); * if (bull.owner == bulletOwner.ENEMY) * { * switch (bull.type) * { * case bulletType.SMALL: * damage = 5; * break; * case bulletType.SWORD: * damage = 10; * break; * * } * }*/ 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; //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; if (hurt_time <= 0) { game_state.local_player.hurt = false; } } currTime -= gameTime.ElapsedGameTime; // start timer on actual game if (currTime.TotalSeconds <= 0) { return; } game_state.monster_engine.Update(gameTime.ElapsedGameTime.Milliseconds); game_state.bullet_engine.Update(); game_state.coll_engine.Update(); game_state.fx_engine.Update(); 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); } }