コード例 #1
0
ファイル: GearSpawner.cs プロジェクト: ds5678/ModComponent
        /// <summary>
        /// Spawns the items into the scene. However, this can be overwritten by deserialization
        /// </summary>
        /// <param name="sceneName"></param>
        private static void SpawnGearForScene(string sceneName)
        {
            IEnumerable <GearSpawnInfo> sceneGearSpawnInfos = GetSpawnInfos(sceneName);

            if (sceneGearSpawnInfos is null)
            {
                return;
            }

            foreach (GearSpawnInfo eachGearSpawnInfo in sceneGearSpawnInfos)
            {
                string normalizedGearName = GetNormalizedGearName(eachGearSpawnInfo.PrefabName);
                Object prefab             = Resources.Load(normalizedGearName);

                if (prefab is null)
                {
                    Logger.LogWarning("Could not find prefab '{0}' to spawn in scene '{1}'.", eachGearSpawnInfo.PrefabName, sceneName);
                    continue;
                }

                float spawnProbability = ProbabilityManager.GetAdjustedProbability(eachGearSpawnInfo);
                if (ModComponentUtils.RandomUtils.RollChance(spawnProbability))
                {
                    Object gear = Object.Instantiate(prefab, eachGearSpawnInfo.Position, eachGearSpawnInfo.Rotation);
                    gear.name = prefab.name;
                    DisableObjectForXPMode xpmode = gear.Cast <GameObject>().GetComponent <DisableObjectForXPMode>();
                    if (xpmode != null)
                    {
                        Object.Destroy(xpmode);
                    }
                }
            }
        }
コード例 #2
0
        void Update()
        {
            if (ModComponentMain.Settings.instance.disableRandomItemSpawns)
            {
                return;
            }
            if (this.ItemNames is null || this.ItemNames.Length == 0)
            {
                Logger.LogWarning("'{0}' had an invalid list of potential spawn items.", this.name);
                Destroy(this.gameObject);
                return;
            }

            int        index  = RandomUtils.Range(0, this.ItemNames.Length);
            GameObject prefab = Resources.Load(this.ItemNames[index])?.Cast <GameObject>();

            if (prefab is null)
            {
                Logger.LogWarning("Could not use '{0}' to spawn random item '{1}'", this.name, this.ItemNames[index]);
                Destroy(this.gameObject);
                return;
            }

            GameObject gear = Instantiate(prefab, this.transform.position, this.transform.rotation);

            gear.name = prefab.name;
            DisableObjectForXPMode xpmode = gear?.GetComponent <DisableObjectForXPMode>();

            if (xpmode != null)
            {
                Destroy(xpmode);
            }
            Destroy(this.gameObject);
        }