private void SetSpawner() { Spawner[] allSpawners = GameObject.FindObjectsOfType<Spawner>(); foreach (Spawner spawner in allSpawners) { if (spawner.transform.position.y == transform.position.y) _spawner = spawner; } }
private void Start() { spawner = GetComponent <Spawner>(); }
private IEnumerator LoadLevelCoroutine(Level level) { var asyncOperation = SceneManager.LoadSceneAsync(level.SceneName, LoadSceneMode.Additive); while (!asyncOperation.isDone) { yield return(new WaitForEndOfFrame()); } var scene = SceneManager.GetSceneByName(level.SceneName); var rootGameObject = scene.GetRootGameObjects(); var paths = rootGameObject.SelectMany(obj => obj.GetComponentsInChildren <Path>()); var script = new Script(CoreModules.Preset_HardSandbox); script.Options.DebugPrint = Debug.Log; UserData.RegisterType <ISpawner>(); var spawners = new List <Spawner>(); foreach (var path in paths) { var spawner = new Spawner(path.GetPath()); spawners.Add(spawner); var spawnerName = CreateSpawnerName(path.name); script.Globals.Set(spawnerName, UserData.Create(spawner)); } var scriptPath = new FileInfo(level.ScriptPath); var scriptCode = File.ReadAllText(scriptPath.FullName); script.DoString(scriptCode); var getCashFunction = script.Globals.Get(LuaScriptConstants.GetCashFunctionName); if (getCashFunction.IsNil()) { throw new InvalidOperationException(string.Format( "The '{0}' function does not exist in script '{1}'.", LuaScriptConstants.GetCashFunctionName, scriptPath.Name)); } var setupSpawnFunction = script.Globals.Get(LuaScriptConstants.SetupWaveFunctionName); if (setupSpawnFunction.IsNil()) { throw new InvalidOperationException(string.Format( "The '{0}' function does not exist in script '{1}'.", LuaScriptConstants.SetupWaveFunctionName, scriptPath.Name)); } for (var wave = 0; wave < level.Waves; wave++) { foreach (var spawner in spawners) { spawner.Clear(); } var cashResult = script.Call(script.Globals[LuaScriptConstants.GetCashFunctionName], wave); Cash += (int)cashResult.Number; script.Call(script.Globals[LuaScriptConstants.SetupWaveFunctionName], wave); yield return(new WaitForSeconds(5)); foreach (var spawner in spawners) { StartCoroutine(SpawnCoroutine(spawner)); } while (spawners.Any(spawner => !spawner.Finished) || _enemies.Count > 0) { yield return(new WaitForEndOfFrame()); } Debug.Log("wave finished"); } Debug.Log("level finished"); yield return(new WaitForSeconds(4)); UnloadLevel(); }