/// <summary> /// Attaches the specified component to an entity. /// /// If a component of the same type is already attached to the entity, then nothing happens /// and the method returns false. /// </summary> public bool Add(IEntityRecord entity, IComponent component) { bool componentSuccessfullyAttached = false; bool entityWasAlreadyRegistered = true; lock (_keyhole) { if (component != null) { IEntityRecord previousRecord = component.Record; if (previousRecord != null) { if (!previousRecord.Equals(entity)) { Remove(previousRecord, component); } } } IDictionary <Type, IComponent> components = GetComponentsForRecord(entity); if (components == null) { components = new Dictionary <Type, IComponent>(1); entityWasAlreadyRegistered = false; _records[entity] = components; } if (component != null) { Type key = component.GetType(); if (!entityWasAlreadyRegistered || !components.ContainsKey(key)) { components.Add(key, component); if (component.Record == null || !component.Record.Equals(entity)) { component.Record = entity; } PrepareComponentForSynchronization(component); componentSuccessfullyAttached = true; } } } if (!entityWasAlreadyRegistered) { OnEntered(entity); } return(componentSuccessfullyAttached); }