private void DrawTriggerFoldout(WaypointObject myTarget) { EditorGUILayout.LabelField("Trigger"); EditorGUI.indentLevel += 1; EditorGUILayout.PropertyField(activeAtStart, activeLabel); EditorGUI.indentLevel -= 1; }
// Function used internally by LoadWaypointCollection to fill a list of WaypointObject script references from a list of names of scene game objects // Returns whether all waypoints in the list were successfully loaded bool TryToLoadWaypointsIntoList(List <string> waypointNames, List <WaypointObject> recipientList) { foreach (string waypoint in waypointNames) { GameObject waypointObj = GameObject.Find(waypoint); if (waypointObj == null) { Debug.LogError("Failed to find " + waypoint + "\nAborting waypoint collection load"); return(false); } else { WaypointObject waypointObjectRef = waypointObj.GetComponent <WaypointObject>(); if (waypointObjectRef == null) { Debug.LogError(waypoint + " has no WaypointObject script attached\nAborting waypoint collection load"); return(false); } else { recipientList.Add(waypointObjectRef); } } } return(true); }
private void DrawBehaviorFoldout(WaypointObject myTarget) { EditorGUILayout.LabelField("Behavior"); EditorGUI.indentLevel += 1; EditorGUILayout.PropertyField(behavior, behaviorLabel); EditorGUILayout.PropertyField(manualDurration, manualLabel); EditorGUI.indentLevel -= 1; }
public static Waypoint Create(GameObject prefab, Transform parent, Vector3 position, Vector3 vertexPosition) { GameObject newObject = Instantiate(prefab, parent); newObject.transform.localPosition = position; WaypointObject waypointObject = newObject.GetComponent <WaypointObject>(); Waypoint waypoint = new Waypoint(position, newObject.transform.position, vertexPosition); waypointObject.waypoint = waypoint; waypoint.waypointObject = newObject; return(waypoint); }
// Function to handle leaving a waypoint void LeaveWaypoint() { isAtWaypoint = false; WaypointObject oldCurrentWaypointTarget = currentWaypointTarget; NavigateToNextWaypoint(); allWaypointsOnExit.Invoke(); if (oldCurrentWaypointTarget == waypoints[currentWaypointIndex]) { oldCurrentWaypointTarget.onWaypointExit.Invoke(); } }
private void CreateCornerWaypoints(WaypointObstacle obstacle) { //create waypoints at corners of obstacles for (int j = 0; j < vertices.Length; j++) { Vector3 globalPoint = obstacle.transform.localToWorldMatrix.MultiplyPoint3x4(vertices[j]); //get point of vertex in global position float dist = Mathf.Sqrt(3 * Mathf.Pow(marginedRadius, 2)); Vector3 globalPosition = globalPoint + directions[j].normalized * dist; //and calculate location of waypoint in global position Vector3 localPosition = obstacle.transform.worldToLocalMatrix.MultiplyPoint3x4(globalPosition); //then transform back to local position currentWaypointsList.Add(WaypointObject.Create(waypointPrefab, obstacle.transform, localPosition, vertices[j])); //add waypoint to list } }
public override void OnInspectorGUI() { WaypointObject myTarget = (WaypointObject)target; serializedObject.Update(); looping = myTarget.behavior == WaypointObject.Behavior.Looping; backTracking = myTarget.behavior == WaypointObject.Behavior.Backtracking; DrawBehaviorFoldout(myTarget); DrawTriggerFoldout(myTarget); EditorGUILayout.PropertyField(wayPoints, true); DrawDurration(myTarget, wayPoints, durrations); serializedObject.ApplyModifiedProperties(); }
public void OnEnable() { WaypointObject myTarget = (WaypointObject)target; behavior = serializedObject.FindProperty("behavior"); wayPoints = serializedObject.FindProperty("wayPoints"); durrations = serializedObject.FindProperty("durrations"); manualDurration = serializedObject.FindProperty("manualTuning"); activeAtStart = serializedObject.FindProperty("active"); behaviorLabel = new GUIContent("Behavior Mode:"); activeLabel = new GUIContent("Active at Start"); manualLabel = new GUIContent("Manually Tune Durrations"); looping = myTarget.behavior == WaypointObject.Behavior.Looping; backTracking = myTarget.behavior == WaypointObject.Behavior.Backtracking; }
// Function to navigate to a specific waypoint, regardless of whether it is in this agent's list // Parameter waitAtWaypointTime adds additional time to the newly selected currentWaypointTarget // Returns whether the agent can access the newly selected currentWaypointTarget public bool NavigateToWaypoint(WaypointObject waypoint, float waitAtWaypointTime = 0f) { CancelCurrentWaypoint(); int waypointIndex = waypoints.IndexOf(waypoint); if (waypointIndex >= 0) { nextWaypointIndex = waypointIndex; return(NavigateToNextWaypoint(false, waitAtWaypointTime)); } else { Debug.Log("Navigating to waypoint not in list"); currentWaypointTarget = waypoint; return(BeginMovingToCurrentWaypointTarget(waitAtWaypointTime)); } }
private void DrawDurration(WaypointObject myTarget, SerializedProperty wayPoints, SerializedProperty durrations) { if (!myTarget.manualTuning) { myTarget.totalDurration = EditorGUILayout.FloatField("Total Durration", myTarget.totalDurration); } else { EditorGUILayout.PropertyField(durrations, true); if (looping && durrations.arraySize != wayPoints.arraySize + 1) { EditorGUILayout.LabelField("WARNING! when looping you must have " + (wayPoints.arraySize + 1) + " entries in your durrations Array!"); } else if (backTracking && durrations.arraySize != wayPoints.arraySize) { EditorGUILayout.LabelField("WARNING! when backtracking you must have " + (wayPoints.arraySize) + " entries in your durrations Array!"); } } }
// Publicly accessible function also used internally to update currentWaypointTarget // Parameter allowRandomSelection determines whether to also consider random waypoints in the connected, inner list // Parameter waitAtWaypointTime adds additional time to the newly selected currentWaypointTarget // Returns whether the agent can access the newly selected currentWaypointTarget public bool NavigateToNextWaypoint(bool allowRandomSelection = true, float waitAtWaypointTime = 0) { CancelCurrentWaypoint(); if (allowRandomSelection && currentWaypointIndex >= 0) { if (connectedWaypoints[currentWaypointIndex].connections.Count > 0 && UnityEngine.Random.Range(0f, 1f) <= goRndWaypointChance) { int rndIndex = UnityEngine.Random.Range(0, connectedWaypoints[currentWaypointIndex].connections.Count); currentWaypointTarget = connectedWaypoints[currentWaypointIndex].connections[rndIndex]; } else { currentWaypointTarget = waypoints[nextWaypointIndex]; } } else { currentWaypointTarget = waypoints[nextWaypointIndex]; } return(BeginMovingToCurrentWaypointTarget(waitAtWaypointTime)); }
private void CreateInbetweenWaypoints(WaypointObstacle obstacle) { List <Waypoint> corners = new List <Waypoint>(currentWaypointsList); foreach (Waypoint startWP in corners) // { foreach (Waypoint neighbour in startWP.neighbouringCorner) // for every neighbour of the start waypoint { if (!startWP.checkedBy.Contains(neighbour)) //check if this connection has already been checked the other way around { edges.Add(new List <Waypoint>()); //create a new edge List <Waypoint> currentEdge = edges.Last(); currentEdge.Add(startWP); //add start point to edge float distance = Vector3.Distance(startWP.globalPosition, neighbour.globalPosition); if (distance > obstacle.maxWaypointDistance) // and distance is big enough { int extraCount = Mathf.CeilToInt(distance / obstacle.maxWaypointDistance); float stepDistance = distance / extraCount; Vector3 direction = (neighbour.globalPosition - startWP.globalPosition).normalized; for (int i = 0; i < extraCount - 1; i++) { Vector3 globalPosition = startWP.globalPosition + (stepDistance * (i + 1)) * direction; Vector3 localPosition = obstacle.transform.worldToLocalMatrix.MultiplyPoint3x4(globalPosition); // transform back to local position Vector3 vertexPosition = startWP.localPosition + direction * (Vector3.Distance(startWP.localPosition, neighbour.localPosition) / extraCount); currentWaypointsList.Add(WaypointObject.Create(waypointPrefab, obstacle.transform, localPosition, vertexPosition)); //add waypoint to list currentEdge.Add(currentWaypointsList.Last()); //and add to list of edges } } neighbour.checkedBy.Add(startWP); currentEdge.Add(neighbour); //add end point to edge } } } }
private static bool DrawMissionPatrolWaypoint(ICECreatureControl _control, int _index) { ICEEditorStyle.SplitterByIndent(EditorGUI.indentLevel); WaypointObject _waypoint = _control.Creature.Missions.Patrol.Waypoints.Waypoints[_index]; if (_waypoint == null) { return(false); } // HEADER BEGIN ICEEditorLayout.BeginHorizontal(); _waypoint.WaypointFoldout = ICEEditorLayout.Foldout(_waypoint.WaypointFoldout, "Waypoint #" + (int)(_index + 1) + (!string.IsNullOrEmpty(_waypoint.TargetName) ? " (" + _waypoint.TargetName + ")": ""), "", true); if (ICEEditorLayout.ListDeleteButton <WaypointObject>(_control.Creature.Missions.Patrol.Waypoints.Waypoints, _waypoint)) { return(true); } GUILayout.Space(5); if (ICEEditorLayout.ListUpDownButtons <WaypointObject>(_control.Creature.Missions.Patrol.Waypoints.Waypoints, _control.Creature.Missions.Patrol.Waypoints.Waypoints.IndexOf(_waypoint))) { return(true); } _waypoint.Enabled = ICEEditorLayout.EnableButton(_waypoint.Enabled); ICEEditorLayout.EndHorizontal(Info.MISSION_PATROL_WAYPOINT); // HEADER END // CONTENT BEGIN if (_waypoint.WaypointFoldout) { EditorGUI.indentLevel++; EditorGUI.BeginDisabledGroup(_waypoint.Enabled == false); TargetEditor.DrawMissionTarget(_control, (TargetObject)_waypoint, "Target", Info.MISSION_PATROL_TARGET); ICEEditorLayout.BeginHorizontal(); EditorGUI.BeginDisabledGroup(_waypoint.UseCustomBehaviour == false); _waypoint.BehaviourFoldout = ICEEditorLayout.Foldout(_waypoint.BehaviourFoldout, "Behaviour", true); EditorGUI.EndDisabledGroup(); _waypoint.UseCustomBehaviour = ICEEditorLayout.CheckButton("Override", "", _waypoint.UseCustomBehaviour, ICEEditorStyle.ButtonMiddle); ICEEditorLayout.EndHorizontal(Info.MISSION_PATROL_CUSTOM_BEHAVIOUR); if (!_waypoint.UseCustomBehaviour) { _waypoint.BehaviourFoldout = false; } if (_waypoint.BehaviourFoldout) { _waypoint.BehaviourModeTravel = BehaviourEditor.BehaviourSelect(_control, "Travel", "Travel behaviour to reach this waypoint and to start this mission", _waypoint.BehaviourModeTravel, "WP_TRAVEL_" + (int)(_index + 1)); _waypoint.BehaviourModePatrol = BehaviourEditor.BehaviourSelect(_control, "Patrol", "Patrol behaviour to reach this waypoint", _waypoint.BehaviourModePatrol, "WP_PATROL_" + (int)(_index + 1)); BehaviourEditor.DrawInRangeBehaviour(_control, ref _waypoint.BehaviourModeLeisure, ref _waypoint.BehaviourModeRendezvous, ref _waypoint.DurationOfStay, ref _waypoint.IsTransitPoint, _waypoint.Move.RandomRange, (int)(_index + 1)); EditorGUILayout.Separator(); } EditorGUI.EndDisabledGroup(); EditorGUI.indentLevel--; } // CONTENT END return(false); }
private static void DrawMissionPatrolWaypoint(ICECreatureControl _control, int _index) { ICEEditorStyle.SplitterByIndent(EditorGUI.indentLevel + 1); WaypointObject _waypoint = _control.Creature.Missions.Patrol.Waypoints.Waypoints[_index]; if (_waypoint == null) { return; } // HEADER BEGIN ICEEditorLayout.BeginHorizontal(); _waypoint.Enabled = EditorGUILayout.ToggleLeft("Waypoint #" + (int)(_index + 1), _waypoint.Enabled, EditorStyles.boldLabel); GUILayout.FlexibleSpace(); if (ICEEditorLayout.ButtonUp()) { WaypointObject _wp = _control.Creature.Missions.Patrol.Waypoints.Waypoints[_index]; _control.Creature.Missions.Patrol.Waypoints.Waypoints.RemoveAt(_index); if (_index - 1 < 0) { _control.Creature.Missions.Patrol.Waypoints.Waypoints.Add(_wp); } else { _control.Creature.Missions.Patrol.Waypoints.Waypoints.Insert(_index - 1, _wp); } return; } if (ICEEditorLayout.ButtonDown()) { WaypointObject _wp = _control.Creature.Missions.Patrol.Waypoints.Waypoints[_index]; _control.Creature.Missions.Patrol.Waypoints.Waypoints.RemoveAt(_index); if (_index + 1 > _control.Creature.Missions.Patrol.Waypoints.Waypoints.Count) { _control.Creature.Missions.Patrol.Waypoints.Waypoints.Insert(0, _wp); } else { _control.Creature.Missions.Patrol.Waypoints.Waypoints.Insert(_index + 1, _wp); } return; } if (ICEEditorLayout.ButtonCloseDouble()) { _control.Creature.Missions.Patrol.Waypoints.Waypoints.RemoveAt(_index); --_index; } ICEEditorLayout.EndHorizontal(Info.MISSION_PATROL_WAYPOINT); // HEADER END // CONTENT BEGIN EditorGUI.BeginDisabledGroup(_waypoint.Enabled == false); EditorGUI.indentLevel++; _waypoint = (WaypointObject)EditorSharedTools.DrawTarget(_control, (TargetObject)_waypoint, "Target", Info.MISSION_PATROL_TARGET); EditorGUILayout.Separator(); _waypoint.UseCustomBehaviour = ICEEditorLayout.Toggle("Custom Behaviour", "", _waypoint.UseCustomBehaviour, Info.MISSION_PATROL_CUSTOM_BEHAVIOUR); EditorGUILayout.Separator(); if (_waypoint.UseCustomBehaviour) { EditorGUI.indentLevel++; _waypoint.BehaviourModeTravel = EditorBehaviour.BehaviourSelect(_control, "Travel", "Travel behaviour to reach this waypoint and to start this mission", _waypoint.BehaviourModeTravel, "WP_TRAVEL_" + (int)(_index + 1)); _waypoint.BehaviourModePatrol = EditorBehaviour.BehaviourSelect(_control, "Patrol", "Patrol behaviour to reach this waypoint", _waypoint.BehaviourModePatrol, "WP_PATROL_" + (int)(_index + 1)); EditorSharedTools.DrawInRangeBehaviour(_control, ref _waypoint.BehaviourModeLeisure, ref _waypoint.BehaviourModeRendezvous, ref _waypoint.DurationOfStay, ref _waypoint.IsTransitPoint, _waypoint.TargetRandomRange, (int)(_index + 1)); /* * EditorGUI.BeginDisabledGroup( _waypoint.IsTransitPoint == true ); * * _waypoint.BehaviourModeLeisure = EditorBehaviour.BehaviourSelect( m_creature_control, "Leisure", "Leisure activities after reaching this waypoint range", _waypoint.BehaviourModeLeisure, "WP_LEISURE_" + _index ); * _waypoint.BehaviourModeRendezvous = EditorBehaviour.BehaviourSelect( m_creature_control, "Rendezvous", "Action behaviour after reaching this current target move position", _waypoint.BehaviourModeRendezvous, "WP_RENDEZVOUS_" + _index ); * EditorGUI.EndDisabledGroup(); * * _waypoint.DurationOfStay = ICEEditorLayout.DurationSlider( "Duration Of Stay", "Duration of stay", _waypoint.DurationOfStay, Init.DURATION_OF_STAY_STEP, Init.DURATION_OF_STAY_MIN, Init.DURATION_OF_STAY_MAX,Init.DURATION_OF_STAY_DEFAULT, ref _waypoint.IsTransitPoint ); * */ EditorGUI.indentLevel--; EditorGUILayout.Separator(); } EditorGUI.indentLevel--; EditorGUI.EndDisabledGroup(); // CONTENT END }
// Helper Function to navigate to waypoint from the editor public void NavigateToWaypointHelper(WaypointObject waypoint) { NavigateToWaypoint(waypoint); }