Exemple #1
0
        public void UpdateSensors(BrainCategories category)
        {
            UpdateUserControlled();
            UpdateIsTurning();
            UpdateMouseButtonPresence();

            if (IsLeftMouseButtonPresent)
            {
                MouseEdit.DisableLeftDrag();
            }
            if (IsRightMouseButtonPresent)
            {
                MouseEdit.DisableRightOrbit();
            }

            for (int i = 0; i < reflexes.Count; i++)
            {
                Reflex reflex = reflexes[i] as Reflex;
                reflex.targetSet.Clear();
            }

            if (reflexes.Count > 0)
            {
                UpdateGameThingSensors(category);

                for (int indentationLevel = 0; indentationLevel <= maxReflexIndentation; ++indentationLevel)
                {
                    // After evaluating root-level reflexes, open up to all sensor categories.
                    if (indentationLevel > 0)
                    {
                        category = BrainCategories.NotSpecified;
                    }

                    for (int i = 0; i < reflexes.Count; i++)
                    {
                        Reflex reflex = reflexes[i];

                        // Only evaluate reflexes at the current level of indentation.
                        if (reflex.Indentation != indentationLevel)
                        {
                            continue;
                        }

                        // Only evaluate sub-reflexes of parents that evaluated true except
                        // in the case where the reflex actuator is movement based.
                        if (reflex.Parent != null && !reflex.Parent.targetSet.Action)
                        {
                            // Nested reflexes with once modifiers need to clear their "once" state.
                            reflex.ResetOnceModifiers();

                            // Nested reflexes with active mouse targets should still be acted on
                            // so create valid target and action sets for them.  Note that this
                            // only applies to reflexes with a movement actuator.  This is so you
                            // can have WHEN MouseLeft DO Move and the result will be that the
                            // bot continues to move toward the target (position or bot) even
                            // after the parent reflex goes false.
                            bool mouseTarget = reflex.MousePosition != null || reflex.MouseActor != null;
                            if (mouseTarget && reflex.IsMovement)
                            {
                                // Create a sensor target set
                                if (reflex.MouseActor != null)
                                {
                                    // We need to add the mouse actor to the targetSet.
                                    SensorTarget target = SensorTargetSpares.Alloc();
                                    target.Init(reflex.Task.GameActor, reflex.MouseActor);
                                    reflex.targetSet.Add(target);
                                }
                                else if (reflex.MousePosition != null)
                                {
                                    // We need to add the mouse position to the targetSet.
                                    SensorTarget target = SensorTargetSpares.Alloc();
                                    target.GameThing  = GameActor;
                                    target.Position   = reflex.MousePosition.Value;
                                    target.Direction  = target.Position - reflex.Task.GameActor.Movement.Position;
                                    target.Range      = target.Direction.Length();
                                    target.Direction /= target.Range;
                                    reflex.targetSet.Add(target);
                                }
                                reflex.targetSet.ActionMouseTarget = true;
                                reflex.CreateActionSet(GameActor, 0);
                            }

                            continue;
                        }

                        // Don't evaluate reflexes that don't match the given categories, if any were specified.
                        if ((category != BrainCategories.NotSpecified) && (reflex.Sensor == null || !reflex.Sensor.Categories.Get((int)category)))
                        {
                            continue;
                        }

                        reflex.Update(GameActor, i);
                    }
                }
            }
        }