public override void Save(Unit[] unit) { FileStream outFile = new FileStream(FILE_NAME, FileMode.Create, FileAccess.Write); StreamWriter writer = new StreamWriter(outFile); WizardUnit wCheck = new WizardUnit(); for (int r = 0; r < unit.Length; r++) { if (unit[r].GetType() == wCheck.GetType()) { wCheck = (WizardUnit)unit[r]; writer.WriteLine(wCheck.name + DELIMITER + wCheck.x + DELIMITER + wCheck.y + DELIMITER + wCheck.health + DELIMITER + wCheck.maxHealth + DELIMITER + wCheck.speed + DELIMITER + wCheck.attack + DELIMITER + wCheck.attackRange + DELIMITER + wCheck.faction + DELIMITER + wCheck.symbol + DELIMITER + wCheck.attacking); } } writer.Close(); outFile.Close(); }
// intialize all units & Buildings with their values private void GenerateBattlefield() { for (int l = 0; l < units.Length; l++) // loop through unit array length { switch (rnd.Next(0, 3)) { case 0: MeleeUnit mUnit = new MeleeUnit(); mUnit.X = rnd.Next(0, WIDTH); mUnit.Y = rnd.Next(0, HEIGHT); if (rnd.Next(0, 2) == 0) { mUnit.Faction = 0; } else { mUnit.Faction = 1; } units[l] = mUnit; break; case 1: RangedUnit rUnit = new RangedUnit(); // temp rangedunit rUnit.X = rnd.Next(0, WIDTH); // random x, y rUnit.Y = rnd.Next(0, HEIGHT); if (rnd.Next(0, 2) == 0) // if 0 then faction 0 { rUnit.Faction = 0; } else // else faction 1 { rUnit.Faction = 1; } units[l] = rUnit; // assign temp to unit array break; case 2: WizardUnit wUnit = new WizardUnit(); // temp rangedunit wUnit.X = rnd.Next(0, WIDTH); // random x, y wUnit.Y = rnd.Next(0, HEIGHT); units[l] = wUnit; // assign temp to unit array break; } } for (int l = 0; l < buildings.Length; l++) { if (rnd.Next(0, 2) == 0) // if 0 then create resource building { ResourceBuilding rBuild = new ResourceBuilding(); // temp resource buliding rBuild.X = rnd.Next(0, WIDTH); // random x, y rBuild.Y = rnd.Next(0, HEIGHT); switch (rnd.Next(0, 3)) { case 0: rBuild.ResourceType = "Ores"; rBuild.Generation = 1; rBuild.Remaining = 45; break; case 1: rBuild.ResourceType = "Lumber"; rBuild.Generation = 2; rBuild.Remaining = 120; break; case 2: rBuild.ResourceType = "Wheat"; rBuild.Generation = 3; rBuild.Remaining = 30; break; } if (rnd.Next(0, 2) == 0) // if 0 then faction 0 { rBuild.Faction = 0; } else // else faction 1 { rBuild.Faction = 1; } buildings[l] = rBuild; // assign temp to buildings array } else // else create factory building { FactoryBuilding fBuild = new FactoryBuilding(); // temp factory building fBuild.X = rnd.Next(0, WIDTH); // random x, y fBuild.Y = rnd.Next(0, HEIGHT); if (fBuild.Y == HEIGHT - 1) { fBuild.SpawnX = fBuild.X; fBuild.SpawnY = fBuild.Y - 1; } else { fBuild.SpawnX = fBuild.X; fBuild.SpawnY = fBuild.Y + 1; } if (rnd.Next(0, 2) == 0) { fBuild.UnitType = "MeleeUnit"; fBuild.ProductionSpeed = 7; } else { fBuild.UnitType = "RangedUnit"; fBuild.ProductionSpeed = 9; } if (rnd.Next(0, 2) == 0) // if 0 then faction 0 { fBuild.Faction = 0; } else // else faction 1 { fBuild.Faction = 1; } buildings[l] = fBuild; // assign temp to building array } } }
public void ManageUnits() { int target; for (int l = 0; l < map.units.Length; l++) { if (map.units[l].Dead) { continue; } target = map.units[l].FindUnit(l, map.units); if (target == -1 && map.units[l].Faction != 2) { target = map.units[l].FindBuilding(map.units[l], map.buildings); if (target == -1) { continue; } else { if (map.units[l].InAttackRange(map.buildings[target].X, map.buildings[target].Y) && map.units[l].Health > map.units[l].MaxHealth * 0.25) { map.buildings[target].Health -= map.units[l].Attack; if (map.buildings[target].Health <= 0) { map.buildings[target].Destroy(); } } else { map.units[l].Move(map.units[l], map.buildings[target], map.GetWidth, map.GetHeight); } } } else if (target != -1) { if (map.units[l].Faction != 2) { if (map.units[l].InAttackRange(map.units[target]) && map.units[l].Health > map.units[l].MaxHealth * 0.25) { map.units[l].HandleCombat(map.units[target]); if (map.units[target].Health <= 0) { map.units[target].Die(); if (map.units[l].Faction == 0) { teamResources0++; } else { teamResources1++; } } } else { map.units[l].Move(map.units[l], map.units[target], map.GetWidth, map.GetHeight); } } else { if (map.units[l].InAttackRange(map.units[target]) && map.units[l].Health > map.units[l].MaxHealth * 0.50) { wizardcheck = (WizardUnit)map.units[l]; wizardcheck.targetX = map.units[target].X; wizardcheck.targetY = map.units[target].Y; for (int y = map.units[target].Y - 1; y <= map.units[target].Y + 1; y++) { for (int x = map.units[target].X - 1; x <= map.units[target].X + 1; x++) { for (int u = 0; u < map.units.Length; u++) { if (map.units[u].Faction != 2 && map.units[u].Y == y && map.units[u].X == x) { map.units[l].HandleCombat(map.units[u]); } if (map.units[u].Health <= 0) { map.units[u].Die(); } } } } map.units[l].HandleCombat(map.units[target]); if (map.units[target].Health <= 0) { map.units[target].Die(); if (map.units[l].Faction == 0) { teamResources0++; } else { teamResources1++; } } } else { map.units[l].Move(map.units[l], map.units[target], map.GetWidth, map.GetHeight); } } } } }