Esempio n. 1
0
        /// <inheritdoc/>
        /// <remarks>
        /// The behaviors are stored in the specified <see cref="EntityBehaviors"/> of the <see cref="SimulationWrapper"/>.
        /// This can be a local collection, allowing you to keep a part of the behaviors separate.
        /// </remarks>
        public void Run(IEntityCollection entities)
        {
            void BehaviorsNotFound(object sender, BehaviorsNotFoundEventArgs args)
            {
                if (entities.TryGetEntity(args.Name, out var entity))
                {
                    entity.CreateBehaviors(this);
                    if (EntityBehaviors.TryGetBehaviors(entity.Name, out var container))
                    {
                        args.Behaviors = container;
                    }
                }
                else
                {
                    args.Behaviors = Parent.EntityBehaviors[args.Name];
                }
            }

            EntityBehaviors.BehaviorsNotFound += BehaviorsNotFound;

            foreach (var entity in entities)
            {
                if (!EntityBehaviors.Contains(entity.Name))
                {
                    entity.CreateBehaviors(this);
                }
            }

            EntityBehaviors.BehaviorsNotFound -= BehaviorsNotFound;
        }
Esempio n. 2
0
        /// <summary>
        /// Creates all behaviors for the simulation.
        /// </summary>
        /// <param name="entities">The entities.</param>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="entities"/> is <c>null</c>.</exception>
        protected virtual void CreateBehaviors(IEntityCollection entities)
        {
            entities.ThrowIfNull(nameof(entities));
            EntityBehaviors = new BehaviorContainerCollection(entities.Comparer);

            // Automatically create the behaviors of entities that need priority
            void BehaviorsNotFound(object sender, BehaviorsNotFoundEventArgs args)
            {
                if (entities.TryGetEntity(args.Name, out var entity))
                {
                    entity.CreateBehaviors(this);
                    if (EntityBehaviors.TryGetBehaviors(entity.Name, out var container))
                    {
                        args.Behaviors = container;
                    }
                }
            }

            EntityBehaviors.BehaviorsNotFound += BehaviorsNotFound;

            // Create the behaviors
            Statistics.BehaviorCreationTime.Start();
            try
            {
                foreach (var entity in entities)
                {
                    if (!EntityBehaviors.Contains(entity.Name))
                    {
                        entity.CreateBehaviors(this);
                    }
                }
            }
            finally
            {
                Statistics.BehaviorCreationTime.Stop();
            }

            EntityBehaviors.BehaviorsNotFound -= BehaviorsNotFound;
        }
Esempio n. 3
0
 /// <inheritdoc/>
 public bool TryGetEntity(string name, out IEntity entity) => _entities.TryGetEntity(name, out entity);