/// <summary> /// Listen for a character. /// </summary> virtual protected bool Hear() { Character character = null; ICharacterReference characterRef = null; if (hearingRadius > 0.0f) { Vector2 offset = (Vector2)transform.position + (FacingDirection == -1 ? new Vector2(-hearingOffset.x, hearingOffset.y) : hearingOffset); int hits = Physics2D.OverlapCircleNonAlloc(offset, hearingRadius, proximityColliders, hearingLayers); if (hits > 0) { // Always pick first target in list characterRef = (ICharacterReference)proximityColliders[0].gameObject.GetComponent(typeof(ICharacterReference)); if (characterRef == null) { character = proximityColliders[0].gameObject.GetComponent <Character>(); } else { character = characterRef.Character; } currentTarget = character; currentTargetHealth = character.GetComponent <CharacterHealth>(); return(true); } } return(false); }
/// <summary> /// The sense routine used to detect when something changes. In this /// case cast a ray from the transform in the facing direction to look for the player. /// </summary> override public bool Sense() { if (hideStateTimer <= 0.0f) { RaycastHit2D hit = Physics2D.Raycast(myTransform.position + offset, new Vector3(enemy.LastFacedDirection, 0, 0), sightDistance, sightLayers); if (hit.collider != null) { Character character = null; ICharacterReference characterRef = (ICharacterReference)hit.collider.gameObject.GetComponent(typeof(ICharacterReference)); if (characterRef == null) { character = hit.collider.gameObject.GetComponent <Character>(); } else { character = characterRef.Character; } if (character != null) { shootStateTimer = shootStateTime; CurrentTarget = character; return(true); } } } CurrentTarget = null; return(false); }
/// <summary> /// Look for a character. /// </summary> virtual protected bool See() { Character character = null; ICharacterReference characterRef = null; if (sightDistance > 0.0f) { Vector3 offset = new Vector3(0, sightYOffset, 0); RaycastHit2D hit = Physics2D.Raycast(myTransform.position + offset, new Vector3(LastFacedDirection, 0, 0), sightDistance, sightLayers); if (hit.collider != null) { characterRef = (ICharacterReference)hit.collider.gameObject.GetComponent(typeof(ICharacterReference)); if (characterRef == null) { character = hit.collider.gameObject.GetComponent <Character>(); } else { character = characterRef.Character; } if (character != null) { currentTarget = character; currentTargetHealth = character.GetComponent <CharacterHealth>(); return(true); } } } return(false); }
/// <summary> /// Check if the collection is valid (in this case by checking if its a character colliding). /// </summary> /// <param name="other">Other.</param> virtual protected void CheckCollect(Collider2D other) { ICharacterReference collectBox = (ICharacterReference)other.gameObject.GetComponent(typeof(ICharacterReference)); // Got a collect box and its not ourselves if (collectBox != null && !hasHitCharacter) { hasHitCharacter = true; DoCollect(collectBox.Character); } }
/// <summary> /// The sense routine used to detect when something changes. In this /// case cast a ray from the transform in the facing direction to look for the player. And cast a /// circle from the circle offset to "sense" the player. /// </summary> override public bool Sense() { Character character = null; ICharacterReference characterRef = null; if (sightDistance > 0.0f) { RaycastHit2D hit = Physics2D.Raycast(myTransform.position + offset, new Vector3(enemy.LastFacedDirection, 0, 0), sightDistance, sightLayers); if (hit.collider != null) { characterRef = (ICharacterReference)hit.collider.gameObject.GetComponent(typeof(ICharacterReference)); if (characterRef == null) { character = hit.collider.gameObject.GetComponent <Character>(); } else { character = characterRef.Character; } if (character != null) { shootStateTimer = shootStateTime; CurrentTarget = character; return(true); } } } if (senseRadius > 0.0f) { Vector2 offset = (Vector2)transform.position + (enemy.FacingDirection == -1 ? new Vector2(-senseOffset.x, senseOffset.y) : senseOffset); int hits = Physics2D.OverlapCircleNonAlloc(offset, senseRadius, proximityColliders, senseLayers); if (hits > 0) { // Always pick first target in list characterRef = (ICharacterReference)proximityColliders[0].gameObject.GetComponent(typeof(ICharacterReference)); if (characterRef == null) { character = proximityColliders[0].gameObject.GetComponent <Character>(); } else { character = characterRef.Character; } CurrentTarget = character; shootStateTimer = shootStateTime; return(true); } } CurrentTarget = null; return(false); }
/// <summary> /// Calls a delegate method on each descendant sprite in this sprite. /// </summary> /// <param name="sp"></param> public void SpriteProc(SpriteProcessor sp) { foreach (Frame f in FrameList) { foreach (IDisplayListItem dli in f.DisplayList) { ICharacterReference cr = dli as ICharacterReference; if (cr != null) { Sprite child = cr.Character as Sprite; if (child != null) { sp(child); child.SpriteProc(sp); } } } } }
private ListSet <ICharacterReference> PopulateCharacterRefList(ListSet <ICharacterReference> refs) { foreach (Frame f in FrameList) { foreach (IDisplayListItem dli in f.DisplayList) { if (dli is ICharacterReference) { ICharacterReference cr = (ICharacterReference)dli; refs.Add(cr); if (cr.Character is Sprite && !refs.Contains(cr)) { ((Sprite)cr.Character).PopulateCharacterRefList(refs); } } } } return(refs); }
/// <summary> /// Checks for the player and updates state. /// </summary> virtual protected void CheckForPlayer() { RaycastHit2D hit = Physics2D.Raycast(transform.position, new Vector3(0, 1, 0), sightRange, sightLayers); if (hit.collider != null) { Character character = null; ICharacterReference characterRef = (ICharacterReference)hit.collider.gameObject.GetComponent(typeof(ICharacterReference)); if (characterRef == null) { character = hit.collider.gameObject.GetComponent <Character> (); } else { character = characterRef.Character; } if (character != null) { if (state == SnapperState.IDLE || !mustFullyRetract) { state = SnapperState.CHARGING; } } else { if (state == SnapperState.CHARGING && enemy.transform.position.y >= endingYPos) { state = SnapperState.RETRACTING; } } } else { if (state == SnapperState.CHARGING && enemy.transform.position.y >= endingYPos) { state = SnapperState.RETRACTING; } } }