public Situation HandleSituation(Situation currentSituation, GameController gameController) { Player player = gameController.player; SituationContainer situations = gameController.situations; ItemContainer items = gameController.items; Enemy enemy = situations.GetEnemy(currentSituation.objectID); Situation resultSituation = currentSituation; MenuDrawer.Select( $"{enemy.name} встаёт на вашем пути. У него {enemy.health}/{enemy.maxHealth} здоровья, " + $"{enemy.defense} защиты и {enemy.attack} атаки.", MakeDictionary( Pair("Сразиться", Action(() => { int enemyHealth = enemy.health; string resultSituationID = ""; while (resultSituationID.IsEmpty()) { MenuDrawer.Select( $"Игрок ({player.health}/{player.maxHealth} HP, {player.attack} ATK, {player.defense} DEF) " + $"VS {enemy.name} ({enemyHealth}/{enemy.maxHealth} HP, {enemy.attack} ATK, {enemy.defense} DEF)", MakeDictionary <string, Action>( Pair <string, Action>("Атаковать", () => { enemyHealth -= ComputeRealDamage(player.attack, enemy.defense); if (enemyHealth <= 0) { resultSituationID = enemy.situationsOnDefeat.RandomElement(); player.coins += enemy.coinsReward; foreach (ItemReference dropRef in enemy.drop) { Item item = items.ResolveReference(dropRef); for (int i = 0; i < dropRef.count; i++) { player.AddItemToInventory(item); } } if (enemy.drop.IsEmpty()) { MenuDrawer.ShowInfoDialog($"{enemy.name} повержен! Вы получили за это " + $"{enemy.coinsReward} монет."); } else { IEnumerable <string> dropNames = enemy.drop .Select((ItemReference itemRef) => { Item item = items.GetByTypeAndID(itemRef.type, itemRef.id); return($"{itemRef.count}x {item.name}"); }); string dropString = string.Join(", ", dropNames); MenuDrawer.ShowInfoDialog($"{enemy.name} повержен! Вы получили за это " + $"{dropString} и {enemy.coinsReward} монет."); } } else { int damageToPlayer = ComputeRealDamage(enemy.attack, player.defense); player.health -= damageToPlayer; if (player.health <= 0) { MenuDrawer.ShowInfoDialog($"Вас убил {enemy.name}!"); throw new GameOverException(); } MenuDrawer.ShowInfoDialog($"{enemy.name} нанёс вам {damageToPlayer} урона."); } }), Pair <string, Action>("Инвентарь", () => InventoryController.Start(player)), Pair <string, Action>("Убежать", () => { resultSituationID = enemy.situationsOnRunAway.RandomElement(); }) ) ); } resultSituation = situations.GetSituation(resultSituationID); })), Pair("Убежать", Action(() => { resultSituation = situations.RandomSituationByIDs(enemy.situationsOnRunAway); })), Pair("Инвентарь", Action(() => InventoryController.Start(player))), Pair("Сохраниться", EmptyAction), Pair("Выйти", ThrowAction(new GameOverException())) ) ); return(resultSituation); }
public Situation HandleSituation(Situation currentSituation, GameController gameController) { Player player = gameController.player; SituationContainer situations = gameController.situations; ItemContainer items = gameController.items; CraftingPlace craftingPlace = situations.GetCraftingPlace(currentSituation.objectID); Situation resultSituation = currentSituation; var actions = craftingPlace.crafts.ToDictionary((Craft craft) => { List <Tuple <Item, int> > ingredientsAndCounts = craft.ingredients.Map(items.ResolveReferenceAndCount); List <string> ingredientsNames = ingredientsAndCounts .Map(TupleFunc((Item ingredient, int count) => $"{count}x {ingredient.name}")); Item craftResult = items.ResolveReference(craft.result); var craftTitle = $"{ingredientsNames.Join(" + ")} => {craft.result.count}x {craftResult.name}"; return(Pair(craftTitle, Action(() => { List <Tuple <Item, int> > missingIngredients = ingredientsAndCounts .Map(TupleFunc((Item ingredient, int requiredCount) => { int realCount = player.CountOfItemInInventory(ingredient); return Tuple.Create(ingredient, requiredCount - realCount); })) .Filter(TupleFunc((Item ingredient, int missingCount) => missingCount > 0)); if (missingIngredients.IsEmpty()) { int ingredientsCount = ingredientsAndCounts .Map(TupleFunc((Item ingredient, int count) => count)) .Sum(); int inventorySizeAfterCraft = player.inventory.Count - ingredientsCount + craft.result.count; if (inventorySizeAfterCraft < Player.MAX_INVENTORY_SIZE) { ingredientsAndCounts.ForEach(TupleAction <Item, int>(player.RemoveItemFromInventory)); player.AddItemToInventory(craftResult, craft.result.count); MenuDrawer.ShowInfoDialog($"Вы скрафтили {craft.result.count}x {craftResult.name}!"); } else { MenuDrawer.ShowInfoDialog("Вам нехватает места в инвентаре!"); } } else { string missingIngredientsString = missingIngredients .Map(TupleFunc((Item ingredient, int count) => $"{count}x {ingredient.name}")) .Join(", "); MenuDrawer.ShowInfoDialog($"Вам нехватает {missingIngredientsString}!"); } }))); }); MenuDrawer.Select( craftingPlace.name, Dictionaries.Merge( actions, MakeDictionary( Pair("Продолжить", Action(() => { resultSituation = situations.RandomSituationByIDs(craftingPlace.nextSituations); })), Pair("Инвентарь", Action(() => InventoryController.Start(player))), Pair("Сохраниться", EmptyAction), Pair("Выйти", ThrowAction(new GameOverException())) ) ) ); return(resultSituation); }