/// <summary> /// Resets all unit info stats to the minimum values. /// </summary> /// <returns>void</returns> private void Reset() { listBoxUnits.Items.Clear(); CUnit infantry = new CUnit(65, 80, 11, 1, 1.0f, 1.0f, eUnitType.INFANTRY); infantry.HPMin = 50; infantry.HPMax = 110; infantry.AttackMin = 6; infantry.AttackMax = 16; infantry.RangeMin = 1; infantry.RangeMax = 1; infantry.CostMin = 50; infantry.CostMax = 100; infantry.AttackSpeedMin = .5f; infantry.AttackSpeedMax = 1.5f; infantry.MovementSpeedMin = .5f; infantry.MovementSpeedMax = 1.5f; CUnit cavalry = new CUnit(150, 150, 10, 1, 1.0f, 2.0f, eUnitType.CAVALRY); cavalry.HPMin = 100; cavalry.HPMax = 200; cavalry.AttackMin = 5; cavalry.AttackMax = 15; cavalry.RangeMin = 1; cavalry.RangeMax = 1; cavalry.CostMin = 100; cavalry.CostMax = 200; cavalry.AttackSpeedMin = .5f; cavalry.AttackSpeedMax = 1.5f; cavalry.MovementSpeedMin = 1.0f; cavalry.MovementSpeedMax = 3.0f; CUnit cavalryArcher = new CUnit(150, 90, 8, 6, 1.5f, 2.0f, eUnitType.CAVALRY_ARCHER); cavalryArcher.HPMin = 60; cavalryArcher.HPMax = 120; cavalryArcher.AttackMin = 5; cavalryArcher.AttackMax = 11; cavalryArcher.RangeMin = 2; cavalryArcher.RangeMax = 10; cavalryArcher.CostMin = 100; cavalryArcher.CostMax = 200; cavalryArcher.AttackSpeedMin = 1.0f; cavalryArcher.AttackSpeedMax = 2.0f; cavalryArcher.MovementSpeedMin = 1.0f; cavalryArcher.MovementSpeedMax = 3.0f; CUnit axmen = new CUnit(65, 70, 12, 1, 1.0f, 1.0f, eUnitType.AXMEN); axmen.HPMin = 40; axmen.HPMax = 100; axmen.AttackMin = 6; axmen.AttackMax = 18; axmen.RangeMin = 1; axmen.RangeMax = 1; axmen.CostMin = 50; axmen.CostMax = 100; axmen.AttackSpeedMin = .5f; axmen.AttackSpeedMax = 1.5f; axmen.MovementSpeedMin = .5f; axmen.MovementSpeedMax = 1.5f; CUnit archer = new CUnit(50, 50, 8, 6, 1.5f, .75f, eUnitType.ARCHER); archer.HPMin = 20; archer.HPMax = 80; archer.AttackMin = 5; archer.AttackMax = 11; archer.RangeMin = 2; archer.RangeMax = 10; archer.CostMin = 25; archer.CostMax = 75; archer.AttackSpeedMin = 1.0f; archer.AttackSpeedMax = 2.0f; archer.MovementSpeedMin = .5f; archer.MovementSpeedMax = 1.5f; CUnit warElephant = new CUnit(300, 300, 15, 1, 1.75f, .5f, eUnitType.WAR_ELEPHANT); warElephant.HPMin = 100; warElephant.HPMax = 500; warElephant.AttackMin = 10; warElephant.AttackMax = 20; warElephant.RangeMin = 1; warElephant.RangeMax = 1; warElephant.CostMin = 100; warElephant.CostMax = 500; warElephant.AttackSpeedMin = 1.0f; warElephant.AttackSpeedMax = 3.0f; warElephant.MovementSpeedMin = .25f; warElephant.MovementSpeedMax = 1.0f; listBoxUnits.Items.Add(infantry); listBoxUnits.Items.Add(cavalry); listBoxUnits.Items.Add(cavalryArcher); listBoxUnits.Items.Add(axmen); listBoxUnits.Items.Add(archer); listBoxUnits.Items.Add(warElephant); listBoxUnits.SelectedIndex = 0; }
/// <summary> /// When "Open" is clicked in the File Menu an Open dialog will launch /// for opening of the selected radio button file format (XML or Binary). /// </summary> /// <param name="sender">The sender of the event</param> /// <param name="e">Event data sent with the event</param> /// <returns>void</returns> private void openToolStripMenuItem_Click(object sender, EventArgs e) { try { // Open XML File if (radioButtonXML.Checked) { OpenFileDialog dlg = new OpenFileDialog(); dlg.DefaultExt = "xml"; dlg.Filter = "XML|*.xml"; if (DialogResult.OK == dlg.ShowDialog()) { FileStream fs = new FileStream(dlg.FileName, FileMode.Open); List <CUnit> units = new List <CUnit>(); XmlSerializer serializer = new XmlSerializer(typeof(List <CUnit>)); units = (List <CUnit>)serializer.Deserialize(fs); fs.Close(); listBoxUnits.Items.Clear(); foreach (CUnit unit in units) { listBoxUnits.Items.Add(unit); } } } // Open Binary File else { OpenFileDialog dlg = new OpenFileDialog(); dlg.DefaultExt = "dat"; dlg.Filter = "Binary|*.dat"; if (DialogResult.OK == dlg.ShowDialog()) { listBoxUnits.Items.Clear(); FileStream fs = new FileStream(dlg.FileName, FileMode.Open); BinaryReader br = new BinaryReader(fs); int count = br.ReadInt16(); for (int i = 0; i < count; i++) { CUnit unit = new CUnit(); unit.Type = (eUnitType)br.ReadInt32(); unit.HP = (int)br.ReadInt32(); unit.Attack = (int)br.ReadInt32(); unit.Range = (int)br.ReadInt32(); unit.AttackSpeed = (float)br.ReadDouble(); unit.Movement = (float)br.ReadDouble(); // New stuff unit.Cost = (int)br.ReadInt32(); // New Randomize Stuff unit.HPMin = (int)br.ReadInt32(); unit.HPMax = (int)br.ReadInt32(); unit.AttackMin = (int)br.ReadInt32(); unit.AttackMax = (int)br.ReadInt32(); unit.RangeMin = (int)br.ReadInt32(); unit.RangeMax = (int)br.ReadInt32(); unit.AttackSpeedMin = (float)br.ReadDouble(); unit.AttackSpeedMax = (float)br.ReadDouble(); unit.MovementSpeedMin = (float)br.ReadDouble(); unit.MovementSpeedMax = (float)br.ReadDouble(); unit.CostMin = (int)br.ReadInt32(); unit.CostMax = (int)br.ReadInt32(); listBoxUnits.Items.Add(unit); } br.Close(); fs.Close(); } } listBoxUnits.SelectedIndex = 0; } // Catch the Exception generated if the // we have problems reading the file catch (Exception de) { // Give the user a little info and prevent a crash! MessageBox.Show("Error opening file! Exception: " + de.GetType().Name, "Open Error"); // Reset all values to make sure no garabe was read in Reset(); } }