// This shouldn't be public, since it doesn't do proper add/remove from systems. private Component GetOrAllocateComponentAndUpdateBitField(Entity e, int componentTypeId, out bool allocatedNew) { //int liveIndex = liveIdRoster[e.LiveId]; int liveIndex = e.LiveId; Debug.Assert(e == entityPool[liveIndex], "Entity has wrong id"); ComponentManagerBase componentManager = componentManagers[componentTypeId]; #if ALTERNATE_LAYOUT int index = liveIndex + componentTypeId * MaxEntities; #else int index = liveIndex * EntityManager.MaxComponentTypes + componentTypeId; #endif int componentTypeLookupId = componentLookupIdsPerEntity[index]; if (componentTypeLookupId == -1) { // Allocate a new component for this entity short componentLookupId; Component component = componentManager.AllocateComponentForEntity(e, out componentLookupId); componentLookupIdsPerEntity[index] = componentLookupId; allocatedNew = true; // Make sure the entity is tagged as having this component e.ComponentTypesBitField |= ComponentTypeIdHelper.GetBit(componentTypeId); return(component); } else { // Retrieve allocatedNew = false; return(componentManager.GetComponentAt(componentTypeLookupId)); } }
public EntityManager(Universe universe) { this.universe = universe; //Scripts = new ScriptIdManager(this); liveIdRoster = new short[MaxEntities]; inverseRoster = new short[MaxEntities]; entityPool = new Entity[MaxEntities]; componentLookupIdsPerEntity = new short[MaxEntities * EntityManager.MaxComponentTypes]; for (short i = 0; i < (short)liveIdRoster.Length; i++) { liveIdRoster[i] = i; // To start off with inverseRoster[i] = i; entityPool[i] = new Entity(); } partitionIndex = 0; componentManagers = new ComponentManagerBase[EntityManager.MaxComponentTypes]; // REVIEW: Where should this go? /* * componentManagers[ComponentTypeIds.ColorBlind] = new ComponentManager<ColorBlind>(ComponentTypeIds.ColorBlind, profile.GetDefaultPoolSize(ComponentTypeIds.ColorBlind, 10)); * componentManagers[ComponentTypeIds.Placement] = new ComponentManager<Placement>(ComponentTypeIds.Placement, profile.GetDefaultPoolSize(ComponentTypeIds.Placement, 10)); * componentManagers[ComponentTypeIds.Inventory] = new ComponentManager<Inventory>(ComponentTypeIds.Inventory, profile.GetDefaultPoolSize(ComponentTypeIds.Inventory, 10)); // we don't have these in Tangled. * componentManagers[ComponentTypeIds.Aspect] = new ComponentManager<Aspect>(ComponentTypeIds.Aspect, profile.GetDefaultPoolSize(ComponentTypeIds.Aspect, 10)); * componentManagers[ComponentTypeIds.Selection] = new ComponentManager<Selection>(ComponentTypeIds.Selection, profile.GetDefaultPoolSize(ComponentTypeIds.Selection, 10)); * componentManagers[ComponentTypeIds.Button] = new ComponentManager<Button>(ComponentTypeIds.Button, profile.GetDefaultPoolSize(ComponentTypeIds.Button, 10)); * //componentManagers[ComponentTypeIds.PointLightSource] = new ComponentManager<PointLightSource>(ComponentTypeIds.PointLightSource, 100); * componentManagers[ComponentTypeIds.TransformChildren] = new ComponentManager<TransformChildren>(ComponentTypeIds.TransformChildren, profile.GetDefaultPoolSize(ComponentTypeIds.TransformChildren, 10)); * //componentManagers[ComponentTypeIds.AttachmentPoint] = new ComponentManager<AttachmentPoint>(ComponentTypeIds.AttachmentPoint, 50); * //componentManagers[ComponentTypeIds.Input] = new ComponentManager<Input>(ComponentTypeIds.Input, 10); * //componentManagers[ComponentTypeIds.ViewPoint] = new ComponentManager<ViewPoint>(ComponentTypeIds.ViewPoint, 10); * componentManagers[ComponentTypeIds.Constrained] = new ComponentManager<Constrained>(ComponentTypeIds.Constrained, profile.GetDefaultPoolSize(ComponentTypeIds.Constrained, 10)); * //componentManagers[ComponentTypeIds.Common] = new ComponentManager<Common>(ComponentTypeIds.Common, 1000); * componentManagers[ComponentTypeIds.Animation] = new ComponentManager<Animation>(ComponentTypeIds.Animation, profile.GetDefaultPoolSize(ComponentTypeIds.Animation, 10)); * componentManagers[ComponentTypeIds.Scripts] = new ComponentManager<ScriptComponent>(ComponentTypeIds.Scripts, profile.GetDefaultPoolSize(ComponentTypeIds.Scripts, 10)); * //scriptsComponents = (IComponentMapper<ScriptComponent>)componentManagers[ComponentTypeIds.Scripts]; // Hold onto this * componentManagers[ComponentTypeIds.LaserInteractive] = new ComponentManager<LaserInteractive>(ComponentTypeIds.LaserInteractive, profile.GetDefaultPoolSize(ComponentTypeIds.LaserInteractive, 10)); * componentManagers[ComponentTypeIds.SoundLoop] = new ComponentManager<SoundLoop>(ComponentTypeIds.SoundLoop, profile.GetDefaultPoolSize(ComponentTypeIds.SoundLoop, 10)); * componentManagers[ComponentTypeIds.Text] = new ComponentManager<TextComponent>(ComponentTypeIds.Text, profile.GetDefaultPoolSize(ComponentTypeIds.Text, 10)); * componentManagers[ComponentTypeIds.PropertyBag] = new ComponentManager<PropertyBag>(ComponentTypeIds.PropertyBag, profile.GetDefaultPoolSize(ComponentTypeIds.PropertyBag, 10)); // Because of leaves. * componentManagers[ComponentTypeIds.Entanglement] = new ComponentManager<Entanglement>(ComponentTypeIds.Entanglement, profile.GetDefaultPoolSize(ComponentTypeIds.Entanglement, 10)); * componentManagers[ComponentTypeIds.TitleLocation] = new ComponentManager<TitleLocation>(ComponentTypeIds.TitleLocation, profile.GetDefaultPoolSize(ComponentTypeIds.TitleLocation, 10)); * componentManagers[ComponentTypeIds.MouseInput] = new ComponentManager<MouseInput>(ComponentTypeIds.MouseInput, profile.GetDefaultPoolSize(ComponentTypeIds.MouseInput, 10)); * componentManagers[ComponentTypeIds.FrameAnimation] = new ComponentManager<FrameAnimation>(ComponentTypeIds.FrameAnimation, profile.GetDefaultPoolSize(ComponentTypeIds.FrameAnimation, 10)); * componentManagers[ComponentTypeIds.ChildTransform] = new ComponentManager<ChildTransform>(ComponentTypeIds.ChildTransform, profile.GetDefaultPoolSize(ComponentTypeIds.ChildTransform, 10)); */ /* * Placement p1 = new Placement(); * Placement p2 = new Placement(); * TypedReference td = __makeref(p1); * IntPtr ptr1 = **(IntPtr**)(&td); * td = __makeref(p2); * IntPtr ptr2 = **(IntPtr**)(&td); */ uniqueIdToEntity = new Dictionary <int, Entity>(MaxEntities); }
public Component GetComponent(int entityLiveId, int componentTypeId) { ComponentManagerBase componentManager = componentManagers[componentTypeId]; #if ALTERNATE_LAYOUT int componentTypeLookupId = componentLookupIdsPerEntity[entityLiveId + componentTypeId * MaxEntities]; #else int componentTypeLookupId = componentLookupIdsPerEntity[liveIndex * EntityManager.MaxComponentTypes + componentTypeId]; #endif return((componentTypeLookupId != -1) ? componentManager.GetComponentAt(componentTypeLookupId) : null); }
// Returns null if no such component. public Component GetComponent(Entity e, int componentTypeId) { int liveIndex = e.LiveId; Debug.Assert(e == entityPool[liveIndex], "Entity has wrong id"); ComponentManagerBase componentManager = componentManagers[componentTypeId]; #if ALTERNATE_LAYOUT int componentTypeLookupId = componentLookupIdsPerEntity[liveIndex + componentTypeId * MaxEntities]; #else int componentTypeLookupId = componentLookupIdsPerEntity[liveIndex * EntityManager.MaxComponentTypes + componentTypeId]; #endif return((componentTypeLookupId != -1) ? componentManager.GetComponentAt(componentTypeLookupId) : null); }