public void EnslaveMovementAndSprite(IEntity master, IEntity slave) { slave.RemoveComponent(ComponentFamily.Mover); slave.AddComponent(ComponentFamily.Mover, componentFactory.GetComponent <SlaveMoverComponent>()); slave.GetComponent <SlaveMoverComponent>(ComponentFamily.Mover).Attach(master); if (slave.HasComponent(ComponentFamily.Renderable) && master.HasComponent(ComponentFamily.Renderable)) { slave.GetComponent <IRenderableComponent>(ComponentFamily.Renderable).SetMaster(master); } }
/// <summary> /// Creates an entity from this prototype. /// Do not call this directly, use the server entity manager instead. /// </summary> /// <returns></returns> public Entity CreateEntity(EntityUid uid, IEntityManager manager, IEntityNetworkManager networkManager, IComponentFactory componentFactory) { var entity = (Entity)Activator.CreateInstance(ClassType ?? typeof(Entity)); entity.SetManagers(manager, networkManager); entity.SetUid(uid); entity.Name = Name; entity.Prototype = this; if (DataNode != null) { entity.ExposeData(new YamlEntitySerializer(DataNode)); } foreach (var componentData in Components) { var component = (Component)componentFactory.GetComponent(componentData.Key); component.Owner = entity; component.LoadParameters(componentData.Value); component.ExposeData(new YamlEntitySerializer(componentData.Value)); entity.AddComponent(component); } return(entity); }
public bool TryAddStatusEffect(EntityUid uid, string key, TimeSpan time, bool refresh, string component, StatusEffectsComponent?status = null) { if (!Resolve(uid, ref status, false)) { return(false); } if (TryAddStatusEffect(uid, key, time, refresh, status)) { // If they already have the comp, we just won't bother updating anything. if (!EntityManager.HasComponent(uid, _componentFactory.GetRegistration(component).Type)) { // F**k this shit I hate it var newComponent = (Component)_componentFactory.GetComponent(component); newComponent.Owner = uid; EntityManager.AddComponent(uid, newComponent); status.ActiveEffects[key].RelevantComponent = component; } return(true); } return(false); }
/// <summary> /// Creates an entity from this template /// </summary> /// <returns></returns> public IEntity CreateEntity(IEntityManager manager, IEntityNetworkManager networkManager, IComponentFactory componentFactory) { var entity = (IEntity)Activator.CreateInstance(ClassType, manager, networkManager); foreach (KeyValuePair <string, Dictionary <string, YamlNode> > componentData in components) { IComponent component; try { component = componentFactory.GetComponent(componentData.Key); } catch (UnknowComponentException) { // Ignore nonexistant ones. // This is kind of inefficient but we'll do the sanity on prototype creation // Once the dependency injection stack is fixed. Log.Logger.Log(string.Format("Unable to load prototype component. UnknowComponentException occured for componentKey `{0}`", componentData.Key)); continue; } component.LoadParameters(componentData.Value); entity.AddComponent(component.Family, component); } entity.Name = Name; entity.Prototype = this; return(entity); }
private static string GetLinkTitle(XmlElement linkElement, Localization localization) { string componentUri = linkElement.GetAttribute("xlink:href"); IComponentFactory componentFactory = DD4TFactoryCache.GetComponentFactory(localization); IComponent component = componentFactory.GetComponent(componentUri); return((component == null) ? linkElement.GetAttribute("title") : component.Title); }
/// <summary> /// Ensures that the Component Fields of DCPs on the Page are populated. /// </summary> private static void FullyLoadDynamicComponentPresentations(IPage page, Localization localization) { using (new Tracer(page, localization)) { foreach (ComponentPresentation dcp in page.ComponentPresentations.Where(cp => cp.IsDynamic).OfType <ComponentPresentation>()) { IComponentFactory componentFactory = DD4TFactoryCache.GetComponentFactory(localization); dcp.Component = (Component)componentFactory.GetComponent(dcp.Component.Id, dcp.ComponentTemplate.Id); } } }
public void AddRandomTrigger(EntityUid uid, ArtifactComponent?component = null) { if (!Resolve(uid, ref component)) { return; } var triggerName = _random.Pick(component.PossibleTriggers); var trigger = (Component)_componentFactory.GetComponent(triggerName); trigger.Owner = uid; EntityManager.AddComponent(uid, trigger); }
/// <inheritdoc /> public T AddComponent <T>(IEntity entity) where T : Component, new() { if (entity == null) { throw new ArgumentNullException(nameof(entity)); } var newComponent = _componentFactory.GetComponent <T>(); newComponent.Owner = entity; AddComponent(entity, newComponent); return(newComponent); }
public void CopyInto(IGameObject newObject) { //Copy components foreach (var component in AllComponents()) { var newComponent = componentFactory.GetComponent(component.ConcreteType); newObject.AddComponent(component.InterfaceType, newComponent); component.CopyInto(newComponent); } //Copy metadata and layer info foreach (var entry in metadata) { newObject.AddMetadata(entry.Key, entry.Value); } newObject.Layer = Layer; }
public string GetTitle(TridionSiteMapNode node) { string title = null; string pageUri = null; if (node.Attributes["type"].Equals("64")) { pageUri = node.Attributes["id"]; } else { var landingPageNode = node.ChildNodes .Cast <TridionSiteMapNode>() .FirstOrDefault(tn => tn.Attributes["type"].Equals("64") && tn.Title.StartsWith("000 ")); if (landingPageNode != null) { pageUri = landingPageNode.Attributes["id"]; } } if (!String.IsNullOrEmpty(pageUri)) { IPage landingPage; if (pageFactory.TryGetPage(pageUri, out landingPage)) { var landingCp = landingPage.ComponentPresentations.FirstOrDefault(); if (landingCp != null) { var component = componentFactory.GetComponent(landingCp.Component.Id, landingCp.ComponentTemplate.Id); title = component.Fields["Heading"].Value; } } } if (String.IsNullOrEmpty(title)) { title = node.Title.Remove(0, 4); } return(title); }
private void OnEntityInserted(EntityUid uid, PayloadCaseComponent _, EntInsertedIntoContainerMessage args) { if (!TryComp(args.Entity, out PayloadTriggerComponent? trigger)) { return; } trigger.Active = true; if (trigger.Components == null) { return; } // ANY payload trigger that gets inserted can grant components. It is up to the construction graphs to determine trigger capacity. foreach (var(name, data) in trigger.Components) { if (!_componentFactory.TryGetRegistration(name, out var registration)) { continue; } if (HasComp(uid, registration.Type)) { continue; } if (_componentFactory.GetComponent(registration.Type) is not Component component) { continue; } component.Owner = uid; if (_serializationManager.Copy(data, component, null) is Component copied) { EntityManager.AddComponent(uid, copied); } trigger.GrantedComponents.Add(registration.Type); } }
/// <summary> /// Creates an entity from this template /// </summary> /// <returns></returns> public IEntity CreateEntity(IEntityManager manager, IEntityNetworkManager networkManager, IComponentFactory componentFactory) { var entity = (IEntity)Activator.CreateInstance(ClassType, manager, networkManager, componentFactory); entity.Name = Name; entity.Prototype = this; foreach (KeyValuePair <string, YamlMappingNode> componentData in Components) { IComponent component = componentFactory.GetComponent(componentData.Key); component.LoadParameters(componentData.Value); entity.AddComponent(component); } entity.LoadData(DataNode); return(entity); }
public static string GetTitle(TridionSiteMapNode node, DD4T.ContentModel.Factories.IPageFactory pageFactory, IComponentFactory cmpFactory) { string title = null; string pageUri = null; if (node.Attributes["type"].Equals("64")) { pageUri = node.Attributes["id"]; } else { var landingPageNode = node.ChildNodes .Cast<TridionSiteMapNode>() .FirstOrDefault(tn => tn.Attributes["type"].Equals("64") && tn.Title.StartsWith("000 ")); if (landingPageNode != null) { pageUri = landingPageNode.Attributes["id"]; } } if (!String.IsNullOrEmpty(pageUri)) { IPage landingPage; if (pageFactory.TryGetPage(pageUri, out landingPage)) { var landingCp = landingPage.ComponentPresentations.FirstOrDefault(); if (landingCp != null) { var component = cmpFactory.GetComponent(landingCp.Component.Id, landingCp.ComponentTemplate.Id); title = component.Fields["Heading"].Value; } } } if (String.IsNullOrEmpty(title)) { title = node.Title.Remove(0, 4); } return title; }
private void AddRandomTrigger(EntityUid uid, ArtifactComponent?component = null) { if (!Resolve(uid, ref component)) { return; } var triggerName = _random.Pick(component.PossibleTriggers); var trigger = (Component)_componentFactory.GetComponent(triggerName); trigger.Owner = uid; if (EntityManager.HasComponent(uid, trigger.GetType())) { Logger.Error($"Attempted to add a random artifact trigger ({triggerName}) to an entity ({ToPrettyString(uid)}), but it already has the trigger"); return; } EntityManager.AddComponent(uid, trigger); RaiseLocalEvent(uid, new RandomizeTriggerEvent(), true); }
private void OnGotEquipped(EntityUid uid, AddAccentClothingComponent component, GotEquippedEvent args) { if (!TryComp(uid, out ClothingComponent? clothing)) { return; } // check if entity was actually used as clothing // not just taken in pockets or something var isCorrectSlot = clothing.SlotFlags.HasFlag(args.SlotFlags); if (!isCorrectSlot) { return; } // does the user already has this accent? var componentType = _componentFactory.GetRegistration(component.Accent).Type; if (EntityManager.HasComponent(args.Equipee, componentType)) { return; } // add accent to the user var accentComponent = (Component)_componentFactory.GetComponent(componentType); accentComponent.Owner = args.Equipee; EntityManager.AddComponent(args.Equipee, accentComponent); // snowflake case for replacement accent if (accentComponent is ReplacementAccentComponent rep) { rep.Accent = component.ReplacementPrototype !; } component.IsActive = true; }
/// <summary> /// Return an instance of the specified component that match specified type. /// </summary> /// <typeparam name="T">type the component must match</typeparam> /// <param name="name"></param> /// <returns>component instance of desired type</returns> public static T GetComponent <T>(this IComponentFactory factory, string name) { return((T)factory.GetComponent(name, typeof(T))); }
private void HandleEntityState(IComponentManager compMan, IEntity entity, EntityState curState, EntityState nextState) { var compStateWork = new Dictionary <uint, (ComponentState curState, ComponentState nextState)>(); var entityUid = entity.Uid; if (curState?.ComponentChanges != null) { foreach (var compChange in curState.ComponentChanges) { if (compChange.Deleted) { if (compMan.TryGetComponent(entityUid, compChange.NetID, out var comp)) { compMan.RemoveComponent(entityUid, comp); } } else { if (compMan.HasComponent(entityUid, compChange.NetID)) { continue; } var newComp = (Component)_compFactory.GetComponent(compChange.ComponentName); newComp.Owner = entity; compMan.AddComponent(entity, newComp, true); } } } if (curState?.ComponentStates != null) { foreach (var compState in curState.ComponentStates) { compStateWork[compState.NetID] = (compState, null); } } if (nextState?.ComponentStates != null) { foreach (var compState in nextState.ComponentStates) { if (compStateWork.TryGetValue(compState.NetID, out var state)) { compStateWork[compState.NetID] = (state.curState, compState); } else { compStateWork[compState.NetID] = (null, compState); } } } foreach (var kvStates in compStateWork) { if (!compMan.TryGetComponent(entityUid, kvStates.Key, out var component)) { var eUid = entityUid; var eExpectedNetUid = kvStates.Key; var eRegisteredNetUidName = _compFactory.GetRegistration(eExpectedNetUid).Name; DebugTools.Assert($"Component does not exist for state: entUid={eUid}, expectedNetId={eExpectedNetUid}, expectedName={eRegisteredNetUidName}"); continue; } try { component.HandleComponentState(kvStates.Value.curState, kvStates.Value.nextState); } catch (Exception e) { var wrapper = new ComponentStateApplyException( $"Failed to apply comp state: entity={component.Owner}, comp={component.Name}", e); #if EXCEPTION_TOLERANCE _runtimeLog.LogException(wrapper, "Component state apply"); #else throw wrapper; #endif } } }