// Update method public override void Update(GameTime gameTime) { //If there is a temporary tile (as in the player is colliding with a tile) if (tempCollisTile != null) { //If the player is still technically colliding with a tile but is above or below that tile if ((player.ObjectRectangle.Bottom < tempCollisTile.ObjectRectangle.Top) || (player.ObjectRectangle.Top > tempCollisTile.ObjectRectangle.Bottom)) { //The player class will call its method to rest the collision, allowing the player to overcome collision and move left or right when he jumps or is above/below the tile player.ResetCollision(); //The temporary tile is null, preventing collision from being reset at a time it shouldn't be tempCollisTile = null; } } // The player will register as being paused if the screen is anything other than the level screen if (screenState == ScreenState.LevelOneScreen) { //The music only plays during the game's playable level gameMusicInstance.Play(); player.IsPaused = false; } else { //The sound won't play if it paused but reset if (screenState == ScreenState.PauseScreen) { gameMusicInstance.Pause(); bossMusicInstance.Pause(); } //It stops when the game ends, due to player death or victory else if (screenState == ScreenState.DeathScreen || screenState == ScreenState.WinScreen) { gameMusicInstance.Stop(); bossMusicInstance.Stop(); } player.IsPaused = true; } // The B key will be registered as being down by incrementing the int value while it is down and setting that value to 0 while it is up //Thiis will prevent the user from skipping through multiple presses of the same key by holding it down if (userInputObject.UserInputState.IsKeyDown(Keys.B)) { keyBPressNumber++; } else { keyBPressNumber = 0; } // Same as above but with the Enter key if (userInputObject.UserInputState.IsKeyDown(Keys.Enter)) { keyEnterPressNumber++; } else { keyEnterPressNumber = 0; } // Check if the player is dead, victorius, screen collision, and movement player.CheckPlayerDeath(); player.CheckWin(); player.CheckScreenCollision(); player.CheckMovementInput(); // The boss's attack will register as being paused if the game is not in the levelone state if (screenState != ScreenState.LevelOneScreen) { //The game will only allow the player to exit if it is not on the Level, Instruction, or Exposition screens if (screenState != ScreenState.InstructScreen && screenState != ScreenState.ExpositScreen) { CheckExitGame(); } levelBoss.Attack.IsPaused = true; } else { levelBoss.Attack.IsPaused = false; } // The player movement, both left and right, (in that the screen scrolls) is triggered in the world class if (player.LeftScroll == true) { checkMapScrollLeft(); } // To prevent the screen from continuously scrolling, the scroll boolean of the player is falsified player.LeftScroll = false; if (player.RightScroll == true) { checkMapScrollRight(); } player.RightScroll = false; // When the game is started, collision for most of the objects are checked if (gameStarted == true) { // Every enemy in the enemy list is checked for collision with the player and collision with the magic foreach (Enemy badGuy in enemyList) { badGuy.Walk(gameTime); if ((badGuy.ObjectRectangle.X - player.ObjectRectangle.X < 150 && badGuy.ObjectRectangle.X - player.ObjectRectangle.X > -150) && (badGuy.ObjectRectangle.Y - player.ObjectRectangle.Y < 150) && badGuy.ObjectRectangle.Y - player.ObjectRectangle.Y > -150) { if (badGuy.CheckCollision(player) == true) { player.HandleCollision(badGuy); } } // When the magic spell is active (firing), the enemy will check if it is colliding with the magic if (spell.MagicActive == true) { // If it is, both the enemy and the spell will handle their respective collision methods if (badGuy.CheckCollision(spell) == true) { badGuy.HandleCollision(spell); spell.HandleCollision(badGuy); // Magic is set back to the center of the player spell.SetToCenter(player.ObjectRectangle.X, player.ObjectRectangle.Y); } } // When the enemy is killed, his rectangle is moved off screen so it will no longer be in play if (badGuy.IsAlive == false) { Rectangle tempRectX = badGuy.ObjectRectangle; tempRectX.X = 5000; badGuy.ObjectRectangle = tempRectX; } } // Checks if the player collides with the tile foreach (GameWorldTile tileInWorld in gameWorldTileArray) { if (tileInWorld.CollidableProp == true) { //The only tiles that are checked for collision with the player are those that are within 3 tile-lengths of the player if ((tileInWorld.ObjectRectangle.X - player.ObjectRectangle.X < 150 && tileInWorld.ObjectRectangle.X - player.ObjectRectangle.X > -150) && (tileInWorld.ObjectRectangle.Y - player.ObjectRectangle.Y < 150) && tileInWorld.ObjectRectangle.Y - player.ObjectRectangle.Y > -150) { if (tileInWorld.CheckCollision(player) == true) { //If one of those tiles are colliding with the player, the player will handle the collision accordingly player.HandleCollision(tileInWorld); //In addition, the temporary tile is set to the tile the player is currently colliding with, for use with the player's "ResetCollision" method tempCollisTile = tileInWorld; } } } } // Checks if the magic collides with the tiles foreach (GameWorldTile tileInWorld in gameWorldTileArray) { if (tileInWorld.CollidableProp == true) { if (tileInWorld.CheckCollision(spell) == true) { spell.HandleCollision(tileInWorld); // Magic is set back to the center of the player spell.SetToCenter(player.ObjectRectangle.X, player.ObjectRectangle.Y); } } } // When the player fires magic, it will check the boss's collision if (spell.MagicActive == true) { if (levelBoss.CheckCollision(spell) == true) { levelBoss.HandleCollision(spell); spell.HandleCollision(levelBoss); // Magic is set back to the center of the player spell.SetToCenter(player.ObjectRectangle.X, player.ObjectRectangle.Y); } } // The boss and player will check if they are colliding and thusly kill the player if (levelBoss.CheckCollision(player) == true) { player.HandleCollision(levelBoss); } // If the boss dies, it's rectangle will be sent off map to prevent collisions from the grave if (levelBoss.IsAlive == false) { Rectangle tempRectX = levelBoss.ObjectRectangle; tempRectX.X = 5000; levelBoss.ObjectRectangle = tempRectX; } // While the boss is alive and on the screen if (levelBoss.IsAlive == true && levelBoss.ObjectRectangle.X < 1000) { // And while the game is actually playing if (screenState == ScreenState.LevelOneScreen) { //When the player fights the boss, the game music stops and the boss music starts gameMusicInstance.Stop(); bossMusicInstance.Play(); // The boss will attack and the player will make sure if they are colliding or not levelBoss.AttackPattern(); if (levelBoss.Attack.CheckCollision(player) == true) { player.HandleCollision(levelBoss.Attack); } } } // Check to see if the boss needs to die levelBoss.CheckBossDeath(); if (levelBoss.IsAlive == false) { //When the boss dies, the boss music stops bossMusicInstance.Stop(); } // Gravity will only act on the player if the game is not paused if (player.IsPaused == false) { Gravity(player); } } //Continuously make sure that the right screens are showing and everything that should be on screen is CheckScreenState(); CheckShowAssets(); }
// Load up the tiles that comprise the game world #region Load World In // The LoadWorldIn method takes a text file and loads up tiles onto the screen depending on the symbols found within the text document that is loaded // The game loads in enemies and a boss in via the level's text file in much the same way as it does with the ground tiles public void LoadWorldIn(Game game, string file) { // A StreamReader object called mapReader which will be used to read the map file StreamReader mapReader = null; try { // Setting mapReader to a new StreamReader mapReader = new StreamReader(file); //Creating a string that will get and hold a specific line in ther text file. String line = (""); //Row int counter = 0; while (line != null) { line = mapReader.ReadLine(); if (line != null) { //Parse the text file line into individual letters String[] data = line.Split(','); //Column for (int j = 0; j < 192; j++) { if (data[j] == "D") { //The position of the tile's rectangle is assigned by multiplying the current j (X) and counter (Y) gameWorldTileArray[j, counter] = new GameWorldTile(this.Game, groundTile, new Rectangle(50 * j, 50 * counter, 50, 50), true); } else if (data[j] == "G") { //The position of the tile's rectangle is assigned by multiplying the current j (X) and counter (Y) gameWorldTileArray[j, counter] = new GameWorldTile(this.Game, grassTile, new Rectangle(50 * j, 50 * counter, 50, 50), true); } //When the map file contains an "E" at the location, an enemy will be drawn else if (data[j] == "E") { //The enemyList will have an object (enemy) added to it for future referencing of all enemies enemyList.Add(new Enemy(this.Game)); //The added enemy's rectangle's X and Y are set to the coordinates of the tile Rectangle tempRectX = enemyList.ElementAt(enemyList.Count - 1).ObjectRectangle; tempRectX.X = 50 * j; enemyList.ElementAt(enemyList.Count - 1).ObjectRectangle = tempRectX; Rectangle tempRectY = enemyList.ElementAt(enemyList.Count - 1).ObjectRectangle; tempRectY.Y = 50 * counter; enemyList.ElementAt(enemyList.Count - 1).ObjectRectangle = tempRectY; //The starting position of the enemy will be copied and stored for future reloads and reference enemyList.ElementAt(enemyList.Count - 1).StartPositX = enemyList.ElementAt(enemyList.Count - 1).ObjectRectangle.X; enemyList.ElementAt(enemyList.Count - 1).StartPositY = enemyList.ElementAt(enemyList.Count - 1).ObjectRectangle.Y; //Where the enemy would be, a sky tile is added so that the tile array is not incomplete gameWorldTileArray[j, counter] = new GameWorldTile(this.Game, skyTile, new Rectangle(50 * j, 50 * counter, 50, 50), false); } //The load portion for the boss else if (data[j] == "B") { //The earlier created instance of boss will be addeed to and implemented //The added boss's rectangle's X and Y are set to the coordinates of the tile Rectangle tempRectX = levelBoss.ObjectRectangle; tempRectX.X = 50 * j; levelBoss.ObjectRectangle = tempRectX; Rectangle tempRectY = levelBoss.ObjectRectangle; tempRectY.Y = 50 * counter; levelBoss.ObjectRectangle = tempRectY; //The starting position of the boss will be copied and stored for future reloads and reference levelBoss.StartPositX = levelBoss.ObjectRectangle.X; levelBoss.StartPositY = levelBoss.ObjectRectangle.Y; //Where the boss would be, a sky tile is added so that the tile array is not incomplete gameWorldTileArray[j, counter] = new GameWorldTile(this.Game, /*worldPhysics,*/ skyTile, new Rectangle(50 * j, 50 * counter, 50, 50), false); } else { //Otherwise, the tile is set as a "sky" tile that is not intersectable. //Later on, there will be other tiles to be denoted by letters. gameWorldTileArray[j, counter] = new GameWorldTile(this.Game, /*worldPhysics,*/ skyTile, new Rectangle(50 * j, 50 * counter, 50, 50), false); } } counter++; } } } catch (Exception e) { Console.WriteLine("There was an error: " + e.Message); } finally { //If the map reader has not bugged out and is null (otherwise a crash would occure) if (mapReader != null) { //Regardless of a crash or not, the loader closes down mapReader.Close(); } } }