Example #1
0
        private void Loot(ref World world, ref Entity e)
        {
            if (e.HasComponent <Coins>())
            {
                world.GetPlayer().GetComponent <Coins>().coins += e.GetComponent <Coins>().coins;
                e.GetComponent <Coins>().coins = 0;
            }

            if (e.HasComponent <Inventory>())
            {
                Trace.WriteLine("Inventory menu");
                InventoryMenu.Start("Player", "Ground", ref world.GetPlayer().GetComponent <Inventory>().items, ref e.GetComponent <Inventory>().items);
            }

            if (e.HasComponent <Weapon>())
            {
                WeaponMenu.Start(ref world, ref e);
            }

            if (e.HasComponent <Inventory>() && e.HasComponent <Weapon>())
            {
                if (e.GetComponent <Inventory>().items.Count == 0 &&
                    (e.GetComponent <Weapon>().name == "" || e.GetComponent <Weapon>().name == "none" || e.GetComponent <Weapon>().damage == 1))
                {
                    e.Removed = true;
                }
            }
            else if (e.HasComponent <Inventory>())
            {
                if (e.GetComponent <Inventory>().items.Count == 0)
                {
                    e.Removed = true;
                }
            }
            else if (e.HasComponent <Weapon>())
            {
                if (e.GetComponent <Weapon>().name == "" || e.GetComponent <Weapon>().name == "none" || e.GetComponent <Weapon>().damage == 1)
                {
                    e.Removed = true;
                }
            }
            else
            {
                e.Removed = true;
            }

            Renderer.RenderPlayerInfo(world.GetPlayer());
        }
Example #2
0
        private bool HandleEnemy(ref World world, int dx, int dy)
        {
            // Get the entity at the specified position
            Entity enemy = world.GetCurrentZone().GetEntityAt(world.GetPlayer().GetComponent <Position>().x + dx, world.GetPlayer().GetComponent <Position>().y + dy);

            // Check if an entity is found
            if (enemy == null)
            {
                return(false);
            }
            else if (enemy.HasComponent <Enemy>())
            {
                // Ensure the enemy is able to be killed.
                if (!enemy.HasComponent <Health>() || !enemy.HasComponent <Damager>())
                {
                    return(false);
                }

                ref bool isAttacking = ref enemy.GetComponent <Enemy>().isPlayerAttacking;
                Console.CursorVisible = false;

                // Create a stopwatch to keep track of delay between loops
                Stopwatch stopwatch = new Stopwatch();

                // Store the enemy details for easy, efficient access.
                Enemy enemyCP = enemy.GetComponent <Enemy>();

                PrintEnemyDetails(enemy, isAttacking);

                // The index of the vertical line which the player tries to stop in the right region.
                int scrollIndex = 0;

                while (!(Console.KeyAvailable && Console.ReadKey(true).Key == ConsoleKey.Escape))
                {
                    if (enemy.GetComponent <Health>().health == 0 || world.GetPlayer().GetComponent <Health>().health == 0)
                    {
                        break;
                    }

                    if (isAttacking)
                    {
                        for (int i = 0; i < enemyCP.playerAttackPattern.Length; i++)
                        {
                            if (i == scrollIndex)
                            {
                                Renderer.PrintAt(Constants.TextInputX + i, Constants.TextInputY, '|', Color.White);
                            }
                            else
                            {
                                Renderer.PrintAt(Constants.TextInputX + i, Constants.TextInputY, '#', Color.FromArgb(51 * enemyCP.playerAttackPattern[i], 255, 255 - 51 * enemyCP.playerAttackPattern[i]));
                            }
                        }

                        if (scrollIndex == enemyCP.playerAttackPattern.Length - 1)
                        {
                            scrollIndex = 0;
                        }
                        else
                        {
                            scrollIndex++;
                        }
                    }
                    else
                    {
                        for (int i = 0; i < enemyCP.playerDefendPattern.Length; i++)
                        {
                            if (i == scrollIndex)
                            {
                                Renderer.PrintAt(Constants.TextInputX + i, Constants.TextInputY, '|', Color.White);
                            }
                            else
                            {
                                Renderer.PrintAt(Constants.TextInputX + i, Constants.TextInputY, '#', Color.FromArgb(51 * enemyCP.playerDefendPattern[i], 255, 255 - 51 * enemyCP.playerDefendPattern[i]));
                            }
                        }

                        if (scrollIndex == enemyCP.playerDefendPattern.Length - 1)
                        {
                            scrollIndex = 0;
                        }
                        else
                        {
                            scrollIndex++;
                        }
                    }

                    // Delay using the stopwatch's elapsed time.
                    stopwatch.Start();
                    while (stopwatch.ElapsedMilliseconds < enemyCP.delay)
                    {
                        if (Console.KeyAvailable && Console.ReadKey(true).Key == ConsoleKey.Spacebar)
                        {
                            // Player attack action

                            if (isAttacking)
                            {
                                if (enemy.GetComponent <Health>().health - world.GetPlayer().GetComponent <Damager>().damage *enemyCP.playerAttackPattern[scrollIndex] > 0)
                                {
                                    enemy.GetComponent <Health>().health -= world.GetPlayer().GetComponent <Damager>().damage *enemyCP.playerAttackPattern[scrollIndex];

                                    isAttacking = false;
                                    scrollIndex = 0;
                                    PrintEnemyDetails(enemy, isAttacking);
                                }
                                else
                                {
                                    enemy.GetComponent <Health>().health = 0;
                                    enemy.Removed = true;

                                    // Add the enemy's items to the player.
                                    if (enemy.HasComponent <Inventory>())
                                    {
                                        //world.GetPlayer().GetComponent<Inventory>().items =
                                        //    (new Inventory(world.GetPlayer().GetComponent<Inventory>().items) + enemy.GetComponent<Inventory>()).items;

                                        InventoryMenu.Start("Player", "Ground", ref world.GetPlayer().GetComponent <Inventory>().items, ref enemy.GetComponent <Inventory>().items);

                                        Renderer.RenderPlayerInfo(world.GetPlayer());

                                        // Create a loot drop on the ground if the player does not collect all the items.
                                        if (enemy.GetComponent <Inventory>().items.Count > 0)
                                        {
                                            Entity loot = new Entity(EntityFlags.Loot);
                                            loot.AddComponent(new Position(enemy.GetComponent <Position>()))
                                            .AddComponent(new Symbol('o'))
                                            .AddComponent(new Colour(Color.Gray))
                                            .AddComponent(new Inventory(enemy.GetComponent <Inventory>().items));

                                            Renderer.RenderEntity(loot);

                                            world.GetCurrentZone().Entities.Add(loot);
                                        }
                                    }

                                    // Give the enemy's weapon to the player.
                                    if (enemy.HasComponent <Weapon>())
                                    {
                                        // Clear input fields
                                        Renderer.PrintAt(Constants.TextInputX, Constants.TextInputY, new string(' ', 100), Color.White);
                                        // Open menu to take weapon.
                                        WeaponMenu.Start(ref world, ref enemy);
                                        // Show the player's new weapon.
                                        Renderer.RenderPlayerInfo(world.GetPlayer());

                                        // Create a loot drop on the ground for the weapon if the enemy has a propper weapon.
                                        if (!(enemy.GetComponent <Weapon>().name == "" || enemy.GetComponent <Weapon>().name == "none" || enemy.GetComponent <Weapon>().damage == 1))
                                        {
                                            Entity loot = world.GetCurrentZone().GetEntityAt(enemy.GetComponent <Position>());
                                            if (loot == null)
                                            {
                                                loot = new Entity(EntityFlags.Loot);
                                                loot.AddComponent(new Position(enemy.GetComponent <Position>().x, enemy.GetComponent <Position>().y))
                                                .AddComponent(new Symbol('o'))
                                                .AddComponent(new Colour(Color.Gray))
                                                .AddComponent(new Weapon(enemy.GetComponent <Weapon>().name, enemy.GetComponent <Weapon>().damage));
                                            }
                                            else
                                            {
                                                if (loot.HasComponent <Weapon>())
                                                {
                                                    // Set the weapon to the weapon with the highest damage.
                                                    loot.GetComponent <Weapon>().name = enemy.GetComponent <Weapon>().damage >= loot.GetComponent <Weapon>().damage
                                                        ? enemy.GetComponent <Weapon>().name : loot.GetComponent <Weapon>().name;
                                                    loot.GetComponent <Weapon>().damage = enemy.GetComponent <Weapon>().damage >= loot.GetComponent <Weapon>().damage
                                                        ? enemy.GetComponent <Weapon>().damage : loot.GetComponent <Weapon>().damage;
                                                }
                                                else
                                                {
                                                    // Set the loot's weapon to a copy of the enemy's weapon.
                                                    loot.AddComponent(new Weapon(enemy.GetComponent <Weapon>()));
                                                }
                                            }

                                            Renderer.RenderEntity(loot);

                                            world.GetCurrentZone().Entities.Add(loot);
                                        }
                                    }

                                    // Give the enemy's coins to the player.
                                    if (enemy.HasComponent <Coins>())
                                    {
                                        world.GetPlayer().GetComponent <Coins>().coins += enemy.GetComponent <Coins>().coins;
                                        enemy.GetComponent <Coins>().coins              = 0;
                                        // Show the player's new coins.
                                        Renderer.RenderPlayerInfo(world.GetPlayer());
                                    }

                                    // Check if the enemy complete's the hermit quest.
                                    if (enemy.HasFlag(EntityFlags.HermitCompleter))
                                    {
                                        Quests.HermitQuest = QuestStatus.Completed;
                                    }

                                    PrintOutcome(enemy, true);
                                }

                                // Clear text input
                                Console.SetCursorPosition(Constants.TextInputX, Constants.TextInputY);
                                Console.WriteLine(new string(' ', 100));
                            }
                            else
                            {
                                if (world.GetPlayer().GetComponent <Health>().health - enemy.GetComponent <Damager>().damage *enemyCP.playerDefendPattern[scrollIndex] > 0)
                                {
                                    world.GetPlayer().GetComponent <Health>().health -= enemy.GetComponent <Damager>().damage *enemyCP.playerDefendPattern[scrollIndex];
                                    // Show the player's health.
                                    //Renderer.PrintAt(Constants.PlayerStatsX, Constants.PlayerStatsY + 1, $"Health: {world.GetPlayer().GetComponent<Health>().health} / {world.GetPlayer().GetComponent<Health>().maxHealth}", Color.LightGray);
                                    Renderer.RenderPlayerInfo(world.GetPlayer());
                                    isAttacking = true;
                                    scrollIndex = 0;
                                }
                                else
                                {
                                    world.GetPlayer().GetComponent <Health>().health = 0;
                                    // Show the player's health.
                                    //Renderer.PrintAt(Constants.PlayerStatsX, Constants.PlayerStatsY + 1, $"Health: {world.GetPlayer().GetComponent<Health>().health} / {world.GetPlayer().GetComponent<Health>().maxHealth}", Color.LightGray);
                                    Renderer.RenderPlayerInfo(world.GetPlayer());
                                    PrintOutcome(enemy, false);
                                    Game.Stop(false);
                                }

                                PrintEnemyDetails(enemy, isAttacking);
                                // Clear text input
                                Console.SetCursorPosition(Constants.TextInputX, Constants.TextInputY);
                                Console.WriteLine(new string(' ', 100));
                            }
                        }
                        if (Console.KeyAvailable && Console.ReadKey(true).Key == ConsoleKey.Escape)
                        {
                            // Clear output fields
                            Renderer.PrintAt(Constants.TextInputX, Constants.TextInputY, new string(' ', 100), Color.White);
                            Renderer.PrintGameOutput(
                                "Enter a command... ".PadRight(100) + "\n" +
                                new string(' ', 100) + "\n" +
                                new string(' ', 100) + "\n" +
                                new string(' ', 100) + "\n");
                            Console.CursorVisible = true;
                            return(true);
                        }
                    }
                    stopwatch.Reset();
                }

                Renderer.PrintAt(Constants.TextInputX, Constants.TextInputY, new string(' ', 100), Color.White);
                Renderer.PrintGameOutput(
                    "Enter a command... ".PadRight(100) + "\n" +
                    new string(' ', 100) + "\n" +
                    new string(' ', 100) + "\n" +
                    new string(' ', 100) + "\n");
                Console.CursorVisible = true;

                return(true);
            }