/// <summary> /// Calls the ClearList method and Adds elements to the list /// </summary> /// <param name="leafList">lsit of leaves</param> /// <param name="flowerList">list of flowers</param> /// <param name="rockList">list of rocks</param> /// <param name="jaguarList">list of jaguars</param> /// <param name="viewPortRect">rectangle the size of the screen</param> /// <param name="rand">random integer passed in to determine sprite placement</param> /// <param name="spriteDict">key to pass in to access the sprite image</param> /// <param name="roomClass">room class</param> public void ClearAndPopulate(List<Leaf> leafList, List<Flower> flowerList, List<Rock> rockList, List<Jaguar> jaguarList, Rectangle viewPortRect, Random rand, Dictionary<string, Texture2D> spriteDict, Room roomClass) { ListClear(leafList,flowerList,rockList,jaguarList); LeafPopulate(leafList, viewPortRect, rand, spriteDict, roomClass); FlowerPopulate(flowerList, viewPortRect, rand, spriteDict, roomClass); RockPopulate(rockList, viewPortRect, rand, spriteDict, roomClass); JaguarPopulate(jaguarList, viewPortRect, rand, spriteDict, roomClass); }
/// <summary> /// Loads the game from the save file. /// </summary> public void LoadGame(Room roomClass) { TextReader XMLReader = new StreamReader("RoomSave.xml"); // creates new StreamReader which reads in the file path XmlSerializer deserializer = new XmlSerializer(typeof(RoomData)); saveRoomData = (RoomData)deserializer.Deserialize(XMLReader); roomClass.RoomNumber = saveRoomData.roomNumber; XMLReader.Close(); }
public void Stats(SpriteBatch spriteBatch, Dictionary<string, SpriteFont> fontDict, Sloth slothClass, Room roomClass) { // Displaying Sloth Attributes (Health, Attack, Room#, Experience) spriteBatch.DrawString(fontDict["Small"], "Health: " + slothClass.Health, new Vector2(20, 20), Color.Crimson); spriteBatch.DrawString(fontDict["Small"], "Claw Sharpness: " + slothClass.Attack, new Vector2(20, 40), Color.BurlyWood); spriteBatch.DrawString(fontDict["Small"], "Room Number: " + (roomClass.RoomNumber + 1), new Vector2(20, 60), Color.Ivory); spriteBatch.DrawString(fontDict["Small"], "Experience: " + slothClass.Experience, new Vector2(20, 80), Color.Gainsboro); }
/// <summary> /// Update /// </summary> /// <param name="gameTime">current game time</param> /// <param name="roomClass">room reference</param> public void Update(GameTime gameTime, Room roomClass) { if (spritePosition.X > -50) // if the x coord of net is displayed on screen { spritePosition.X -= 5; // net flies to the right of the screen } base.Update(gameTime); }
/// <summary> /// Pop's a net off the net stack /// </summary> /// <param name="gameTime">current time of the game</param> /// <param name="netCount">the total net count</param> /// <param name="netStack">the stack data structure</param> public void RemoveNet(Room roomClass, Stack<Net> netStack) { // every time the net reaches the left side of the screen, pop it off the stack foreach (Net net in netStack) { if (net.spritePosition.X <= 0) { netStack.Pop(); roomClass.NetNumber++; } } }
/// <summary> /// Saves the game in the same directory as the executable. /// </summary> private void SaveGame(Room roomClass) { saveRoomData = new RoomData() // prime SaveData for saving game info { roomNumber = roomClass.RoomNumber, }; DeleteExisting(); // Deletes old files if they exist System.IO.StreamWriter file = new System.IO.StreamWriter("RoomSave.xml"); XmlSerializer serializer = new XmlSerializer(typeof(RoomData)); serializer.Serialize(file, saveRoomData); file.Close(); }
/// <summary> /// Checks every frame for a new request to save /// </summary> public void Update(GameTime gameTime, Room roomClass) { if (roomSaveRequest) { SaveGame(roomClass); roomSaveRequest = false; } else if (roomLoadRequest) { LoadGame(roomClass); roomLoadRequest = false; } }
/// <summary> /// Adds elements to list /// </summary> /// <param name="leafList">list of leaves</param> /// <param name="viewPortRect">size of the screen</param> /// <param name="rand">random number to determine pickup location</param> /// <param name="spriteDict">value to access the dictonary of sprites where leaf is located</param> /// <param name="roomClass">room class</param> public void LeafPopulate(List<Leaf> leafList, Rectangle viewPortRect, Random rand, Dictionary<string, Texture2D> spriteDict, Room roomClass) { for (int leavesToAdd = 0; leavesToAdd < roomClass.LeafNumber; leavesToAdd++) leafList.Add(new Leaf(spriteDict["leaf"], (int)rand.Next(viewPortRect.Width - spriteDict["leaf"].Width), (int)rand.Next(viewPortRect.Height - spriteDict["leaf"].Height))); }
/// <summary> /// Adds elements to list /// </summary> /// <param name="leafList">list of jaguars</param> /// <param name="viewPortRect">size of the screen</param> /// <param name="rand">random number to determine pickup location</param> /// <param name="spriteDict">value to access the dictonary of sprites where jaguar is located</param> /// <param name="roomClass">room class</param> public void JaguarPopulate(List<Jaguar> jaguarList, Rectangle viewPortRect, Random rand, Dictionary<string, Texture2D> spriteDict, Room roomClass) { for (int jaguarsToAdd = 0; jaguarsToAdd < roomClass.JaguarNumber; jaguarsToAdd++) jaguarList.Add(new Jaguar(spriteDict["jaguar"], (int)rand.Next(viewPortRect.Width - spriteDict["jaguar"].Width), (int)rand.Next(viewPortRect.Height - spriteDict["jaguar"].Height))); }
/// <summary> /// Adds elements to list /// </summary> /// <param name="leafList">list of flowers</param> /// <param name="viewPortRect">size of the screen</param> /// <param name="rand">random number to determine pickup location</param> /// <param name="spriteDict">value to access the dictonary of sprites where flower is located</param> /// <param name="roomClass">room class</param> public void FlowerPopulate(List<Flower> flowerList, Rectangle viewPortRect, Random rand, Dictionary<string, Texture2D> spriteDict, Room roomClass) { for (int flowersToAdd = 0; flowersToAdd < roomClass.FlowerNumber; flowersToAdd++) flowerList.Add(new Flower(spriteDict["flower"], (int)rand.Next(viewPortRect.Width - spriteDict["flower"].Width), (int)rand.Next(viewPortRect.Height - spriteDict["flower"].Height))); }
/// <summary> /// Adds elements to list /// </summary> /// <param name="leafList">list of rocks</param> /// <param name="viewPortRect">size of the screen</param> /// <param name="rand">random number to determine pickup location</param> /// <param name="spriteDict">value to access the dictonary of sprites where rock is located</param> /// <param name="roomClass">room class</param> public void RockPopulate(List<Rock> rockList, Rectangle viewPortRect, Random rand, Dictionary<string, Texture2D> spriteDict, Room roomClass) { for (int rocksToAdd = 0; rocksToAdd < roomClass.RockNumber; rocksToAdd++) rockList.Add(new Rock(spriteDict["sharpeningRock"], (int)rand.Next(viewPortRect.Width - spriteDict["sharpeningRock"].Width), (int)rand.Next(viewPortRect.Height - spriteDict["sharpeningRock"].Height))); }
/// <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> /// LoadContent will be called once per game and is the place to load all of your content. /// </summary> protected override void LoadContent() { #region Dictonary Loading // Loads all sprites into their respective dictonary keys spriteDict.Add("darkJungle", Content.Load<Texture2D>("Sprites\\Backgrounds\\darkJungle")); spriteDict.Add("gameOver", Content.Load<Texture2D>("Sprites\\Backgrounds\\gameOver")); spriteDict.Add("winScreen", Content.Load<Texture2D>("Sprites\\Backgrounds\\winScreen")); spriteDict.Add("greenJungle", Content.Load<Texture2D>("Sprites\\Backgrounds\\greenJungle")); spriteDict.Add("infoScreen", Content.Load<Texture2D>("Sprites\\Backgrounds\\infoScreen")); spriteDict.Add("jungleTree", Content.Load<Texture2D>("Sprites\\Backgrounds\\jungleTree")); spriteDict.Add("smallTitle", Content.Load<Texture2D>("Sprites\\Backgrounds\\smallTitle")); spriteDict.Add("waterfall", Content.Load<Texture2D>("Sprites\\Backgrounds\\waterfall")); spriteDict.Add("jaguar", Content.Load<Texture2D>("Sprites\\Characters\\jaguarFinal")); spriteDict.Add("slothathor", Content.Load<Texture2D>("Sprites\\Characters\\finalSloth1")); spriteDict.Add("slothathorClaws", Content.Load<Texture2D>("Sprites\\Characters\\sloth2")); spriteDict.Add("slothathorDefend", Content.Load<Texture2D>("Sprites\\Characters\\slothathorDefend")); spriteDict.Add("flower", Content.Load<Texture2D>("Sprites\\Pickups\\smallFlower")); spriteDict.Add("leaf", Content.Load<Texture2D>("Sprites\\Pickups\\leafFinal")); spriteDict.Add("sharpeningRock", Content.Load<Texture2D>("Sprites\\Pickups\\rockFinal")); spriteDict.Add("zap", Content.Load<Texture2D>("Sprites\\Characters\\smallNet")); // Adds fonts to their keys fontDict.Add("Medium", Content.Load<SpriteFont>("Fonts\\MediumFont")); fontDict.Add("Small", Content.Load<SpriteFont>("Fonts\\SmallFont")); fontDict.Add("Title", Content.Load<SpriteFont>("Fonts\\TitleFont")); // Adds sounds to their keys soundDict.Add("intro", Content.Load<SoundEffect>("Sounds\\beginningGame")); soundDict.Add("jaguarCry", Content.Load<SoundEffect>("Sounds\\jaguarCry")); soundDict.Add("leafCrunch", Content.Load<SoundEffect>("Sounds\\leafCrunch")); soundDict.Add("rockScrape", Content.Load<SoundEffect>("Sounds\\rockScrape")); soundDict.Add("netWoosh", Content.Load<SoundEffect>("Sounds\\netWoosh")); soundDict.Add("slothScratch", Content.Load<SoundEffect>("Sounds\\slothScratch")); // Populates arrays with new Rooms and Menus levels[0] = new Room(spriteDict["darkJungle"], viewPortRect, 0, 1, rand.Next(3), rand.Next(3), rand.Next(2), 1); levels[1] = new Room(spriteDict["greenJungle"], viewPortRect, 1, 1, rand.Next(4), rand.Next(2), rand.Next(2), 1); levels[2] = new Room(spriteDict["jungleTree"], viewPortRect, 2, 1, rand.Next(2), rand.Next(4), rand.Next(2), 1); levels[3] = new Room(spriteDict["waterfall"], viewPortRect, 3, 1, rand.Next(2), rand.Next(2), rand.Next(2), 1); menus[0] = new Menu(spriteDict["smallTitle"], viewPortRect, 0); menus[1] = new Menu(spriteDict["infoScreen"], viewPortRect, 1); menus[2] = new Menu(spriteDict["gameOver"], viewPortRect, 2); menus[3] = new Menu(spriteDict["winScreen"], viewPortRect, 2); fonts[0] = fontDict["Medium"]; fonts[1] = fontDict["Title"]; fonts[2] = fontDict["Small"]; #endregion menuOpen = true; // initial screen will be the title screen (a menu) menuClass = menus[0]; // loads the first menu into the menu class roomClass = levels[0]; // load first level into the room class slothClass = new Sloth(spriteDict["slothathor"], spriteDict["slothathor"], spriteDict["slothathorClaws"], spriteDict["slothathorDefend"]); // loads all sloth sprites into sloth classes soundDict["intro"].Play(.25f,0,0); }