/* * 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; } }
/** * Retrieves the npc position from the json file in respect to both x and y coordinates * */ private Vector2 getNPCpos(string data) { string str_x = Json_Methods.GetValue(data, "x"); string str_y = Json_Methods.GetValue(data, "y"); float.TryParse(str_x, out float x); float.TryParse(str_y, out float y); Vector2 pos = new Vector2(x, y); return(pos); }
/* * * 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; } } } }
/** * Given an npc id and a level, return a list of all instance id's that match * */ public List <int> GetIDofNPCTypeInLevel(int npcID, string level) { List <int> ids = new List <int>(); foreach (npc_data data in id_to_data.Values) { if (data.level.Equals(level)) { string savedID = Json_Methods.GetValue(data.npc, "ID"); if (int.TryParse(savedID, out int newid)) { if (newid == npcID) { ids.Add(data.instance_id); } } } } return(ids); }