/** * Main play routine. Loops until end of play. */ public void Play() { PrintWelcome(); // Enter the main command loop. Here we repeatedly read commands and // execute them until the game is over. bool finished = false; while (!finished) { if (player.IsAlive()) { Command command = parser.GetCommand(); finished = ProcessCommand(command); } if (!player.IsAlive()) { finished = true; TextEffects.ColoredMessage("Game Over\n", "DarkRed"); TextEffects.ColoredMessage("You've Died\n", "DarkRed"); } } Console.WriteLine("Saving..."); SaveFile.GenerateSaveFile(player); Console.WriteLine("Thank you for playing."); System.Threading.Thread.Sleep(500); }
public static Player LoadPlayerFromSaveFile() { string[] input = System.IO.File.ReadAllLines(saveFileName); Player player = serializer.Deserialize <Player>(input[0]); Player output = new Player(true); output.Health = player.Health; output.equippedItem = player.equippedItem; for (int i = 1; i <= (input.Length - 1); i++) { string[] itemInfo = input[i].Split('-'); switch (itemInfo[0]) { case "Weapon": output.inventory.AddItem(serializer.Deserialize <Weapon>(itemInfo[1])); break; case "HealthPotion": output.inventory.AddItem(serializer.Deserialize <HealthPotion>(itemInfo[1])); break; case "Key": Key key = serializer.Deserialize <Key>(itemInfo[1]); TextEffects.ColoredMessage("As you come back to this world the " + key.Name + " vanishes from your inventory.", "Yellow"); break; case "Item": output.inventory.AddItem(serializer.Deserialize <Item>(itemInfo[1])); break; } } output.CurrentRoom = player.CurrentRoom; return(output); }
public override string UseItem(Command command, Room currentRoom) { string input = command.ThirdWord; if (!command.HasThirdWord()) { TextEffects.ColoredMessage("Use it on what?", "DarkRed"); return(null); } Room target = currentRoom.GetExit(input); if (target == null) { TextEffects.ColoredMessage("There is door there to unlock!", "DarkRed"); return(null); } else if (!target.IsLocked()) { TextEffects.ColoredMessage("That door isn't locked!", "DarkRed"); return(null); } else if (!target.UseKey(this)) { TextEffects.ColoredMessage("You fiddle with the key in the lock but you can't seem to unlock it.", "DarkRed"); return(null); } return("You put the key in the lock and turn it.\nThe lock opens and the key disappears."); }
/** * Print out the opening message for the player. */ private void PrintWelcome() { Console.WriteLine(); Console.WriteLine("Welcome to Zuul!"); Console.WriteLine("Zuul is a new, incredibly boring adventure game."); Console.WriteLine("Type 'help' if you need help."); Console.WriteLine(); Console.WriteLine(player.CurrentRoom.GetLongDescription()); TextEffects.ColoredMessage("You have " + player.Health + " health!\n", "Red"); }
private void InitiateCombat(Room room) { inCombat = true; player.FightingInRoom = room; Random RNG = new Random(); while (room.Enemies.Count > 0) { Console.WriteLine("An enemy engages you in combat!"); Console.WriteLine(room.Enemies[0].DisplayName + " appeared to fight you!"); while (room.Enemies[0].Health > 0) { Console.WriteLine("What do you do?"); Command command = parser.GetCommand(); ProcessCommand(command); if (command.CommandWord == "attack" || command.CommandWord == "use" || command.CommandWord == "equip" || command.CommandWord == "unequip") { if (room.Enemies[0].Health > 0) { player.Damage(room.Enemies[0].AttackPlayer()); TextEffects.ColoredMessage(room.Enemies[0].attackDesc, "DarkRed"); } } if (!player.IsAlive()) { break; } } Console.WriteLine("You've successfully defeated the " + room.Enemies[0].DisplayName + "!"); switch (RNG.Next(2)) { case 0: room.inventory.AddItem(room.Enemies[0].DropWeapon()); break; case 1: room.inventory.AddItem(room.Enemies[0].DropHealthPotion()); break; case 3: Console.WriteLine(room.Enemies[0].DisplayName + " has not dropped anything!"); break; } room.Enemies.RemoveAt(0); } inCombat = false; player.FightingInRoom = null; }
/** * Try to go to one direction. If there is an exit, enter the new * room, otherwise print an error message. */ private void GoRoom(Command command) { if (!command.HasSecondWord()) { // if there is no second word, we don't know where to go... TextEffects.ColoredMessage("Go where?\n", "DarkRed"); return; } string direction = command.SecondWord; // Try to leave current room. Room nextRoom = player.CurrentRoom.GetExit(direction); if (direction.ToLower() == "back") { nextRoom = player.LastRoom; } if (nextRoom == null) { TextEffects.ColoredMessage("There is no door to " + direction + "!\n", "DarkRed"); } else if (nextRoom.IsLocked()) { TextEffects.ColoredMessage("This door is locked.\n", "DarkRed"); } else { if (nextRoom.Enemies.Count > 0) { InitiateCombat(nextRoom); } player.LastRoom = player.CurrentRoom; player.CurrentRoom = nextRoom; Console.WriteLine(player.CurrentRoom.GetLongDescription()); } }
/** * Given a command, process (that is: execute) the command. * If this command ends the game, true is returned, otherwise false is * returned. */ private bool ProcessCommand(Command command) { bool wantToQuit = false; if (command.IsUnknown()) { TextEffects.ColoredMessage("I don't know what you mean...\n", "DarkRed"); return(false); } string commandWord = command.CommandWord; switch (commandWord) { case "help": PrintHelp(); break; case "go": if (inCombat) { TextEffects.ColoredMessage("You can't do that in combat!\n", "DarkRed"); break; } GoRoom(command); break; case "quit": if (inCombat) { TextEffects.ColoredMessage("You can't do that in combat!\n", "DarkRed"); break; } wantToQuit = true; break; case "look": if (inCombat) { TextEffects.ColoredMessage("You can't do that in combat!\n", "DarkRed"); break; } TextEffects.CheckNullWriteLine(player.CurrentRoom.GetLongDescription()); break; case "inventory": TextEffects.ColoredMessage(player.GetHealth(), "Red"); TextEffects.SecondaryColoredMessage(player.GetInventoryDesc(), "White", "DarkGray"); break; case "take": if (inCombat) { TextEffects.ColoredMessage("You can't do that in combat!\n", "DarkRed"); break; } TextEffects.CheckNullWriteLine(player.PickupItem(command)); break; case "drop": if (inCombat) { TextEffects.ColoredMessage("You can't do that in combat!\n", "DarkRed"); break; } TextEffects.CheckNullWriteLine(player.DropItem(command)); break; case "use": TextEffects.CheckNullWriteLine(player.UseItem(command)); break; case "attack": if (inCombat) { TextEffects.CheckNullWriteLine(player.AttackEnemy()); break; } TextEffects.CheckNullWriteLine(player.Attack()); break; case "equip": TextEffects.CheckNullWriteLine(player.EquipItem(command)); break; case "unequip": TextEffects.CheckNullWriteLine(player.Unequip()); break; } return(wantToQuit); }