Пример #1
0
    public void Routine()
    {
        if (ReferenceEquals(_constructionBehaviour, null))
        {
            CancelInvoke();
        }

        _isValid = false;
        if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out _hit, Mathf.Infinity, _layerMask))
        {
            _constructionBehaviour.transform.position = MapUtils.GetMapPositionFromWorldPosition(_hit.point);
            _isValid = _constructionBehaviour.Constructable.IsPlacementValid();
        }

        // Rotate
        if (Input.GetKeyUp(KeyCode.R))
        {
            _constructionBehaviour.Rotate();
        }

        if (Input.GetMouseButtonUp(0))
        {
            if (_isValid)
            {
                _constructionBehaviour.StartConstruction();
                StopPlacement();
            }
            else
            {
                Debug.Log("Placement is impossible.");
            }
        }
        else if (Input.GetMouseButtonUp(1))
        {
            CancelPlacement();
        }
    }
Пример #2
0
            /// <summary>
            /// Place an object unto the map based on the available areas.
            /// </summary>
            /// <param name="constructionData"></param>
            public void Construct(ConstructionData constructionData)
            {
                ConstructionBehaviour constructionBehaviour = Foreman.CreateConstructionBehaviour(constructionData);
                ObjectBehaviour       objectBehaviour       = constructionBehaviour.GetComponent <ObjectBehaviour>();

                objectBehaviour.Initialize();

                bool        placed    = false;
                List <Area> splitArea = new List <Area>();
                int         count     = _availableAreas.Count;

                while (count > 0)
                {
                    // Generate a random index to check if the areas is ok.
                    int i = Random.Range(0, _availableAreas.Count);
                    if (_availableAreas[i].ObstacleCanFitInTheArea(objectBehaviour.Obstacle, out bool rotate))
                    {
                        Debug.Log("The obstacle " + objectBehaviour.Obstacle.Size + " can fit in: " + _availableAreas[i] + " with rotation: " + rotate);

                        // If the Obstacle fits if the area is rotated, rotate it.
                        if (rotate)
                        {
                            constructionBehaviour.Rotate();
                        }

                        // Remove the obstacle from the area.
                        if (Area.RemoveObstacleFromArea(_availableAreas[i], objectBehaviour.Obstacle, ref splitArea))
                        {
                            Vector3 origin    = Map.GetMapOriginInWorldPos(_encampment.Map, _encampment.transform.position);
                            Vector3 objectPos = new Vector3(_availableAreas[i].Origin.x, 0, _availableAreas[i].Origin.y);
                            Vector3 pos       = objectPos + origin + (new Vector3(_availableAreas[i].Size.x, 0, _availableAreas[i].Size.y) / 2);

                            // TODO get height at position on terrain.
                            constructionBehaviour.transform.position = pos;

                            if (!_encampment.Map.IsPositionValid(objectBehaviour.Obstacle, _encampment))
                            {
                                break;
                            }


                            objectBehaviour.RegisterZone(_encampment);
                            constructionBehaviour.StartConstruction();

                            _availableAreas.Remove(_availableAreas[i]);
                            RegisterAreas(splitArea);
                            placed = true;
                            break;
                        }
                        else
                        {
                            throw new UnityException("It should work.");
                        }
                    }
                    else
                    {
                        Debug.Log("The obstacle " + objectBehaviour.Obstacle.Size + " cannot fit in: " + _availableAreas[i]);
                    }
                    count--;
                }

                if (!placed)
                {
                    Destroy(constructionBehaviour.gameObject);
                }
            }