private void AddTroop(UnitData unitData) { RaycastGizmos(out var gizmo); if (gizmo is ProvinceGizmo provinceGizmo) { var province = provinceGizmo.Province; var unit = Troops.Create(unitData.id, 1, province.Owner); province.Monsters.Add(unit); provinceGizmo.CreateUnitGizmo(unit); } else if (gizmo is CommanderGizmo commanderGizmo) { var unit = Troops.Create(unitData.id, 1, commanderGizmo.OwnerProvince.Province.Owner); commanderGizmo.Data.UnitsUnderCommand.Add(unit); commanderGizmo.OwnerProvince.CreateUnitGizmo(unit); } }
public static Dictionary <int, Province> LoadProvinces(Map map) { var mapElements = map.AllElements; //Find province points (white pixels) var provincePoints = new List <Vector2>(); for (int y = 0; y < map.MapTexture.height; y++) { for (int x = 0; x < map.MapTexture.width; x++) { var pixel = map.MapTexture.GetPixel(x, y); if (pixel == Color.white) { provincePoints.Add(new Vector2(x, y)); } } } //Create all provinces var provinces = new Dictionary <int, Province>(); int provinceCount = mapElements.OfType <Terrain>().Max(x => x.ProvinceNum); for (int num = 1; num <= provinceCount; num++) { var provinceBorders = mapElements.OfType <ProvinceBorders>().Where(x => x.ProvinceNum == num).ToList(); if (provinceBorders.Count == 0) { continue; } var provincePoint = provincePoints[num - 1]; // var centerPos = Vector2.zero; // foreach (var pb in provinceBorders) // { // centerPos.x += pb.X; // centerPos.y += pb.Y; // } // centerPos /= provinceBorders.Count(); var province = new Province(num, provincePoint); province.ProvinceBorders = provinceBorders; provinces.Add(num, province); } //Set owner of provinces foreach (var provinceOwner in mapElements.OfType <ProvinceOwner>()) { if (provinces.ContainsKey(provinceOwner.ProvinceNum)) { var nation = DomEdit.I.Nations.GetNationById(provinceOwner.NationNum); provinces[provinceOwner.ProvinceNum].Owner = nation; } } foreach (var startLocation in mapElements.OfType <StartLocation>()) { if (provinces.ContainsKey(startLocation.ProvinceNum)) { var nation = DomEdit.I.Nations.GetNationById(startLocation.NationNum); provinces[startLocation.ProvinceNum].Owner = nation; } } //Create commanders foreach (var commanderElement in mapElements.OfType <CommanderElement>()) { var province = provinces[commanderElement.ProvinceNum]; var commander = Commander.Create(commanderElement.MonsterId, province.Owner); commander.Nationality = province.Owner; province.Monsters.Add(commander); //Create owned-be-commander objects foreach (var ownedByCommander in mapElements.OfType <IOwnedByCommander>().Where(x => x.Commander == commanderElement)) { switch (ownedByCommander) { case BodyguardsElement bodyguardsElement: var bodyguard = Troops.Create(bodyguardsElement.MonsterId, bodyguardsElement.Amount, commander.Nationality); commander.Bodyguards.Add(bodyguard); break; case ItemElement itemElement: var item = Item.Create(itemElement.ItemName); commander.Items.Add(item); break; case UnitsElement unitsElement: var unit = Troops.Create(unitsElement.MonsterId, unitsElement.Amount, commander.Nationality); commander.UnitsUnderCommand.Add(unit); break; case Experience _: //TODO break; case Magic magic: var path = magic switch { AirMagic _ => MagicPath.Air, AstralMagic _ => MagicPath.Astral, BloodMagic _ => MagicPath.Blood, DeathMagic _ => MagicPath.Death, EarthMagic _ => MagicPath.Earth, FireMagic _ => MagicPath.Fire, HolyMagic _ => MagicPath.Holy, NatureMagic _ => MagicPath.Nature, WaterMagic _ => MagicPath.Water, _ => throw new ArgumentOutOfRangeException(nameof(magic)) }; var magicOverride = new MagicOverride(path, magic.MagicLevel); commander.MagicOverrides.Add(magicOverride); break; case ClearMagic _: //TODO break; default: throw new ArgumentOutOfRangeException(nameof(ownedByCommander)); } } } return(provinces); }