using System.Collections.Generic; using System.Linq; using NodeCanvas.Framework; using ParadoxNotion.Design; using UnityEngine; namespace NodeCanvas.Tasks.Conditions { [Category("GameObject")] [Description("A combination of line of sight and view angle check")] public class CanSeeTargetAny2D : ConditionTask { public BBParameter> targetObjects; public BBParameter maxDistance = 50; public BBParameter layerMask = (LayerMask)( -1 ); public BBParameter awarnessDistance = 0f; [SliderField(1, 180)] public BBParameter viewAngle = 70f; public Vector2 offset; [BlackboardOnly] public BBParameter> allResults; [BlackboardOnly] public BBParameter closerResult; protected override string info { get { return "Can See Any " + targetObjects; } } protected override bool OnCheck() { var r = false; var store = !allResults.isNone || !closerResult.isNone; var temp = store ? new List() : null; foreach ( var o in targetObjects.value ) { if ( o == agent.gameObject ) { continue; } var t = o.transform; if ( !t.gameObject.activeInHierarchy ) { continue; } if ( Vector2.Distance(agent.position, t.position) < awarnessDistance.value ) { var hit = Physics2D.Linecast((Vector2)agent.position + offset, (Vector2)t.position + offset, layerMask.value); if ( hit.collider != t.GetComponent() ) { continue; } if ( store ) { temp.Add(o); } r = true; continue; } if ( Vector2.Distance(agent.position, t.position) > maxDistance.value ) { continue; } if ( Vector2.Angle((Vector2)t.position - (Vector2)agent.position, agent.right) > viewAngle.value ) { continue; } var hit2 = Physics2D.Linecast((Vector2)agent.position + offset, (Vector2)t.position + offset, layerMask.value); if ( hit2.collider != t.GetComponent() ) { continue; } if ( store ) { temp.Add(o); } r = true; } if ( store ) { var ordered = temp.OrderBy(x => Vector3.Distance(agent.position, x.transform.position)); if ( !allResults.isNone ) { allResults.value = ordered.ToList(); } if ( !closerResult.isNone ) { closerResult.value = ordered.FirstOrDefault(); } } return r; } public override void OnDrawGizmosSelected() { if ( agent != null ) { Gizmos.DrawLine((Vector2)agent.position, (Vector2)agent.position + offset); Gizmos.DrawLine((Vector2)agent.position + offset, (Vector2)agent.position + offset + ( (Vector2)agent.right * maxDistance.value )); Gizmos.DrawWireSphere((Vector2)agent.position + offset + ( (Vector2)agent.right * maxDistance.value ), 0.1f); Gizmos.DrawWireSphere((Vector2)agent.position, awarnessDistance.value); Gizmos.matrix = Matrix4x4.TRS((Vector2)agent.position + offset, agent.rotation, Vector3.one); Gizmos.DrawFrustum(Vector3.zero, viewAngle.value, 5, 0, 1f); } } } }