public void AddComponent <T>(Entity e, Component component) where T : Component { ComponentType type = ComponentTypeManager.GetTypeFor <T>(); if (type.GetId() >= componentsByType.GetCapacity()) { componentsByType.Set(type.GetId(), null); } Bag <Component> components = componentsByType.Get(type.GetId()); if (components == null) { components = new Bag <Component>(); componentsByType.Set(type.GetId(), components); } components.Set(e.GetId(), component); e.AddTypeBit(type.GetBit()); if (AddedComponentEvent != null) { AddedComponentEvent(e, component); } }
/// <summary> /// Add a component to the given entity /// If the component's type does not already exist, add it to the bag of availalbe component types /// </summary> /// <typeparam name="T">Component type you want to add</typeparam> /// <param name="e">The entity to which you want to add the component</param> /// <param name="component">The component instance you want to add</param> internal void AddComponent <T>(Entity e, Component component) where T : Component { System.Diagnostics.Debug.Assert(component != null); System.Diagnostics.Debug.Assert(e != null); ComponentType type = ComponentTypeManager.GetTypeFor <T>(); if (type.Id >= componentsByType.Capacity) { componentsByType.Set(type.Id, null); } Bag <Component> components = componentsByType.Get(type.Id); if (components == null) { components = new Bag <Component>(); componentsByType.Set(type.Id, components); } components.Set(e.Id, component); e.AddTypeBit(type.Bit); if (AddedComponentEvent != null) { AddedComponentEvent(e, component); } }