コード例 #1
0
    void DestroyOrPlaceObject()
    {
        if (_currentPlaceableObject.IsDraggable)
        {
            for (int i = _draggedGameObjects.Count - 1; i >= 0; i--)
            {
                SimpleMapPlaceable previewObject = _draggedGameObjects[i].GetComponent <SimpleMapPlaceable>();
                if (!_placementController.PlaceObject(previewObject))
                {
                    GameObject.Destroy(previewObject.gameObject);
                }
            }
            _draggedGameObjects.Clear();
        }

        if (_currentPlaceableObject is ComplexMapPlaceable &&
            !_placementController.PlaceObject((ComplexMapPlaceable)_currentPlaceableObject))
        {
            GameObject.Destroy(_currentPlaceableObject.gameObject);
        }
        else if (_currentPlaceableObject is SimpleMapPlaceable &&
                 !_placementController.PlaceObject((SimpleMapPlaceable)_currentPlaceableObject))
        {
            GameObject.Destroy(_currentPlaceableObject.gameObject);
        }
    }
コード例 #2
0
    private void IsPlaceableFeedback()
    {
        if (_currentPlaceableObject is SimpleMapPlaceable placeable)
        {
            placeable.IsPlaceable =
                _placementController.IsPlaceable(placeable.transform.position, placeable.UsedCoordinates);
        }
        else if (_currentPlaceableObject is ComplexMapPlaceable complexMapPlaceable)
        {
            foreach (SimpleMapPlaceable simpleMapPlaceable in complexMapPlaceable.ChildMapPlaceables)
            {
                simpleMapPlaceable.IsPlaceable = _placementController.IsPlaceable(simpleMapPlaceable.transform.position,
                                                                                  simpleMapPlaceable.UsedCoordinates);
            }
        }

        foreach (MapPlaceable mapPlaceable in _draggedGameObjects)
        {
            SimpleMapPlaceable simpleMapPlaceable = mapPlaceable as SimpleMapPlaceable;
            if (simpleMapPlaceable)
            {
                mapPlaceable.IsPlaceable =
                    _placementController.IsPlaceable(mapPlaceable.transform.position,
                                                     simpleMapPlaceable.UsedCoordinates);
            }
        }
    }
コード例 #3
0
    public void OnPlaceableClick(SimpleMapPlaceable mapPlaceable)
    {
        Debug.Log("Placeable clicked: " + mapPlaceable.name);
        if (_routeCreationView && _routeCreationView.VisibleObject.activeSelf)
        {
            CityBuilding cityBuilding = mapPlaceable as CityBuilding;
            if (cityBuilding != null)
            {
                mapPlaceable = cityBuilding.CityPlaceable.MainBuilding;
            }

            _routeCreationView.StationManager.OnTransportStationClick((PathFindingNode)mapPlaceable);
            return;
        }

        ProductProcessorBehaviour productProcessorBehaviour = mapPlaceable.gameObject.GetComponent <ProductProcessorBehaviour>();

        if (productProcessorBehaviour)
        {
            _productProcessorView.ProductProcessorBehaviour = productProcessorBehaviour;
        }
        else if (mapPlaceable is ICityBuilding)
        {
            _cityView.CityBuilding = (ICityBuilding)mapPlaceable;
        }
        else if (mapPlaceable is AbstractStorageContainer)
        {
            _storageContainerView.StorageContainer = (AbstractStorageContainer)mapPlaceable;
        }
    }
コード例 #4
0
    /// <summary>
    /// Adds a MapPlaceable to the placedBuildingDictionary
    /// </summary>
    /// <param name="placedObject"></param>
    /// <returns></returns>
    public void AddMapPlaceable(SimpleMapPlaceable placedObject)
    {
        if (!placedObject)
        {
            return;
        }
        Vector3 placedPosition = TransformPosition(placedObject.transform.position);

        for (int i = 0; i < placedObject.UsedCoordinates.Count; i++)
        {
            Vector3 occupiedSpace = placedPosition + placedObject.UsedCoordinates[i].UsedCoordinate;

            if (!_placedBuildingDictionary.ContainsKey(occupiedSpace))
            {
                _placedBuildingDictionary.Add(occupiedSpace, placedObject);
//                Debug.Log("BuildingManager Added: " + placedObject.name + " at " + occupiedSpace);
            }
            else
            {
                // Remove all previously added entries
                for (int removeIndex = i; removeIndex > 0; removeIndex--)
                {
                    Vector3 removedSpace = TransformPosition(placedObject.transform.position) +
                                           placedObject.UsedCoordinates[removeIndex].UsedCoordinate;
                    _placedBuildingDictionary.Remove(removedSpace);
                }

                return;
            }
        }

//        placedObject.transform.position = placedPosition + (Vector3.one / 2f);
        placedObject.OnPlacement();
    }
コード例 #5
0
    /// <summary>
    /// Removes a MapPlaceable at the specified position.
    /// </summary>
    /// <param name="position"></param>
    /// <returns>The removed MapPlaceable. May be null</returns>
    public void RemoveMapPlaceable(Vector3 position)
    {
        Vector3            placedPosition = TransformPosition(position);
        SimpleMapPlaceable mapPlaceable   = _placedBuildingDictionary[placedPosition];

        if (mapPlaceable)
        {
            RemoveMapPlaceable(mapPlaceable);
        }
    }
コード例 #6
0
 public ThreadsafePlaceable(SimpleMapPlaceable mapPlaceable, Vector3 startPosition)
 {
     MapPlaceable  = mapPlaceable;
     _neededSpaces = new List <NeededSpace>();
     foreach (NeededSpace neededSpace in mapPlaceable.UsedCoordinates)
     {
         _neededSpaces.Add(new NeededSpace(neededSpace,
                                           Vector3Int.FloorToInt(mapPlaceable.transform.localPosition)));
     }
     _position = startPosition;
 }
コード例 #7
0
    // private int CountNeighbors(Street street)
    // {
    //     int count = 0;
    //     foreach (PathFindingNode neighborNode in street.NeighborNodes)
    //     {
    //         if (!neighborNode) continue;
    //         count++;
    //     }
    //
    //     return count;
    // }

    private SimpleMapPlaceable PlaceStreetAt(Vector3 position)
    {
        GameObject         gameObject         = Instantiate(_streetData.Prefab);
        SimpleMapPlaceable simpleMapPlaceable = gameObject.GetComponent <SimpleMapPlaceable>();

        gameObject.transform.position = position;
        simpleMapPlaceable.OnPlacement();
        _placementController.PlaceObject(simpleMapPlaceable);
        _cityPlaceable.ChildMapPlaceables.Add(simpleMapPlaceable);
        gameObject.transform.SetParent(transform);
        return(simpleMapPlaceable);
    }
コード例 #8
0
 private void RemoveMapPlaceable(SimpleMapPlaceable mapPlaceable)
 {
     foreach (NeededSpace usedCoordinate in mapPlaceable.UsedCoordinates)
     {
         Vector3 position      = TransformPosition(mapPlaceable.ThreadsafePosition);
         Vector3 occupiedSpace = position + usedCoordinate.UsedCoordinate;
         Debug.Log("Remove " + mapPlaceable.name + " at: " + occupiedSpace);
         if (!_placedBuildingDictionary.Remove(occupiedSpace))
         {
             Debug.LogError("Position was already empty. " + occupiedSpace.ToString());
         }
         else
         {
             Object.Destroy(mapPlaceable.gameObject);
         }
     }
 }
コード例 #9
0
    // Update is called once per frame
    void Update()
    {
        if (!DestructionActive || !Input.GetMouseButtonDown(0))
        {
            return;
        }

        Ray        ray = _mainCamera.ScreenPointToRay(Input.mousePosition);
        RaycastHit hitInfo;

        if (!Physics.Raycast(ray, out hitInfo, Mathf.Infinity, _buildingMask))
        {
            return;
        }

        SimpleMapPlaceable mapPlaceable = hitInfo.collider.gameObject.GetComponent <SimpleMapPlaceable>();

        if (!mapPlaceable)
        {
            return;
        }
        _buildingManager.RemoveMapPlaceable(mapPlaceable.transform.position);
    }
コード例 #10
0
    private void MoveObject(Transform movedTransform, Camera camera)
    {
        Ray ray = camera.ScreenPointToRay(Input.mousePosition);

        if (!Physics.Raycast(ray, out var hitInfo, Mathf.Infinity, _buildingsMask))
        {
            return;
        }
        _gridDisplayRenderer.sharedMaterial.SetVector("_Vector3_GridCullingPosition", hitInfo.point);
        hitInfo.point -= new Vector3(_offsetVec3.x, _offsetVec3.y, _offsetVec3.z);

        Vector3 currentPosition = _currentPlaceableObject.transform.position;
        Vector3 position        = _placementController.MoveObject(hitInfo.point, currentPosition, _offsetVec3);

        movedTransform.position = position;

        // Check position change
        if (!(Math.Abs(position.x - currentPosition.x) < 0.5f) &&
            !(Math.Abs(position.z - currentPosition.z) < 0.5f))
        {
            return;
        }

        SimpleMapPlaceable mapPlaceable    = _currentPlaceableObject.GetComponent <SimpleMapPlaceable>();
        Transform          objectTransform = _currentPlaceableObject.transform;

        if (!mapPlaceable || !mapPlaceable.IsDraggable || !_isDragging)
        {
            return;
        }

        if (_draggedGameObjects.Count == 0)
        {
            _draggedGameObjects.Add(GameObject.Instantiate(_currentPlaceableObject, objectTransform.position,
                                                           objectTransform.rotation));
        }
        else
        {
            Vector3 start      = _draggedGameObjects[0].transform.position;
            Vector3 end        = position;
            Vector3 difference = end - start;
            int     x          = Mathf.RoundToInt(difference.x);
            int     z          = Mathf.RoundToInt(difference.z);

            int neededCount = Mathf.Abs(x) + Mathf.Abs(z);

            // Remove Objects until neededCount is met
            while (_draggedGameObjects.Count >= 1 && _draggedGameObjects.Count > neededCount)
            {
                GameObject.Destroy(_draggedGameObjects[_draggedGameObjects.Count - 1].gameObject);
                _draggedGameObjects.RemoveAt(_draggedGameObjects.Count - 1);
            }

            // Add Objects until neededCount is met
            while (_draggedGameObjects.Count < neededCount)
            {
                _draggedGameObjects.Add(GameObject.Instantiate(_currentPlaceableObject,
                                                               objectTransform.position,
                                                               objectTransform.rotation));
            }

            for (int i = 1; i < _draggedGameObjects.Count; i++)
            {
                MapPlaceable draggedObject = _draggedGameObjects[i];
                if (x != 0)
                {
                    Vector3 direction = x > 0 ? Vector3.right : Vector3.left;
                    start += direction;
                    draggedObject.transform.position = start;
                    x = x > 0 ? x - 1 : x + 1;
                }
                else if (z != 0)
                {
                    Vector3 direction = z > 0 ? Vector3.forward : Vector3.back;
                    start += direction;
                    draggedObject.transform.position = start;
                    z = z > 0 ? z - 1 : z + 1;
                }
            }
        }
    }
コード例 #11
0
    private IEnumerator GenerateCity(int seed, int maxCityProgress, Vector2Int streetLengthMinMax, float buildingProbability)
    {
        List <SimpleMapPlaceable> endpoints    = new List <SimpleMapPlaceable>();
        SimpleMapPlaceable        mapPlaceable = GetComponentInChildren <SimpleMapPlaceable>();

        endpoints.Add(mapPlaceable);
        System.Random random = new System.Random(seed);

        float waitTime = 0.0f;
        int   progress = 0;

        while (progress < maxCityProgress)
        {
            progress++;
            if (endpoints.Count == 0)
            {
                break;
            }
            Street street = (Street)endpoints[0];
            endpoints.RemoveAt(0);
            for (int i = 0; i < street.NeighborNodes.Length; i++)
            {
                if (street.NeighborNodes[i])
                {
                    continue;
                }

                Vector3 directionVec                 = Util.DirectionIntToVector(i);
                Vector3 directionClockwiseVec        = Util.DirectionIntToVector((i + 1) % 4);
                Vector3 directionCounterClockwiseVec = Util.DirectionIntToVector((i + 3) % 4);

                int amount = random.Next(streetLengthMinMax.x, streetLengthMinMax.y);
                for (int j = 1; j < amount + 1; j++)
                {
                    Vector3 placedPosition = street.transform.position + (directionVec * j);

                    if (IsRasterizing(placedPosition) || !_placementController.IsPlaceable(placedPosition, street.UsedCoordinates))
                    {
                        break;
                    }
                    SimpleMapPlaceable placeable = PlaceStreetAt(placedPosition);
                    yield return(new WaitForSeconds(waitTime));

                    if (j < amount)
                    {
                        if (random.NextDouble() > buildingProbability)
                        {
                            continue;
                        }

                        List <NeededSpace> neededSpaces = new List <NeededSpace>()
                        {
                            NeededSpace.Zero(TerrainGenerator.TerrainType.Flatland)
                        };

                        bool isClockwisePlaceable        = _placementController.IsPlaceable(placedPosition + directionClockwiseVec, neededSpaces);
                        bool isCounterClockwisePlaceable = _placementController.IsPlaceable(placedPosition + directionCounterClockwiseVec, neededSpaces);

                        Vector3 buildingStartPosition;
                        Vector3 streetDirection;

                        if (isClockwisePlaceable)
                        {
                            buildingStartPosition = placedPosition + directionClockwiseVec;
                            streetDirection       = directionCounterClockwiseVec;
                        }
                        else if (isCounterClockwisePlaceable)
                        {
                            buildingStartPosition = placedPosition + directionCounterClockwiseVec;
                            streetDirection       = directionClockwiseVec;
                        }
                        else
                        {
                            continue;
                        }
                        GameObject building = new GameObject("Procedural Building");
                        building.transform.position = buildingStartPosition;
                        SimpleMapPlaceable simpleMapPlaceable;

                        if (!_cityPlaceable.MainBuilding)
                        {
                            CityMainBuilding cityMainBuilding = building.AddComponent <CityMainBuilding>();
                            cityMainBuilding.CityPlaceable   = _cityPlaceable;
                            cityMainBuilding.UsedCoordinates = new List <NeededSpace>()
                            {
                                NeededSpace.Zero(TerrainGenerator.TerrainType.Flatland)
                            };
                            _cityPlaceable.MainBuilding = cityMainBuilding;
                            simpleMapPlaceable          = cityMainBuilding;
                        }
                        else
                        {
                            ProceduralCityBuilding cityBuilding = building.AddComponent <ProceduralCityBuilding>();
                            cityBuilding.CityPlaceable = _cityPlaceable;
                            cityBuilding.Height        = 1f;
                            cityBuilding.Generate(random.Next(1, amount - j), directionVec, streetDirection, _placementController);
                            simpleMapPlaceable = cityBuilding;
                        }

                        if (!_placementController.PlaceObject(simpleMapPlaceable))
                        {
                            Destroy(simpleMapPlaceable.gameObject);
                        }
                        ;
                        _cityPlaceable.ChildMapPlaceables.Add(simpleMapPlaceable);
                        building.transform.SetParent(transform);
                    }

                    if (j != amount)
                    {
                        continue;
                    }
                    endpoints.Add(placeable);
                    break;
                }
                yield return(new WaitForSeconds(waitTime));
            }
        }
    }