/// <summary> /// Creates a new AnimalTile and stores it for use. /// /// Under construction. Needs work. /// Problem: Too specific to scene 'CollectAndFollowAnimalTestScene'; needs generalization. /// </summary> public virtual void CreateAnimalTile(AnimalStoredInfo animalStoredInfo, bool isNewTile) { if (animalStoredInfo == null) { return; } newTile = Instantiate <GameObject> (animalTilePrefab); // the prefab must have AnimalTile if (!newTile.GetComponent <AnimalTile> ()) { string errMsg = "[AnimalStorageUI.cs] Whoops! You tried creating a new Animal Tile in the animal storage process, and the prefab you spawned does not include the 'AnimalTile' component!"; OutputLog.Write(errMsg); Debug.LogError(errMsg); } AniTile = newTile.GetComponent <AnimalTile> (); // pull script from spawned object // ===== // Set the animal sprite, gender and index in the script data AniTile.animalImage.sprite = animalStoredInfo.icon; AniTile.sexIcon.sprite = animalStoredInfo.sex.ToLower() == "female" ? References.instance.sprites["femalesymbol"] : References.instance.sprites["malesymbol"]; if (isNewTile) { AniTile.index = AnimalStorage.instance.animalInfoDictionary.Count + 1; // Update animalStoredInfo's index to match the tile, then store this index with a reference into memory connecting the tile to the NPC. animalStoredInfo.index = AniTile.index; } else { AniTile.index = animalStoredInfo.index; } // If we call this function from an "Add" button, or similar, we modify the storage to now include the new information. if (!AnimalStorage.instance.animalInfoDictionary.ContainsKey(animalStoredInfo.index)) { AnimalStorage.instance.animalInfoDictionary.Add(animalStoredInfo.index, animalStoredInfo); } AniTile.info = animalStoredInfo; // Parent the tile to the menu viewport newTile.transform.SetParent(animalListContent); newTile.transform.localScale = Vector3.one; // grab the gameObject references and set to specific components newTile.transform.GetChild(1).GetChild(0).GetComponent <TextMeshProUGUI>().text = AniTile.info.animalName; // name on tile newTile.transform.GetChild(2).GetComponent <Image> ().sprite = AniTile.sexIcon.sprite; // gender icon on tile }
/// <summary> /// Stores the current animal into the Creature Storage: /// /// Accesses 'Command Animal' script, finds selectedNPC to store. /// /// Takes and stores the 'Animal Info' component from the animal. /// /// 'Animal Stored Info' contains all necessary animal information, /// 'Animal Info' contains 'Animal Stored Info' and can be taken /// as an object from the info object. /// /// Once stored, the animal MUST be removed from 'Command Animal', /// and the best way to do this is to call Deactivate() from /// 'Recruit Animal', a component attached to the animal AI /// which allows the player to interact with them. /// /// At this point, the animal is stored as data... so we no longer /// need the animal's gameObject lying around. Call Destroy(). /// /// Previously, I had CreateAnimalTile() called separately from outside /// the script, but we've condensed it as the tile represents the /// stored animal and provides the only access to it. Plus, the /// animal can only be stored from a menu displaying it. /// /// </summary> public void StoreAnimalInfo() { NPCAIPathController npc = CommandAnimal.instance.selectedNPC; if (!npc) { return; } AnimalInfo info = npc.GetComponent <AnimalInfo> (); if (!info) { return; } storedNPC = info.info; storedNPC.icon = info.info.icon; npc.GetComponentInChildren <RecruitAnimal> ().Deactivate(); Destroy(npc.gameObject); CreateAnimalTile(storedNPC, true); }