/// <summary> /// Adds a component to the entity. /// If the type of the component is already added to the list, /// it will returns this type of component. /// </summary> /// <typeparam name="T">The type of the component.</typeparam> /// <returns>Returns the added component.</returns> public T AddComponent <T>() where T : Component, new() { // check if the entity owns alreday this type of component. if (components.ContainsKey(typeof(T))) { return((T)components[typeof(T)]); } var component = new T(); InitializeComponent(component); // requires the comonent other components? foreach (var cmpType in component.RequiredComponents) { if (components.ContainsKey(cmpType)) { continue; } var newCmp = (Component)Activator.CreateInstance(cmpType); InitializeComponent(newCmp); } components.Add(typeof(T), component); ComponentAdded?.Invoke(this, new ComponentEventArgs(component)); return(component); }
public virtual IStudioComponent AddComponent(IStudioComponent component) { if (_components.Contains(component)) { return(null); } IStudioComponent newComponent = null; if (ComponentRepository.Contains(component)) { if (component is PlaceholderStudioComponent placeholderComponent) { newComponent = ComponentRepository.GetPlaceholderClone(placeholderComponent); } else { newComponent = component; } _components.Add(newComponent); newComponent.InputRemoved += Component_EndpointRemoved; newComponent.OutputRemoved += Component_EndpointRemoved; ComponentAdded?.Invoke(this, newComponent); } return(newComponent); }
public T Add <T>() where T : Component { var component = gameObject.AddComponent <T>(); ComponentAdded.Invoke(this, typeof(T)); return(component); }
public void Handle(ComponentAdded <State> message) { if (!_states.Contains(message.Component)) { _states.Add(message.Component); } }
protected virtual void OnComponentAdded(GameComponentEventArgs <ComponentType> e) { IUpdateable asUpdateable; IDrawable asDrawable; if (m_IsInitialized) { InitializeComponent(e.GameComponent); } else { m_UninitializedComponents.Add(e.GameComponent); } asUpdateable = e.GameComponent as IUpdateable; if (asUpdateable != null) { insertSorted(asUpdateable); asUpdateable.UpdateOrderChanged += new EventHandler <EventArgs>(child_UpdateOrderChanged); } asDrawable = e.GameComponent as IDrawable; if (asDrawable != null) { insertSorted(asDrawable); asDrawable.DrawOrderChanged += new EventHandler <EventArgs>(child_DrawOrderChanged); } if (ComponentAdded != null) { ComponentAdded.Invoke(this, e); } }
public void Add(object component) { var type = component.GetType(); if (component is IKnowsEntity knowsEntity) { knowsEntity.Entity = this; } if (component is IKnowsParent knowsParent && HasComponent <HasParent>()) { knowsParent.Parent = Get <HasParent>().Parent; } components.Add(type, component); ComponentAdded?.Invoke(this, component); if (!(component is HasParent parent)) { return; } foreach (var c in Components) { if (c is IKnowsParent knows) { knows.Parent = parent.Parent; } } }
private void AddState(State state) { var screen = screens[state]; gameStates.Push(screen); ComponentAdded?.Invoke(this, screen); }
public void OnComponentAdded(ComponentAdded message) { var index = message.Component as IIndexable; if (index != null) { AddIndex(index, index.IndexId); } }
public void OnComponentAdded(ComponentAdded message) { var squad = message.Component as Squad; if (squad != null) { MakeIdentitySquad(squad); } }
protected internal virtual void OnComponentAdded(ComponentEventArgs args) { if (args.Binding.Equals(typeof(Transform))) { Requires.That <ArgumentException>(Transform == null); Transform = (Transform)args.Component; } ComponentAdded?.Invoke(this, args); }
public GO AddComponent <T>(ComponentAdded <T> callback = null) where T : Component { T component = current.AddComponent <T>(); if (callback != null) { callback(component); } return(this); }
public void OnComponentAdded(ComponentAdded message) { // Check if the component is of the right type. var attributes = message.Component as Attributes <TAttribute>; if (attributes != null) { attributes.RecomputeAttributes(); } }
private static void OnComponentAdded(ComponentEventArgs args) { if (args.Component.Active) { ICmpInitializable cInit = args.Component as ICmpInitializable; if (cInit != null) { cInit.OnInit(Component.InitContext.Activate); } } ComponentAdded?.Invoke(current, args); }
public void AddComponent(ComponentDescriptor componentDescriptor) { Type vmType = componentDescriptor.ComponentType; Type componentType = BehaviourViewModelFactory.GetBehaviourFromViewModelProxy(vmType); BehaviourComponent component = BehaviourFactory.CreateFromType(componentType, entityData); entityData.AddComponent(component); //Create a viewmodel proxy for our new component BehaviourViewModel vm = BehaviourViewModelFactory.GetViewModelProxy(component); ComponentAdded?.Invoke(vm); }
private void OnComponentAdded(MyEntityComponent obj) { var cast = obj as TComp; if (cast == null) { return; } if (_components.Add(cast)) { ComponentAdded?.Invoke(cast); } }
/// <summary> /// Adds a given component /// </summary> /// <param name="component"></param> /// <returns></returns> public Entity AddComponent(IComponent component) { if (_components.ContainsKey(component.GetType())) { throw new ArgumentException("Component already exists"); } _components.Add(component.GetType(), component); ComponentAdded?.Invoke(this, component); return(this); }
public IComponent AddComponent(Type type) { if (HasComponent(type)) { return(null); } IComponent component = EntityManager.SharedManager.CreateComponent(type); components.Add(component); EntityManager.SharedManager.RegisterComponent(this, component); ComponentAdded?.Invoke(this, component); return(component); }
/// <summary>Called by the manager when a new component was added.</summary> /// <param name="message"></param> public override void OnComponentAdded(ComponentAdded message) { base.OnComponentAdded(message); // Check if the component is of the right type. var shield = message.Component as Shield; if (shield != null) { var typedComponent = shield; // Keep components in order, to stay deterministic. var index = _shields.BinarySearch(typedComponent); Debug.Assert(index < 0); _shields.Insert(~index, typedComponent); } }
public void AddComponent <ComponentType>() where ComponentType : class, IComponent, new() { Type type = typeof(ComponentType); ComponentRequestEventArgs args; if (components.ContainsKey(type)) { throw new ArgumentException($"Component with type \"{type.Name}\" already exists.", nameof(ComponentType)); } args = new ComponentRequestEventArgs(typeof(ComponentType)); ComponentRequested?.Invoke(this, args); components.Add(type, args.Component); ComponentAdded?.Invoke(this, args); }
public virtual void OnComponentAdded(ComponentAdded message) { Debug.Assert(message.Component.Entity > 0, "component.Entity > 0"); Debug.Assert(message.Component.Id > 0, "component.Id > 0"); // Check if the component is of the right type. if (!(message.Component is TComponent)) { return; } // Keep components in order, to stay deterministic. var component = (TComponent)message.Component; var index = Components.BinarySearch(component); Debug.Assert(index < 0); Components.Insert(~index, component); }
public Result <IDisposable> AddComponent(Identity identity, RenderedComponent component) { if (renderedComponents.ContainsKey(identity)) { return(Result.FailWith <IDisposable>(State.Forbidden, $"TargetRenderer already contains component {identity.Id}.")); } renderedComponents.Add(identity, component); ComponentAdded?.Invoke(this, new RenderedComponentEventArgs(identity, component)); return(Result.Ok(Disposable.For(() => { if (renderedComponents.ContainsKey(identity)) { renderedComponents.Remove(identity); ComponentRemoved?.Invoke(this, new RenderedComponentEventArgs(identity, component)); } }))); }
public void Dispose() { if (ComponentAdded != null) { //Remove all Events associated to this control (That haven't been unsubscribed !) foreach (Delegate d in ComponentAdded.GetInvocationList()) { ComponentAdded -= (EventHandler <GameComponentCollectionEventArgs>)d; } } if (ComponentRemoved != null) { //Remove all Events associated to this control (That haven't been unsubscribed !) foreach (Delegate d in ComponentRemoved.GetInvocationList()) { ComponentRemoved -= (EventHandler <GameComponentCollectionEventArgs>)d; } } }
/// <summary> /// Add component to the screen and game. /// </summary> /// <param name="i_GameComponents">Game components.</param> public void AddComponents(params IGameComponent[] i_GameComponents) { foreach (GameComponent component in i_GameComponents) { m_gameComponentCollection.Add(component); component.Initialize(); if (component is DrawableGameComponent) { m_gameDrawableComponentCollection.Add(component); } if (!Game.Components.Contains(component)) { Game.Components.Add(component); } } if (ComponentAdded != null) { ComponentAdded.Invoke(i_GameComponents); } }
public override void OnComponentAdded(ComponentAdded message) { base.OnComponentAdded(message); var shield = message.Component as ShieldEnergyStatusEffect; if (shield == null) { return; } var body = Manager.GetComponent(shield.Entity, Body.TypeId) as Body; if (body == null) { return; } var attributes = Manager.GetComponent(shield.Entity, Attributes <AttributeType> .TypeId) as Attributes <AttributeType>; if (attributes == null) { return; } var existingFixture = (Fixture)body.Fixtures.First(); System.Diagnostics.Debug.Assert(existingFixture != null); var shieldRadius = UnitConversion.ToSimulationUnits(attributes.GetValue(AttributeType.ShieldRadius)); shield.Fixture = Manager.AttachCircle( body, shieldRadius, collisionCategory: existingFixture.CollisionCategory | Factions.Shields.ToCollisionGroup(), collisionMask: existingFixture.CollisionMask).Id; }
/// <inheritdoc /> public void AddComponent <T>(IEntity entity, T component, bool overwrite = false) where T : Component { if (entity == null || !entity.IsValid()) { throw new ArgumentException("Entity is not valid.", nameof(entity)); } if (component == null) { throw new ArgumentNullException(nameof(component)); } if (component.Owner != entity) { throw new InvalidOperationException("Component is not owned by entity."); } var uid = entity.Uid; // get interface aliases for mapping var reg = _componentFactory.GetRegistration(component); // Check that there are no overlapping references. foreach (var type in reg.References) { if (!TryGetComponent(uid, type, out var duplicate)) { continue; } if (!overwrite) { throw new InvalidOperationException( $"Component reference type {type} already occupied by {duplicate}"); } // these two components are required on all entities and cannot be overwritten. if (duplicate is ITransformComponent || duplicate is IMetaDataComponent) { throw new InvalidOperationException("Tried to overwrite a protected component."); } RemoveComponentImmediate((Component)duplicate); } // add the component to the grid foreach (var type in reg.References) { _entTraitDict[type].Add(uid, component); _entCompIndex.Add(uid, component); } // add the component to the netId grid if (component.NetID != null) { // the main comp grid keeps this in sync var netId = component.NetID.Value; _entNetIdDict[netId].Add(uid, component); // mark the component as dirty for networking component.Dirty(); ComponentAdded?.Invoke(this, new AddedComponentEventArgs(component)); } component.ExposeData(DefaultValueSerializer.Reader()); component.OnAdd(); if (!entity.Initialized && !entity.Initializing) { return; } component.Initialize(); DebugTools.Assert(component.Initialized, "Component is not initialized after calling Initialize(). " + "Did you forget to call base.Initialize() in an override?"); if (entity.Initialized) { component.Running = true; } }
/// <inheritdoc /> public void AddComponent(IEntity entity, Component component, bool overwrite = false) { if (entity == null || !entity.IsValid()) { throw new ArgumentException("Entity is not valid.", nameof(entity)); } if (component == null) { throw new ArgumentNullException(nameof(component)); } if (component.Owner != entity) { throw new InvalidOperationException("Component is not owned by entity."); } // get interface aliases for mapping var reg = _componentFactory.GetRegistration(component); // Check that there are no overlapping references. foreach (var type in reg.References) { if (!TryGetComponent(entity.Uid, type, out var duplicate)) { continue; } if (!overwrite) { throw new InvalidOperationException($"Component reference type {type} already occupied by {duplicate}"); } // these two components are required on all entities and cannot be overwritten. if (duplicate is ITransformComponent || duplicate is IMetaDataComponent) { throw new InvalidOperationException("Tried to overwrite a protected component."); } RemoveComponentImmediate((Component)duplicate); } // add the component to the grid foreach (var type in reg.References) { _dictComponents[type].Add(entity.Uid, component); } // add the component to the netId grid if (component.NetID != null) { // the main comp grid keeps this in sync if (!_netComponents.TryGetValue(entity.Uid, out var netDict)) { netDict = new Dictionary <uint, Component>(CompTypeCapacity); _netComponents.Add(entity.Uid, netDict); } netDict.Add(component.NetID.Value, component); // mark the component as dirty for networking component.Dirty(); ComponentAdded?.Invoke(this, new ComponentEventArgs(component)); } component.OnAdd(); if (entity.Initialized || entity.Initializing) { component.Initialize(); if (entity.Initialized) { component.Running = true; } } }
protected internal void OnComponentAdded(ComponentEventArgs args) { ComponentAdded?.Invoke(this, args); }
private void OnComponentAdded(ComponentCollectionEventArgs eventArgs) => ComponentAdded?.Invoke(this, eventArgs);
private void OnSceneOnComponentAdded(object sender, ComponentEventArgs args) { ComponentAdded?.Invoke(sender, args); }
private void ComponentAddedHandler(object sender, ComponentEventArgs e) { ComponentAdded?.Invoke(sender, e); }