示例#1
0
        public void CanShowRoomDescription()
        {
            // Arrange
            var playerEntity    = new TWEntity("player");
            var roomEntities    = new List <TWEntity>();
            var commandEntity   = new TWEntity("Command Entity");
            var outputEntity    = new TWEntity("Output Entity");
            var commandSystem   = new CommandSystem();
            var roomId          = Guid.NewGuid();
            var roomDescription = "You are standing in an open field. All around you stands vibrant green grass. You can hear the sound of flowing water off in the distance which you suspect is a stream.";
            var room            = new TWEntity(roomId, "Test Room", new List <TWComponent>()
            {
                new DescriptionComponent("open field description", roomDescription),
            });

            playerEntity.AddComponent(new IdComponent("player current room", roomId, IdType.Room));
            roomEntities.Add(room);

            Helper.AddCommandComponentToEntity(commandEntity, "look");

            // Act
            commandSystem.Run(commandEntity, playerEntity, roomEntities, outputEntity);

            var showRoomDescription = outputEntity.GetComponentByType <ShowDescriptionComponent>();

            // Assert
            showRoomDescription.Should().NotBeNull();
            showRoomDescription !.Entity.Should().NotBeNull();

            var roomDescriptionComponent = showRoomDescription !.Entity !.GetComponentByType <DescriptionComponent>();

            roomDescriptionComponent.Should().NotBeNull();
            roomDescriptionComponent !.Description.Should().Be(roomDescription);
        }
示例#2
0
        public override void Run(TWEntity commandEntity, TWEntity playerEntity, TWEntity outputEntity)
        {
            var processedComponents = new List <CommandComponent>();

            foreach (var commandComponent in commandEntity.GetComponentsByType <CommandComponent>())
            {
                var command = commandComponent.Command !.ToLower();

                if (command == "inv" || command == "inventory")
                {
                    processedComponents.Add(commandComponent);

                    // get the players inventory component
                    var inventoryComponent = playerEntity.GetComponentByType <InventoryComponent>();

                    if (inventoryComponent != null)
                    {
                        var itemsAsString = inventoryComponent.GetItemsAsString();

                        if (string.IsNullOrEmpty(itemsAsString))
                        {
                            outputEntity.AddComponent(new OutputComponent("show player inventory", "You don't have any items in your inventory", OutputType.Regular));
                        }
                        else
                        {
                            outputEntity.AddComponent(new OutputComponent("show player inventory", $"inventory: {itemsAsString}", OutputType.Regular));
                        }
                    }
                }
            }

            commandEntity.RemoveComponents(processedComponents);
        }
示例#3
0
        public static void AddItemToPlayersInventory(TWEntity playerEntity, TWEntity itemOnEntity, ItemDropComponent itemDropComponent)
        {
            var inventoryComponent = playerEntity.GetComponentByType <InventoryComponent>();

            if (inventoryComponent != null)
            {
                var itemInInventory = inventoryComponent.Items.FirstOrDefault(x => x.Id == itemDropComponent.Item.Id);

                if (itemInInventory != null)
                {
                    itemInInventory.Quantity += itemDropComponent.Item.Quantity;
                }
                else
                {
                    inventoryComponent.AddItem(
                        new InventoryItem
                    {
                        Id       = itemDropComponent.Item.Id,
                        Name     = itemDropComponent.Item.Name,
                        Quantity = itemDropComponent.Item.Quantity
                    }
                        );
                }

                var itemToRemove = GetItemDropComponentOnEntity(itemOnEntity, itemDropComponent);

                if (itemToRemove != null)
                {
                    itemOnEntity.RemoveComponent(itemToRemove);
                }
            }
        }
示例#4
0
        public void CanProcessLookSelfCommand()
        {
            // Arrange
            var playerEntity  = new TWEntity("player");
            var roomEntities  = new List <TWEntity>();
            var commandEntity = new TWEntity("Command Entity");
            var commandSystem = new CommandSystem();

            var roomId = Guid.NewGuid();
            var room   = new TWEntity(roomId, "Test Room");

            playerEntity.AddComponent(new InventoryComponent("player inventory"));
            playerEntity.AddComponent(new IdComponent("player current room", roomId, IdType.Room));
            roomEntities.Add(room);

            Helper.AddCommandComponentToEntity(commandEntity, "look self");

            // Act
            commandSystem.Run(commandEntity, playerEntity, roomEntities, playerEntity);
            var showRoomDescriptionComponent = playerEntity.GetComponentByType <ShowDescriptionComponent>();

            // Assert
            showRoomDescriptionComponent.Should().NotBeNull();

            if (showRoomDescriptionComponent != null)
            {
                showRoomDescriptionComponent.Entity.Should().Be(playerEntity);
            }
        }
示例#5
0
        public void CanProcessDropCommand()
        {
            // Arrange
            var playerEntity  = new TWEntity("player");
            var roomEntities  = new List <TWEntity>();
            var commandEntity = new TWEntity("Command Entity");
            var commandSystem = new CommandSystem();

            var roomId = Guid.NewGuid();
            var room   = new TWEntity(roomId, "Test Room", new List <TWComponent>()
            {
                new ItemComponent("health potion item", new HealthPotion(healthPotionId, "health potion", 50, "An ordinary health potion", Array.Empty <string>()))
            });

            playerEntity.AddComponent(new InventoryComponent("player inventory"));
            playerEntity.AddComponent(new IdComponent("player current room", roomId, IdType.Room));
            roomEntities.Add(room);

            Helper.AddCommandComponentToEntity(commandEntity, "drop health potion");
            commandSystem.Run(commandEntity, playerEntity, roomEntities, playerEntity);
            var itemActionComponent = playerEntity.GetComponentByType <ItemActionComponent>();

            // Act
            itemActionComponent.Should().NotBeNull();

            if (itemActionComponent != null)
            {
                itemActionComponent.ActionType.Should().Be(ItemActionType.Drop);
            }
        }
示例#6
0
        public void CanProcessQuitCommand()
        {
            // Arrange
            var playerEntity  = new TWEntity("player");
            var roomEntities  = new List <TWEntity>();
            var commandEntity = new TWEntity("Command Entity");
            var commandSystem = new CommandSystem();

            var roomId = Guid.NewGuid();
            var room   = new TWEntity(roomId, "New Room");

            playerEntity.AddComponent(new InventoryComponent("player inventory"));
            playerEntity.AddComponent(new IdComponent("player current room", roomId, IdType.Room));
            roomEntities.Add(room);

            Helper.AddCommandComponentToEntity(commandEntity, "quit");

            // Act
            commandSystem.Run(commandEntity, playerEntity, roomEntities, playerEntity);
            var quitComponent = playerEntity.GetComponentByType <QuitComponent>();

            // Assert
            quitComponent.Should().NotBeNull();

            if (quitComponent != null)
            {
                quitComponent.Name.Should().Be("quit game");
            }
        }
示例#7
0
        public override void Run(TWEntity motdEntity, TWEntity outputEntity)
        {
            var motdDescriptionComponent = motdEntity.GetComponentByType <DescriptionComponent>();

            if (motdDescriptionComponent != null)
            {
                outputEntity.AddComponent(new OutputComponent("motd output for description", motdDescriptionComponent.Description, OutputType.MessageOfTheDay));
            }
        }
示例#8
0
        public override void Use(TWEntity playerEntity, List <TWEntity> itemEntities, TWEntity outputEntity)
        {
            var currencyComponent = playerEntity.GetComponentByType <CurrencyComponent>();

            if (currencyComponent != null)
            {
                currencyComponent.Coins += NumberOfCoins;

                outputEntity.AddComponent(new OutputComponent("output for item used", $"{Name} used: +{NumberOfCoins} coins", OutputType.Regular));
            }
        }
示例#9
0
        public override void Run(TWEntity playerEntity, Action action)
        {
            var component = playerEntity.GetComponentByType <QuitComponent>();

            if (component != null)
            {
                action();

                playerEntity.RemoveComponent(component);
            }
        }
示例#10
0
        public void CanTakeItemOnEntity()
        {
            // Arrange
            var itemsSystem   = new ItemSystem();
            var playerEntity  = new TWEntity("player");
            var roomEntities  = new List <TWEntity>();
            var outputEntity  = new TWEntity("output");
            var commandEntity = new TWEntity("command");

            var roomId = Guid.NewGuid();
            var room   = new TWEntity(roomId, "New Room", new List <TWComponent>()
            {
                new ItemDropComponent("item", new InventoryItem
                {
                    Id       = Guid.NewGuid(),
                    Name     = "leather coin purse",
                    Quantity = 1
                }),
            });

            Helper.AddCommandComponentToEntity(commandEntity, "take leather coin purse");
            var commandComponent = commandEntity.GetComponentByType <CommandComponent>();

            playerEntity.AddComponent(new InventoryComponent("player inventory"));
            playerEntity.AddComponent(new IdComponent("player current room", roomId, IdType.Room));
            playerEntity.AddComponent(new ItemActionComponent("take item from room", "leather coin purse", commandComponent !, ItemActionType.Take, Helper.TakeItemAction));
            roomEntities.Add(room);

            // we don't care that itemEntities is empty here.
            var itemEntities = new List <TWEntity>();

            // Act
            itemsSystem.Run(playerEntity, itemEntities, roomEntities, outputEntity);
            var outputComponent   = outputEntity.GetComponentByType <OutputComponent>();
            var playerCurrentRoom = Helper.GetPlayersCurrentRoom(playerEntity, roomEntities);

            // Assert
            playerCurrentRoom.Should().NotBeNull();
            outputComponent.Should().NotBeNull();

            if (playerCurrentRoom != null)
            {
                var takenItemComponent = playerCurrentRoom.GetComponentsByType <ItemComponent>().FirstOrDefault(x => x.Item.Name == "leather coin purse");
                takenItemComponent.Should().BeNull();
            }

            if (outputComponent != null)
            {
                outputComponent.Value.Should().Contain("You've taken leather coin purse");
            }
        }
示例#11
0
        public override void Use(TWEntity player, List <TWEntity> itemEntities, TWEntity outputEntity)
        {
            var healthComponent = player.GetComponentByType <HealthComponent>();

            if (healthComponent != null && healthComponent.CurrentHealth < healthComponent.MaxHealth)
            {
                healthComponent.CurrentHealth += HealthImmediately;
                if (healthComponent.CurrentHealth > healthComponent.MaxHealth)
                {
                    healthComponent.CurrentHealth = healthComponent.MaxHealth;
                }

                outputEntity.AddComponent(new OutputComponent("output for item used", $"{Name} used: +{HealthImmediately} health", OutputType.Regular));
            }
        }
示例#12
0
        public static void UseItemFromInventoryAction(List <TWEntity> roomEntities, List <TWEntity> itemEntities, TWEntity playerEntity, TWEntity outputEntity, ItemActionComponent component)
        {
            // look at player inventory to make sure item exists
            var inventoryComponent = playerEntity.GetComponentByType <InventoryComponent>();

            if (inventoryComponent != null)
            {
                // create a variable that is the item name, it's stored in the itemActionComponent.CommandComponent.Args
                var itemName = component.CommandComponent.ArgsJoined;
                // search inventoryComponent.Items for itemName
                var itemInInventory = inventoryComponent.Items.FirstOrDefault(x => x.Name == itemName);

                // if item exists then look the item up in the itemEntities
                if (itemInInventory != null)
                {
                    // get the item entity from the itemEntities
                    var itemEntity = itemEntities.FirstOrDefault(x => x.GetComponentByType <ItemComponent>()?.Item.Name == itemName);

                    if (itemEntity != null)
                    {
                        var itemComponent = itemEntity.GetComponentByType <ItemComponent>();

                        if (itemComponent != null)
                        {
                            var item = itemComponent.Item;

                            item.Use(playerEntity, itemEntities, outputEntity);
                            //outputEntity.AddComponent(new OutputComponent("output for item used", $"You used {itemName}", OutputType.Regular));

                            // if the item is consumable then execute it's Use function
                            if (item.Consumable)
                            {
                                Helper.RemoveOrDecrementItemFromPlayersInventory(playerEntity, playerEntity, itemInInventory);
                            }
                            //else
                            //{
                            //    outputEntity.AddComponent(new OutputComponent("output for item not used", $"You can't use {itemName}", OutputType.Regular));
                            //}
                        }
                    }
                }
                else
                {
                    outputEntity.AddComponent(new OutputComponent("output for item not found", $"You don't have a {itemName}", OutputType.Regular));
                }
            }
        }
示例#13
0
        public override void Run(TWEntity playerEntity, TWEntity commandEntity, TWEntity outputEntity)
        {
            var healthComponent = playerEntity.GetComponentByType <HealthComponent>();

            if (healthComponent != null)
            {
                Console.Write($"[health:{healthComponent.CurrentHealth}/{healthComponent.MaxHealth}]> ");
            }
            else
            {
                Console.Write("> ");
            }

            var command = Console.ReadLine();

            outputEntity.AddComponent(new OutputComponent("command output", command !, OutputType.Command));
            Helper.AddCommandComponentToEntity(commandEntity, command !);
        }
示例#14
0
        public static void RemoveOrDecrementItemFromPlayersInventory(TWEntity playerEntity, TWEntity itemOnEntity, InventoryItem inventoryItem)
        {
            var inventoryComponent = playerEntity.GetComponentByType <InventoryComponent>();

            if (inventoryComponent != null)
            {
                var itemInInventory = inventoryComponent.Items.FirstOrDefault(x => x.Id == inventoryItem.Id);

                if (itemInInventory != null)
                {
                    itemInInventory.Quantity--;
                }

                if (itemInInventory != null && itemInInventory.Quantity <= 0)
                {
                    inventoryComponent.RemoveItem(itemInInventory);
                }
            }
        }
示例#15
0
        public void CanShowItemOnEntity()
        {
            // Arrange
            var itemsSystem   = new ItemSystem();
            var playerEntity  = new TWEntity("player");
            var roomEntities  = new List <TWEntity>();
            var outputEntity  = new TWEntity("output");
            var commandEntity = new TWEntity("command");

            var roomId = Guid.NewGuid();
            var room   = new TWEntity(roomId, "New Room", new List <TWComponent>()
            {
                new ItemDropComponent("item", new InventoryItem
                {
                    Id       = Guid.NewGuid(),
                    Name     = "leather coin purse",
                    Quantity = 1
                }),
            });

            roomEntities.Add(room);

            Helper.AddCommandComponentToEntity(commandEntity, "show leather coin purse");
            var commandComponent = commandEntity.GetComponentByType <CommandComponent>();

            playerEntity.AddComponent(new IdComponent("player current room", roomId, IdType.Room));
            playerEntity.AddComponent(new ItemActionComponent("show room items", "leather coin purse", commandComponent !, ItemActionType.Show, Helper.ShowItemAction));

            // we don't care that itemEntities is empty here.
            var itemEntities = new List <TWEntity>();

            // Act
            itemsSystem.Run(playerEntity, itemEntities, roomEntities, outputEntity);
            var outputComponent = outputEntity.GetComponentByType <OutputComponent>();

            // Assert
            outputComponent.Should().NotBeNull();

            if (outputComponent != null)
            {
                outputComponent.Value.Should().Contain("leather coin purse");
            }
        }
示例#16
0
        public void CanGetEntityComponentByType()
        {
            // Arrange
            var    entity        = new TWEntity("test entity");
            string componentName = "test description component";
            string description   = "This is a test description";

            entity.AddComponent(new DescriptionComponent(componentName, description));
            // Act
            var component = entity.GetComponentByType <DescriptionComponent>();

            // Assert
            component.Should().NotBeNull();

            if (component != null)
            {
                component.Name.Should().Be(componentName);
                component.Description.Should().Be(description);
            }
        }
示例#17
0
        public void CanGetCommandWithArgs()
        {
            // Arrange
            var commandEntity = new TWEntity("commands");
            var command       = "get";
            var arg           = "lamp";

            // Act
            Helper.AddCommandComponentToEntity(commandEntity, $"{command} {arg}");
            var commandComponent = commandEntity.GetComponentByType <CommandComponent>();

            // Assert
            commandComponent.Should().NotBeNull();

            if (commandComponent != null)
            {
                commandComponent.Command.Should().Be(command);
                commandComponent.Args.Should().HaveCount(1);
                commandComponent.Args.First().Should().Be(arg);
            }
        }
示例#18
0
        public void MOTDSystemOutputsMOTD()
        {
            // Arrange
            var outputEntity = new TWEntity("Output Entity");
            var motdEntity   = new TWEntity("MOTD Entity");
            var motdSystem   = new MOTDSystem();
            var motd         = "This is the message of the day.";

            motdEntity.AddComponent(new DescriptionComponent("description", motd));

            // Act
            motdSystem.Run(motdEntity, outputEntity);

            var outputComponent = outputEntity.GetComponentByType <OutputComponent>();

            // Assert
            outputComponent.Should().NotBeNull();
            if (outputComponent != null)
            {
                outputComponent.Value.Should().Be(motd);
            }
        }