private void HandleTravelInteraction(string[] commandWords, Person actor) { var traveller = actor as ITraveller; if (traveller != null) { var targetLocation = this.locationByName[commandWords[2]]; peopleByLocation[traveller.Location].Remove(actor); traveller.TravelTo(targetLocation); peopleByLocation[traveller.Location].Add(actor); foreach (var item in actor.ListInventory()) { item.UpdateWithInteraction("travel"); } } }
private void HandlePickUpInteraction(Person actor) { foreach (var item in strayItemsByLocation[actor.Location]) { this.AddToPerson(actor, item); item.UpdateWithInteraction("pickup"); } strayItemsByLocation[actor.Location].Clear(); }
private void HandleSellInteraction(string[] commandWords, Person actor) { Item saleItem = null; string saleItemName = commandWords[2]; foreach (var item in actor.ListInventory()) { if (ownerByItem[item] == actor && saleItemName == item.Name) { saleItem = item; } } var buyer = personByName[commandWords[3]] as Shopkeeper; if (buyer != null && peopleByLocation[actor.Location].Contains(buyer)) { var price = buyer.CalculateBuyingPrice(saleItem); moneyByPerson[buyer] -= price; moneyByPerson[actor] += price; this.RemoveFromPerson(actor, saleItem); this.AddToPerson(buyer, saleItem); saleItem.UpdateWithInteraction("sell"); } }
private void HandleDropInteraction(Person actor) { foreach (var item in actor.ListInventory()) { if (ownerByItem[item] == actor) { strayItemsByLocation[actor.Location].Add(item); this.RemoveFromPerson(actor, item); item.UpdateWithInteraction("drop"); } } }
private void HandleListInventoryInteraction(Person actor) { var inventory = actor.ListInventory(); foreach (var item in inventory) { if (ownerByItem[item] == actor) { Console.WriteLine(item.Name); item.UpdateWithInteraction("inventory"); } } if (inventory.Count == 0) { Console.WriteLine("empty"); } }
protected virtual void HandlePersonCommand(string[] commandWords, Person actor) { switch (commandWords[1]) { case "drop": HandleDropInteraction(actor); break; case "pickup": HandlePickUpInteraction(actor); break; case "sell": this.HandleSellInteraction(commandWords, actor); break; case "buy": HandleBuyInteraction(commandWords, actor); break; case "inventory": HandleListInventoryInteraction(actor); break; case "money": Console.WriteLine(moneyByPerson[actor]); break; case "travel": HandleTravelInteraction(commandWords, actor); break; default: break; } }
protected void RemoveFromPerson(Person actor, Item item) { actor.RemoveFromInventory(item); ownerByItem[item] = null; }
protected void AddToPerson(Person actor, Item item) { actor.AddToInventory(item); ownerByItem[item] = actor; }
private void handleGatherInteraction(Person actor, string gatheredItemName) { var gatheringLocation = actor.Location as IGatheringLocation; if (gatheringLocation != null) { if (actor.ListInventory().Any((item) => item.ItemType == gatheringLocation.RequiredItem)) { var producedItem = gatheringLocation.ProduceItem(gatheredItemName); this.AddToPerson(actor, producedItem); } } }
private void handleCraftInteraction(Person actor, string craftemItemType, string craftedItemName) { switch (craftemItemType) { case "armor": this.CraftArmor(actor, craftedItemName); break; case "weapon": this.CraftWeapon(actor, craftedItemName); break; default: break; } }
private void CraftWeapon(Person actor, string craftedItemName) { var actorInventory = actor.ListInventory(); if (actorInventory.Any((item) => item.ItemType == ItemType.Iron) && actorInventory.Any((item) => item.ItemType == ItemType.Wood)) { this.AddToPerson(actor, new Weapon(craftedItemName)); } }
protected override void HandlePersonCommand(string[] commandWords, Person actor) { switch (commandWords[1]) { case "gather": this.handleGatherInteraction(actor, commandWords[2]); break; case "craft": this.handleCraftInteraction(actor, commandWords[2], commandWords[3]); break; default: base.HandlePersonCommand(commandWords, actor); break; } }