using System.Collections.Generic; using UnityEngine; namespace ET { [FriendClass(typeof(Unit))] public static class UnitFactory { public static Unit Create(Scene currentScene, UnitProto unitProto) { UnitComponent unitComponent = currentScene.GetComponent(); Unit unit = unitComponent.AddChildWithId(unitProto.Id); unit.Water = unitProto.Water; unit.Food = unitProto.Food; unit.Season = unitProto.Season; unit.GameTime = unitProto.GameTime; unit.SilverTael = unitProto.SilverTael; unit.GoldIngot = unitProto.GoldIngot; unit.Name = unitProto.Name; unit.Scale = unitProto.Scale; unit.Day = unitProto.Day; unit.EventSeed = unitProto.EventSeed; unit.EventNames = unitProto.EventNames; for (int i = 0; i < unitProto.FighterList.Count / 2; i++) { unit.FighterDic[(int)unitProto.FighterList[i * 2]] = unitProto.FighterList[i * 2 + 1]; } BuildingComponent buildingComponent = unit.AddComponent(); foreach (var info in unitProto.Buildings) { Building building = buildingComponent.AddChildWithId(info.Id); building.FromMessage(info); //buildingComponent.Add(building); } PeopleComponent pc = unit.GetOrAddComponent(); foreach (var v in unitProto.PeopleList) { People people = pc.AddChildWithId(v.Id,v.ConfigId); people.FromMessage(v); people.InitNumericComponent(); } ResourcePointComponent rc = unit.GetOrAddComponent(); foreach (var v in unitProto.ResourceList) { ResourcePoint res = rc.AddChildWithId(v.Id); res.FromMessage(v); } StoreComponent sc = unit.GetOrAddComponent(); foreach (var v in unitProto.Store) { sc.Add(v.ConfigId,v.Amount); } if (unitProto.GatherList.Count > 0) { GatherComponent gc = unit.GetOrAddComponent(); foreach (var v in unitProto.GatherList) { Gather gather = gc.AddChildWithId(v.Id); gather.FromMessage(v); } } if (unitProto.ConstructList.Count > 0) { ConstructComponent gc = unit.GetOrAddComponent(); foreach (var v in unitProto.ConstructList) { Construct construct = gc.AddChildWithId(v.Id); construct.FromMessage(v); } } if (unitProto.SynthesisList.Count > 0) { var now = TimeHelper.ServerNow(); SynthesisComponent synthesisComponent = unit.GetOrAddComponent(); foreach (var v in unitProto.SynthesisList) { Synthesis synthesis = synthesisComponent.AddChildWithId(v.ConfigId); synthesis.FromMessage(v); // synthesis.UpdateTime = now; } } if (unitProto.MenuList.Count > 0) { MenuComponent menuComponent = unit.GetOrAddComponent(); foreach (var v in unitProto.MenuList) { Menu menu = menuComponent.AddChildWithId(v.ConfigId); menu.FromMessage(v); } } if (unitProto.MonsterGroupList.Count > 0) { MonsterGroupComponent monsterGroupComponent = unit.GetOrAddComponent(); foreach (var v in unitProto.MonsterGroupList) { MonsterGroup monsterGroup = monsterGroupComponent.AddChildWithId(v.Id,v.ConfigId); monsterGroup.FromMessage(v); } } unitComponent.Add(unit); unit.AddComponent(); unit.InitGrandChildren(); Game.EventSystem.Publish(new EventType.AfterUnitCreate() {Unit = unit}); return unit; } } }