|
|
using System; |
|
|
using System.Collections.Generic; |
|
|
|
|
|
namespace ET |
|
|
{ |
|
|
public class UnitAwakeSystem: AwakeSystem<Unit> |
|
|
{ |
|
|
public override void Awake(Unit self) |
|
|
{ |
|
|
} |
|
|
} |
|
|
|
|
|
#if SERVER |
|
|
public class UnitUpdateSystem: UpdateSystem<Unit> |
|
|
{ |
|
|
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 (Unit))] |
|
|
[FriendClass(typeof (Farmland))] |
|
|
public static class UnitSystem |
|
|
{ |
|
|
public static void AddTime(this Unit self, int tick) |
|
|
{ |
|
|
#if SERVER |
|
|
self.UpdateGameTime(tick); |
|
|
var gatherComponent = self.GetComponent<GatherComponent>(); |
|
|
if (gatherComponent != null) |
|
|
{ |
|
|
gatherComponent.Update(self, tick); |
|
|
} |
|
|
|
|
|
var constructComponent = self.GetComponent<ConstructComponent>(); |
|
|
if (constructComponent != null) |
|
|
{ |
|
|
constructComponent.Update(self, tick); |
|
|
} |
|
|
|
|
|
var cuisine = self.GetComponent<CuisineComponent>(); |
|
|
if (cuisine!=null) |
|
|
{ |
|
|
cuisine.Update(self, tick); |
|
|
} |
|
|
#endif |
|
|
} |
|
|
|
|
|
public static void Update(this Unit self, long now) |
|
|
{ |
|
|
#if SERVER |
|
|
var synthesisComponent = self.GetComponent<SynthesisComponent>(); |
|
|
if (synthesisComponent != null) |
|
|
{ |
|
|
synthesisComponent.Update(self, now); |
|
|
} |
|
|
|
|
|
#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.GameTime = time; |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
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<PeopleComponent>(); |
|
|
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.InitFarmlandDic(); |
|
|
} |
|
|
|
|
|
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<int> { NumericType.Season }, new List<long> { self.Season }); |
|
|
#endif |
|
|
} |
|
|
|
|
|
public static void SetEmbattle(this Unit self, Dictionary<int, long> FighterDic) |
|
|
{ |
|
|
self.FighterDic = FighterDic; |
|
|
} |
|
|
|
|
|
public static void EnterBattle(this Unit self, long enemyUnitId) |
|
|
{ |
|
|
} |
|
|
|
|
|
public static bool ExitBattle(this Unit self) |
|
|
{ |
|
|
var battle = self.GetChild<Battle>(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<BuildingComponent>()?.DurableReduce(); |
|
|
} |
|
|
|
|
|
public static void UpdateFarmland(this Unit self) |
|
|
{ |
|
|
//update farmland |
|
|
List<long> farmlandIds = new List<long>(); |
|
|
List<int> progresses = new List<int>(); |
|
|
List<long> ripeIds = new List<long>(); |
|
|
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<PeopleComponent>().Children.Values) |
|
|
{ |
|
|
((People) v).GrowUp(); |
|
|
} |
|
|
} |
|
|
|
|
|
public static void InitFarmlandDic(this Unit self) |
|
|
{ |
|
|
var bc = self.GetComponent<BuildingComponent>(); |
|
|
if (bc == null) |
|
|
{ |
|
|
return; |
|
|
} |
|
|
|
|
|
foreach (var v in self.GetComponent<BuildingComponent>().Children.Values) |
|
|
{ |
|
|
foreach (var c in v.Children.Values) |
|
|
{ |
|
|
self.AddGrandChild(c); |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
public static T GetGrandChild<T>(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<T>(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); |
|
|
} |
|
|
} |
|
|
} |