public void Recycle(IEcsComponent item) { if (item != null) { _pool.Push(item); } }
void OnComponentDetach(int entity, IEcsComponent component) { if (component is GameObjectComponent) { GameObject.Destroy((component as GameObjectComponent).Data); } }
public void AddComponent(int index, IEcsComponent comp) { if (index >= _comps.Length) { Array.Resize(ref _comps, 2 * _comps.Length); } _comps[index] = (TC)comp; }
public void DestroyComponentInstance(Type componentType, IEcsComponent instance) { IEcsComponentManager existing; if (!ComponentManagers.TryGetValue(componentType, out existing)) { return; } existing.UnRegisterComponent(instance); }
// TODO: Make a FilterCollection or something that does all this so systems can have multiple filters running. private void OnComponentAdded(int entity, IEcsComponent component) { if (!entities.Contains(entity) && world.MatchesComponentFilter <TComponentFilter>(entity)) { TComponentFilter filter = world.CreateComponentFilter <TComponentFilter>(entity); filters.Add(filter); entities.Add(entity); Console.WriteLine(GetType().Name + " will process " + entity); } }
private void OnComponentRemoved(int entity, IEcsComponent component) { int index = entities.IndexOf(entity); if (index >= 0 && !world.MatchesComponentFilter <TComponentFilter>(entity)) { filters.RemoveAt(index); entities.RemoveAt(index); Console.WriteLine(GetType().Name + " will no longer process " + entity + " because it does not have the required components."); } }
public bool TryGetComponent(int entityId, int componentId, out IEcsComponent component) { IEcsComponentManager manager; if (ComponentManagersById.TryGetValue(componentId, out manager)) { component = manager.ForEntity(entityId); return(true); } component = null; return(false); }
public void RegisterComponentInstance(Type componentType, IEcsComponent instance) { IEcsComponentManager existing; if (!ComponentManagers.TryGetValue(componentType, out existing)) { var type = typeof(EcsComponentManagerOf <>).MakeGenericType(componentType); existing = Activator.CreateInstance(type) as EcsComponentManager; ComponentManagers.Add(componentType, existing); } existing.RegisterComponent(instance); }
public void DestroyComponentInstance(Type componentType, IEcsComponent instance) { IEcsComponentManager existing; if (!ComponentManagers.TryGetValue(componentType, out existing)) { return; } if (_componentRemovedSubject != null) { _componentRemovedSubject.OnNext(instance); } existing.UnRegisterComponent(instance); }
protected override void AddItem(IEcsComponent component) { if (_components.ContainsKey(component.EntityId)) { return; } else { _components.Add(component.EntityId, (TComponentType)component); _componentList.Add((TComponentType)component); } if (_CreatedObservable != null) { _CreatedObservable.OnNext((TComponentType)component); } }
internal void AddComponent(byte index, IEcsComponent component) { EcsArchetype newArchetype = _archetypeManager.FindOrCreateNextArchetype(_archetype, index); foreach (byte curIndex in _archetype.Indices) { IEcsComponentPool componentPool = _archetype.GetComponentPool(curIndex); newArchetype.AddComponent(curIndex, componentPool.Get(ArchetypeIndex)); } newArchetype.AddComponent(index, component); _archetype.RemoveEntity(this); _archetype = newArchetype; _archetype.AddEntity(this); }
protected override void RemoveItem(IEcsComponent component) { if (component == null) { return; } if (_components == null || !_components.ContainsKey(component.EntityId)) { return; } _components.Remove(component.EntityId); _componentList.Remove((TComponentType)component); if (_RemovedObservable != null) { _RemovedObservable.OnNext((TComponentType)component); } }
/// <summary> /// This method should be used to add any entity to the ecs component system /// /// > You can use this if you want to register components that aren't derived from EcsComponent which requires MonoBehaviour, but you won't be able to see it in the unity inspector. /// </summary> /// <param name="componentType">The type of component to register.</param> /// <param name="instance">The actual instance that is being registered</param> /// <code> /// System.ComponentSystem.RegisterComponent(typeof(CustomComponent), new CustomComponent()); /// </code> public void RegisterComponentInstance(Type componentType, IEcsComponent instance) { IEcsComponentManager existing; if (!ComponentManagers.TryGetValue(componentType, out existing)) { var type = typeof(EcsComponentManagerOf <>).MakeGenericType(componentType); existing = Activator.CreateInstance(type) as EcsComponentManager; ComponentManagers.Add(componentType, existing); if (instance.ComponentId > 0) { ComponentManagersById.Add(instance.ComponentId, existing); } } if (_componentCreatedSubject != null) { _componentCreatedSubject.OnNext(instance); } existing.RegisterComponent(instance); }
public int GetIndex() { int id; if (_reservedItemsCount > 0) { _reservedItemsCount--; id = _reservedItems[_reservedItemsCount]; _reservedItems.RemoveAt(_reservedItemsCount); } else { id = _itemsCount; if (_itemsCount == Items.Length) { var newItems = new IEcsComponent[_itemsCount << 1]; Array.Copy(Items, newItems, _itemsCount); Items = newItems; } Items[_itemsCount++] = Activator.CreateInstance(_type) as IEcsComponent; } return(id); }
protected abstract void RemoveItem(IEcsComponent component);
public static T As <T>(this IEcsComponent component) where T : class, IEcsComponent { IEcsComponentManager manager = EcsComponentService.Instance.RegisterComponent(typeof(T)); return((T)manager.ForEntity(component.EntityId)); }
public void RegisterComponent(IEcsComponent item) { AddItem(item); }
public void UnRegisterComponent(IEcsComponent item) { RemoveItem(item); }
protected abstract void AddItem(IEcsComponent component);
public void AddComponent(byte index, IEcsComponent component) { _compPools[index].AddComponent(_length, component); }
public bool TryGetComponent(int entity, Type componentType, out IEcsComponent component) { return(components[componentType].TryGetValue(entity, out component)); }
protected override void AddItem(IEcsComponent component) { base.AddItem(component); }
private void OnNext(IEcsComponent _) { AddItem(_); }