Ejemplo n.º 1
0
        private void GenerateFeatures()
        {
            AllFeatures       = new List <IFeature>();
            _currFeatureCount = 0;
            // start top left
            FeaturePosition position    = new FeaturePosition(1, 1, Direction.None);
            Vector3Int      size        = RoomSize.GetRandomValue();
            IFeature        tempFeature = null;

            StartPos = position + new Vector3Int(size.x / 2, size.y / 2, 0);

            _factories[0].TryCreateFeature(position, size.ToVector2Int(), Tilemap, ref tempFeature);

            _featureQueue.Enqueue(tempFeature);
            AllFeatures.Add(tempFeature);

            _currFeatureCount++;

            while (_featureQueue.Count > 0 && _currFeatureCount < FeatureCount)
            {
                var lastFeature = _featureQueue.Dequeue();

                position = lastFeature.GetNewFeaturePosition();
                size     = RoomSize.GetRandomValue();
                Debug.Log($"feature at {position.ToString()} of size {size}");

                IFeatureFactory factory    = _factories[Random.Range(0, _factories.Count)];
                IFeature        newFeature = null;
                if (factory.TryCreateFeature(position, size.ToVector2Int(), Tilemap, ref newFeature))
                {
                    lastFeature.AddExit(position);
                    AllFeatures.Add(newFeature);


                    _featureQueue.Enqueue(newFeature);
                    _currFeatureCount++;
                }

                if (lastFeature.CanMakeNewFeature())
                {
                    _featureQueue.Enqueue(lastFeature);
                }
            }
        }
Ejemplo n.º 2
0
        public bool TryCreateFeature(FeaturePosition position, Vector2Int size, Tilemap tilemap, ref IFeature feature)
        {
            Vector3Int entrance = position;

            switch (position.Direction)
            {
            case Direction.Top:
                position.x = position.x - Random.Range(1, size.x - 1);
                position.y = position.y + 1;
                break;

            case Direction.Right:
                position.x = position.x + 1;
                position.y = position.y - Random.Range(1, size.y - 1);
                break;

            case Direction.Bottom:
                position.x = position.x - Random.Range(1, size.x - 1);
                position.y = position.y - size.y;
                break;

            case Direction.Left:
                position.x = position.x - size.x;
                position.y = position.y - Random.Range(1, size.y - 1);
                break;
            }

            if (CanCreateFeature(position, size, tilemap))
            {
                Debug.Log($"room at {position.ToString()}");
                // we aren't gonna check if we can add here, we just gonna add the room
                // even if it overlaps, checking should be done where this is called
                RoomData room = new RoomData(position, size, entrance, tilemap);
                _currentRooms.Add(room);
                feature = room;

                return(true);
            }
            return(false);
        }