Пример #1
0
    /**
     * <summary>
     * Watch for the item on top of the belt
     * </summary>
     */
    protected override void WatchForItem()
    {
        Bounds  bounds = this.transform.GetComponent <Collider2D>().bounds;
        Vector2 size   = bounds.size;

        Collider2D[] colliders = Physics2D.OverlapBoxAll(this.transform.position, size, 0);
        foreach (Collider2D collider in colliders)
        {
            if (collider.tag == Tags.Item)
            {
                Transform item       = collider.GetComponent <Transform>();
                Bounds    itemBounds = item.GetComponent <Collider2D>().bounds;
                Vector2   itemPoint  = new Vector2(itemBounds.min.x, itemBounds.max.y);

                // Do not move the item if the item point is not on the belt
                if (!bounds.Contains(itemPoint))
                {
                    continue;
                }

                ItemBehaviour itemBehaviour = item.GetComponent <ItemBehaviour>();
                itemBehaviour.direction = ItemDirection.Down;
                itemBehaviour.speed     = 5f;
                itemBehaviour.MoveDown();
            }
        }
    }
    /**
     * <summary>
     * Watch for the item on top of the belt
     * </summary>
     */
    protected override void WatchForItem()
    {
        Bounds  bounds = this.transform.GetComponent <Collider2D>().bounds;
        Vector2 size   = bounds.size;

        Collider2D[] colliders = Physics2D.OverlapBoxAll(this.transform.position, size, 0);
        foreach (Collider2D collider in colliders)
        {
            if (collider.tag == Tags.Item)
            {
                Transform     item          = collider.GetComponent <Transform>();
                ItemBehaviour itemBehaviour = item.GetComponent <ItemBehaviour>();

                Bounds  itemBounds = item.GetComponent <Collider2D>().bounds;
                Vector2 itemPoint  = new Vector2(itemBounds.min.x, itemBounds.max.y);

                // Always move when the item is exactly or more on top of the collider
                if (!bounds.Contains(itemPoint))
                {
                    continue;
                }

                // Determine the direction of the item it is moving.
                // If the item is heading up and it has not reached the origin point. Keep moving up.
                if (itemBehaviour.direction == ItemDirection.Up && item.position.y < this.transform.position.y)
                {
                    itemBehaviour.direction = ItemDirection.Up;
                    itemBehaviour.speed     = 5f;
                    itemBehaviour.MoveUp();
                }
                // If the item is heading down and it has not reached the origin point. Keep moving down.
                else if (itemBehaviour.direction == ItemDirection.Down && item.position.y > this.transform.position.y)
                {
                    itemBehaviour.direction = ItemDirection.Down;
                    itemBehaviour.speed     = 5f;
                    itemBehaviour.MoveDown();
                }
                // Move right
                else
                {
                    itemBehaviour.direction = ItemDirection.Right;
                    itemBehaviour.speed     = 5f;
                    itemBehaviour.MoveRight();
                }
            }
        }
    }