/// <summary> /// Constructor for the Class. Asks for a file from which to parse its fields. /// </summary> /// <param name="filename">The file from which to get its fields.</param> public Scenario(string filename) { controller = Game_Controller.controller; scenario_file = filename; //Initialize Lists rewards = new List <string>(); bonus_rewards = new List <string>(); List <int> unlocks_scenarios = new List <int>(); List <int> unlocks_scenarios_on_loss = new List <int>(); List <int> unlocks_scenarios_on_win = new List <int>(); List <int> unlocks_scenarios_on_bonus = new List <int>(); //Read in the file string[] lines = System.IO.File.ReadAllLines(scenario_file); string line = ""; string[] elements = new string[2]; //30 is the default grid size; int grid_width = 30; int grid_length = 30; Material[] materials = new Material[10]; double[] tile_modifiers = new double[10]; int[,] tile_mat_ids = new int[grid_width, grid_length]; int[,] object_sprite_ids = new int[grid_width, grid_length]; int[,] character_ids = new int[grid_width, grid_length]; //Read through the file line by line, looking for specific headings for (int i = 0; i < lines.Length; i++) { line = lines[i]; switch (line) { //ID of the scenario case "[ID]": scenario_name = lines[i + 1]; i += 2; break; //What Sector the scenario can be found in case "[Sector]": scenario_sector = lines[i + 1]; i += 2; break; //Name of the Scenario case "[Name]": scenario_name = lines[i + 1]; i += 2; break; //Description for the Scenario case "[Description]": description = lines[i + 1]; i += 2; break; //Objective for the scenario (see Objectives enum for list of possible objectives.) case "[Objective]": foreach (Scenario_Objectives obj in System.Enum.GetValues(typeof(Scenario_Objectives))) { if (obj.ToString() == lines[i + 1]) { objective = obj; } } i += 2; break; //Reward for winning the scenario case "[Reward]": for (int j = i + 1; j < lines.Length; j++) { if (lines[j] != "") { rewards.Add(lines[j]); } else { i = j; j = lines.Length; } } break; //Bonus objective for the scenario case "[Bonus Objective]": bonus_objective = lines[i + 1]; i += 2; break; //Reward for completing the Bonus Objective case "[Bonus Reward]": for (int j = i + 1; j < lines.Length; j++) { if (lines[j] != "") { bonus_rewards.Add(lines[j]); } else { i = j; j = lines.Length; } } break; //Scenario IDs that will be unlocked regardless of wether or not the Objective is met case "[Unlocks]": elements = lines[i + 1].Split(';'); int id = scenario_id; foreach (string s in elements) { if (int.TryParse(s, out id)) { unlocks_scenarios.Add(id); } } i += 2; break; //Scenario IDs that will be unlocked if the Objective is not met case "[Unlocks on Loss]": elements = lines[i + 1].Split(';'); id = scenario_id; foreach (string s in elements) { if (int.TryParse(s, out id)) { unlocks_scenarios_on_loss.Add(id); } } i += 2; break; //Scenario IDs that will be unlocked if the Objective is met case "[Unlocks on Win]": elements = lines[i + 1].Split(';'); id = scenario_id; foreach (string s in elements) { if (int.TryParse(s, out id)) { unlocks_scenarios_on_win.Add(id); } } i += 2; break; //Scenario IDs that will be unlocked if the Bonus Objective is met case "[Unlocks on Bonus]": elements = lines[i + 1].Split(';'); id = scenario_id; foreach (string s in elements) { if (int.TryParse(s, out id)) { unlocks_scenarios_on_bonus.Add(id); } } i += 2; break; //Size of the tile grid map case "[Grid Size]": int width = 30; int length = 30; if (int.TryParse(lines[i + 1].Split(';')[0], out width)) { grid_width = width; } if (int.TryParse(lines[i + 1].Split(';')[1], out length)) { grid_length = length; } i += 2; break; case "[Tiles]": //build materials materials = new Material[10]; for (int j = i + 1; j < i + 10 + 1; j++) { string[] entries = lines[j].Split(' '); materials[(j - i - 1)] = (Material)Resources.Load("Objects/Materials/TileMat0" + (j - i - 1)); materials[(j - i - 1)].name = entries[1]; materials[(j - i - 1)].mainTexture = (Texture)Resources.Load("Textures/" + entries[1]); double modifier; if (double.TryParse(entries[2], out modifier)) { tile_modifiers[j - i - 1] = modifier; } } break; case "[Tile Map]": tile_mat_ids = new int[grid_width, grid_length]; for (int k = i + 1; k < i + grid_length + 1; k++) { string[] entries = lines[k].Split(';'); for (int l = 0; l < grid_width; l++) { int sprite; if (int.TryParse(entries[l], out sprite)) { tile_mat_ids[k - i - 1, l] = sprite; } } } break; case "[Object Map]": object_sprite_ids = new int[grid_width, grid_length]; for (int k = i + 1; k < i + grid_length + 1; k++) { string[] entries = lines[k].Split(';'); for (int l = 0; l < grid_width; l++) { int sprite; if (int.TryParse(entries[l], out sprite)) { object_sprite_ids[k - i - 1, l] = sprite; } } } break; case "[Character Map]": character_ids = new int[grid_width, grid_length]; for (int k = i + 1; k < i + grid_length + 1; k++) { string[] entries = lines[k].Split(';'); for (int l = 0; l < grid_width; l++) { int sprite; if (int.TryParse(entries[l], out sprite)) { character_ids[k - i - 1, l] = sprite; } } } break; //Default in case none of the above cases are met default: break; } //Resume new scenario creation tile_grid = new Tile_Grid(grid_width, grid_length, materials, tile_modifiers, tile_mat_ids, object_sprite_ids, character_ids); curr_round = 0; curr_character_num = 0; turn_order = new List <GameObject>(); reachable_tiles = new List <Transform>(); reachable_tile_objects = new List <GameObject>(); tile_objects = new List <GameObject>(); } }
public Scenario(string filename) { controller = Game_Controller.controller; scenario_file = filename; //Initialize Lists rewards = new List<string>(); bonus_rewards = new List<string>(); List<int> unlocks_scenarios = new List<int>(); List<int> unlocks_scenarios_on_loss = new List<int>(); List<int> unlocks_scenarios_on_win = new List<int>(); List<int> unlocks_scenarios_on_bonus = new List<int>(); //Read in the file string[] lines = System.IO.File.ReadAllLines(scenario_file); string line = ""; string[] elements = new string[2]; //30 is the default grid size; int grid_width=30; int grid_length=30; int[,] tile_sprite_ids = new int[grid_width, grid_length]; int[,] object_sprite_ids = new int[grid_width, grid_length]; int[,] character_sprite_ids = new int[grid_width, grid_length]; //Read through the file line by line, looking for specific headings for (int i = 0; i < lines.Length; i++) { line = lines[i]; switch (line) { //ID of the scenario case "[ID]": scenario_name = lines[i + 1]; i += 2; break; //What Sector the scenario can be found in case "[Sector]": scenario_sector = lines[i + 1]; i += 2; break; //Name of the Scenario case "[Name]": scenario_name = lines[i + 1]; i+=2; break; //Description for the Scenario case "[Description]": description = lines[i + 1]; i += 2; break; //Objective for the scenario (see Objectives enum for list of possible objectives.) case "[Objective]": foreach (Objectives obj in System.Enum.GetValues(typeof(Objectives))) { if(obj.ToString() == lines[i + 1]) { objective = obj; } } i += 2; break; //Reward for winning the scenario case "[Reward]": for(int j = i+1; j<lines.Length; j++) { if (lines[j] != "") { rewards.Add(lines[j]); } else { i = j; j = lines.Length; } } break; //Bonus objective for the scenario case "[Bonus Objective]": bonus_objective = lines[i + 1]; i += 2; break; //Reward for completing the Bonus Objective case "[Bonus Reward]": for (int j = i + 1; j < lines.Length; j++) { if (lines[j] != "") { bonus_rewards.Add(lines[j]); } else { i = j; j = lines.Length; } } break; //Scenario IDs that will be unlocked regardless of wether or not the Objective is met case "[Unlocks]": elements = lines[i + 1].Split(';'); int id = scenario_id; foreach (string s in elements) { if (int.TryParse(s, out id)) { unlocks_scenarios.Add(id); } } i += 2; break; //Scenario IDs that will be unlocked if the Objective is not met case "[Unlocks on Loss]": elements = lines[i + 1].Split(';'); id = scenario_id; foreach (string s in elements) { if (int.TryParse(s, out id)) { unlocks_scenarios_on_loss.Add(id); } } i += 2; break; //Scenario IDs that will be unlocked if the Objective is met case "[Unlocks on Win]": elements = lines[i + 1].Split(';'); id = scenario_id; foreach (string s in elements) { if (int.TryParse(s, out id)) { unlocks_scenarios_on_win.Add(id); } } i += 2; break; //Scenario IDs that will be unlocked if the Bonus Objective is met case "[Unlocks on Bonus]": elements = lines[i + 1].Split(';'); id = scenario_id; foreach (string s in elements) { if (int.TryParse(s, out id)) { unlocks_scenarios_on_bonus.Add(id); } } i += 2; break; //Size of the tile grid map case "[Grid Size]": int width = 30; int length = 30; if (int.TryParse(lines[i + 1].Split(';')[0], out width)) { grid_width = width; } if (int.TryParse(lines[i + 1].Split(';')[1], out length)) { grid_length = length; } i += 2; break; case "[Tile Map]": tile_sprite_ids = new int[grid_width, grid_length]; for( int k = i+1; k<i+grid_length+1; k++) { string[] entries = lines[k].Split(';'); for (int l = 0; l<grid_width; l++) { int sprite; if (int.TryParse(entries[l], out sprite)) { tile_sprite_ids[k-i-1, l] = sprite; } } } break; case "[Object Map]": object_sprite_ids = new int[grid_width, grid_length]; for (int k = i + 1; k < i + grid_length+1; k++) { string[] entries = lines[k].Split(';'); for (int l = 0; l < grid_width; l++) { int sprite; if (int.TryParse(entries[l], out sprite)) { object_sprite_ids[k-i-1, l] = sprite; } } } break; case "[Character Map]": character_sprite_ids = new int[grid_width, grid_length]; for (int k = i + 1; k < i + grid_length + 1; k++) { string[] entries = lines[k].Split(';'); for (int l = 0; l < grid_width; l++) { int sprite; if (int.TryParse(entries[l], out sprite)) { character_sprite_ids[k - i - 1, l] = sprite; } } } break; //Default in case none of the above cases are met default: break; } //Resume new scenario creation tile_grid = new Tile_Grid(grid_width, grid_length, tile_sprite_ids, object_sprite_ids, character_sprite_ids); tile_grid.tile_prefab.GetComponent<SpriteRenderer>().color = new Color(255f, 255f, 255f, 1f); curr_round = 0; curr_character_num = 0; turn_index = 0; turn_order = new List<GameObject>(); reachable_tiles = new List<Transform>(); reachable_tile_objects = new List<GameObject>(); } }