/* * Given the specified level, spawn the npc's into it that are not currently active * */ public void InstantiateNPCsinLevel(string level) { List <npc_data> to_modify = new List <npc_data>(); foreach (npc_data data in id_to_data.Values) { if (data.level == level & data.isActive && data.level_id == -1) { string dataval = data.npc; string id_string = Json_Methods.GetValue(dataval, "NPCID"); if (int.TryParse(id_string, out int id)) { NPC npc = NPC_Lib.npcLib.GetNPC(id); npc = LoadNPConJson(npc, dataval); //Set the npc position Vector2 pos = getNPCpos(dataval); //Set the npc to LOADED npc.instantiation_type = NPC.INSTANCE_TYPE.LOADED; Instantiate(npc, pos, Quaternion.identity); to_modify.Add(data); } } } //For each npc we are adding, set to active foreach (npc_data data in to_modify) { int instance_id = data.instance_id; npc_data copy = id_to_data[instance_id]; copy.isActive = true; id_to_data[instance_id] = copy; } }
/** * Removes an NPC of instance id from the dictionary * */ public void DeregisterNPCInstance(int id) { if (id_to_data.ContainsKey(id)) { npc_data data = id_to_data[id]; data.isActive = false; id_to_data[id] = data; } }
/** * Takes in an instance ID and a new set of data. If the instance ID is stored, then replace the data * with the new set of JSON data * */ public void ReplaceNPC(int passedInInstanceID, string npc_data) { if (!id_to_data.ContainsKey(passedInInstanceID)) { Debug.Log("Instance ID " + passedInInstanceID + " does not exist in persistent data"); return; } npc_data data = id_to_data[passedInInstanceID]; data.npc = npc_data; id_to_data[passedInInstanceID] = data; }
/* * * When an npc is manually placed in the level, check to see if there is data that can be assigned first * */ public void LoadNPCfromLevel(NPC npc, int levelID, string level) { int id = npc.GetID(); Destroy(npc.gameObject); foreach (npc_data data in id_to_data.Values) { //If in current level, the player specific level id equals the saved one, and the saved npc is active, load it if (data.level == level & data.level_id == levelID & data.isActive) { string strCheckId = Json_Methods.GetValue(data.npc, "NPCID"); int.TryParse(strCheckId, out int checkID); //If the id of the JSON equals the saved npc id, then load the npc into the level if (checkID == id) { //Create a copy of the npc from the library prefab (must use copy to not override prefab information) NPC newnpc = Instantiate(NPC_Lib.npcLib.GetNPC(id)); NPC npcCopy = LoadNPConJson(newnpc, data.npc); //Get the saved position from JSON Vector2 pos = getNPCpos(data.npc); //Set the instantiation type to loaded so it does not save as new instance once created npcCopy.instantiation_type = NPC.INSTANCE_TYPE.LOADED; //Create the npc at the position specified in the JSON Instantiate(npcCopy, pos, Quaternion.identity); int instance_id = data.instance_id; npc_data copy = id_to_data[data.instance_id]; copy.isActive = true; id_to_data[instance_id] = copy; //Destroy the copy so we only have one in the world Destroy(newnpc.gameObject); break; } } } }
/** * Save a new npc into the dictionary and return the instance id it is saved as. * */ public int RegisterNPCInstance(string npc, int levelID, string level, bool persistent) { //Increment instance id instance_id += 1; npc = Json_Methods.ReplaceValue(npc, "instance_id", instance_id.ToString()); //Establish data for the npc npc_data new_data = new npc_data(); new_data.instance_id = instance_id; new_data.level = level; new_data.level_id = levelID; new_data.npc = npc; new_data.persistent = persistent; new_data.isActive = true; //Reference instance id to new data id_to_data.Add(instance_id, new_data); return(instance_id); }