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); } }
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); } }
public override void Run(TWEntity outputEntity) { foreach (var component in outputEntity.GetComponentsByType <OutputComponent>()) { if (component.OutputType == OutputType.Regular) { Console.WriteLine(component.Value); Console.WriteLine(); } //else if (component.OutputType == OutputType.Command) //{ // Console.WriteLine($"command: {component.Value}"); // Console.WriteLine(); //} else if (component.OutputType == OutputType.Separator) { Console.WriteLine(); } else if (component.OutputType == OutputType.MessageOfTheDay) { Console.WriteLine($"-[ {component.Value} ]-"); Console.WriteLine(); } } outputEntity.Components.Clear(); }
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); }
public static void AddCommandComponentToEntity(TWEntity commandEntity, string command) { if (!string.IsNullOrEmpty(command)) { commandEntity.AddComponent(new CommandComponent("add command", command.ToLower())); } }
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); } } }
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"); } }
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); }
public void CanCreateEmptyEntity() { // Arrange // Act var entity = new TWEntity("test entity"); // Assert entity.Id.Should().NotBe(Guid.Empty); }
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)); } }
public static ShowDescriptionComponent GetRoomExitInfoForRoom(TWEntity playerEntity, List <TWEntity> roomEntities, TWEntity roomEntity) { var newRoomExits = roomEntity !.GetComponentsByType <ExitComponent>(); var exitDictionary = newRoomExits.ToDictionary(x => x.RoomId); var exitRooms = roomEntities.Where(x => exitDictionary.TryGetValue(x.Id, out var e) && !e.Hidden).ToList(); var exitInfo = exitRooms.Select(x => $"{exitDictionary[x.Id].Direction} -> {x.Name}".ToString()).ToList(); return(new ShowDescriptionComponent($"Exits: {string.Join(", ", exitInfo)}", exitRooms, DescriptionType.Exit)); }
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); }
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)); } }
public override void Run(TWEntity playerEntity, Action action) { var component = playerEntity.GetComponentByType <QuitComponent>(); if (component != null) { action(); playerEntity.RemoveComponent(component); } }
public override void Run(TWEntity playerEntity, List <TWEntity> itemEntities, List <TWEntity> roomEntities, TWEntity outputEntity) { var processedComponents = new List <TWComponent>(); foreach (var component in playerEntity.GetComponentsByType <ItemActionComponent>()) { processedComponents.Add(component); component.Action !(roomEntities, itemEntities, playerEntity, outputEntity, component); } playerEntity.RemoveComponents(processedComponents); }
public static TWEntity?GetPlayersCurrentRoom(TWEntity playerEntity, List <TWEntity> roomEntities) { TWEntity?result = null; var roomIdComponent = playerEntity.GetComponentByName <IdComponent>("player current room"); if (roomIdComponent != null) { result = roomEntities.FirstOrDefault(x => x.Id == roomIdComponent.Id); } return(result); }
public void CanRemoveEntityComponentsByType() { // Arrange var entity = new TWEntity("test entity"); entity.AddComponent(new TestComponent("test component 1")); entity.AddComponent(new TestComponent("test component 2")); // Act entity.RemoveComponentsByType <TestComponent>(); // Assert entity.Components.Should().BeEmpty(); }
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); }
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"); } }
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)); } }
public static ItemDropComponent?GetItemDropComponentFromEntity(TWEntity entity, string itemName) { var items = entity.GetComponentsByType <ItemDropComponent>(); if (items.Count > 0) { var takeItem = items.FirstOrDefault(x => x.Item.Name == itemName); if (takeItem != null) { return(takeItem); } } return(null); }
public override void Run(TWEntity playerEntity, List <TWEntity> roomEntities, TWEntity outputEntity) { var processedComponents = new List <TWComponent>(); foreach (var component in playerEntity.Components .Where(x => x.GetType() == typeof(ShowDescriptionComponent))) { processedComponents.Add(component); var showDescriptionComponent = component as ShowDescriptionComponent; if (showDescriptionComponent !.DescriptionType == DescriptionType.Room) { var entity = showDescriptionComponent !.Entity; var descriptionComponent = entity !.GetComponentByType <DescriptionComponent>(); outputEntity.AddComponent(new OutputComponent("room description output", descriptionComponent !.Description, OutputType.Regular)); }
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 !); }
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); } } }
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"); } }
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(); }
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); } }
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)); }); } }
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); } }
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); } }