public void InitializeAI()
 {
     gNPCController        = GetComponent <NPCController>();
     CurrentPathfinder     = Pathfinders[SelectedPathfinder];
     g_NextRecalculateTime = Time.time + PathRecalculationTime;
     InitBehaviorTree();
 }
 public override void SetMainAgent(NPCController Agent)
 {
     if (g_NPCAssertion != null)
     {
         g_NPCAssertion.Agent = Agent;
     }
 }
        void Reset()
        {
            g_Controller = GetComponent <NPCController>();
            g_Controller.Debug("Initializing NPCPerception...");
            // add perception fields
            g_Controller = gameObject.GetComponent <NPCController>();
            GameObject pf;
            Component  sCol = g_Controller.GetComponent(PERCEPTION_FIELD_OBJECT);

            if (sCol == null)
            {
                // take into account not reading a duplicate Sphere Collider in the same layer
                pf = new GameObject();
            }
            else
            {
                pf = sCol.gameObject;
            }
            pf.name             = PERCEPTION_FIELD_OBJECT;
            pf.layer            = LayerMask.NameToLayer(PERCEPTION_LAYER);
            pf.transform.parent = g_Controller.transform;
            // reset transform
            pf.transform.rotation      = g_Controller.transform.rotation;
            pf.transform.localPosition = Vector3.zero;
            pf.transform.localScale    = Vector3.one;
            gPerceptionField           = pf.AddComponent <SphereCollider>();
            gPerceptionField.isTrigger = true;
            gPerceptionField.radius    = (MIN_PERCEPTION_FIELD + MAX_PERCEPTION_FIELD) / 4;
            gViewAngle = (MIN_VIEW_ANGLE + MAX_VIEW_ANGLE) / 2;
            // collisions / reach
        }
 public override void SetMainAgent(NPCController agent)
 {
     if (g_Affordance != null)
     {
         g_Affordance.Agent = agent;
     }
 }
 public void SetTarget(GameObject o)
 {
     g_Target = o.GetComponent <NPCController>();
     if (g_Target != null)
     {
         g_Target.SetSelected(true);
     }
 }
        public void InitializePerception()
        {
            g_PerceptionFieldRadius = PerceptionRadius;
            Animator aC = GetComponent <Animator>();

            g_Head       = aC.GetBoneTransform(HumanBodyBones.Head);
            g_Controller = GetComponent <NPCController>();
            g_Perceiving = false;
            g_CurrentlyPerceivedTarget           = null;
            g_PerceivedObjects                   = new HashSet <INPCPerceivable>();
            g_PerceivedNPCAgents                 = new HashSet <INPCPerceivable>();
            g_HalfViewAngle                      = 1f - (ViewAngle / 180f); // 2 for each half, 90 forward to right
            gPerceptionField.transform.position += transform.up;
        }
 void FindMainNPC()
 {
     foreach (NPCController npc in FindObjectsOfType <NPCController>())
     {
         if (npc.MainAgent)
         {
             if (NPCControllerTarget == null)
             {
                 NPCControllerTarget = npc;
             }
             else if (npc != NPCControllerTarget)
             {
                 NPCControllerTarget = null;
                 Debug("NPCControlManager --> Many NPCs marked as MainAgents, Target defaults to empty");
             }
         }
     }
 }
        // Use this for initialization
        void Start()
        {
            g_NPCController = GetComponent <NPCController>();
            g_NPCBody       = g_NPCController.Body;
            g_Animator      = GetComponent <Animator>();


            if (g_Animator == null)
            {
                g_NPCController.Debug("NPCIKController --> An animator controller is needed for IK, disabling component during runtime");
                this.enabled = false;
            }

            // default weight
            IK_WEIGHT = IK_WEIGHT < 0.1f ? 1f : IK_WEIGHT;

            g_RightFootPosition = RIGHT_FOOT.position;
            g_LeftFootPosition  = LEFT_FOOT.position;
        }
Example #9
0
 /// <summary>
 /// Executes and returns the value of the intended method, for the specified entity instance.
 /// </summary>
 /// <returns></returns>
 public BEHAVIOR_STATUS Execute()
 {
     try {
         object[] pms = new object[Parameters.Count];
         for (var i = 0; i < Parameters.Count; ++i)
         {
             pms[i] = Parameters[i].GetValue();
         }
         if (OverrideAgent)
         {
             GameObject go = GameObject.Find(AgentName);
             if (go != null)
             {
                 NPCController agent = go.GetComponent <NPCController>();
                 if (agent == null)
                 {
                     Debug.LogWarning("GameObject 'AgentName' is not an agent: " + this);
                 }
                 else
                 {
                     AgentName = agent.name;
                     Agent     = agent;
                 }
             }
             else
             {
                 Debug.LogWarning("Invalid AgentName for affordance: " + this);
             }
         }
         if (Agent.AI != null)
         {
             return((BEHAVIOR_STATUS)Agent.AI.GetType().GetMethod(MethodName).Invoke(Agent.AI, pms));
         }
         return(BEHAVIOR_STATUS.SUCCESS);
     } catch (Exception e) {
         Debug.LogError("Couldn't execute affordance due to: " + e.Message);
         return(BEHAVIOR_STATUS.FAILURE);
     }
 }
        private void UpdateKeys()
        {
            // Only if targetting agent
            if (gIOTargetting &&
                (g_Camera.CurrentMode == NPCCamController.CAMERA_MODE.THIRD_PERSON ||
                 g_Camera.CurrentMode == NPCCamController.CAMERA_MODE.FIRST_PERSON))
            {
                // modifiers
                if (Input.GetKey((KeyCode)INPUT_KEY.RUN))
                {
                    g_NPCController.Body.Move(LOCO_STATE.RUN);
                }
                else if (Input.GetKey((KeyCode)INPUT_KEY.DUCK))
                {
                    g_NPCController.Body.Move(LOCO_STATE.DUCK);
                }
                else
                {
                    g_NPCController.Body.Move(LOCO_STATE.WALK);
                }

                // forward motion
                if (Input.GetKey((KeyCode)INPUT_KEY.FORWARD))
                {
                    g_NPCController.Body.Move(LOCO_STATE.FORWARD);
                }
                else if (Input.GetKey((KeyCode)INPUT_KEY.BACKWARD))
                {
                    g_NPCController.Body.Move(LOCO_STATE.BACKWARDS);
                }
                else
                {
                    g_NPCController.Body.Move(LOCO_STATE.IDLE);
                }

                // turning
                if (Input.GetKey((KeyCode)INPUT_KEY.LEFT))
                {
                    g_NPCController.Body.Move(LOCO_STATE.LEFT);
                }
                else if (Input.GetKey((KeyCode)INPUT_KEY.RIGHT))
                {
                    g_NPCController.Body.Move(LOCO_STATE.RIGHT);
                }
                else
                {
                    g_NPCController.Body.Move(LOCO_STATE.FRONT);
                }

                // jumping
                if (Input.GetKeyDown((KeyCode)INPUT_KEY.JUMP))   // Note we only read jump once
                {
                    g_NPCController.Body.Move(LOCO_STATE.JUMP);
                }

                // focusing
                if (Input.GetKey((KeyCode)INPUT_KEY.FOCUS_LOOK))
                {
                    if (g_Camera != null && g_NPCController != null && !g_Camera.CloseUp)
                    {
                        g_Camera.CloseUp = true;
                        g_NPCController.Body.LookAround(true);
                    }
                    else
                    {
                        float xDelta = Input.GetAxis("Mouse X");
                        float yDelta = Input.GetAxis("Mouse Y");
                        if (Mathf.Abs(xDelta) > 0.75f)
                        {
                            Vector3 pos = g_NPCController.Body.TargetObject.position + (xDelta > 0 ? 1 : -1) * g_NPCController.Body.TargetObject.right;
                            g_NPCController.Body.TargetObject.position = Vector3.Lerp(g_NPCController.Body.TargetObject.position, pos, Time.deltaTime * gMouseDragSmootFactor);
                        }
                        if (Mathf.Abs(yDelta) > 0.75f)
                        {
                            Vector3 pos = g_NPCController.Body.TargetObject.position + (yDelta > 0 ? 1 : -1) * g_NPCController.Body.TargetObject.up;
                            g_NPCController.Body.TargetObject.position = Vector3.Lerp(g_NPCController.Body.TargetObject.position, pos, Time.deltaTime * gMouseDragSmootFactor);
                        }
                    }
                }
                else if (Input.GetKeyUp((KeyCode)INPUT_KEY.FOCUS_LOOK))
                {
                    g_Camera.CloseUp = false;
                    g_NPCController.Body.LookAround(false);
                    g_Camera.ResetView();
                }
            }
            else
            {
                // select agent
                if (Input.GetKey((KeyCode)INPUT_KEY.SELECT_AGENT))
                {
                    // not dragging
                    if (g_SelectDragging)
                    {
                        // TODO
                    }
                    else
                    {
                        RaycastHit hitInfo   = new RaycastHit();
                        bool       clickedOn = Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hitInfo);
                        if (clickedOn)
                        {
                            // handle single selection
                            NPCController npc = hitInfo.transform.gameObject.GetComponent <NPCController>();
                            if (npc != null)
                            {
                                if (g_NPCController != null)
                                {
                                    g_NPCController.SetSelected(false);
                                }
                                g_NPCController = npc;
                                npc.SetSelected(true);
                            }
                            else
                            {
                                // deselect
                                if (g_NPCController != null)
                                {
                                    g_NPCController.SetSelected(false);
                                    g_NPCController = null;
                                }
                            }
                        }
                    }
                }

                if (Input.GetKeyDown((KeyCode)INPUT_KEY.CONTEXT_ACTION))
                {
                    if (g_NPCController != null)
                    {
                        if (g_Camera.CurrentMode == NPCCamController.CAMERA_MODE.ISOMETRIC)
                        {
                            RaycastHit hitInfo = new RaycastHit();
                            if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hitInfo))
                            {
                                if (Input.GetKey((KeyCode)INPUT_KEY.MODIFIER))
                                {
                                    g_NPCController.Body.OrientTowards(hitInfo.point);
                                }
                                else if (Input.GetKey((KeyCode)INPUT_KEY.MODIFIER_SEC))
                                {
                                    g_NPCController.Body.RunTo(hitInfo.point);
                                }
                                else
                                {
                                    g_NPCController.Body.GoTo(hitInfo.point);
                                }
                            }
                        }
                    }
                }
            }
        }
 public void SetTarget(NPCController c)
 {
     NPCControllerTarget = c;
 }
 /// <summary>
 /// Assert for a specific tag on either self or perceived agents.
 /// </summary>
 /// <param name="Target"></param>
 /// <param name="Assert"></param>
 /// <param name="Tag"></param>
 public NPCAssertion(NPCController Agent, TARGET Target, ASSERT Assert, string Tag) : this(Agent, Target, Assert)
 {
     TargetValue.SetValue(Tag);
 }
 public virtual void SetMainAgent(NPCController Agent)
 {
     MainAgent = Agent;
 }
 /// <summary>
 /// Assert for a particular state on the transform of either self of perceived agents.
 /// </summary>
 /// <param name="Target"></param>
 /// <param name="Assert"></param>
 /// <param name="Transform"></param>
 /// <param name="TargetValue"></param>
 public NPCAssertion(NPCController Agent, TARGET Target, ASSERT Assert, TRANSFORM_ASSERT Transform, object TargetValue) : this(Agent, Target, Assert)
 {
     TransformAssert = Transform;
     this.TargetValue.SetValue(TargetValue);
 }
        public override void OnInspectorGUI()
        {
            // Update all serialized objects
            serializedObject.Update();
            gController = (NPCController)target;

            // Variables for multiple selected agents
            float agentAttractionForce = 0, agentRepulsionWeight = 0, distanceTolerance = 0, proximityScaleMultiplier = 0, followAgentsFlow = 0, velocityModifierScale = 0;
            float stairsClimbingForceFactor = 0, heightAdjustInterpolation = 0, stepHeight = 0, maxStepHeight = 0, colliderRadiusCorrection = 0, stepColliderHeightCorrection = 0,
                  colliderTestDistance = 0, colliderTestAngle = 0;
            bool enableVelocityModifier = false;

            EditorGUI.BeginChangeCheck();

            /* Load Modules */
            if (gController.GetComponent <INPCModule>() != null)
            {
                gShowMods = EditorGUILayout.Foldout(gShowMods, "NPC Modules");
                if (gShowMods)
                {
                    gController.LoadNPCModules();
                    INPCModule[]      mods = gController.NPCModules;
                    GUILayoutOption[] ops  = new GUILayoutOption[1];
                    foreach (INPCModule m in mods)
                    {
                        EditorGUILayout.BeginHorizontal();
                        EditorGUILayout.LabelField(m.NPCModuleName());
                        m.SetEnable(EditorGUILayout.Toggle(m.IsEnabled()));
                        EditorGUILayout.EndHorizontal();
                    }
                }
            }
            else
            {
                EditorGUILayout.LabelField("No NPC Modules Loaded");
            }

            /* General Controller */
            gGeneralSettings = EditorGUILayout.Foldout(gGeneralSettings, "General Settings");
            if (gGeneralSettings)
            {
                gController.UpdateTime = EditorGUILayout.FloatField(label_ControllerUpdateTime, gController.UpdateTime);
                gController.MainAgent  = EditorGUILayout.Toggle(label_MainAgent, gController.MainAgent);
                gController.DebugMode  = EditorGUILayout.Toggle(label_DebugPrint, gController.DebugMode);
                gController.AgentType  = (AGENT_TYPE)EditorGUILayout.EnumPopup("Agent Type", gController.AgentType);
                gController.TestNPC    = EditorGUILayout.Toggle(label_TestNPC, gController.TestNPC);
                if (gController.TestNPC)
                {
                    gController.TestTargetLocation = (Transform)EditorGUILayout.ObjectField("Test Target Location", (Transform)gController.TestTargetLocation, typeof(Transform), true);
                }
                else
                {
                    gController.TestTargetLocation = null;
                }
                gController.EnableAudio = EditorGUILayout.Toggle(label_EnableAgentAudio, gController.EnableAudio);
                EditorGUILayout.BeginVertical(EditorStyles.helpBox);
                EditorGUILayout.LabelField(label_NPCAgentTags);
                if (serializedObject.FindProperty("AgentTags") != null)
                {
                    EditorGUILayout.PropertyField(serializedObject.FindProperty("AgentTags").FindPropertyRelative("Array.size"), true);
                }
                if (gController.AgentTags == null)
                {
                    gController.AgentTags = new string[0];
                }
                else
                {
                    for (int at = 0; at < gController.AgentTags.Length; ++at)
                    {
                        gController.AgentTags[at] = EditorGUILayout.TextField("Agent Tag " + (at + 1), gController.AgentTags[at]);
                    }
                }
                EditorGUILayout.EndVertical();
            }

            /* Perception */
            gShowPerception = EditorGUILayout.Foldout(gShowPerception, "Perception") && gController.Perception != null;
            if (gShowPerception)
            {
                gController.Perception.ViewAngle = EditorGUILayout.IntSlider(label_ViewAngle, (int)gController.Perception.ViewAngle,
                                                                             (int)NPCPerception.MIN_VIEW_ANGLE,
                                                                             (int)NPCPerception.MAX_VIEW_ANGLE);
                gController.Perception.PerceptionRadius = EditorGUILayout.Slider(label_PerceptionRadius, gController.Perception.PerceptionRadius,
                                                                                 (int)NPCPerception.MIN_PERCEPTION_FIELD,
                                                                                 (int)NPCPerception.MAX_PERCEPTION_FIELD);
                gController.Perception.PerceptionWeight = EditorGUILayout.FloatField(
                    label_PerceptionPercpWeight,
                    gController.Perception.PerceptionWeight);
            }

            /* AI */
            gShowAI = EditorGUILayout.Foldout(gShowAI, "AI") && gController.AI != null;
            if (gShowAI)
            {
                if (gController.AI.Pathfinders != null)
                {
                    gController.AI.PathRecalculationTime = EditorGUILayout.FloatField(label_AIPathRecalculationTime, gController.AI.PathRecalculationTime);
                    string[] pfds = new string[gController.AI.Pathfinders.Count];
                    gController.AI.Pathfinders.Keys.CopyTo(pfds, 0);
                    selectedPathfinder = 0;
                    for (int i = 0; i < pfds.Length; ++i)
                    {
                        if (pfds[i] == gController.AI.SelectedPathfinder)
                        {
                            selectedPathfinder = i;
                        }
                    }
                    if (gController.AI.Pathfinders.ContainsKey(pfds[selectedPathfinder]))
                    {
                        selectedPathfinder = EditorGUILayout.Popup("Pathfinders", selectedPathfinder, pfds);
                        gController.AI.SelectedPathfinder = pfds[selectedPathfinder];
                    }
                    else
                    {
                        gController.AI.SelectedPathfinder = pfds[0];
                    }
                    if (gController.Body.Navigation == NAV_STATE.STEERING_NAV)
                    {
                        gController.AI.NavMeshAgentPathfinding = EditorGUILayout.Toggle(label_NavMeshAgentPathfinding, gController.AI.NavMeshAgentPathfinding);
                        if (gController.AI.NavMeshAgentPathfinding)
                        {
                            gController.AI.SelectedPathfinder = pfds[0];
                        }
                    }
                    else
                    {
                        gController.AI.NavMeshAgentPathfinding = false;
                    }
                }
            }

            /* Body */
            gShowBody = EditorGUILayout.Foldout(gShowBody, "Body") && gController.Body != null;
            if (gShowBody)
            {
                // Navigation
                gController.Body.LocoControlMode      = (LOCO_CONTROL)EditorGUILayout.EnumPopup(label_BodyLocoControlMode, gController.Body.LocoControlMode);
                gController.Body.LocomotionBlendDelay = EditorGUILayout.Slider(label_BodyLocomotionBlendDelay, gController.Body.LocomotionBlendDelay, 0f, 5f);
                gController.Body.TurningBlendDelay    = EditorGUILayout.Slider(label_BodyTurningBlendDelay, gController.Body.TurningBlendDelay, 0f, 5f);
                gController.Body.JumpHeight           = EditorGUILayout.Slider(label_BodyJumpHeight, gController.Body.JumpHeight, 0.8f, 100f);
                EditorGUILayout.BeginVertical(EditorStyles.helpBox);
                EditorGUILayout.LabelField(label_BodyNavigation);
                if (gController.Body.Navigation != NAV_STATE.DISABLED)
                {
                    gController.Body.NavDistanceThreshold = EditorGUILayout.FloatField(label_NavStoppingThresh, gController.Body.NavDistanceThreshold);
                    gController.Body.NavDistanceThreshold = gController.Body.NavDistanceThreshold < 0.08f ? 0.1f : gController.Body.NavDistanceThreshold;
                }
                gController.Body.Navigation   = (NAV_STATE)EditorGUILayout.EnumPopup((NAV_STATE)gController.Body.Navigation);
                gController.Body.TurningAngle = EditorGUILayout.Slider(label_BodyTurningAngle, gController.Body.TurningAngle,
                                                                       gController.Body.MinTurningAngle,
                                                                       gController.Body.MaxTurningAngle);
                EditorGUILayout.EndVertical();

                // Obstacles detection
                EditorGUILayout.BeginVertical(EditorStyles.helpBox);
                gController.Body.ObstaclesDetectionEnabled = EditorGUILayout.Toggle(label_StepObstacleDetection, gController.Body.ObstaclesDetectionEnabled);
                gController.Body.UseSingleCapsuleCollider  = EditorGUILayout.Toggle(label_BodyUseSingleCapsuleCollider, gController.Body.UseSingleCapsuleCollider);
                if (gController.Body.ObstaclesDetectionEnabled)
                {
                    stairsClimbingForceFactor = gController.Body.StairsClimbingForceFactor = EditorGUILayout.Slider(label_BodyStairsClimbingForceFactor, gController.Body.StairsClimbingForceFactor, 0f, 2f);
                    heightAdjustInterpolation = gController.Body.HeightAdjustInterpolation = EditorGUILayout.FloatField(label_BodyHeightAdjustInterpolation, gController.Body.HeightAdjustInterpolation);
                    stepHeight               = gController.Body.StepHeight = EditorGUILayout.FloatField(label_StepHeight, gController.Body.StepHeight);
                    maxStepHeight            = gController.Body.MaxStepHeight = EditorGUILayout.FloatField(label_MaxStepHeight, gController.Body.MaxStepHeight);
                    colliderRadiusCorrection = gController.Body.ColliderRadiusCorrection = EditorGUILayout.Slider(label_BodyColliderRadiusCorrection,
                                                                                                                  gController.Body.ColliderRadiusCorrection,
                                                                                                                  0f,
                                                                                                                  1f);
                    stepColliderHeightCorrection = gController.Body.StepColliderHeightCorrection = EditorGUILayout.Slider(label_BodyStepHeightCorrection,
                                                                                                                          gController.Body.StepColliderHeightCorrection,
                                                                                                                          0f,
                                                                                                                          1f);
                    colliderTestDistance = gController.Body.ColliderTestDistance = EditorGUILayout.Slider(label_BodyColliderTestDistance,
                                                                                                          gController.Body.ColliderTestDistance,
                                                                                                          0f,
                                                                                                          1f);
                    colliderTestAngle = gController.Body.ColliderTestAngle = EditorGUILayout.Slider(label_BodyColliderTestAngle,
                                                                                                    gController.Body.ColliderTestAngle,
                                                                                                    -2f,
                                                                                                    2f);
                }
                EditorGUILayout.EndVertical();

                // IK
                EditorGUILayout.BeginVertical(EditorStyles.helpBox);
                gController.Body.IKEnabled = EditorGUILayout.Toggle(label_IKEnabled, gController.Body.IKEnabled);
                if (gController.Body.IKEnabled)
                {
                    gController.Body.IK_START_LOOK_AT_ADJUST = EditorGUILayout.Slider(label_IKLookStartAdjust, gController.Body.IK_START_LOOK_AT_ADJUST, 0.1f, 1f);
                    gController.Body.IK_LOOK_AT_SMOOTH       = EditorGUILayout.Slider(label_IKLookSmoothness, gController.Body.IK_LOOK_AT_SMOOTH, 1f, 10f);
                    gController.Body.MaxArmsReach            = EditorGUILayout.Slider(label_IKMaxArmsReach, gController.Body.MaxArmsReach, 0f, 10f);
                    gController.Body.IK_HANDS_SMOOTH         = EditorGUILayout.Slider(label_IKHandsSmoothing, gController.Body.IK_HANDS_SMOOTH, 0.1f, 3f);
                    gController.Body.IK_USE_HINTS            = EditorGUILayout.Toggle(label_IKUseHints, gController.Body.IK_USE_HINTS);
                    gController.Body.IK_FEET_Enabled         = EditorGUILayout.Toggle(label_IKFeetEnabled, gController.Body.IK_FEET_Enabled);
                    if (gController.Body.IK_FEET_Enabled)
                    {
                        gController.Body.IK_FEET_HEIGHT_CORRECTION         = EditorGUILayout.Slider(label_IKFeetHeight, gController.Body.IK_FEET_HEIGHT_CORRECTION, 0f, 0.5f);
                        gController.Body.IK_FEET_FORWARD_CORRECTION        = EditorGUILayout.Slider(label_IKFeetForward, gController.Body.IK_FEET_FORWARD_CORRECTION, -0.5f, 0.5f);
                        gController.Body.IK_FEET_HEIGHT_EFFECTOR_CORRECTOR = EditorGUILayout.Slider(label_IKFeetEffectorCorrector, gController.Body.IK_FEET_HEIGHT_EFFECTOR_CORRECTOR, 0f, 0.3f);
                    }
                }
                EditorGUILayout.EndVertical();

                EditorGUILayout.BeginVertical(EditorStyles.helpBox);
                gController.Body.EnableSocialForces = EditorGUILayout.Toggle(label_UseSocialForces, gController.Body.EnableSocialForces);
                if (gController.Body.EnableSocialForces)
                {
                    agentAttractionForce     = gController.Body.AgentAttractionForce = EditorGUILayout.FloatField(label_BodySFAgentsAttractionForce, gController.Body.AgentAttractionForce);
                    agentRepulsionWeight     = gController.Body.AgentRepulsionWeight = EditorGUILayout.FloatField(label_AgentRepulsionWeight, gController.Body.AgentRepulsionWeight);
                    distanceTolerance        = gController.Body.DistanceTolerance = EditorGUILayout.FloatField(label_AgentRepulsionDistanceTolerance, gController.Body.DistanceTolerance);
                    proximityScaleMultiplier = gController.Body.ProximityScaleMultiplier = EditorGUILayout.FloatField(label_AgentProximityScaleMultiplier, gController.Body.ProximityScaleMultiplier);
                    followAgentsFlow         = gController.Body.FollowAgentsFlow = EditorGUILayout.FloatField(label_BodySFMatchAgentsFlow, gController.Body.FollowAgentsFlow);
                    EditorGUILayout.BeginVertical(EditorStyles.helpBox);
                    EditorGUILayout.LabelField(label_BodyEnableVelocityModifier);
                    enableVelocityModifier = gController.Body.EnableVelocityModifier = EditorGUILayout.Toggle(gController.Body.EnableVelocityModifier);
                    velocityModifierScale  = gController.Body.VelocityModifierScale = EditorGUILayout.FloatField(label_BodyVelocityModifierFactor, gController.Body.VelocityModifierScale);
                    EditorGUILayout.EndVertical();
                }
                EditorGUILayout.EndVertical();
            }

            /* Animations */
            gShowAnimationsSection = EditorGUILayout.Foldout(gShowAnimationsSection, "Animations");

            if (gShowAnimationsSection)
            {
                EditorGUILayout.BeginVertical(EditorStyles.helpBox);
                gController.Body.EnableBlinking = EditorGUILayout.Toggle(label_EnableBlinking, gController.Body.EnableBlinking);
                if (gController.Body.EnableBlinking)
                {
                    gController.Body.BlinkingSpeed = EditorGUILayout.FloatField(label_BlinkingSpeed, gController.Body.BlinkingSpeed);
                }
                gController.Body.UseAnimatorController = EditorGUILayout.Toggle(label_AnimatorEnabled, gController.Body.UseAnimatorController);
                gController.Body.UseCurvesAndParams    = EditorGUILayout.Toggle(label_UseAnimCurves, gController.Body.UseCurvesAndParams);
                EditorGUILayout.BeginVertical(EditorStyles.helpBox);
                gController.UseAnimatedClips = EditorGUILayout.Toggle(label_UseAduioClips, gController.UseAnimatedClips);

                if (gController.UseAnimatedClips)
                {
                    if (gController.AnimatedAudioClips == null)
                    {
                        gController.AnimatedAudioClips = new NPCAnimatedAudio[0];
                    }

                    EditorGUI.indentLevel++;

                    EditorGUILayout.PropertyField(serializedObject.FindProperty("AnimatedAudioClips").FindPropertyRelative("Array.size"), true);

                    for (int i = 0; i < gController.AnimatedAudioClips.Length; ++i)
                    {
                        NPCAnimatedAudio au = gController.AnimatedAudioClips[i];
                        if (au == null)
                        {
                            gController.AnimatedAudioClips[i] = au = ScriptableObject.CreateInstance <NPCAnimatedAudio>();
                        }
                        EditorGUILayout.LabelField("Clip " + (i + 1));
                        SerializedObject so = new SerializedObject(au);
                        // so.Update();
                        EditorGUILayout.BeginVertical(EditorStyles.helpBox);
                        EditorGUILayout.PropertyField(so.FindProperty("Name"));
                        EditorGUILayout.PropertyField(so.FindProperty("Type"));
                        EditorGUILayout.PropertyField(so.FindProperty("Clips"), true);

                        if (au.Type == ANIMATED_AUDIO_TYPE.SPEECH)
                        {
                            EditorGUILayout.PropertyField(so.FindProperty("Animations"), new GUIContent("Phonemes"), true);
                        }
                        else
                        {
                            EditorGUILayout.PropertyField(so.FindProperty("Animations"), true);
                        }

                        EditorGUI.indentLevel++;
                        EditorGUILayout.PropertyField(so.FindProperty("RandomizeAnimations"));
                        EditorGUI.indentLevel--;
                        EditorGUILayout.EndVertical();
                        so.ApplyModifiedProperties();
                    }
                    EditorGUI.indentLevel--;
                }
                else
                {
                    gController.AnimatedAudioClips = null;
                }

                EditorGUILayout.EndVertical();
                EditorGUILayout.EndVertical();
            }

            foreach (Object o in targets)
            {
                NPCController c = (NPCController)o;
                c.DebugMode  = gController.DebugMode;
                c.UpdateTime = gController.UpdateTime;
                c.Perception.PerceptionRadius = gController.Perception.PerceptionRadius;
                c.AI.PathRecalculationTime    = gController.AI.PathRecalculationTime;

                // Social Forces
                if (c.Body.EnableSocialForces)
                {
                    c.Body.AgentAttractionForce     = agentAttractionForce;
                    c.Body.AgentRepulsionWeight     = agentRepulsionWeight;
                    c.Body.DistanceTolerance        = distanceTolerance;
                    c.Body.ProximityScaleMultiplier = proximityScaleMultiplier;
                    c.Body.FollowAgentsFlow         = followAgentsFlow;
                    c.Body.EnableVelocityModifier   = enableVelocityModifier;
                    c.Body.VelocityModifierScale    = velocityModifierScale;
                }

                // Obstacles Detection
                if (c.Body.ObstaclesDetectionEnabled)
                {
                    c.Body.StairsClimbingForceFactor = stairsClimbingForceFactor;
                    c.Body.HeightAdjustInterpolation = heightAdjustInterpolation;
                    c.Body.StepHeight                   = stepHeight;
                    c.Body.MaxStepHeight                = maxStepHeight;
                    c.Body.ColliderRadiusCorrection     = colliderRadiusCorrection;
                    c.Body.StepColliderHeightCorrection = stepColliderHeightCorrection;
                    c.Body.ColliderTestDistance         = colliderTestDistance;
                    c.Body.ColliderTestAngle            = colliderTestAngle;
                }
            }

            if (EditorGUI.EndChangeCheck())
            {
                Undo.RecordObject(gController, "Parameter Changed");
                EditorUtility.SetDirty(gController);
            }

            serializedObject.ApplyModifiedProperties();
        }
Example #16
0
 public void SetTarget(NPCController t)
 {
     Target = t;
 }
 private NPCAssertion(NPCController Agent, TARGET Target, ASSERT Assert) : this()
 {
     this.Agent  = Agent;
     this.Target = Target;
     this.Assert = Assert;
 }
Example #18
0
        public override void HandlePointer()
        {
            Ray        ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit rayHit;
            bool       hit = Physics.Raycast(ray, out rayHit);
            GameObject go  = hit ? rayHit.collider.gameObject : null;

            if (Input.anyKeyDown)
            {
                float currentClick = Time.realtimeSinceStartup,
                      deltaTime    = Time.realtimeSinceStartup - g_LastClick;

                if (deltaTime < DoubleClickThreshold)
                {
                    g_DoubleClick = true;
                }
                else
                {
                    g_DoubleClick = false;
                }

                g_LastClick = currentClick;

                if (hit)
                {
                    if (Input.GetKeyDown((KeyCode)IO_CONTROLS.MOUSE_RIGHT))
                    {
                        if (!FocusedTarget)
                        {
                            NPCController c = go.GetComponent <NPCController>();
                            if (c != null)
                            {
                                g_Target = c;
                                g_Target.SetSelected(true);
                            }
                            else
                            {
                                if (g_Target != null)
                                {
                                    g_Target.SetSelected(false);
                                    g_Target = null;
                                }
                            }
                        }
                    }

                    if (Input.GetKeyDown((KeyCode)IO_CONTROLS.MOUSE_LEFT))
                    {
                        if (g_Target != null)
                        {
                            if (Input.GetKey(KeyCode.LeftShift))
                            {
                                g_Target.Body.OrientTowards(rayHit.point);
                            }
                            else
                            {
                                if (g_DoubleClick)
                                {
                                    g_Target.Body.RunTo(rayHit.point);
                                }
                                else
                                {
                                    g_Target.Body.GoTo(rayHit.point);
                                }
                            }
                        }
                    }

                    if (Input.GetKeyDown((KeyCode)IO_CONTROLS.MOUSE_MIDDLE))
                    {
                        Debug.Log("Mouse 3 clicked");
                    }
                }
                else if (hit && go.GetComponent <INPCPerceivable>() != null)
                {
                    if (g_CurrentHoverTarget != go)
                    {
                        Renderer renderer = go.GetComponent <Renderer>();
                        if (!g_ObjectsMaterials.ContainsKey(go))
                        {
                            g_ObjectsMaterials.Add(go, renderer.material);
                        }
                        renderer.sharedMaterial = g_Outliner;
                        g_CurrentHoverTarget    = go;
                    }
                }
                else
                {
                    if (g_CurrentHoverTarget != null && g_ObjectsMaterials.ContainsKey(go))
                    {
                        g_CurrentHoverTarget.GetComponent <Renderer>().sharedMaterial = g_ObjectsMaterials[go];
                    }
                    g_CurrentHoverTarget = null;
                }
            }
        }
 /// <summary>
 /// Assert for a specific Property on either self or perceived agents.
 /// </summary>
 /// <param name="Target"></param>
 /// <param name="Assert"></param>
 /// <param name="Property"></param>
 /// <param name="Equality"></param>
 /// <param name="TargetValue"></param>
 public NPCAssertion(NPCController Agent, TARGET Target, ASSERT Assert, PropertyInfo Property, OPERATION Equality, Parameter TargetValue) : this(Agent, Target, Assert)
 {
     EqualityOperation = Equality;
     this.Property     = Property;
     this.TargetValue  = TargetValue;
 }
 void Reset()
 {
     this.gNPCController = gameObject.GetComponent <NPCController>();
     InitPathfinders();
 }
 public void SetTarget(NPCController target)
 {
     g_NPCController = target;
     gIOTargetting   = (g_NPCController != null);
 }
Example #22
0
        public override void OnInspectorGUI()
        {
            gController = (NPCController)target;

            /*
             * Look for added compatible added components, extend with interface later on
             */
            /**/

            EditorGUI.BeginChangeCheck();

            /* Load Modules */
            if (gController.GetComponent <INPCModule>() != null)
            {
                gShowMods = EditorGUILayout.Foldout(gShowMods, "NPC Modules");
                if (gShowMods)
                {
                    gController.LoadNPCModules();
                    INPCModule[]      mods = gController.NPCModules;
                    GUILayoutOption[] ops  = new GUILayoutOption[1];
                    foreach (INPCModule m in mods)
                    {
                        EditorGUILayout.BeginHorizontal();
                        EditorGUILayout.LabelField(m.NPCModuleName());
                        m.SetEnable((bool)EditorGUILayout.Toggle((bool)m.IsEnabled()));
                        EditorGUILayout.EndHorizontal();
                    }
                }
            }
            else
            {
                EditorGUILayout.LabelField("No NPC Modules Loaded");
            }

            /* General Controller */
            gGeneralSettings = EditorGUILayout.Foldout(gGeneralSettings, "General Settings");
            if (gGeneralSettings)
            {
                gController.MainAgent = (bool)EditorGUILayout.Toggle(label_MainAgent, (bool)gController.MainAgent);
                gController.DebugMode = (bool)EditorGUILayout.Toggle(label_DebugPrint, (bool)gController.DebugMode);
                gController.DisplaySelectedHighlight = (bool)EditorGUILayout.Toggle(label_SelectHighlight, (bool)gController.DisplaySelectedHighlight);
                gController.TestNPC = (bool)EditorGUILayout.Toggle(label_TestNPC, (bool)gController.TestNPC);
                if (gController.TestNPC)
                {
                    gController.TestTargetLocation = (Transform)EditorGUILayout.ObjectField("Test Target Location", (Transform)gController.TestTargetLocation, typeof(Transform), true);
                }
                else
                {
                    gController.TestTargetLocation = null;
                }
            }

            /* Perception */
            gShowPerception = EditorGUILayout.Foldout(gShowPerception, "Perception") && gController.Perception != null;
            if (gShowPerception)
            {
                gController.Perception.ViewAngle = (float)EditorGUILayout.IntSlider(label_ViewAngle, (int)gController.Perception.ViewAngle,
                                                                                    (int)NPCPerception.MIN_VIEW_ANGLE,
                                                                                    (int)NPCPerception.MAX_VIEW_ANGLE);
                gController.Perception.PerceptionRadius = (float)EditorGUILayout.IntSlider(label_PerceptionRadius, (int)gController.Perception.PerceptionRadius,
                                                                                           (int)NPCPerception.MIN_PERCEPTION_FIELD,
                                                                                           (int)NPCPerception.MAX_PERCEPTION_FIELD);
            }

            /* AI */
            gShowAI = EditorGUILayout.Foldout(gShowAI, "AI") && gController.AI != null;
            if (gShowAI)
            {
                if (gController.AI.Pathfinders != null)
                {
                    string[] pfds = new string[gController.AI.Pathfinders.Count];
                    gController.AI.Pathfinders.Keys.CopyTo(pfds, 0);
                    selectedPathfinder = 0;
                    for (int i = 0; i < pfds.Length; ++i)
                    {
                        if (pfds[i] == gController.AI.SelectedPathfinder)
                        {
                            selectedPathfinder = i;
                        }
                    }
                    if (gController.AI.Pathfinders.ContainsKey(pfds[selectedPathfinder]))
                    {
                        selectedPathfinder = EditorGUILayout.Popup("Pathfinders", selectedPathfinder, pfds);
                        gController.AI.SelectedPathfinder = pfds[selectedPathfinder];
                    }
                    else
                    {
                        gController.AI.SelectedPathfinder = pfds[0];
                    }
                    if (gController.Body.Navigation == NAV_STATE.STEERING_NAV)
                    {
                        gController.AI.NavMeshAgentPathfinding = (bool)EditorGUILayout.Toggle(label_NavMeshAgentPathfinding, (bool)gController.AI.NavMeshAgentPathfinding);
                        if (gController.AI.NavMeshAgentPathfinding)
                        {
                            gController.AI.SelectedPathfinder = pfds[0];
                        }
                    }
                    else
                    {
                        gController.AI.NavMeshAgentPathfinding = false;
                    }
                }
            }

            /* Body */
            gShowBody = EditorGUILayout.Foldout(gShowBody, "Body") && gController.Body != null;
            if (gShowBody)
            {
                EditorGUILayout.BeginHorizontal();
                EditorGUILayout.LabelField(label_BodyNavigation);
                gController.Body.Navigation = (NAV_STATE)EditorGUILayout.EnumPopup((NAV_STATE)gController.Body.Navigation);
                EditorGUILayout.EndHorizontal();
                if (gController.Body.Navigation != NAV_STATE.DISABLED)
                {
                    gController.Body.NavDistanceThreshold = (float)EditorGUILayout.FloatField(label_NavStoppingThresh, (float)gController.Body.NavDistanceThreshold);
                }
                gController.Body.IKEnabled = (bool)EditorGUILayout.Toggle(label_IKEnabled, (bool)gController.Body.IKEnabled);
                if (gController.Body.IKEnabled)
                {
                    gController.Body.IK_USE_HINTS    = (bool)EditorGUILayout.Toggle(label_IKUseHints, (bool)gController.Body.IK_USE_HINTS);
                    gController.Body.IK_FEET_Enabled = (bool)EditorGUILayout.Toggle(label_IKFeetEnabled, (bool)gController.Body.IK_FEET_Enabled);
                    if (gController.Body.IK_FEET_Enabled)
                    {
                        gController.Body.IK_LOOK_AT_SMOOTH                 = (float)EditorGUILayout.Slider(label_IKLookSmoothness, gController.Body.IK_LOOK_AT_SMOOTH, 1f, 10f);
                        gController.Body.IK_FEET_HEIGHT_CORRECTION         = (float)EditorGUILayout.Slider(label_IKFeetHeight, gController.Body.IK_FEET_HEIGHT_CORRECTION, 0f, 0.5f);
                        gController.Body.IK_FEET_FORWARD_CORRECTION        = (float)EditorGUILayout.Slider(label_IKFeetForward, gController.Body.IK_FEET_FORWARD_CORRECTION, -0.5f, 0.5f);
                        gController.Body.IK_FEET_HEIGHT_EFFECTOR_CORRECTOR = (float)EditorGUILayout.Slider(label_IKFeetEffectorCorrector, gController.Body.IK_FEET_HEIGHT_EFFECTOR_CORRECTOR, 0f, 0.3f);
                        gController.Body.IK_FEET_STAIRS_INTERPOLATION      = (float)EditorGUILayout.Slider(label_IKFeetStairsInt, gController.Body.IK_FEET_STAIRS_INTERPOLATION, 0f, 20f);
                    }
                }
                gController.Body.StepHeight            = (float)EditorGUILayout.FloatField(label_StepHeight, (float)gController.Body.StepHeight);
                gController.Body.UseAnimatorController = (bool)EditorGUILayout.Toggle(label_AnimatorEnabled, (bool)gController.Body.UseAnimatorController);
                gController.Body.UseCurves             = (bool)EditorGUILayout.Toggle(label_UseAnimCurves, (bool)gController.Body.UseCurves);
                gController.Body.EnableSocialForces    = (bool)EditorGUILayout.Toggle(label_UseSocialForces, (bool)gController.Body.EnableSocialForces);
                if (gController.Body.EnableSocialForces)
                {
                    gController.Body.AgentRepulsionWeight = (float)EditorGUILayout.FloatField(label_AgentRepulsionWeight, (float)gController.Body.AgentRepulsionWeight);
                    gController.Body.DistanceTolerance    = (float)EditorGUILayout.FloatField(label_AgentRepulsionDistanceTolerance, (float)gController.Body.DistanceTolerance);
                }
            }

            if (EditorGUI.EndChangeCheck())
            {
                Undo.RecordObject(gController, "Parameter Changed");
                EditorUtility.SetDirty(gController);
            }
        }
 /// <summary>
 /// Creates an empty assertion instance for a specific agent
 /// </summary>
 /// <param name="Agent"></param>
 public NPCAssertion(NPCController Agent) : this()
 {
     this.Agent = Agent;
 }