/// <summary> /// Add the specified targetable. /// Performs some checks to make sure the targetable is valid for the Area. /// </summary> /// <param name='targetable'> /// If set to <c>true</c> targetable. /// </param> public void Add(Targetable targetable) { // Get a target struct which will also cache information, such as the Transfrom, // GameObject and ITargetable component // Note this uses the targetable overload which has no component lookups so this // is light. var target = new Target(targetable, this.targetTracker); // Run the main Add overload. this.Add(target); }
// Init by copy public Target(Target otherTarget) { this.gameObject = otherTarget.gameObject; this.transform = otherTarget.transform; this.targetable = otherTarget.targetable; this.targetTracker = otherTarget.targetTracker; this.fireController = otherTarget.fireController; this.eventTrigger = otherTarget.eventTrigger; this.collider = otherTarget.collider; this.collider2D = otherTarget.collider2D; }
/// <summary> /// Use like List<Targetable>.Remove to remove the passed targetable from the ignore /// list and add it to the TargetTracker /// </summary> /// <param name='targetable'> /// Targetable. /// </param> public void Remove(Targetable targetable) { if (targetable == null) { return; } // Does nothing if there is nothing to remove. this._ignoreList.Remove(targetable); // Don't affect the TargetTracker if this is disabled. If disabled, all // are added back to the Area anyway and OnEnable only Remove is done. if (this.enabled && this.tracker.area != null) { // The TargetTracker will handle preventing multiples from being added this.tracker.area.Add(targetable); } }
/// <summary> /// Uses a physics test based on the largest bounds dimension to see if the targetable's /// collider is in range of this Area. /// This has to use the largest dimension for non-uniform sized colliders. It avoids /// situations where the object is inside but not added. the check is still a radius /// though, so a long object that is oriented in a way that doesn't cross in to the /// Area will still return true. /// </summary> /// <returns> /// <param name='targetable'> /// Targetable. /// </param> public bool IsInRange(Targetable targetable) { if (this.coll != null) { Vector3 size = this.coll.bounds.size; float testSize = Mathf.Max(Mathf.Max(size.x, size.y), size.z); var colls = new List <Collider>(Physics.OverlapSphere(this.transform.position, testSize)); return(colls.Contains(this.coll)); } else if (this.coll2D != null) { var coll2Ds = new List <Collider2D>(); var box2d = this.coll2D as BoxCollider2D; if (box2d != null) { var pos2D = new Vector2(this.transform.position.x, this.transform.position.y); Vector2 worldPos2D = box2d.offset + pos2D; Vector2 extents = box2d.size * 0.5f; var pntA = worldPos2D + extents; var pntB = worldPos2D - extents; coll2Ds.AddRange(Physics2D.OverlapAreaAll(pntA, pntB)); } else { var circ2D = this.coll2D as CircleCollider2D; if (circ2D != null) { coll2Ds.AddRange ( Physics2D.OverlapCircleAll(this.transform.position, circ2D.radius * 2) ); } } return(coll2Ds.Contains(this.coll2D)); } Debug.Log("IsInRange returning false due to no collider set. This may be fine."); return(false); }
/// <summary> /// Use like List<Targetable>.Add() to add the passed targetable to the ignore /// list and remove it from the TargetTracker /// </summary> /// <param name='targetable'> /// Targetable. /// </param> public void Add(Targetable targetable) { if (targetable == null) { return; } // Only add this once. if (!this._ignoreList.Contains(targetable)) { this._ignoreList.Add(targetable); } // Don't affect the TargetTracker if this is disabled. It will sync OnEnable if (this.enabled && this.tracker != null && this.tracker.area != null && targetable.trackers.Contains(this.tracker)) { // Removing multiple times doesn't hurt and lets the inspector use this. this.tracker.area.Remove(targetable); } }
/// <summary> /// Initializes a new instance of the <see cref="PoolUtil.Target"/> struct. /// This is the most efficient constructor because it just stores references to /// caches that the Targetable already holds. /// </summary> /// <param name='targetable'> /// Targetable. /// </param> /// <param name='targetTracker'> /// Target tracker that detected the targetable. /// </param> public Target(Targetable targetable, TargetTracker targetTracker) { this.gameObject = targetable.go; this.transform = targetable.transform; this.targetable = targetable; this.targetTracker = targetTracker; // The targetTracker arg could also be serived type. If it is. populate more. // Also handle colliders to make the struct easier to use when trying to figure // out what collider triggered the OnHit event. this.eventTrigger = null; this.collider = null; this.collider2D = null; this.eventTrigger = targetTracker as EventTrigger; if (this.eventTrigger != null) { this.collider = this.eventTrigger.coll; this.collider2D = this.eventTrigger.coll2D; } this.fireController = null; }
/// <summary> /// Remove a Targetable /// </summary> /// <param name="xform">The transform component of the target to remove</param> /// <returns>True if somethign was removed</returns> public bool Remove(Targetable targetable) { return(this.Remove(new Target(targetable, this.targetTracker))); }