Ejemplo n.º 1
0
    public static List<Vector2> searchEmptySpace(Vector2 centerpos, ItemBasis src)
    {
        List<Vector2> space = new List<Vector2>();
        for (int y = 0; y < 5; y++)
        {
            for (int x = 0; x < 5; x++)
            {
                Vector2 target = centerpos + new Vector2(x - 2, y - 2);
                List<Collider2D> hits = new List<Collider2D>(Physics2D.OverlapPointAll(target, searchmask));
                if (hits.Count == 0) space.Add(target);
                if (hits.Count == 1)
                {
                    if (hits[0].transform.tag.Equals(TagList.Item) && src != null)
                    {
                        if (hits[0].gameObject.GetComponent<ItemEntity>().Object == src) space.Add(target);
                    }
                }
            }
        }
        space.Sort((a, b) => (int)((a - centerpos).sqrMagnitude - (b - centerpos).sqrMagnitude));
        //Debug.Log("SpaceL: " + space.Count);
        Debug.Log("Pos:" + centerpos);
        var c2d = Physics2D.OverlapPoint(centerpos, TagList.getLayerMask(TagList.Road));
        List<Vector2> ret = new List<Vector2>();
        if (c2d != null) {
            TileEntity srctile = c2d.gameObject.GetComponent<TileEntity>();

            foreach (Vector2 dst in space)
            {
                c2d = Physics2D.OverlapPoint(dst, TagList.getLayerMask(TagList.Road));
                //Debug.Log("C2D: "+c2d);
                TileEntity dsttile = c2d != null ? c2d.gameObject.GetComponent<TileEntity>() : null;
                //Debug.Log("Dst: " + dsttile);
                if (dsttile != null)
                {
                    AStarSearch astar = new AStarSearch(srctile, dsttile);
                    //Debug.Log(astar.reachableWith(3));
                    //Debug.Log("TatalCost: "+AStarSearch.getTotalCost(astar.EndNode));
                    if (astar.reachableWith(3)) ret.Add(dst);

                }
            }
            ret.Sort((a, b) => (int)((a - centerpos).sqrMagnitude - (b - centerpos).sqrMagnitude));
            //Debug.Log("RetL: "+ret.Count);
        }

        return ret;
    }