/// <summary>Samples the attributes to apply to the item.</summary> /// <param name="manager"> </param> /// <param name="faction">The faction the ship belongs to.</param> /// <param name="position">The position at which to spawn the ship.</param> /// <param name="random">The randomizer to use.</param> /// <return>The entity with the attributes applied.</return> public int Sample(IManager manager, Factions faction, FarPosition position, IUniformRandom random) { var entity = CreateShip(manager, faction, position); // Create initial equipment. var equipment = (ItemSlot)manager.GetComponent(entity, ItemSlot.TypeId); equipment.Item = FactoryLibrary.SampleItem(manager, _items.Name, position, random); if (equipment.Item > 0) { foreach (var item in _items.Slots) { SampleItems(manager, position, random, equipment.Item, item); } } // Add our attributes. var attributes = (Attributes <AttributeType>)manager.GetComponent(entity, Attributes <AttributeType> .TypeId); foreach (var attribute in _attributes) { var modifier = attribute.SampleAttributeModifier(random); if (modifier.ComputationType == AttributeComputationType.Multiplicative) { throw new InvalidOperationException("Base attributes must be additive."); } attributes.SetBaseValue(modifier.Type, modifier.Value); } // Fill up our values. var health = ((Health)manager.GetComponent(entity, Health.TypeId)); var energy = ((Energy)manager.GetComponent(entity, Energy.TypeId)); health.Value = health.MaxValue; energy.Value = energy.MaxValue; // Add experience points if we're worth any. if (_xp > 0) { manager.AddComponent <ExperiencePoints>(entity).Initialize(_xp); } return(entity); }
/// <summary>Samples the items for the specified item info and children (recursively).</summary> /// <param name="manager">The manager.</param> /// <param name="position">The position.</param> /// <param name="random">The random.</param> /// <param name="parent">The parent.</param> /// <param name="itemInfo">The item info.</param> private static void SampleItems( IManager manager, FarPosition position, IUniformRandom random, int parent, ItemInfo itemInfo) { // Create the actual item. var itemId = FactoryLibrary.SampleItem(manager, itemInfo.Name, position, random); if (itemId < 1) { // No such item. return; } var item = (Item)manager.GetComponent(itemId, Item.TypeId); // Then equip it in the parent. foreach (ItemSlot slot in manager.GetComponents(parent, ItemSlot.TypeId)) { if (slot.Item == 0 && slot.Validate(item)) { // Found a suitable empty slot, equip here. slot.Item = itemId; // Recurse to generate children. foreach (var childInfo in itemInfo.Slots) { SampleItems(manager, position, random, itemId, childInfo); } // Done. return; } } // If we get here we couldn't find a slot to equip the item in. manager.RemoveEntity(itemId); Logger.Warn("Parent item did not have a slot for the requested child item."); }