예제 #1
0
        /// <summary>
        /// Handles behavior attach events
        /// </summary>
        protected override void OnProcessAttachBehavior(AttachBehaviorEventArgs e)
        {
            var behaviorName = e.BehaviorName;

            if (!String.IsNullOrEmpty(behaviorName))
            {
                var behavior = default(SciterBehavior);
                if (!(HasBehaviors && Behaviors.TryGetValue(behaviorName, out behavior)))
                {
                    base.OnProcessAttachBehavior(e);
                    Behaviors[behaviorName] = e.Behavior;
                }
                else
                {
                    e.Behavior = e.Behavior ?? behavior;
                }
            }
        }
예제 #2
0
        public static void ResolveBehavior(Entity entity)
        {
            BehaviorDef b;

            if (Behaviors.TryGetValue(entity.ObjectType, out b))
            {
                entity.MovementBehavior  = b.Item1;
                entity.AttackBehavior    = b.Item2;
                entity.ReproduceBehavior = b.Item3;
                entity.CondBehaviors     = b.Item4;
            }
            else
            {
                entity.MovementBehavior          =
                    entity.AttackBehavior        =
                        entity.ReproduceBehavior = NullBehavior.Instance;
                entity.CondBehaviors             = Empty <ConditionalBehavior> .Array;
            }
        }
예제 #3
0
        /// <summary>
        /// Create a behavior of a specific base type for a simulation.
        /// </summary>
        /// <typeparam name="T">The behavior base type.</typeparam>
        /// <param name="simulation">The simulation that will use the behavior.</param>
        /// <returns>A behavior of the requested type, or null if it doesn't apply to this entity.</returns>
        /// <exception cref="ArgumentNullException">simulation</exception>
        public virtual T CreateBehavior <T>(Simulation simulation) where T : Behavior
        {
            if (simulation == null)
            {
                throw new ArgumentNullException(nameof(simulation));
            }

            // Try to extract the right behavior from our behavior factories
            if (Behaviors.TryGetValue(typeof(T), out var factory))
            {
                // Create the behavior
                var behavior = factory();

                // Setup the behavior
                var provider = BuildSetupDataProvider(simulation.EntityParameters, simulation.EntityBehaviors);
                behavior.Setup(simulation, provider);
                return((T)behavior);
            }

            // None found
            return(null);
        }