/// <summary> /// Starts a new thread that will process a board in the background. The OnBoardCreated callback will be called when the board is complete /// </summary> private bool StartCreatingBoard(string id, string[] words, OnBoardFinished callback, int randomNumberSeed, long restartTime) { if (IsCreatingBoard(id)) { UnityEngine.Debug.LogErrorFormat("Could not start board creation because the id \"{0}\" already exist.", id); return(false); } // Create a new Board object WordBoard board = new WordBoard(); board.id = id; board.words = words; board.randSeed = randomNumberSeed; board.rand = new System.Random(randomNumberSeed); board.boardState = WordBoard.BoardState.Processing; board.stopwatch = new System.Diagnostics.Stopwatch(); board.restartTime = restartTime; // Create a new ActiveThread object that will hold information about the thread/board ActiveThread activeThread = new ActiveThread(); activeThread.id = id; activeThread.board = board; activeThread.callback = callback; // Create the new Thread to start processing the Board activeThread.thread = new Thread(new ThreadStart(() => ProcessBoard(board, words))); activeThread.thread.Start(); activeThreads.Add(activeThread); return(true); }
public void Update() { // Checks for completed boards for (int i = activeThreads.Count - 1; i >= 0; i--) { switch (activeThreads[i].board.boardState) { case WordBoard.BoardState.DoneSuccess: case WordBoard.BoardState.DoneFailed: { WordBoard wordBoard = activeThreads[i].board; OnBoardFinished callback = activeThreads[i].callback; activeThreads.RemoveAt(i); callback(wordBoard); break; } case WordBoard.BoardState.Restart: { string id = activeThreads[i].id; string[] words = activeThreads[i].board.words; long restartTimer = activeThreads[i].board.restartTime; OnBoardFinished callback = activeThreads[i].callback; AbortBoardCreation(id); StartCreatingBoard(id, words, callback, restartTimer); break; } } } }
/// <summary> /// Starts the creation of a board using the given random seed number. Passing the same seed will generate the same /// board every time. /// </summary> public bool StartCreatingBoard(string id, string[] words, int randomNumberSeed, OnBoardFinished callback) { return(StartCreatingBoard(id, words, callback, randomNumberSeed, long.MaxValue)); }
/// <summary> /// Starts the creation of a new board, if it takes longer than restartTimer (in milliseconds) then it will abort and /// try again. Most of the time board creation takes less than a couple seconds but if there are alot of long words then the /// board can get itself in a state that takes a long time to find a fit for all the words. The easiest way to reslove /// this issue is to restart the board creation. /// </summary> public bool StartCreatingBoard(string id, string[] words, OnBoardFinished callback, long restartTimer) { return(StartCreatingBoard(id, words, callback, Random.Range(0, int.MaxValue), restartTimer)); }