Пример #1
0
        public void CanGetEntityComponentsByType()
        {
            // Arrange
            var entity = new TWEntity("test entity");

            entity.AddComponent(new TestComponent("test component 1"));
            entity.AddComponent(new TestComponent("test component 2"));
            // Act
            var components = entity.GetComponentsByType <TestComponent>();

            // Assert
            components.Should().HaveCount(2);
        }
Пример #2
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");
            }
        }
Пример #3
0
        public static void ShowItemAction(List <TWEntity> roomEntities, List <TWEntity> itemEntities, TWEntity playerEntity, TWEntity outputEntity, ItemActionComponent component)
        {
            var roomEntity = GetPlayersCurrentRoom(playerEntity, roomEntities);

            var showItem = Helper.GetItemDropComponentFromEntity(roomEntity !, component.ItemName ?? string.Empty);

            if (showItem != null)
            {
                outputEntity.AddComponent(new OutputComponent("output for item in room", $"{showItem.Item.Name} ({showItem.Item.Quantity})", OutputType.Regular));
            }
            else
            {
                outputEntity.AddComponent(new OutputComponent("output for item in room", "That item does not exist here", OutputType.Regular));
            }
        }
Пример #4
0
        public static void TakeItemAction(List <TWEntity> roomEntities, List <TWEntity> itemEntities, TWEntity playerEntity, TWEntity outputEntity, ItemActionComponent component)
        {
            var roomEntity = GetPlayersCurrentRoom(playerEntity, roomEntities);
            var takeItem   = Helper.GetItemDropComponentFromEntity(roomEntity !, component.ItemName ?? string.Empty);

            if (takeItem != null)
            {
                Helper.AddItemToPlayersInventory(playerEntity, roomEntity !, takeItem);
                outputEntity.AddComponent(new OutputComponent("output for item taken", $"You've taken {component.ItemName}", OutputType.Regular));
            }
            else
            {
                var itemName = string.IsNullOrEmpty(component.ItemName) ? "that item" : component.ItemName;
                outputEntity.AddComponent(new OutputComponent("output for non existant item", $"{itemName} does not exist here.", OutputType.Regular));
            }
        }
Пример #5
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);
        }
Пример #6
0
 public static void AddCommandComponentToEntity(TWEntity commandEntity, string command)
 {
     if (!string.IsNullOrEmpty(command))
     {
         commandEntity.AddComponent(new CommandComponent("add command", command.ToLower()));
     }
 }
Пример #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 static void TakeAllItemsAction(List <TWEntity> roomEntities, List <TWEntity> itemEntities, TWEntity playerEntity, TWEntity outputEntity, ItemActionComponent component)
        {
            var roomEntity = GetPlayersCurrentRoom(playerEntity, roomEntities);
            var roomItems  = roomEntity !.GetComponentsByType <ItemDropComponent>();

            if (roomItems.Count > 0)
            {
                roomItems.ForEach(item =>
                {
                    Helper.AddItemToPlayersInventory(playerEntity, roomEntity !, item);
                    outputEntity.AddComponent(new OutputComponent("output for item taken", $"You've taken {item.Item.Name}", OutputType.Regular));
                });
            }
            else
            {
                outputEntity.AddComponent(new OutputComponent("output for non existant item", $"Can't find any items here.", OutputType.Regular));
            }
        }
Пример #9
0
        public static void ShowAllItemAction(List <TWEntity> roomEntities, List <TWEntity> itemEntities, TWEntity playerEntity, TWEntity outputEntity, ItemActionComponent component)
        {
            var roomEntity         = GetPlayersCurrentRoom(playerEntity, roomEntities);
            var itemDropComponents = roomEntity !.GetComponentsByType <ItemDropComponent>();

            var items = new List <string>();

            itemDropComponents.ForEach(item => items.Add($"{item.Item.Name} ({item.Item.Quantity})"));

            if (items.Count > 0)
            {
                outputEntity.AddComponent(new OutputComponent("output for items in room", $"The following items are here: {string.Join(", ", items.ToArray())}", OutputType.Regular));
            }
            else
            {
                outputEntity.AddComponent(new OutputComponent("output for no items in room", "There are no items here.", OutputType.Regular));
            }
        }
Пример #10
0
        public void CanCreateEntityWithComponent()
        {
            // Arrange
            var entity = new TWEntity("test entity");

            entity.AddComponent(new DescriptionComponent("test component", "This is a test description"));
            // Act
            // Assert
            entity.Components.Should().HaveCount(1);
        }
Пример #11
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));
            }
        }
Пример #12
0
        public override void Run(TWEntity commandEntity, TWEntity playerEntity, List <TWEntity> roomEntities, TWEntity outputEntity)
        {
            TextInfo myTI = new CultureInfo("en-US", false).TextInfo;
            var      processedComponents = new List <CommandComponent>();

            foreach (var commandComponent in commandEntity.GetComponentsByType <CommandComponent>())
            {
                var commandAsTitleCase = myTI.ToTitleCase(commandComponent.Command !);

                if (Enum.TryParse <Direction>(commandAsTitleCase, out Direction direction))
                {
                    processedComponents.Add(commandComponent);

                    var currentRoomComponent = playerEntity.GetComponentByName <IdComponent>("player current room");
                    var currentRoomEntity    = roomEntities.FirstOrDefault(x => x.Id == currentRoomComponent !.Id);
                    var currentRoomExits     = currentRoomEntity !.GetComponentsByType <ExitComponent>();
                    var exit = currentRoomExits.FirstOrDefault(x => x.Direction.ToString() == commandAsTitleCase);

                    if (exit != null)
                    {
                        var newRoomEntity = roomEntities.FirstOrDefault(x => x.Id == exit.RoomId);

                        if (newRoomEntity != null)
                        {
                            currentRoomComponent !.Id = newRoomEntity.Id;

                            playerEntity.AddComponent(new ShowDescriptionComponent("player new room", newRoomEntity, DescriptionType.Room));
                            playerEntity.AddComponent(Helper.GetRoomExitInfoForRoom(playerEntity, roomEntities, newRoomEntity));
                        }
                    }
                    else
                    {
                        outputEntity.AddComponent(new OutputComponent("output for inaccessible direction", "I cannot go in that direction", OutputType.Regular));
                    }

                    commandEntity.RemoveComponents(processedComponents);
                }
            }
        }
Пример #13
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));
            }
        }
Пример #14
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));
                }
            }
        }
Пример #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 CanRemoveEntityComponent()
        {
            // 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.GetComponentByName <DescriptionComponent>(componentName);

            if (component != null)
            {
                entity.RemoveComponent(component);
            }

            // Assert
            entity.Components.Should().BeEmpty();
        }
Пример #17
0
        public void CanGetEntityComponentByName()
        {
            // 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.GetComponentByName <DescriptionComponent>(componentName);

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

            if (component != null)
            {
                component.Name.Should().Be(componentName);
                component.Description.Should().Be(description);
            }
        }
Пример #18
0
        public override void Run(TWEntity commandEntity, TWEntity outputEntity)
        {
            var unknownCommandComponents = new List <UnknownCommandComponent>();

            commandEntity.GetComponentsByType <CommandComponent>().ForEach(x =>
            {
                unknownCommandComponents.Add(new UnknownCommandComponent("unknown command", x.Command !));
            });

            commandEntity.Components.Clear();

            if (unknownCommandComponents.Count > 0)
            {
                commandEntity.Components.AddRange(unknownCommandComponents);

                unknownCommandComponents.ForEach(x =>
                {
                    outputEntity.AddComponent(new OutputComponent("output for unknown command", $"I don't know how to do: {x.Command}", OutputType.Regular));
                });
            }
        }
Пример #19
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);
            }
        }
Пример #20
0
 public override void Use(TWEntity entity, List <TWEntity> itemEntities, TWEntity outputEntity)
 {
     outputEntity.AddComponent(new OutputComponent("output for item used", $"You rub the {Name} with all your might but it doesn't seem to do anything", OutputType.Regular));
 }
Пример #21
0
 public virtual void Use(TWEntity entity, List <TWEntity> entities, TWEntity outputEntity)
 {
     outputEntity.AddComponent(new OutputComponent("output for item used", $"Hmm, nothing happened", OutputType.Regular));
 }