using System; using System.Collections.Generic; namespace ET { public class UnitAwakeSystem: AwakeSystem { public override void Awake(Unit self) { } } #if SERVER public class UnitUpdateSystem: UpdateSystem { public override void Update(Unit self) { if (self.UpdateTime > 0) { var now = TimeHelper.ServerNow(); var nowSec = now / 1000; var dt = now - self.UpdateTime; //更新桃谷时间 var timeRate = WorldParametersConfigCategory.Instance.Get(WorldParam.TimeShift).Value[0]; if (dt > timeRate) { int tick = Convert.ToInt32(dt / timeRate); self.UpdateTime += tick* timeRate; UnitSystem.AddTime(self, tick); } UnitSystem.Update(self,now); var delta = nowSec - self.UpdateValleyTime; if (delta > 59) { self.UpdateValleyTime += 60; UnitOperate.UpdateFood(self); UnitOperate.UpdateWater(self); self.DurableReduce(); self.UpdateFarmland(); } } } } #endif [FriendClass(typeof(People))] [FriendClass(typeof (Unit))] [FriendClass(typeof (Farmland))] [FriendClass(typeof (Building))] [FriendClass(typeof (Cabin))] public static class UnitSystem { public static void AddTime(this Unit self, int tick) { #if SERVER self.UpdateGameTime(tick); var gatherComponent = self.GetComponent(); if (gatherComponent != null) { gatherComponent.Update(self, tick); } var constructComponent = self.GetComponent(); if (constructComponent != null) { constructComponent.Update(self, tick); } var cuisine = self.GetComponent(); if (cuisine!=null) { cuisine.Update(self, tick); } #endif } public static void Update(this Unit self, long now) { #if SERVER var synthesisComponent = self.GetComponent(); if (synthesisComponent != null) { synthesisComponent.Update(self, now); } var dt = now - self.UpdateTimeSec; dt = dt / 1000; if (dt > 0) { self.GetChild(self.WeatherId)?.Update(self,(int)dt); self.UpdateTimeSec += dt * 1000; } #endif } public static void UpdateGameTime(this Unit self, int tick) { var gameTime = self.GameTime; if (gameTime > 0) { var m = gameTime % 100; var h = gameTime / 100; m = m + tick; h = (h + m / 60); m = m % 60; var time = h * 100 + m; // todo 月圆之夜 入夜时间加长 var nightTime = self.GetNightTime(); if (time >= nightTime) { Log.Info($"----------------day: {self.Day}, nightTime: {nightTime}"); self.Day++; self.updateSeason(); time = self.SeasonConfig.DayTime[0]; self.EventSeed = 1; UnitOperate.NightEvent(self, nightTime, time); //每天长大一岁 self.AddAge(); //触发天气 self.GetChild(self.WeatherId)?.Produce(self); } self.GameTime = time; self.GetChild(self.WeatherId)?.CheckStart(self,self.GameTime); } } public static int GetNightTime(this Unit self) { var nightTime = self.SeasonConfig.DayTime[1]; nightTime = UnitOperate.CheckMoonNightTime(self.Day, nightTime); return nightTime; } public static bool IsMoonNight(this Unit self) { return self.GameTime >= self.SeasonConfig.DayTime[1]; } public static bool CanProductDew(this Unit self) { var canProductTime = self.SeasonConfig.DayTime[0] + 200; return self.GameTime < canProductTime; } public static void InitPeopleNumberic(this Unit self) { PeopleComponent pc = self.GetComponent(); foreach (var v in pc.Children.Values) { ((People) v).InitNumericComponent(); } } public static void Init(this Unit self) { self.UpdateTime = TimeHelper.ServerNow(); self.UpdateValleyTime = TimeHelper.ServerUnix(); self.SeasonConfig = SeasonConfigCategory.Instance.Get(self.Season); self.InitPeopleNumberic(); self.InitGrandChildren(); self.OnCheckCabin(); self.UpdateTimeSec = self.UpdateTime; } public static void updateSeason(this Unit self) { int seasonDay = WorldParametersConfigCategory.Instance.Get(WorldParam.SeasonDay).Value[0]; int season = (self.Day - 1)/seasonDay%4+1; if (season == self.Season) { return; } self.Season = season; self.SeasonConfig = UnitOperate.GetSeasonConfigByDay(self.Day,season); #if SERVER UnitHelper.NofityUpdateValley(self, new List { NumericType.Season }, new List { self.Season }); #endif } public static SeasonConfig GetSeasonConfig(this Unit self) { return UnitOperate.GetSeasonConfigByDay(self.Day, self.Season); } public static void SetEmbattle(this Unit self, Dictionary FighterDic) { self.FighterDic = FighterDic; } public static void EnterBattle(this Unit self, long enemyUnitId) { } public static bool ExitBattle(this Unit self) { var battle = self.GetChild(self.BattleId); if (battle == null) { return false; } self.BattleId = 0; battle.Dispose(); return true; } public static bool CanBorn(this Unit self, int resConfigId) { var resConfig = ResourcesConfigCategory.Instance.Get(resConfigId); for (int i = 0; i < resConfig.RebornCondition.Length; i++) { if (resConfig.RebornCondition[i] == (int) ResourcesRebornConditionEnum.SEASON && resConfig.RebornConditionParameters[i] != self.Season) { return false; } } return true; } public static bool CanReborn(this Unit self, int resConfigId) { var resConfig = ResourcesConfigCategory.Instance.Get(resConfigId); for (int i = 0; i < resConfig.RebornCondition.Length; i++) { if (resConfig.RebornCondition[i] == (int) ResourcesRebornConditionEnum.SEASON && resConfig.RebornConditionParameters[i] != self.Season) { return false; } } return true; } //耐久度的更新 public static void DurableReduce(this Unit self) { self.GetComponent()?.DurableReduce(); } public static void UpdateFarmland(this Unit self) { //update farmland List farmlandIds = new List(); List progresses = new List(); List ripeIds = new List(); foreach (var v in self.GrandChildren.Values) { if (v.GetType() == typeof (Farmland)) { Farmland farmland = (Farmland) v; if (farmland.FarmlandState == FarmlandState.FARMLAND_STATE_GROW) { farmland.Update(self); if (farmland.FarmlandState == FarmlandState.FARMLAND_STATE_RIPE) { ripeIds.Add(farmland.Id); } else { farmlandIds.Add(farmland.Id); progresses.Add(farmland.Duration); } } } } #if SERVER FarmlandHelper.NotifyFarmlandRipe(self,ripeIds); FarmlandHelper.M2C_NtfFarmlandRipeProgress(self,farmlandIds,progresses); #endif } public static void AddAge(this Unit self) { foreach (var v in self.GetComponent().Children.Values) { ((People) v).GrowUp(); } } public static void InitGrandChildren(this Unit self) { // var bc = self.GetComponent(); if (bc != null) { foreach (var v in self.GetComponent().Children.Values) { foreach (var c in v.Children.Values) { self.AddGrandChild(c); } } } } public static T GetGrandChild(this Unit self, long id) where T : Entity { Entity child; if (self.GrandChildren.TryGetValue(id, out child)) { return (T) child; } return null; } public static void AddGrandChild(this Unit self, T entity) where T : Entity { self.GrandChildren[entity.Id] = entity; } public static void RemoveGrandChild(this Unit self, long id) { self.GrandChildren.Remove(id); } public static void OnWeatherEnd(this Unit self, int configId) { var config = WeatherConfigCategory.Instance.Get(configId); foreach (var v in self.GetComponent().Children.Values) { ((People) v).Disease += config.Disease; ((People) v).Injury += config.Injury; } foreach (var v in self.GetComponent().Children.Values) { ((Building) v).Durable -= config.DurableDestroy; } } public static void OnCheckCabin(this Unit self) { foreach (var v in self.GrandChildren.Values) { if (v.GetType() == typeof (Cabin)) { Cabin cabin = (Cabin) v; if (cabin.ResidentState == 1) { foreach (var id in cabin.FarmlandIds) { var farmland = self.GetGrandChild(id); if (farmland != null && farmland.FarmlandState == FarmlandState.FARMLAND_STATE_SEED) { farmland.FarmlandState = FarmlandState.FARMLAND_STATE_GROW; } } } } } } public static List GetGrandChildren(this Unit self) where T : Entity { List children = new List(); foreach (Entity grandChildrenValue in self.GrandChildren.Values) { if (grandChildrenValue is T) { children.Add(grandChildrenValue as T); } } return children; } } }