Esempio n. 1
0
        public override bool OnUpdate(float inDeltaSeconds)
        {
            // Aquire target

            Actor   self         = GameObject as Actor;
            Vector2 selfPosition = self.Spatial.WorldPosition;
            Vector2 selfForward  = self.Spatial.WorldForward;

            DebugRenderer.Instance.Triangle(selfPosition, selfPosition + RotateVector(selfForward, -perceptionFov) * perceptionDistance, selfPosition + RotateVector(selfForward, perceptionFov) * perceptionDistance, DebugRenderer.Alpha(System.Drawing.Color.Blue, 128), true);

            IList <Actor> actors = LevelManager.Instance.FindGameObjects <Actor>();

            Actor foundTarget = null;

            float closestDistance = float.MaxValue;

            foreach (Actor actor in actors)
            {
                if (actor == self)
                {
                    // Skip self
                    continue;
                }

                Vector2 toOther = actor.Spatial.WorldPosition - selfPosition;
                float   dot     = (float)toOther.Normalize().Dot(selfForward);
                if (dot >= Math.Cos(perceptionFov))
                {
                    // Within field-of-vision
                    float distance = (float)toOther.Length();
                    if (distance <= perceptionDistance)
                    {
                        // Within the perception range
                        if (distance < closestDistance)
                        {
                            closestDistance = distance;
                            foundTarget     = actor;
                        }
                    }
                }
            }

            if (self.Blackboard.Target != foundTarget)
            {
                // Target changed
                new DebugObject(DebugObject, "Event", ReViewFeedObject.EDebugType.GenericItem).Log("New target '" + (foundTarget != null ? foundTarget.Name : "<none>") + "' aquired!").End();

                self.Blackboard.Target = foundTarget;
            }

            return(true);
        }