You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
204 lines
7.7 KiB
204 lines
7.7 KiB
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<Unit> Create(Scene scene, long id, UnitType unitType) |
|
{ |
|
UnitComponent unitComponent = scene.GetComponent<UnitComponent>(); |
|
switch (unitType) |
|
{ |
|
case UnitType.Player: |
|
{ |
|
Unit unit = unitComponent.AddChildWithId<Unit>(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<Unit> Create(Scene scene, long id) |
|
{ |
|
UnitComponent unitComponent = scene.GetComponent<UnitComponent>(); |
|
Unit unit = await DBManagerComponent.Instance.GetZoneDB(scene.Zone).Query<Unit>(id); |
|
if (unit != null) |
|
{ |
|
SynthesisComponent sc = unit.GetComponent<SynthesisComponent>(); |
|
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<Unit>(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<PeopleComponent>(); |
|
var initConfig = InitialResourceConfigCategory.Instance.Get(1); |
|
|
|
int pos = 1; |
|
foreach (var id in initConfig.Villagers) |
|
{ |
|
People people = peopleComponent.AddChild<People, int>(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<SkillComponent>(); |
|
var skill = skillcom.AddChild<Skill>(); |
|
skill.ConfigId = attriConfig.SkillSet; |
|
skill.Level = 1; |
|
people.CurrSkillId = attriConfig.SkillSet; |
|
pos++; |
|
} |
|
} |
|
|
|
var roleInfo = await DBManagerComponent.Instance.GetZoneDB(scene.Zone).Query<RoleInfo>(unit.Id); |
|
var hero = peopleComponent.AddChild<People, int>(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<SkillComponent>(); |
|
var skill = skillcom.AddChild<Skill>(); |
|
skill.ConfigId = attriConfig.SkillSet; |
|
skill.Level = 1; |
|
hero.CurrSkillId = attriConfig.SkillSet; |
|
} |
|
} |
|
|
|
public static void InitValleyComponent(Unit unit) |
|
{ |
|
// NumericComponent nc= unit.GetOrAddComponent<NumericComponent>(); |
|
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<ResourcePointComponent>(); |
|
MonsterGroupComponent mgc = unit.GetOrAddComponent<MonsterGroupComponent>(); |
|
|
|
// read from json |
|
string jsonData = File.ReadAllText("../Config/Resource/InitResource.json"); |
|
MainSceneInitResource ir = JsonHelper.FromJson<MainSceneInitResource>(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>(); |
|
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<MenuComponent>(); |
|
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<MenuComponent>(); |
|
foreach (var v in initConfig.Value) |
|
{ |
|
menuComponent.Add(v, 0); |
|
}*/ |
|
} |
|
|
|
public static void InitWeather(Unit unit) |
|
{ |
|
Weather weather = unit.AddChild<Weather>(); |
|
unit.WeatherId = weather.Id; |
|
} |
|
|
|
|
|
} |
|
} |