Exemple #1
0
    private void DequeuePlaceEvents()
    {
        while (placeQueue.Count > 0)
        {
            Vector3 origin = Camera.main.transform.position;

            Vector3 point = gridPlacement.DirectionTo2DGridPosition(origin, placeQueue.Dequeue());

            if (point.x > bounds.xMin && point.x < bounds.xMax &&
                point.z > bounds.zMin && point.z < bounds.zMax)
            {
                if (!ExistsAtPoint(point))
                {
                    placedObjects.Add(gridPlacement.SpawnAt2DGridPoint(point, prefabName));
                }
                else
                {
                    if (placeMode == PlaceMode.Vertical)
                    {
                        while (ExistsAtPoint(point))
                        {
                            point.y += gridPlacement.cellSize.y;
                        }

                        placedObjects.Add(gridPlacement.SpawnAtPoint(point, prefabName));
                    }
                }
            }
        }
    }
    public bool PlaceFromMouseDirection(string objName)
    {
        List <Vector2> cellsOccupied = new List <Vector2>();

        Vector3 continuousPosition = GetPointOnPlaneFromMouseDirection();

        Vector2 discretePosition = GetDiscretePositionFromPoint(continuousPosition);

        Vector2 dimensions = nameDimensionMap[objName];

        for (int i = (int)(discretePosition.x - dimensions.x);
             i < discretePosition.x + dimensions.x; ++i)
        {
            for (int k = (int)(discretePosition.y - dimensions.y);
                 k < discretePosition.y + dimensions.y; ++k)
            {
                Vector2 occupiedCell = new Vector2(i, k);

                cellsOccupied.Add(occupiedCell);
            }
        }

        foreach (Vector2 v in cellsOccupied)
        {
            if (discreteGrid.ContainsKey(v))
            {
                if (discreteGrid[v] != null)
                {
                    return(false);
                }
            }
            else
            {
                return(false);
            }
        }

        GameObject toPlace = gridPlacement.SpawnAtPoint(continuousPosition, objName);

        foreach (Vector2 v in cellsOccupied)
        {
            if (discreteGrid.ContainsKey(v))
            {
                discreteGrid[v] = toPlace;
            }
            else
            {
                return(false);
            }
        }

        return(true);
    }