using System; using System.Collections.Generic; using System.IO; using ET; using UnityEngine; namespace ET { [FriendClass(typeof(SynthesisComponent))] [FriendClass(typeof(Synthesis))] [FriendClass(typeof(Unit))] [FriendClass(typeof(People))] [FriendClass(typeof(Skill))] [FriendClass(typeof(RoleInfo))] [FriendClass(typeof(MonsterGroup))] public static class UnitFactory { public static async ETTask Create(Scene scene, long id, UnitType unitType) { UnitComponent unitComponent = scene.GetComponent(); switch (unitType) { case UnitType.Player: { Unit unit = unitComponent.AddChildWithId(id); await InitPeopleComponent(unit, scene); InitValleyComponent(unit); InitResourceComponent(unit); InitStoreComponent(unit); InitMenuComponent(unit); InitPrescriptionComponent(unit); return unit; } default: throw new Exception($"not such unit type: {unitType}"); } } public static async ETTask Create(Scene scene, long id) { UnitComponent unitComponent = scene.GetComponent(); Unit unit = await DBManagerComponent.Instance.GetZoneDB(scene.Zone).Query(id); if (unit != null) { SynthesisComponent sc = unit.GetComponent(); if (sc != null) { foreach (var v in sc.Children) { Synthesis synthesis =(Synthesis) v.Value; synthesis.UpdateTime = TimeHelper.ServerNow(); } } unit.UpdateValleyTime = TimeHelper.ServerUnix(); Log.Info($"unit.UpdateValleyTime: {unit.UpdateValleyTime}"); unit.SeasonConfig = SeasonConfigCategory.Instance.Get(unit.Season); unitComponent.AddChild(unit); } else { unit = unitComponent.AddChildWithId(id); await InitPeopleComponent(unit, scene); InitValleyComponent(unit); InitResourceComponent(unit); InitStoreComponent(unit); InitMenuComponent(unit); InitPrescriptionComponent(unit); InitWeather(unit); } unit.InitPeopleNumberic(); return unit; } public static async ETTask InitPeopleComponent(Unit unit, Scene scene) { PeopleComponent peopleComponent = unit.GetOrAddComponent(); var initConfig = InitialResourceConfigCategory.Instance.Get(1); int pos = 1; foreach (var id in initConfig.Villagers) { People people = peopleComponent.AddChild(id); var role = RoleConfigCategory.Instance.Get(id); people.Name = role.Name; people.Level = 1; people.Exp = 0; people.Labor = 1; people.LaborExp = 0; people.Age = role.Age; if (role.AttributesGroup > 0) { var attriConfig = AttributesGroupConfigCategory.Instance.Get(role.AttributesGroup); unit.FighterDic[pos] = people.Id; var skillcom = people.GetOrAddComponent(); var skill = skillcom.AddChild(); skill.ConfigId = attriConfig.SkillSet; skill.Level = 1; people.CurrSkillId = attriConfig.SkillSet; pos++; } } var roleInfo = await DBManagerComponent.Instance.GetZoneDB(scene.Zone).Query(unit.Id); var hero = peopleComponent.AddChild(roleInfo.Gender); var heroConfig = RoleConfigCategory.Instance.Get(roleInfo.Gender); hero.Name = roleInfo.Name; hero.Level = 1; hero.Exp = 0; hero.Labor = 1; hero.LaborExp = 0; hero.Age = heroConfig.Age; if (heroConfig.AttributesGroup > 0) { var attriConfig = AttributesGroupConfigCategory.Instance.Get(heroConfig.AttributesGroup); unit.FighterDic[pos] = hero.Id; var skillcom = hero.GetOrAddComponent(); var skill = skillcom.AddChild(); skill.ConfigId = attriConfig.SkillSet; skill.Level = 1; hero.CurrSkillId = attriConfig.SkillSet; } } public static void InitValleyComponent(Unit unit) { // NumericComponent nc= unit.GetOrAddComponent(); var initConfig = InitialResourceConfigCategory.Instance.Get(1); unit.Food = initConfig.Food; unit.Water = initConfig.Water; unit.SilverTael = initConfig.SilverTael; unit.GoldIngot = initConfig.GoldIngot; unit.Season = initConfig.StartSeason; unit.GameTime = initConfig.StartTime; unit.Scale = 1; unit.Day = 1; unit.SeasonConfig = SeasonConfigCategory.Instance.Get(unit.Season); } public static void InitResourceComponent(Unit unit) { ResourcePointComponent rc = unit.GetOrAddComponent(); MonsterGroupComponent mgc = unit.GetOrAddComponent(); // read from json string jsonData = File.ReadAllText("../Config/Resource/InitResource.json"); MainSceneInitResource ir = JsonHelper.FromJson(jsonData); foreach (var item in ir.InitResourceItems) { var monsterGroup = mgc.CreateMonsterGroup(item); long monsterGroupId = 0; if (monsterGroup != null) { monsterGroupId = monsterGroup.Id; } var res = rc.CreateResourceByInitResItem(unit,item,monsterGroupId); if (monsterGroup != null) { monsterGroup.ResId = res.Id; } } } public static void InitStoreComponent(Unit unit) { var initConfig = InitialResourceConfigCategory.Instance.Get(1); var storeComponent = unit.GetOrAddComponent(); storeComponent.Add(1013, initConfig.Stone); storeComponent.Add(1011, initConfig.Wood); } public static void InitMenuComponent(Unit unit) { var initConfig = WorldParametersConfigCategory.Instance.Get(WorldParam.InitialMenu); var menuComponent = unit.GetOrAddComponent(); foreach (var v in initConfig.Value) { menuComponent.initMenu(v); } } public static void InitPrescriptionComponent(Unit unit) { var initConfig = WorldParametersConfigCategory.Instance.Get(WorldParam.InitialPrescription); /*var menuComponent = unit.GetOrAddComponent(); foreach (var v in initConfig.Value) { menuComponent.Add(v, 0); }*/ } public static void InitWeather(Unit unit) { Weather weather = unit.AddChild(); unit.WeatherId = weather.Id; } } }