// Update is called once per frame void Update() { time_elapsed_since_last_simulation_ += Time.deltaTime; // Toggles the settings window. if (Input.GetKeyDown(KeyCode.Escape)) { b_is_window_settings_enabled_ = !b_is_window_settings_enabled_; } // Accelerates the simulation if (Input.GetKey(KeyCode.E)) { if (simulation_time_ > 0.001F) { simulation_time_ /= 1.2F; } } // Slows the simulation else if (Input.GetKey(KeyCode.Q)) { if (simulation_time_ < 100F) { simulation_time_ *= 1.2F; } } // If enough time has passed we need to run another simulation. I also need to make sure that the simulation is running. if (b_is_simulation_running_ && (time_elapsed_since_last_simulation_ >= simulation_time_)) { GameSimulator.SimulationStep temp_simulation_step = game_simulator_.Simulate(); // Under multithreading environment the simulation queue might be empty if the render thread is faster than the simulating thread if (temp_simulation_step != null) { game_grid_ = temp_simulation_step.cell_grid; last_simulation_duration_ = temp_simulation_step.time_to_simulate; } updateGridGraphics(); time_elapsed_since_last_simulation_ = 0; } }
/** * Responsible for drawing the settings window. */ private void settingsWindow(int _windowID) { GUILayout.Label("Rows"); grid_rows_ = GUILayout.TextField(grid_rows_); GUILayout.Label("Columns"); grid_columns_ = GUILayout.TextField(grid_columns_); // Rebuilds the grid. if (GUILayout.Button("Rebuild")) { buildGrid(); if (game_simulator_ != null) { game_simulator_.Dispose(); } // By setting the game simulator to null I can ensure that no simulation is running whenever the "Start" button is clicked afterwards. game_simulator_ = null; b_is_simulation_running_ = false; } // Starts the simulation. if (GUILayout.Button("Start")) { b_is_simulation_running_ = true; // If the game simulator is set to null it means that we need a new game simulator object to run the simulation since we've most likely rebuilt the grid. if (game_simulator_ == null) { game_simulator_ = new GameSimulator(game_grid_, b_use_multithreading_, true); } else { game_simulator_.IsSimulationRunning = true; // I need to reset the threading environment since it might have changed if the user clicked on the "Step" button game_simulator_.UseThreading = b_use_multithreading_; } } // Stops the simulation if (GUILayout.Button("Stop")) { b_is_simulation_running_ = false; game_simulator_.IsSimulationRunning = false; } // Runs one step of the simulation if (GUILayout.Button("Step (Disables Multithreading)")) { b_is_simulation_running_ = false; b_use_multithreading_ = false; // I can only run valid simulations. If no simulations are found I start a new one without multithreading and ticking. if (game_simulator_ == null) { game_simulator_ = new GameSimulator(game_grid_, false, false); } else { game_simulator_.IsSimulationRunning = false; game_simulator_.UseThreading = false; } GameSimulator.SimulationStep temp_simulation_step = game_simulator_.Simulate(); game_grid_ = temp_simulation_step.cell_grid; last_simulation_duration_ = temp_simulation_step.time_to_simulate; updateGridGraphics(); } if (GUILayout.Toggle(b_use_multithreading_, "Multithreading") != b_use_multithreading_) { b_use_multithreading_ = !b_use_multithreading_; // If we still have not created a simulation create a non ticking, single threaded one // Threading environment will be set immediately after and ticking should only start with the appropriate button if (game_simulator_ == null) { game_simulator_ = new GameSimulator(game_grid_, false, false); } game_simulator_.UseThreading = b_use_multithreading_; } GUILayout.Label("Last simulation time: " + last_simulation_duration_ + "ms"); GUI.DragWindow(); }