private static void ProcessEquipCommand(string[] parts) //write to equip from backpack { string itemName = parts[1]; Item item; if (CurrentArea.HasItem(parts[1])) { item = CurrentArea.GetItem(parts[1]); // turn into backpack if (item is IEquippableItem) { try { ((IEquippableItem)item).Equip(); } catch (WorldException) { return; } } else { throw new WorldException("You can't equip that!"); } } else { throw new WorldException("That's not in your backpack!"); } }
private static void ProcessUseCommand(string[] parts) { if (parts.Length == 1) { PrintLineWarning("Please specify which item."); return; } if (parts.Length == 2) { CurrentArea.GetItem(parts[1]); IUseableItem itemToUse = CurrentArea.GetItem(parts[1]) as IUseableItem; if (itemToUse != null) { try { ((IUseableItem)itemToUse).Use(); PrintLinePositive("Neat thing!"); } catch (ItemDepletedException ide) { PrintLineSpecial(ide.Message); } catch (WorldException we) { PrintLineDanger(we.Message); } } else { PrintLineWarning("This item cannot be used..."); return; } } string targetName = parts[3]; object target; if (CurrentArea.HasItem(targetName)) { target = CurrentArea.GetItem(targetName); } else if (CurrentArea.CreatureExists(targetName)) { target = CurrentArea.GetCreature(targetName); } else { PrintLineWarning("I don't see any of that around here..."); return; } }
/// <summary> /// Parses the command and do any required actions. /// </summary> /// <param name="command">Command as typed by the user.</param> private static void ParseCommand(string command) { // Break apart the command into individual words: // This is why command words and unique names for objects cannot contain spaces. string[] parts = command.Split(' '); // The first word is the command. string cmdWord = parts.First(); if (!_commands.ContainsKey(cmdWord)) { if (parts.Length > 1) { string target = parts[1]; if (CurrentArea.HasItem(target)) { Item item = CurrentArea.GetItem(target); if (item is IInteractable && ((IInteractable)item).Interactions.ContainsKey(cmdWord)) { ((IInteractable)item).Interactions[cmdWord](parts); return; } } else if (CurrentArea.CreatureExists(target)) { Creature creature = CurrentArea.GetCreature(target); if (creature is IInteractable && ((IInteractable)creature).Interactions.ContainsKey(cmdWord)) { ((IInteractable)creature).Interactions[cmdWord](parts); return; } } } PrintLineWarning("I don't understand...(type \"help\" to see a list of commands I know.)"); return; } // execute the command associated with the given command word! _commands[cmdWord](parts); // TODO: Many Achievements // Implement more commands like "use" and "get" and "talk" }