Exemplo n.º 1
0
 public override int GetHashCode()
 {
     unchecked
     {
         var hashCode = SpawnProbability.GetHashCode();
         hashCode = (hashCode * 397) ^ PayoutInterval.GetHashCode();
         return(hashCode);
     }
 }
Exemplo n.º 2
0
            /// <summary>
            /// Given an empty position in the grid, select a random room
            /// prefab for that position that is viable in that the doors will
            /// connect properly, and that takes into account room spawn
            /// probabilities.
            /// </summary>
            /// <param name="position">
            /// The position in the grid of rooms. The units used are relative
            /// to the starting room. So 0,0 is the starting room. 1,0 is the
            /// room east of that, etc.
            /// </param>
            /// <param name="enclosedPrefabRequired">
            /// Whether or not the random prefab needs to be enclosed, meaning
            /// all of its doors will connect to already existing rooms and the
            /// room will contain no unconnected doors.
            /// </param>
            /// <returns>
            /// A random room prefab that meets the requirements for the given position.
            /// </returns>
            public GameObject SelectPrefabFor(Vector2Int position, bool enclosedPrefabRequired, int depth)
            {
                float depthPercentage = 1.0f - ((depth - 1) / (_initialDepth - 1));

                GameObject[]             viablePrefabs      = FindViablePrefabsFor(position, enclosedPrefabRequired, depthPercentage);
                List <SpawnProbability?> spawnProbabilities = viablePrefabs
                                                              .Select(prefab => prefab.GetComponent <RoomGenerationBehaviour>())
                                                              .Select(room => room.GetSpawnProbability(depthPercentage))
                                                              .ToList();

                // if there are any guaranteed prefabs for this depthPercentage, pick the first that is found.
                int guaranteedIndex = -1;

                for (int i = 0; i < spawnProbabilities.Count; i++)
                {
                    GameObject       viablePrefab     = viablePrefabs[i];
                    SpawnProbability?spawnProbability = spawnProbabilities[i];

                    bool isGuaranteed = true &&
                                        spawnProbability.HasValue &&
                                        spawnProbability.Value.Guarantee != SpawnProbability.SpawnGuarantee.None &&
                                        !HasSpawnedGuaranteedRoomPrefab(viablePrefab, spawnProbability.Value.DepthPercentage);

                    if (isGuaranteed)
                    {
                        guaranteedIndex = i;
                        break;
                    }
                }

                if (guaranteedIndex != -1)
                {
                    GameObject       guaranteedPrefab           = viablePrefabs[guaranteedIndex];
                    SpawnProbability guaranteedSpawnProbability = spawnProbabilities[guaranteedIndex].Value;
                    AddSpawnedGuaranteedRoomPrefab(guaranteedPrefab, guaranteedSpawnProbability.DepthPercentage);
                    return(guaranteedPrefab);
                }

                // no guaranteed prefabs found, pick a random prefab.
                return(viablePrefabs
                       .Zip(spawnProbabilities, Tuple.Create)
                       .GetRandomElementWithProbability(tuple => tuple.Item2?.ProbabilityMultiplier ?? 1.0f)
                       .Item1);
            }
Exemplo n.º 3
0
        /// <summary>
        /// Given a depthPercentage, find the SpawnProbability that should be
        /// used for that level generation depth.
        ///
        /// A SpawnProbability should be used for a given depth if it is within
        /// the range of the current SpawnProbabilities DepthPercentage and the
        /// next SpawnProbabilities DepthPercentage.
        ///
        /// For example. If there is one SpawnProbability with a
        /// DepthPercentage of 0.5f, the SpawnProbability settings will only be
        /// applied when the level generation depth is between 0.5f to 1.0f.
        /// </summary>
        /// <param name="depthPercentage">
        /// The current level generation depth as a percentage value
        /// represented between 0.0f and 1.0f
        /// </param>
        /// <returns>
        /// The SpawnProbability containing settings that should be used for
        /// the given depth. Will return null if no SpawnProbability should
        /// apply to the given depth.
        /// </returns>
        public SpawnProbability?GetSpawnProbability(float depthPercentage)
        {
            if (SpawnProbabilities.Count == 0 || SpawnProbabilities[0].DepthPercentage > depthPercentage)
            {
                return(null);
            }

            SpawnProbability currentSpawnProbability = SpawnProbabilities[0];

            foreach (SpawnProbability nextSpawnProbability in SpawnProbabilities.Skip(1))
            {
                if (nextSpawnProbability.DepthPercentage > depthPercentage)
                {
                    break;
                }
                currentSpawnProbability = nextSpawnProbability;
            }

            return(currentSpawnProbability);
        }
Exemplo n.º 4
0
 public bool Equals(ResourceSpawnInfo other)
 {
     return(SpawnProbability.Equals(other.SpawnProbability) && PayoutInterval.Equals(other.PayoutInterval));
 }