/// <summary> /// This callback fires when a wildcard tile receives input. /// </summary> /// <remarks> /// This method is assigned in the Wildcard Tile Manager's inspector. /// Callbacks like these can also be assigned dynamically at runtime, /// and anonymous delegates are supported as well. /// </remarks> public void OnWildcardTileSelect() { // Get the currently selected letter tile. var wildcardManagerTile = WGBEvent.currentLetterTile; // If the letter is blank, // show the wildcard tile selection panel. if (!(wildcardManagerTile.defaultLetter.hasValue || wildcardManagerTile.wildcardLetter.hasValue)) { // Show the panel and initialize UI variables. m_ViewModel.wildcardPanelSelection = -1; m_ViewModel.wildcardPanelScrollPosition = Vector2.zero; m_ViewModel.showWildcardPanel = true; // Cache manager information. m_WildcardManagerLanguage = WGBEvent.currentLanguage; m_WildcardManagerTile = wildcardManagerTile; } // If the letter is not blank, reset the wildcard value. else { wildcardManagerTile.RemoveWildcard(); // Deselect the tile. m_PlayerCached.DeselectTile(wildcardManagerTile); } }
// This method changes the current language. void ChangeLanguage(WordGameLanguage language) { // Abort all tasks before switching languages. // This is usually good practice. TaskManager.AbortAllTasksWaitForSeconds(10f); // Set the current language. WordGameLanguage.current = language; // Set the current language for the UI. m_ViewModel.languages.currentLanguage = language.languageName; // If a tile pool currently exists, destroy tiles to free up some memory. // Also remove the callback method from the tile pool. if (m_CurrentTilePool != null) { m_CurrentTilePool.DestroyAllTiles(); m_CurrentTilePool.onTileDistribution -= OnTileDistribution; } // Sometimes, the language may already be loaded. // This usually happens when switching between scenes. m_ViewModel.languages.currentLanguageIsLoaded = WordGameLanguage.current.wordSet.isExpanded; if (m_ViewModel.languages.currentLanguageIsLoaded) { OnLanguageLoad(); } }
// This method is called by the ViewModel, // when the user presses a button to change the language. void OnChangeLanguage(string language) { // Convert the language from string representation. WordGameLanguage lang = WordGameLanguage.FindByName(language); if (WordGameLanguage.current != lang) { ChangeLanguage(lang); } }
// This method is called by the ViewModel, // when the user presses a button to change a wildcard letter. void OnWildcardLetterSelect(string letter) { // Hide the wildcard tile selection panel. m_ViewModel.showWildcardPanel = false; // Set the letter to the currently cached tile, if the language has not changed. if (!string.IsNullOrEmpty(letter) && m_WildcardManagerTile != null && m_WildcardManagerLanguage == WordGameLanguage.current) { // Get a letter struct from a letter string. var selectedLetter = m_WildcardManagerLanguage.GetLetter(letter); // Set the wildcard letter. A value of 0 points is given, as well. m_WildcardManagerTile.SetWildcard(selectedLetter, 0); // Select the tile. m_PlayerCached.SelectTile(m_WildcardManagerTile); } m_WildcardManagerTile = null; m_WildcardManagerLanguage = null; }
void OnEnable() { // To prevent any issues in the editor, some sanity checks will go here. if (!m_ViewModel || !m_Player || !m_Agent || m_TilePools == null || m_TilePools.Length == 0) { WGBBase.LogError("Some scripts are not assigned in the editor!", "Word Game Builder", "ExmapleGameLogic", this); return; } // Get the player. m_PlayerCached = m_Player.GetComponentFromInterface <IWordGamePlayer>(); if (m_PlayerCached == null) { WGBBase.LogError(string.Format("Unable to find a player named {0}!", m_PlayerCached.name), "Word Game Builder", "ExampleGameLogic", this); return; } // Get the agent. m_AgentCached = m_Agent.GetComponentFromInterface <IWordGameAgent>(); if (m_AgentCached == null) { WGBBase.LogError(string.Format("Unable to find an agent named {0}!", m_AgentCached.name), "Word Game Builder", "ExampleGameLogic", this); return; } // Subscribe to UI events. m_ViewModel.game.onWildcardLetterSelect += OnWildcardLetterSelect; m_ViewModel.game.onResetTiles += OnResetTiles; m_ViewModel.languages.onChangeLanguage += OnChangeLanguage; m_ViewModel.player.onSubmitWord += OnSubmitWord; m_ViewModel.player.onClearSelection += OnClearSelection; m_ViewModel.agent.onAutomaticModeToggle += OnAutomaticModeToggle; // Here we get a list of languages for the UI. To do this, we need to load all language files (without decompressing them). // We trim any languages that don't have a matching tile pool, and any tile pools that don't have a matching language. WordGameLanguage.LoadAllLanguages(); // Trimming languages... var languages = new List <WordGameLanguage>(WordGameLanguage.GetAllLanguages()); var trimmedTilePoolObjects = new List <GameObject>(); var tilePools = new List <ITilePool>(); for (int i = languages.Count - 1; i >= 0; --i) { bool remove = true; for (int j = 0; j < m_TilePools.Length; ++j) { if (!m_TilePools[j]) { continue; } // Get the tile pool component from the ITilePool interface. var tilePool = m_TilePools[j].GetComponentFromInterface <ITilePool>(); if (tilePool == null) { continue; } if (i == languages.Count - 1) { tilePools.Add(tilePool); trimmedTilePoolObjects.Add(m_TilePools[j]); } if (languages[i] == tilePool.language) { remove = false; if (i < languages.Count - 1) { break; } } } if (remove) { languages.RemoveAt(i); } } // Trimming tile pools... for (int i = tilePools.Count - 1; i >= 0; --i) { bool remove = true; for (int j = 0; j < languages.Count; ++j) { // Get the tile pool component from the ITilePool interface. var tilePool = tilePools[i]; if (tilePool.language == languages[j]) { remove = false; break; } } if (remove) { tilePools.RemoveAt(i); trimmedTilePoolObjects.RemoveAt(i); } } m_TilePoolsCached = tilePools; m_TilePools = trimmedTilePoolObjects.ToArray(); // Get a list of languages for the UI. m_ViewModel.languages.languageNames = languages.Select(lang => lang.languageName).ToArray(); // Disable editing of inspector variables here. hideFlags = HideFlags.NotEditable; // We set this flag to load languages asynchronously. // This offloads decompression into another thread, improving performance. // In this example, language decompression happens during Update(). WordGameLanguage.decompressOnLoad = false; if (m_TilePoolsCached.Count > 0) { // Load the first tile pool's language. ChangeLanguage(m_TilePoolsCached[0].language); } // Setting some UI variables. m_ViewModel.game.tilePoolExists = m_CurrentTilePool != null; m_ViewModel.game.tilesRemaining = m_ViewModel.game.tilePoolExists ? m_CurrentTilePool.Count + m_PlayerCached.heldTiles.Count : 0; // Let the UI know that it is ready to draw m_ViewModel.game.isInitialized = true; }