using System; using UnityEngine; using Framework.Services; namespace Framework.Navigation.Platformer { /// A navigation target from and to which paths can be calculated public class Locator : ScriptComponent { /// Ground the game object is currently on or has most recently touched public Ground CurrentGround; /// Called when the component is loaded into a game object protected override void Awake() { base.Awake(); // Needed to filter colliders touched by the locator to only see things meant // for the navigation system. this.navigationLayer = LayerMask.NameToLayer("Navigation"); if(this.navigationLayer == -1) { Debug.LogWarning("No 'Navigation' layer defined. Locator will not work!"); } // If the locator is on a layer that is not configured to collide with // the navigation layer, then the locator cannot work! if(Physics.GetIgnoreLayerCollision(gameObject.layer, this.navigationLayer)) { string layerName = LayerMask.LayerToName(gameObject.layer); Debug.LogWarning( "Navigation target '" + gameObject.name + "' is on layer '" + layerName + "', " + "which ignores collisions with the 'Navigation' layer. Locator will not work!" ); } // If the locator has no collider, create one Collider collider = gameObject.GetComponent(); if(collider == null) { SphereCollider sphereCollider = gameObject.AddComponent(); sphereCollider.radius = 0.1f; sphereCollider.isTrigger = true; } } /// Called when another collider enters the navigation target /// Other collider that has made contact protected virtual void OnTriggerEnter(Collider other) { #if false if(transform.parent == null) { Debug.Log("Target " + name + " touched " + other.name); } else { Debug.Log("Target " + transform.parent.name + " touched " + other.name); } #endif int otherLayer = other.gameObject.layer; if(otherLayer == this.navigationLayer) { Transform otherTransform = other.transform; while(otherTransform != null) { Ground ground = otherTransform.GetComponent(); if(ground != null) { this.CurrentGround = ground; return; } otherTransform = otherTransform.parent; } } } /// Index of the layer the navigation colliders are on private int navigationLayer; } } // namespace Framework.Navigation.Platformer