Beispiel #1
0
    public static ConstructionBehaviour CreateConstructionBehaviour(ConstructionData constructionData)
    {
        ConstructionBehaviour constructionBehaviour = Instantiate(constructionData.Prefab).AddComponent <ConstructionBehaviour>();

        constructionBehaviour.Initialize(constructionData, constructionBehaviour.GetComponent <IConstructable>());
        return(constructionBehaviour);
    }
Beispiel #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);
                }
            }