/// <summary> /// Allows the game to perform any initialization it needs to before starting to run. /// This is where it can query for any required services and load any non-graphic /// related content. Calling base.Initialize will enumerate through any components /// and initialize them as well. /// </summary> protected override void Initialize() { assignConsts = new ConstantsAssigning(); readIn = new ReadIn(); writeOutConsts = new WriteOutConsts(); spriteDict = new Dictionary <string, Texture2D>(); fontDict = new Dictionary <string, SpriteFont>(); soundDict = new Dictionary <string, SoundEffect>(); nets = new Stack <Net>(5); jaguars = new List <Jaguar>(); rocks = new List <Rock>(); flowers = new List <Flower>(); leaves = new List <Leaf>(); spawnNet = new SpawnNet(); menuText = new MenuText(); listManagement = new ListManagement(); rand = new Random(); saveSloth = new SaveSloth(); saveRoom = new SaveRoom(); fonts = new SpriteFont[3]; levels = new Room[4]; menus = new Menu[4]; spriteBatch = new SpriteBatch(GraphicsDevice); // Create a new SpriteBatch, which can be used to draw textures. viewPortRect = new Rectangle(0, 0, graphics.GraphicsDevice.Viewport.Width, graphics.GraphicsDevice.Viewport.Height); // creates new rectangle with the same dimensions as the screen base.Initialize(); }
/// <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) { KeyboardState currentKeyboardState = Keyboard.GetState(); // gets the current state of the keyboard if (currentKeyboardState.IsKeyDown(Keys.Escape)) // will close program if escape key is pressed { Environment.Exit(0); } // All Logic that occurs during a menu is open (main screen, info screen, and game over) if (menuOpen) { // Getting Past Initial Menus if (menuClass.MenuNumber == 0) { if (currentKeyboardState.IsKeyDown(Keys.Enter)) { menuClass = menus[1]; // sets menu class to the info screen roomClass = levels[0] = new Room(spriteDict["darkJungle"], viewPortRect, 0, PICKUP_NUMBER, PICKUP_NUMBER, PICKUP_NUMBER, JAGUAR_NUMBER, NET_NUMBER); // loads the level and pickups into the 0th index of the level array levels[1] = new Room(spriteDict["greenJungle"], viewPortRect, 1, PICKUP_NUMBER, PICKUP_NUMBER, PICKUP_NUMBER, JAGUAR_NUMBER, NET_NUMBER); // loads the level and pickups into the 0th index of the level array levels[2] = new Room(spriteDict["jungleTree"], viewPortRect, 2, PICKUP_NUMBER, PICKUP_NUMBER, PICKUP_NUMBER, JAGUAR_NUMBER, NET_NUMBER); // loads the level and pickups into the 0th index of the level array levels[3] = new Room(spriteDict["waterfall"], viewPortRect, 3, PICKUP_NUMBER, PICKUP_NUMBER, PICKUP_NUMBER, JAGUAR_NUMBER, NET_NUMBER); // loads the level and pickups into the 0th index of the level array roomClass.InitializeRoom(); slothClass.InitializeSloth(); listManagement.ClearAndPopulate(leaves, flowers, rocks, jaguars, viewPortRect, rand, spriteDict, roomClass); writeOutConsts.RoomConsts(); writeOutConsts.SlothConsts(); writeOutConsts.ItemConsts(); } } // Enter the game if (menuClass.MenuNumber == 1) { if (currentKeyboardState.IsKeyDown(Keys.B)) { menuOpen = false; } } // Goes back to main screen if (menuClass.MenuNumber == 2 || menuClass.MenuNumber == 3) { if (currentKeyboardState.IsKeyDown(Keys.A)) { menuClass = menus[0]; } } } else { spawnNet.SpawnANet(gameTime, 1, nets, spriteDict); // Call SpawnNet to send net across screen, pass in GameTime spawnNet.RemoveNet(roomClass, nets); // Call SpawnNet to remove net #region Clamping Movement slothClass.spritePosition.Y = MathHelper.Clamp(slothClass.SpritePosition.Y, 0.0f, graphics.GraphicsDevice.Viewport.Height - (slothClass.SpriteImage.Height - 40)); // Restricting vertical sloth movement if (roomClass.RoomNumber == 0) { slothClass.spritePosition.X = MathHelper.Clamp(slothClass.SpritePosition.X, 0.0f, graphics.GraphicsDevice.Viewport.Width); // Disallowing to walk back into the title screen } #endregion // Game Over, sets menu to game over screen if (slothClass.Health <= 0) { menuClass = menus[2]; menuOpen = true; soundDict["jaguarCry"].Play(.25f, 0, 0); } // Accessing Help Screen if (roomClass.RoomNumber >= 0) { if (currentKeyboardState.IsKeyDown(Keys.H)) { menuOpen = true; } } // XML Saving stats if (currentKeyboardState.IsKeyDown(Keys.X)) { SaveSloth.slothSaveRequest = true; SaveRoom.roomSaveRequest = true; } // XML Loading stats if (currentKeyboardState.IsKeyDown(Keys.L)) { SaveSloth.slothLoadRequest = true; SaveRoom.roomLoadRequest = true; } // Read in XML files if (currentKeyboardState.IsKeyDown(Keys.R)) { // Try to read in xml files, if errors assign constants to values try { ReadIn.RoomPassIn(); ReadIn.ItemPassIn(); ReadIn.SlothPassIn(); } catch { assignConsts.Assign(); } } #region Sloth Collision with Objects // Pickup Collision with Sloth // remove element from list, detract 1 from number of elements for that room, add health or experience to sloth foreach (Leaf leaf in leaves) { if (slothClass.SpriteRectangle.Intersects(leaf.SpriteRectangle)) { leaves.Del(leaf); roomClass.LeafNumber--; slothClass.Health += Leaf.HEALTH; soundDict["leafCrunch"].Play(.25f, 0, 0); } } foreach (Flower flower in flowers) { if (slothClass.SpriteRectangle.Intersects(flower.SpriteRectangle)) { flowers.Del(flower); roomClass.FlowerNumber--; slothClass.Health += Flower.HEALTH; } } foreach (Rock rock in rocks) { if (slothClass.SpriteRectangle.Intersects(rock.SpriteRectangle)) { rocks.Del(rock); roomClass.RockNumber--; slothClass.Experience += Rock.EXPERIENCE; soundDict["rockScrape"].Play(.25f, 0, 0); } } foreach (Net net in nets) { if (slothClass.SpriteRectangle.Intersects(net.SpriteRectangle)) { slothClass.Health = 0; soundDict["netWoosh"].Play(.25f, 0, 0); } } #endregion #region Combat // Combat, jaguar health subtracts from sloth attack when C is pressed and there is overlap between the 2 for (int jaguar = 0; jaguar < jaguars.Count; jaguar++) { if (slothClass.SpriteRectangle.Intersects(jaguars[jaguar].SpriteRectangle) && !currentKeyboardState.IsKeyDown(Keys.D)) { if (currentKeyboardState.IsKeyDown(Keys.C)) { jaguars[jaguar].Health -= slothClass.Attack; } else { slothClass.Health -= jaguars[jaguar].Attack; } // if jaguar health is less than or equal to 0, remove element from list, detract 1 from number of elements for that room, experience to sloth if (jaguars[jaguar].Health <= 0) { jaguars.Del(jaguars[jaguar]); roomClass.JaguarNumber--; slothClass.Experience += Jaguar.EXPERIENCE; } } } #endregion // Loading Next Level foreach (Room level in levels) { #region Moving to Next Level, Right Side of Screen if (slothClass.SpriteCenter.X >= viewPortRect.Width) { if (roomClass.RoomNumber < levels.Count() - 1) { roomClass = levels[roomClass.RoomNumber + 1]; slothClass.SpritePosition = new Vector2(Sloth.X_START, Sloth.Y_START); listManagement.ClearAndPopulate(leaves, flowers, rocks, jaguars, viewPortRect, rand, spriteDict, roomClass); } else { menuOpen = true; menuClass = menus[3]; // win screen menuClass.MenuNumber = 3; } } #endregion #region Moving to Previous Level, Left Side of Screen if (slothClass.SpriteCenter.X < -1) { if (roomClass.RoomNumber >= 1) // when sloth touches left side of screen { slothClass.SpritePosition = new Vector2(Sloth.X_PREVIOUS, slothClass.SpritePosition.Y); // moves sloth to a new position roomClass = levels[roomClass.RoomNumber - 1]; listManagement.ClearAndPopulate(leaves, flowers, rocks, jaguars, viewPortRect, rand, spriteDict, roomClass); } } #endregion } } #region Object Update foreach (Leaf leaf in leaves) { leaf.Update(gameTime); } foreach (Flower flower in flowers) { flower.Update(gameTime); } foreach (Rock rock in rocks) { rock.Update(gameTime); } foreach (Jaguar jaguar in jaguars) { jaguar.Update(gameTime); } foreach (Net net in nets) { net.Update(gameTime, roomClass); } #endregion slothClass.Update(gameTime, currentKeyboardState); // calls sloth's update saveSloth.Update(gameTime, slothClass); saveRoom.Update(gameTime, roomClass); base.Update(gameTime); }
/// <summary> /// Allows the game to perform any initialization it needs to before starting to run. /// This is where it can query for any required services and load any non-graphic /// related content. Calling base.Initialize will enumerate through any components /// and initialize them as well. /// </summary> protected override void Initialize() { assignConsts = new ConstantsAssigning(); readIn = new ReadIn(); writeOutConsts = new WriteOutConsts(); spriteDict = new Dictionary<string, Texture2D>(); fontDict = new Dictionary<string, SpriteFont>(); soundDict = new Dictionary<string, SoundEffect>(); nets = new Stack<Net>(5); jaguars = new List<Jaguar>(); rocks = new List<Rock>(); flowers = new List<Flower>(); leaves = new List<Leaf>(); spawnNet = new SpawnNet(); menuText = new MenuText(); listManagement = new ListManagement(); rand = new Random(); saveSloth = new SaveSloth(); saveRoom = new SaveRoom(); fonts = new SpriteFont[3]; levels = new Room[4]; menus = new Menu[4]; spriteBatch = new SpriteBatch(GraphicsDevice); // Create a new SpriteBatch, which can be used to draw textures. viewPortRect = new Rectangle(0, 0, graphics.GraphicsDevice.Viewport.Width, graphics.GraphicsDevice.Viewport.Height); // creates new rectangle with the same dimensions as the screen base.Initialize(); }