/// <summary> /// Attempts to inject a component into a field, and adds the component to the specified entity. /// </summary> /// <remarks> /// Note that the dependency will remain, even if it becomes dettached from its entity. /// </remarks> void InjectDependency(FieldInfo field, IEntityRecord record, bool allowingDerivedTypes) { if (field == null || record == null) { return; } Type componentType = field.FieldType; IComponent dependency = record.Registry.GetComponent(record, componentType, allowingDerivedTypes); if (dependency == null) { dependency = Create(componentType); if (dependency == null) { return; } record.Add(dependency); } if (dependency != null) { field.SetValue(this, dependency); } }
public static IEntityRecord CreateFromDefinition(string definition, string name, params IComponent[] components) { IEntityRecord entity = _definitions.Make(definition, Create(name)); foreach (IComponent component in components) { entity.Add(component); } return(entity); }
/// <summary> /// Creates and registers a new entity in the specified registry. The specified components will also be attached to it. /// </summary> public static IEntityRecord Create(string name, IEntityRecordCollection registry, params IComponent[] components) { IEntityRecord record = Create(name, registry); foreach (IComponent component in components) { record.Add(component); } return(record); }
public static IEntityRecord CreateFromDefinition(string definition, params IComponent[] components) { IEntityRecord entity = CreateFromDefinition(definition); foreach (IComponent component in components) { entity.Add(component); } return(entity); }
/// <summary> /// Realizes the specified definition for a given entity. /// </summary> public IEntityRecord Make(string definition, IEntityRecord entity) { if (!_definitions.ContainsKey(definition)) { return(null); } IList <Type> componentTypes = _definitions[definition]; foreach (Type componentType in componentTypes) { try { entity.Add(Component.Create(componentType)); } catch (InvalidOperationException) { /// A component that doesn't provide a parameter-less constructor will fail instantiation. /// If that is the case, skip it. continue; } } return(entity); }
static void Main(string[] args) { Entity.Define("Dragon", typeof(Health), typeof(FireBreathing)); Entity.Define("Player", typeof(Health), typeof(Combustible)); IEntityRecord player = Entity.CreateFromDefinition("Player", "You!"); IEntityRecord dragon = Entity.CreateFromDefinition("Dragon", "A firebreathing dragon!"); List <Combustible> combustibles = new List <Combustible>() { new Combustible(), new Combustible(), new Combustible(), new Combustible(), new Combustible(), new Combustible(), new Combustible(), new Combustible(), new Combustible(), }; foreach (Combustible item in combustibles) { player.Add(item); } EntityRegistry.Current.SetTrigger( component => component is Combustible, (sender, eventArgs) => { foreach (IComponent component in eventArgs.Components) { Combustible combustible = component as Combustible; if (combustible != null) { if (combustible.IsOutOfSync) { combustibles.Remove(combustible); } else { combustibles.Add(combustible); } } } }); EntityRegistry.Current.Synchronize(); dragon.GetComponent <FireBreathing>().Breathe(player); Random r = new Random(); for (int turn = 0; turn < 10; turn++) { foreach (Combustible combustible in combustibles) { if (combustible.IsBurning) { combustible.Burn(); } } Combustible fire = player.GetComponent <Combustible>(); Health condition = player.GetComponent <Health>(); if (fire.IsBurning) { // There's a 10% chance that the player figures out how to extinguish himself! bool playerStoppedDroppedAndRolled = r.Next(0, 100) <= 10; if (playerStoppedDroppedAndRolled) { Console.WriteLine("Player extinguished himself !!!"); fire.Extinguish(); } } if (condition.IsDead) { // Unfortunately for the player, he did not figure it out in time. Console.WriteLine("Player is dead ://"); break; } } }
/// <summary> /// Attempts to inject a component into a field, and adds the component to the specified entity. /// </summary> /// <remarks> /// > Note that the dependency will remain, even if it becomes dettached from its entity. /// </remarks> void InjectDependency(FieldInfo field, IEntityRecord record, bool allowingDerivedTypes) { if (field == null || record == null) { return; } Type componentType = field.FieldType; IComponent dependency = record.Registry.GetComponent(record, componentType, allowingDerivedTypes); if (dependency == null) { dependency = Create(componentType); record.Add(dependency); } if (dependency != null) { field.SetValue(this, dependency); } }
void CreateSquad(SpriteBatch spriteBatch) { Entity.Define("squad-unit", typeof(Health)); Texture unitTexture = Texture.FromFile("Content/Graphics/unit.png"); _squadLeader = CreateSquadUnit("leader", _dungeon, new Vector2(unitTexture.Width * (DungeonColumns / 2), 0)); _squadLeader.Add( new SquadLeader() { MovementInPixels = unitTexture.Width }); IEntityRecord squadUnitLeft = CreateSquadUnit("left", _squadLeader, new Vector2(-unitTexture.Width, -unitTexture.Width)); IEntityRecord squadUnitRight = CreateSquadUnit("right", _squadLeader, new Vector2(unitTexture.Width, -unitTexture.Width)); IEntityRecord squadUnitMiddle = CreateSquadUnit("middle", _squadLeader, new Vector2(0, -unitTexture.Width)); spriteBatch.Add(_squadLeader.GetComponent<Sprite>()); spriteBatch.Add(squadUnitLeft.GetComponent<Sprite>()); spriteBatch.Add(squadUnitRight.GetComponent<Sprite>()); spriteBatch.Add(squadUnitMiddle.GetComponent<Sprite>()); }