Beispiel #1
0
    private void DetermineCorrectlyLocatedEntities(List <Area> areas, HeightLevelType levelType, Dictionary <Tile, GameObject> entities, Dictionary <Tile, HeightLevelType> heightEntities)
    {
        List <Tile> oldEntityTiles        = entities.Keys.ToList();
        List <Tile> allTilesOnHeightLevel = new List <Tile>();

        foreach (Area area in areas)
        {
            allTilesOnHeightLevel.AddRange(area.GetAllTiles().ToList());
        }

        foreach (Tile newTile in allTilesOnHeightLevel)
        {
            foreach (Tile oldTile in oldEntityTiles)
            {
                heightEntities.TryGetValue(oldTile, out HeightLevelType heightTile);
                if (newTile.GridPosX == oldTile.GridPosX && newTile.GridPosZ == oldTile.GridPosZ && heightTile == levelType)
                {
                    entities.TryGetValue(oldTile, out GameObject entity);
                    entitiesPosition.Add(newTile, entity);
                    entitiesHeightLevel.Add(newTile, levelType);
                    entities.Remove(oldTile);
                }
            }
        }
    }
Beispiel #2
0
        private void DebugContiguousAreas()
        {
            //disable visual debugging in stopped editor mode
            if (!(EditorApplication.isPlaying))
            {
                return;
            }

            foreach (HeightLevel heightLevel in heightLevels)
            {
                HeightLevelType heightLevelType = heightLevel.type;
                List <Area>     heighGrid       = ContiguousAreasOnHeightLevel(heightLevelType);

                if (heighGrid == null)
                {
                    continue;
                }

                int j = 0;
                foreach (Area contiguousArea in heighGrid)
                {
                    //draw contiguous areas
                    string          innerName           = heightLevelType.ToString() + j++;
                    DebugVisualizer contiguousAreaDV    = DebugVisualizer.GetInstance(transform, innerName);
                    List <Vector3>  contiguousAreaTiles = contiguousArea.GetTiles();
                    contiguousAreaDV.VisualDebuggingTiles(contiguousAreaTiles, grid.TileLengthX, grid.TileLengthZ);
                }
            }
        }
Beispiel #3
0
    public GameObject Spawn(Vector3 position, HeightLevelType levelType)
    {
        GameObject prefab = null;

        if (levelType == HeightLevelType.VILLAGE)
        {
            prefab = housePrefab;
        }
        else if (levelType == HeightLevelType.TREE)
        {
            prefab = treePrefab;
        }
        else if (levelType == HeightLevelType.WATER)
        {
            prefab = wellPrefab;
        }

        GameObject newEntity = GameObject.Instantiate(prefab, position, Quaternion.identity, transform);

        newEntity.transform.rotation = prefab.transform.rotation;
        if (prefab == housePrefab)
        {
            newEntity.transform.Rotate(0, UnityEngine.Random.Range(-25, 25), 0);
        }

        return(newEntity);
    }
 public SpawnArea(Area a, float lowerB, float upperB, int entities, HeightLevelType level, List <float> allowedEntities)
 {
     area = a;
     areaLevelLowerBorder = lowerB;
     areaLevelUpperBorder = upperB;
     currentEntities      = entities;
     levelType            = level;
     treesPerSqm          = allowedEntities.ElementAt(0);
     wellsPerSqm          = allowedEntities.ElementAt(1);
     housesPerSqm         = allowedEntities.ElementAt(2);
     SetAllowedEntitiesForArea();
 }
Beispiel #5
0
    private float[] GetHeightLevelBorders(HeightLevelType levelType)
    {
        float[] borders = new float[2];

        if (levelType != HeightLevelType.UNDEFINED)
        {
            List <HeightLevel> levels = areaCalculator.GetHeightLevels();
            borders[0] = levels.Where(a => a.type == levelType).FirstOrDefault().lowerBorder;
            borders[1] = levels.Where(a => a.type == levelType).FirstOrDefault().upperBorder;
        }
        return(borders);
    }
        public List <Tile> CalculateNeighbours(Tile tile)
        {
            HeightLevelType heightLevelType = tile.HeightLevel.type;
            List <Tile>     neighbours      = new List <Tile>();
            List <Tile>     candidates      = new List <Tile>();
            int             x = tile.GridPosX;
            int             z = tile.GridPosZ;

            //fetch candidates
            //candidates north?
            if (z < ResolutionZ - 1)
            {
                candidates.Add(Tiles[x, z + 1]);
            }
            //candidates east?
            if (x < ResolutionX - 1)
            {
                candidates.Add(Tiles[x + 1, z]);
            }
            //candidates south?
            if (z > 0)
            {
                candidates.Add(Tiles[x, z - 1]);
            }
            //candidates west?
            if (x > 0)
            {
                candidates.Add(Tiles[x - 1, z]);
            }

            //candidate is neighbour?
            foreach (Tile candidate in candidates)
            {
                if (candidate != null && candidate.HeightLevel.type == heightLevelType)
                {
                    neighbours.Add(candidate);
                }
            }

            return(neighbours);
        }
Beispiel #7
0
    private IEnumerable <SpawnArea> SetupSpawnAreasForHeightLevel(List <Area> areas, HeightLevelType levelType)
    {
        List <SpawnArea> spawnAreas = new List <SpawnArea>();

        entitiesAllowed.Clear();
        entitiesAllowed.Add(treesPerSquareMeter);
        entitiesAllowed.Add(wellsPerSquareMeter);
        entitiesAllowed.Add(housesPerSquareMeter);

        //obere und untere Grenze des Höhenleveltyps bestimmen
        float[] borders = GetHeightLevelBorders(levelType);

        foreach (Area area in areas)
        {
            //int entityCount = CountEntitiesInArea(area);
            int       entityCount = area.GetAllTiles().ToList().Where(t => entitiesPosition.ContainsKey(t)).Count();
            SpawnArea s           = new SpawnArea(area, borders[0], borders[1], entityCount, levelType, entitiesAllowed);
            //SetSpawnableInSpawnArea(s, levelType);
            spawnAreas.Add(s);
        }
        return(spawnAreas);
    }
Beispiel #8
0
 public List <Area> ContiguousAreasOnHeightLevel(HeightLevelType heightLevelType)
 {
     contiguousAreas.TryGetValue(heightLevelType, out List <Area> contiguousAreasOnHeightLevel);
     return(contiguousAreasOnHeightLevel);
 }
Beispiel #9
0
        private void CalculateContiguousAreas(Grid grid, out Dictionary <HeightLevelType, List <Area> > contiguousAreas)
        {
            contiguousAreas = new Dictionary <HeightLevelType, List <Area> >();
            HashSet <Tile> explored        = new HashSet <Tile>();
            int            gridResolutionZ = grid.ResolutionZ;

            //iterate over tiles in grid
            for (int x = 0; x < gridResolutionX; x++)
            {
                for (int z = 0; z < gridResolutionZ; z++)
                {
                    Tile tileInGrid = grid.TileAt(x, z);

                    //tiles can be null after grid calculation
                    if (tileInGrid == null)
                    {
                        continue;
                    }

                    //if the tile has already been explored, then it already is allocated to a contiguous area
                    //and can be skipped
                    if (explored.Contains(tileInGrid))
                    {
                        continue;
                    }

                    //define height for new contiguous area from current tile in grid
                    HeightLevelType heightLevelType = tileInGrid.HeightLevel.type;

                    //initialize new contiguous area
                    Area contiguousArea = new Area();

                    //initialize set of detected tiles
                    SortedSet <Tile> detected = new SortedSet <Tile>();
                    //add first tile to detected (current tile in grid)
                    detected.Add(tileInGrid);

                    //as long as detected contains elements get one element
                    while (detected.Count > 0)
                    {
                        Tile detectedTile = detected.Min;

                        //get all neighbours of current detected tile
                        List <Tile> neighbours = grid.CalculateNeighbours(detectedTile);

                        //for all neighbours check if they already have been detected or explored
                        while (neighbours.Count > 0)
                        {
                            Tile neighbour = neighbours[0];
                            if (!detected.Contains(neighbour) && !explored.Contains(neighbour))
                            {
                                detected.Add(neighbour);
                            }
                            neighbours.RemoveAt(0);
                        }

                        //if all neighbours are detected or explored, then mark current detected tile as explored
                        explored.Add(detectedTile);
                        //remove explored tile from detected set
                        detected.Remove(detectedTile);
                        //allocate current detected tile to current contiguous area
                        contiguousArea.Add(detectedTile);
                    }
                    //all tiles in contiguous area have been added
                    //add contiguous area to set of contiguous areas
                    //check if set has already a slot for height level of current contiguous area
                    List <Area> contiguousAreasOnLevel;
                    contiguousAreas.TryGetValue(heightLevelType, out contiguousAreasOnLevel);
                    if (contiguousAreasOnLevel == null)
                    {
                        //create new slot for height level of current contiguous area
                        contiguousAreasOnLevel = new List <Area>();
                        contiguousAreas.Add(heightLevelType, contiguousAreasOnLevel);
                    }
                    contiguousAreasOnLevel.Add(contiguousArea);
                }
            }
            if (debugMode)
            {
                DebugContiguousAreas();
            }
        }