IEnumerator Idle() { var duration = Random.Range(0.5f, 0.7f); yield return(new WaitForSeconds(duration)); var dir = HushPuppy.GenerateValidPosition( () => new Vector3( Random.Range(-distance, distance), Random.Range(-distance, distance)), (vec) => { var hit = Physics2D.BoxCast( this.transform.position, Vector2.one, 0f, vec.normalized, vec.magnitude, 1 << LayerMask.NameToLayer("Walls") | (1 << LayerMask.NameToLayer("Stairs")) | TilemapHelper.GetLayerMaskCreatureCollision(this.currFloorLevel) ); var outOfBounds = !TilemapHelper.InsideSameRoom( this.transform.position, this.transform.position + vec, RoomManager.Get().dimensions ); return(!hit && !outOfBounds); }); this.target = this.transform.position + dir; }
void UseBottle() { Bounds bounds = new Bounds(this.transform.position + movement.lastDirection, Vector2.one * 0.5f); var hits = Physics2D.OverlapAreaAll(bounds.min, bounds.max); if (this.bottled == null || this.bottled.sprite == null) { foreach (var hit in hits) { var bottlable = hit.transform.GetComponent <Bottlable>(); if (bottlable != null) { this.bottled = new BottlableData( bottlable.data.prefabName, bottlable.data.sprite ); inventory.ChangeBottleIcon(bottled.sprite); Destroy(hit.transform.gameObject); bottleUseIn.Invoke(); return; } } itemUseError.Invoke(); } else { List <Vector3> directions = new List <Vector3>() { movement.lastDirection }; if (movement.lastDirection == Vector3.left) { directions.Add(new Vector2(-1, 1)); directions.Add(new Vector2(-1, -1)); } else if (movement.lastDirection == Vector3.right) { directions.Add(new Vector2(1, 1)); directions.Add(new Vector2(1, -1)); } foreach (var dir in directions) { var nextTile = TilemapHelper.TilePosInFrontOfObj(this.gameObject, dir); Bounds tileBounds = new Bounds(nextTile, Vector2.one * 1.5f); var tileHits = Physics2D.OverlapAreaAll(tileBounds.min, tileBounds.max, 1 << LayerMask.NameToLayer("Walls") | (1 << LayerMask.NameToLayer("Stairs")) | (TilemapHelper.GetLayerMaskCreatureCollision(flooring.currentLevel))); int k = 0; foreach (var hit in tileHits) { if (!hit.CompareTag("Player")) { k++; } } if (k == 0) { var obj = Instantiate( Resources.Load( "Prefabs/Creatures/" + bottled.prefabName, typeof(GameObject)) as GameObject, this.transform.position, Quaternion.identity, this.transform.parent); obj.transform.localPosition = nextTile; bottled = null; inventory.ChangeBottleIcon(null); bottleUseOut.Invoke(); return; } } itemUseError.Invoke(); } }