public void AddLaserReceiver(ILaserReceiver receiver) { if (receivers.Contains(receiver)) { return; } receivers.Add(receiver); }
/// <summary> /// Determines the end point of this Laser beam. /// The Laser beam can only be drawn after this method is invoked. /// Also, until this method is invoked, the value of the <c>endpoint</c> /// parameter is undefined. /// </summary> public void Create() { HitEventArgs args = this.DoRaycast(); ILaserReceiver receiver = args.Receiver; if (receiver != null) { receiver.OnLaserHit(this, args); } }
/// <summary> /// Initializes a new instance of the <see cref="HitEventArgs"/> class. /// </summary> /// <param name="laser">The Laser beam that Hit the object, not null.</param> /// <param name="point">The position where the Laser Hit the object.</param> /// <param name="normal">The normal of the surface that the Laser beam Hit.</param> /// <param name="receiver">The ILaserReceiver that got Hit.</param> public HitEventArgs(LaserBeam laser, Vector3 point, Vector3 normal, ILaserReceiver receiver) { if (laser == null) { throw new ArgumentNullException("laser"); } this.Laser = laser; this.Point = point; this.Normal = normal; this.Receiver = receiver; }
public void Cast(Vector3 from, Vector3 direction, Transform parent) { List <Vector3> positions = new List <Vector3>(); ILaserReflector reflector = null; ILaserReceiver receiver = null; lineRenderer.startWidth = rayWidth * Random.Range(1.0f, 1.5f); lineRenderer.endWidth = rayWidth * Random.Range(1.0f, 1.5f); positions.Add(from); RaycastHit hitInfo; int layerMask = 1 << LayerMask.NameToLayer("LaserLayer"); if (Physics.Raycast(new Ray(from, direction), out hitInfo, 20.0f, layerMask)) { positions.Add(hitInfo.point); // Collider is a child of main object reflector = hitInfo.transform.parent.GetComponent <ILaserReflector>(); receiver = hitInfo.transform.parent.GetComponent <ILaserReceiver>(); } else { positions.Add(from + direction * 20.0f); // This should not happen } lineRenderer.SetPositions(positions.ToArray()); if (reflector != null) { Vector3[] refletedFrom = null; Vector3[] reflectedDirection = null; reflector.Reflect(hitInfo.point, direction, hitInfo.normal, out refletedFrom, out reflectedDirection); if (refletedFrom != null) { for (int i = 0; i < refletedFrom.Length; i++) { LaserManager.Instance.CastLaser(refletedFrom[i], reflectedDirection[i], parent); } } } if (receiver != null) { LaserManager.Instance.AddLaserReceiver(receiver); } }
/// <summary> /// Returns a valid ILaserReceiver from the given <c>RaycastHit</c>. /// <para> /// An ILaserReceiver is valid if the argument <c>RaycastHit</c> is not null /// and the Collider of the hit has an ILaseReceiver Component that /// also extends <c>MonoBehaviour</c>. Furthermore, the <c>MonoBehaviour</c> needs /// to be enabled to be valid. /// </para> /// </summary> /// <returns>The ILaserReceiver, or null if no valid ILaserReceiver is found.</returns> /// <param name="hit">The <c>RaycastHit</c>.</param> private static ILaserReceiver GetValidReceiver(RaycastHit hit) { if (hit.collider == null) { return(null); } ILaserReceiver receiver = hit.collider.GetComponent <ILaserReceiver>(); MonoBehaviour script = receiver as MonoBehaviour; if (script == null || !script.enabled) { return(null); } return(receiver); }
/// <summary> /// Finds the Receiver this Laser collided with. /// </summary> /// <returns>The HitEventArgs containing details about the collision.</returns> public HitEventArgs DoRaycast() { RaycastHit hitInfo = new RaycastHit(); bool hit = Physics.Raycast(this.Origin, this.Direction, out hitInfo, MaxRaycastDist); if (hit) { ILaserReceiver receiver = GetValidReceiver(hitInfo); this.Endpoint = hitInfo.point; this.emitter.AddLaser(this); return(new HitEventArgs(this, hitInfo.point, hitInfo.normal, receiver)); } else { this.Endpoint = this.Origin + (MaxLaserLength * this.Direction); this.Emitter.AddLaser(this); return(new HitEventArgs()); } }
public void RemoveLaserReceiver(ILaserReceiver receiver) { receivers.Remove(receiver); }