protected override Vector3 GetFastDirection() { if (destination == null) { destination = GetClosestHidingSpot(); } return(destination.transform.position - transform.position); }
/// <summary> /// Leaves a hiding spot /// </summary> public void leave() { if (hidden) { currentHidingSpot.leave(this.gameObject); currentHidingSpot = null; } }
/// <summary> /// Enters the specified hiding spot if in range /// </summary> /// <param name="hidingSpot">The hiding spot to hide in</param> public void hide(HidingSpot hidingSpot) { if (nearbyHidingSpots.Contains(hidingSpot)) { hidingSpot.hide(this.gameObject); currentHidingSpot = hidingSpot; } }
/// <summary> /// If the other collider is a hiding spot, adds it to the nearbyHidingSpots list /// </summary> /// <param name="other">The colliding object</param> private void OnTriggerEnter(Collider other) { HidingSpot hidingSpot = other.transform.GetComponent <HidingSpot>(); if (hidingSpot != null) { nearbyHidingSpots.Add(hidingSpot); } }
/// <summary> /// Enters the closest hiding spot if there is one in range /// </summary> public void hide() { HidingSpot hidingSpot = getClosestHidingSpot(); if (hidingSpot != null) { hidingSpot.hide(this.gameObject); currentHidingSpot = hidingSpot; } }
/// <summary> /// If the other collider is a hiding spot, removes it from the nearbyHidingSpots list /// </summary> /// <param name="other">The colliding object</param> private void OnTriggerExit(Collider other) { HidingSpot hidingSpot = other.transform.GetComponent <HidingSpot>(); if (hidingSpot != null) { if (nearbyHidingSpots.Contains(hidingSpot)) { nearbyHidingSpots.Remove(hidingSpot); } } }
/// <summary> /// Get the DecisionNode that contains the hidingSpot, if the HidingSpot don't exist in the tree it will reutnr null. /// </summary> /// <param name="hidingSpot"> The HidingSpot that is containd in the returning DecisionNode</param> /// <returns> The DecisionNode that contains hidingSpot if one exists, otherwise returns null </returns> public DecisionNode GetDecisionNode(HidingSpot hidingSpot) { foreach (DecisionNode node in PlaceCreator.Instance.Tree.RootNode.Children) { DecisionNode Cnode = node.GetChild(hidingSpot); if (Cnode != null) { return(Cnode); } } return(null); }
/// <summary> /// Static method to create children of a DecisionNode /// </summary> /// <param name="parent"> The parent of the new node</param> /// <param name="spot"> The HidingSpot that is going to be represented by the DecisionNode</param> /// <param name="type">What type is the new DecisionNod</param> /// <returns> Returns the new DecisionNode, if the parent is null it will return null</returns> public static DecisionNode CreateChild(DecisionNode parent, HidingSpot spot, TypeOfObject type) { DecisionNode newNode = new DecisionNode(parent, spot, type); if (parent != null) { if (parent.Children == null) { parent.Children = new List <DecisionNode>(); } parent.Children.Add(newNode); return(newNode); } return(null); }
/// <summary> /// Get the child DecisionNode that wraps the desired HidingSpot /// </summary> /// <param name="hidingSpot"> The Hidingspot that is reprecented by the desired DecisionNode </param> /// <returns> Reutrns the DecisionNode that wraps the hidingspot, if the hidingSpot is not a child of the DecisionNode it will return null</returns> public DecisionNode GetChild(HidingSpot hidingSpot) { if (Children != null) { if (Children.Count != 0) { foreach (DecisionNode node in Children) { if (node.Spot == hidingSpot) { return(node); } } } } return(null); }
/// <summary> /// Finds the closest hiding spot of nearby hiding spots /// </summary> /// <returns>The closest hiding spot to this agent</returns> public HidingSpot getClosestHidingSpot() { float closestDistance = Mathf.Infinity; HidingSpot closestHidingSpot = null; for (int i = 0; i < nearbyHidingSpots.Count; i++) { Vector3 distanceVector = nearbyHidingSpots[i].transform.position - transform.position; float distance = distanceVector.magnitude; if (distance < closestDistance) { closestDistance = distance; closestHidingSpot = nearbyHidingSpots[i]; } } return(closestHidingSpot); }
// Update is called once per frame void Update() { if (waitTime > 0.0f && currentWaitTime < waitTime) // If the agent needs to wait and has not yet finished waiting, { currentWaitTime += Time.deltaTime; // Increase time return; } if (completed) { return; // Return early if demo completed } if (hideBehaviour.getNearbyHidingSpots().Count == 0) // If no hiding spots are within range, move to location { directionVector = hidingSpotTransform.position - transform.position; } else if (!completed) // If in range { HidingSpot hidingSpot = hideBehaviour.getClosestHidingSpot(); GameObject foundTarget; if (target == null) // If no target { foundTarget = hidingSpot.search(); // Search the hiding spot for the latest entrant } else { foundTarget = hidingSpot.search(target); // Search the hiding spot for the target } if (foundTarget != null) // If a target has been found { hidingSpot.forceLeave(foundTarget); // Force the target to leave completed = true; } } base.Update(); }
/// <summary> /// Creates a new DecisionTree and sets it to the PlaceCreator.Tree /// </summary> public void CreateDecisionTree() { HidingSpots = new List <HidingSpot>(); Tree = new DecisionTree(); foreach (BoxCollider place in places) { HidingSpot placeSpot = place.transform.GetComponent <HidingSpot>(); placeSpot.EnableUI(); placeSpot.ID = placeSpot.name.GetHashCode(); DecisionNode node = DecisionNode.CreateChild(Tree.RootNode, placeSpot, TypeOfObject.PLACE); Collider[] hidingSpots = Physics.OverlapBox(place.bounds.center, place.bounds.size / 2, Quaternion.identity, HidingLayer); foreach (Collider spot in hidingSpots) { HidingSpot hSpot = spot.GetComponent <HidingSpot>(); hSpot.ID = hSpot.transform.position.GetHashCode(); DecisionNode.CreateChild(node, hSpot, TypeOfObject.PLACE); hSpot.EnableUI(); HidingSpots.Add(hSpot); } } }
/// <summary> /// The DecisioNode constructor is only used to create the root node in the tree /// </summary> /// <param name="parent"> The parent of the new node </param> /// <param name="spot"> The HidingSpot that is going to be represented by the DecisionNode</param> /// <param name="type"> What type is the new DecisionNode</param> public DecisionNode(DecisionNode parent, HidingSpot spot, TypeOfObject type) { Parent = parent; Spot = spot; Type = type; }
public override void ResetBehavior() { base.ResetBehavior(); destination = null; }