Пример #1
0
    public Vector2 GetClosest(Vector2 point)
    {
        var newPoint = point - this.Position;

        int x = GetClosestColumn(newPoint.x);
        int y = GetClosestRow(newPoint.y);

        var             result     = points[x, y];
        IUnmoveableSpot unmoveable = result.GetComponent <UnmoveableSpot>();

        if (unmoveable != null)
        {
            float       minDistSq = float.MaxValue;
            PointEntity closest   = null;

            foreach (var other in unmoveable.ClosestPoints)
            {
                var sqDist = (point - other.Position).sqrMagnitude;
                if (sqDist < minDistSq)
                {
                    minDistSq = sqDist;
                    closest   = other;
                }
            }

            return(closest.Position);
        }

        return(result.Position);

        //return new Vector2((x - ((columns - 1) / 2)) * horizontalSpacing + transform.position.x, (y - ((rows - 1) / 2)) * verticalSpacing + transform.position.y);
    }
Пример #2
0
    protected override void OnInitialize()
    {
        base.OnInitialize();
        foreach (var point in this.Entities)
        {
            var pos = point.Position - this.Position;

            int x = GetClosestColumn(pos.x);
            int y = GetClosestRow(pos.y);

            if (points[x, y] != null)
            {
                throw new Exception("Duplicate stuff");
            }

            points[x, y] = point;

            IUnmoveableSpot unmoveable = point.GetComponent <UnmoveableSpot>();
            if (unmoveable != null)
            {
                if (unmoveable.ClosestPoints.Length == 0)
                {
                    throw new Exception("empty unmoveable");
                }
            }
        }

        for (int i = 0; i < 5; i++)
        {
            for (int j = 0; j < 3; j++)
            {
                if (points[i, j] == null)
                {
                    throw new Exception("Not enough stuff");
                }
            }
        }
    }