示例#1
0
 /// <summary>
 /// Creates new instance of ecs world container.
 /// </summary>
 /// <param name="useAsActive">Should this instance be set as Active or not.</param>
 public EcsWorld(bool useAsActive = true)
 {
     if (useAsActive)
     {
         Active = this;
     }
 }
示例#2
0
        /// <summary>
        /// Creates new instance of EcsSystems group.
        /// </summary>
        /// <param name="world">EcsWorld instance.</param>
        /// <param name="name">Custom name for this group.</param>
        public EcsSystems(EcsWorld world, string name = null)
        {
            World = world;
            Name  = name;

            _systemsResolver = new EcsSystemsResolver();
            _systemsResolver.Init();
        }
示例#3
0
        /// <summary>
        /// Creates entity with single component at specified EcsWorld.
        /// </summary>
        /// <param name="world">World instance.</param>
        public static Inc1 Create(EcsWorld world)
        {
            world.GetFilter <EcsFilterSingle <Inc1> > ();
            var data = world.CreateEntityWith <Inc1> ();

            world.ProcessDelayedUpdates();
            return(data);
        }
示例#4
0
文件: EcsSystem.cs 项目: alanyuen/ecs
        /// <summary>
        /// Creates new instance of EcsSystems group.
        /// </summary>
        /// <param name="world">EcsWorld instance.</param>
        /// <param name="name">Custom name for this group.</param>
        public EcsSystems(EcsWorld world, string name = null)
        {
            World = world;
            Name  = name;

            if (name != null)
            {
                World.AddNamedSystems(this, name);
            }
        }
示例#5
0
        public EcsSystems(EcsWorld world)
        {
#if DEBUG
            if (world == null)
            {
                throw new ArgumentNullException();
            }
#endif
            _world = world;
        }
示例#6
0
文件: EcsSystems.cs 项目: sizzles/ecs
        /// <summary>
        /// Creates new instance of EcsSystems group.
        /// </summary>
        /// <param name="world">EcsWorld instance.</param>
        /// <param name="name">Custom name for this group.</param>
        public EcsSystems(EcsWorld world, string name = null)
        {
#if DEBUG
            if (world == null)
            {
                throw new ArgumentNullException("world");
            }
#endif
            _world = world;
            Name   = name;
        }
        public void Setup()
        {
            _defaultWorld                      = new DefaultWorld(EntityCount);
            _defaultEntitySet                  = _defaultWorld.GetEntities().With <DefaultComponent>().AsSet();
            _defaultRunner                     = new DefaultParallelRunner(Environment.ProcessorCount);
            _defaultSystem                     = new DefaultEcsSystem(_defaultWorld);
            _defaultMultiSystem                = new DefaultEcsSystem(_defaultWorld, _defaultRunner);
            _defaultEntityComponentSystem      = new DefaultEcsEntityComponentSystem(_defaultWorld);
            _defaultMultiEntityComponentSystem = new DefaultEcsEntityComponentSystem(_defaultWorld, _defaultRunner);
            _defaultComponentSystem            = new DefaultEcsComponentSystem(_defaultWorld);
            _defaultComponentMultiSystem       = new DefaultEcsComponentSystem(_defaultWorld, _defaultRunner);
            _defaultGeneratorSystem            = new DefaultEcsGeneratorSystem(_defaultWorld);
            _defaultGeneratorMultiSystem       = new DefaultEcsGeneratorSystem(_defaultWorld, _defaultRunner);

            _entitasWorld       = new Context <EntitasEntity>(1, () => new EntitasEntity());
            _entitasSystem      = new EntitasSystem(_entitasWorld);
            _entitasMultiSystem = new EntitasSystem(_entitasWorld, Environment.ProcessorCount);

            _monoWorld = new WorldBuilder().AddSystem(new MonoSystem()).Build();
            _time      = new GameTime();

            _leoWorld   = new LeoWorld();
            _leoSystem  = new LeoSystem();
            _leoSystems = new LeoSystems(_leoWorld).Add(_leoSystem);
            _leoSystems.ProcessInjects().Init();

            SimpleEntitiesSubmissionScheduler sveltoScheduler = new SimpleEntitiesSubmissionScheduler();

            _sveltoWorld  = new EnginesRoot(sveltoScheduler);
            _sveltoSystem = new SveltoSystem();
            _sveltoWorld.AddEngine(_sveltoSystem);
            IEntityFactory sveltoFactory = _sveltoWorld.GenerateEntityFactory();

            for (int i = 0; i < EntityCount; ++i)
            {
                DefaultEntity defaultEntity = _defaultWorld.CreateEntity();
                defaultEntity.Set <DefaultComponent>();

                EntitasEntity entitasEntity = _entitasWorld.CreateEntity();
                entitasEntity.AddComponent(0, new EntitasComponent());

                MonoEntity monoEntity = _monoWorld.CreateEntity();
                monoEntity.Attach(new MonoComponent());

                LeoEntity leoEntity = _leoWorld.NewEntity();
                leoEntity.Get <LeoComponent>();

                sveltoFactory.BuildEntity <SveltoEntity>((uint)i, _sveltoGroup);
            }

            sveltoScheduler.SubmitEntities();
        }
示例#8
0
        public virtual void Initialize()
        {
            _world          = GetWorld();
            _filter         = GetFilter();
            _worker         = GetWorker();
            _minJobSize     = GetMinJobSize();
            _threadsCount   = GetThreadsCount();
            _forceSyncState = GetForceSyncState();
#if DEBUG
            if (_world == null)
            {
                throw new Exception("Invalid EcsWorld");
            }
            if (_filter == null)
            {
                throw new Exception("Invalid EcsFilter");
            }
            if (_minJobSize < 1)
            {
                throw new Exception("Invalid JobSize");
            }
            if (_threadsCount < 1)
            {
                throw new Exception("Invalid ThreadsCount");
            }
#endif
#if !UNITY_WEBGL
            _descs = new WorkerDesc[_threadsCount];
            _syncs = new ManualResetEvent[_threadsCount];
            EcsMultiThreadJob job;
            for (var i = 0; i < _descs.Length; i++)
            {
                job       = new EcsMultiThreadJob();
                job.World = _world;
                var desc = new WorkerDesc();
                desc.Job    = job;
                desc.Thread = new Thread(ThreadProc);
                desc.Thread.IsBackground = true;
#if DEBUG
                desc.Thread.Name = string.Format("ECS-{0:X}-{1}", this.GetHashCode(), i);
#endif
                desc.HasWork  = new ManualResetEvent(false);
                desc.WorkDone = new ManualResetEvent(true);
                desc.Worker   = _worker;
                _descs[i]     = desc;
                _syncs[i]     = desc.WorkDone;
                desc.Thread.Start(desc);
            }
#endif
            _localJob       = new EcsMultiThreadJob();
            _localJob.World = _world;
        }
示例#9
0
        /// <summary>
        /// Injects EcsWorld / EcsFilter fields to IEcsSystem.
        /// </summary>
        /// <param name="system">System to scan for injection.</param>
        /// <param name="world">EcsWorld instance to inject.</param>
        public static void Inject(IEcsSystem system, EcsWorld world, System.Collections.Generic.Dictionary <Type, object> injections)
        {
            var systemType = system.GetType();

            if (!Attribute.IsDefined(systemType, typeof(EcsInjectAttribute)))
            {
                return;
            }
            var worldType  = world.GetType();
            var filterType = typeof(EcsFilter);
            var ignoreType = typeof(EcsIgnoreInjectAttribute);

            foreach (var f in systemType.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance))
            {
                // skip fields with [EcsIgnoreInject] attribute.
                if (Attribute.IsDefined(f, ignoreType))
                {
                    continue;
                }
                // EcsWorld
                if (f.FieldType.IsAssignableFrom(worldType) && !f.IsStatic)
                {
                    f.SetValue(system, world);
                    continue;
                }
                // EcsFilter
#if DEBUG
                if (f.FieldType == filterType)
                {
                    throw new Exception(string.Format("Cant use EcsFilter type at \"{0}\" system for dependency injection, use generic version instead", system));
                }
#endif
                if (f.FieldType.IsSubclassOf(filterType) && !f.IsStatic)
                {
                    f.SetValue(system, world.GetFilter(f.FieldType));
                    continue;
                }
                // Other injections.
                foreach (var pair in injections)
                {
                    if (f.FieldType.IsAssignableFrom(pair.Key) && !f.IsStatic)
                    {
                        f.SetValue(system, pair.Value);
                        break;
                    }
                }
            }
        }
示例#10
0
        public virtual void Destroy()
        {
#if !UNITY_WEBGL
            for (var i = 0; i < _descs.Length; i++)
            {
                var desc = _descs[i];
                _descs[i] = null;
                desc.Thread.Interrupt();
                desc.Thread.Join(10);
                _syncs[i].Close();
                _syncs[i] = null;
            }
#endif
            _world  = null;
            _filter = null;
            _worker = null;
        }
示例#11
0
        /// <summary>
        /// Destroys all registered external data, full cleanup for internal data.
        /// </summary>
        public void Dispose()
        {
#if DEBUG
            if (_isDisposed)
            {
                throw new Exception("World already disposed");
            }
            _isDisposed = true;
            for (var i = _debugListeners.Count - 1; i >= 0; i--)
            {
                _debugListeners[i].OnWorldDestroyed(this);
            }
#endif
#if LEOECS_ENABLE_WORLD_EVENTS
            for (var i = _eventListeners.Count - 1; i >= 0; i--)
            {
                _eventListeners[i].OnWorldDestroyed(this);
            }
#endif
            if (this == Active)
            {
                Active = null;
            }
            for (var i = 0; i < _entitiesCount; i++)
            {
                // already reserved entities cant contains components.
                if (_entities[i].ComponentsCount > 0)
                {
                    var entity = _entities[i];
                    for (int ii = 0, iiMax = entity.ComponentsCount; ii < iiMax; ii += 2)
                    {
                        EcsHelpers.ComponentPools[entity.Components[ii]].RecycleById(entity.Components[ii + 1]);
                    }
                }
            }
            // any next usage of this EcsWorld instance will throw exception.
            _entities      = null;
            _entitiesCount = 0;
            _filters       = null;
            _filtersCount  = 0;
            _oneFrameFilters.Clear();
            _reservedEntities      = null;
            _reservedEntitiesCount = 0;
            _delayedUpdates        = null;
            _delayedUpdatesCount   = 0;
        }
示例#12
0
        /// <summary>
        /// Destroys all registered external data, full cleanup for internal data.
        /// </summary>
        public void Dispose()
        {
#if DEBUG
            if (_isDisposed)
            {
                throw new Exception("World already disposed");
            }
            _isDisposed = true;

            for (var i = _debugListeners.Count - 1; i >= 0; i--)
            {
                _debugListeners[i].OnWorldDestroyed(this);
            }
#endif
            if (this == Active)
            {
                Active = null;
            }
            for (var i = 0; i < _entitiesCount; i++)
            {
                // already reserved entities cant contains components.
                if (_entities[i].ComponentsCount > 0)
                {
                    var entity = _entities[i];
                    for (var ii = 0; ii < entity.ComponentsCount; ii++)
                    {
                        entity.Components[ii].Pool.RecycleById(entity.Components[ii].ItemId);
                    }
                }
            }
            // any next usage of this EcsWorld instance will throw exception.
            _entities              = null;
            _entitiesCount         = 0;
            _filters               = null;
            _filtersCount          = 0;
            _reservedEntities      = null;
            _reservedEntitiesCount = 0;
            _delayedUpdates        = null;
            _delayedUpdatesCount   = 0;
        }
示例#13
0
 public static C1 AddComponent <C1> (this EcsWorld world, in EcsEntity entity) where C1 : class
示例#14
0
 public static void RemoveOneFrameComponents(this EcsWorld world)
 {
     world.EndFrame();
 }
示例#15
0
 public static EcsEntity CreateEntityWith <C1, C2, C3> (this EcsWorld world, out C1 c1, out C2 c2, out C3 c3) where C1 : class where C2 : class where C3 : class
 {
     return(world.NewEntityWith(out c1, out c2, out c3));
 }
示例#16
0
 public static EcsEntity CreateEntityWith <C1> (this EcsWorld world, out C1 c1) where C1 : class
 {
     return(world.NewEntityWith(out c1));
 }
示例#17
0
 public EcsWorld()
 {
     Active = this;
 }
示例#18
0
 /// <summary>
 /// Creates new instance of EcsSystems group.
 /// </summary>
 /// <param name="world">EcsWorld instance.</param>
 /// <param name="name">Custom name for this group.</param>
 public EcsSystems(EcsWorld world, string name = null)
 {
     World = world;
     Name  = name;
 }
示例#19
0
 public static EcsEntity CreateEntity(this EcsWorld world)
 {
     return(world.NewEntity());
 }