void Start()
        {
            // player is in the house at the beginning
            playerHouse.GetComponent <PlayerHouse>().ShowPlayer();
            // Get reference to the nonSpanableSpace class
            nonSpawnableSpaceClass = nonSpawnableSpaceObj.GetComponent <nonSpawnableSpace>();
            // get active level settings - the get home scene always has 50% social distancing
            LevelSettings.GetActiveLevelSettings().SocialDistancingFactor = 18;
            LevelSettings.GetActiveLevelSettings().NumberOfNPCs           = npcNumber;
            // We do not show the infection status in this level
            LevelSettings.GetActiveLevelSettings().ShowInfectionStatus = false;
            // Set the DayLength for this level
            LevelSettings.GetActiveLevelSettings().DayLength = 100;
            // this gets the Main Camera from the Scene
            // the grid cell has to be as large as the player's infection radius
            randomGridForHumans = GameObject.Find("RandomGrid").GetComponent <RandomGrid>();
            // make screen Bounds 80% smaller so that NPCs are placed more in the middle since the player is at the edge
            randomGridForHumans.shrinkScreenBounds(0.8f);

            // generate the random coordinates for humans which depend on the scale of the player (who is largest),
            // their infection radius, since we do not want immediate infection
            // and the number of humans that we want to place
            randomGridForHumans.GenerateRandomCoords(playerPrefab.transform.localScale.x,
                                                     playerPrefab.GetComponentInChildren <InfectionTrigger>().InfectionRadius,
                                                     npcNumber, nonSpawnableSpaceClass);
            // place humans on grid
            CreateHumans();
        }
Exemplo n.º 2
0
        /// <summary>
        /// compute a list of random coords generated so that all prefab clones
        /// are placed on a grid in safe distance from each other
        /// including space where NPCs are not allowed to spawn
        /// <param name="scale"> e.g. the player's scale needed for grid size </param>
        /// <param name="infection_radius"> e.g. the player's infection radius for grid size </param>
        /// <param name="npcNumber"> number of prefabs clones to be placed, player is +1 </param>
        /// <param name="nonSpawnable"> space in which no NPC should spawn </param>
        /// </summary>
        public void GenerateRandomCoords(float scale, float infection_radius, int npcNumber, nonSpawnableSpace nonSpawnable)
        {
            //  Determine the size of a single cell
            float cellRadius     = GetCellRadius(scale, infection_radius);
            float cellSidelength = 2f * cellRadius;

            //  Determine the total size of the grid
            int[] gridSize           = GetGridSize(cellSidelength);
            int   rows               = gridSize[0];
            int   columns            = gridSize[1];
            int   cellCount          = rows * columns;
            bool  notAllAreSpawnable = true;

            Vector2 origin = -screenBounds;


            // Create random coordinates until all are in spawnable space
            // This way it is really ineffective, because we create all coords and
            // then check if they are spawnable and if not create all again.
            // However, we should take the isSpawnable check into ChooseUnique
            // and i currently do not know how to do this.
            //
            // Since this may crash the program, either because it just
            // takes too long, or because the spawnable space is just too small to
            // spawn all smileys, we give up after 50 attempts and just use
            // the last configuration. Even though some may now live in unspawnable space.
            int[]     indices      = null;
            int       countAttempt = 0;
            const int maxAttempts  = 50;

            while (notAllAreSpawnable && countAttempt < maxAttempts)
            {
                Debug.Log("Attempting to spawn humans. Attemp #" + countAttempt);
                //  Randomly select grid indices
                indices = ChooseUnique(npcNumber + 1, 0, cellCount);

                notAllAreSpawnable = false;
                for (int i = 0; i < npcNumber + 1; i++)
                {
                    if (!nonSpawnable.coordinatesAreSpawnable2D(GetCoordinatesInGrid(indices[i], columns, cellRadius, origin)))
                    {
                        // This NPC is non-spawnable. Set notAllAreSpawnable to true and exit the for loop (by setting i = npcNumber, (i dont like break))
                        i = npcNumber;
                        notAllAreSpawnable = true;
                    }
                }
                countAttempt++;
            }
            for (int i = 0; i < npcNumber + 1; i++)
            {
                RandomCoords.Add(GetCoordinatesInGrid(indices[i], columns, cellRadius, origin));
            }
        }