public void execute(Player p, Item origin, Item target) { if (p.inventory.Contains(origin)) throw new InvalidDataException("You already have this item in your inventory."); p.addItem(origin); p.room.removeItem(origin); }
public World(string file) { player = new Player(); List<Node> nodes = WorldParser.parseFile(file); worldName = nodes[0].name; rooms = WorldBuilder.build(player, nodes[0].children); Command look = s => { if (s.StartsWith("at ", StringComparison.OrdinalIgnoreCase)) player.commands["examine"](s.Substring(3).Trim()); else if (DirectionUtils.isDirection(s)) Console.WriteLine(player.room.getWall(s).look()); else Console.WriteLine(player.room.look()); }; Command move = s => { Direction d; try { d = DirectionUtils.parse(s); } catch { Console.WriteLine("I don't think that is a valid direction :P"); return; } Wall wall = player.room.getWall(d); if (wall == null || wall.door == null || !wall.door.visible) Console.WriteLine("You have nowhere to go."); else if (wall.door.locked) Console.WriteLine("This door is locked. Find a key."); else { player.room = wall.door.room; look(""); } }; Command take = s => { if (s == "") { Console.WriteLine("Take what?"); return; } Item i = player.room.getItem(s); if (i == null) { if (player.getItem(s) != null) Console.WriteLine("This item is already equipped."); else Console.WriteLine("You can't see such an item."); return; } if (!i.visible) { Console.WriteLine("You can't see such an item."); return; } if (!i.takeable) { Console.WriteLine("Sorry you can't take this."); return; } player.addItem(i); player.room.removeItem(i); Console.WriteLine(char.ToUpper(i.name[0]) + i.name.Substring(1) + " taken."); }; Command examine = s => { if (s == "") { Console.WriteLine("Examine what?"); return; } Item i = player.getItem(s); if (i == null) { i = player.room.getItem(s); if (i == null || !i.visible) { Console.WriteLine("You can't see such an item."); return; } } if (!i.visible) { Console.WriteLine("You can't see such an item."); return; } Command c = i.getCommand("examine"); if (c != null) c(""); Console.WriteLine(i.examine()); }; Command inventory = s => { if (player.inventory.Count == 0) { Console.WriteLine("Your inventory is empty."); return; } Console.WriteLine("Your inventory:"); foreach (Item i in player.inventory) Console.WriteLine(" - " + i.name); }; Command use = s => { string[] ps = s.Split(new string[] { " on " }, StringSplitOptions.RemoveEmptyEntries); if (ps.Length == 0) { Console.WriteLine("Use what?"); return; } Item i = player.getItem(ps[0].Trim()); if (i == null) { i = player.room.getItem(ps[0].Trim()); if (i == null || !i.visible) { Console.WriteLine("You can't see such an item."); return; } if (!i.useIfNotEquipped) { Console.WriteLine("This item is not equipped."); return; } } if (!i.visible) { Console.WriteLine("You can't see such an item."); return; } Command c = i.getCommand("use"); if (c == null) { Console.WriteLine("You can't use this item."); return; } string p = ""; for (int a = 1; a < ps.Length; a++) p += ps[a].Trim(); c(p); }; Command help = s => { foreach (string c in player.commands.Keys) { if (c == "?" || c == "help") continue; Console.WriteLine(c); } foreach (Item i in player.inventory) { foreach (string c in i.commands.Keys) { if (!player.commands.ContainsKey(c)) Console.WriteLine(c); } } }; player.commands.Add("look", look); player.commands.Add("move", move); player.commands.Add("go", move); player.commands.Add("take", take); player.commands.Add("examine", examine); player.commands.Add("inventory", inventory); player.commands.Add("use", use); player.commands.Add("help", help); player.commands.Add("?", help); }