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.
 
 
 
 
 
 

239 lines
7.1 KiB

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();
}
}
}
}
#endif
[FriendClass(typeof(Unit))]
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.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();
}
public static void updateSeason(this Unit self)
{
self.SeasonConfig = UnitOperate.GetSeasonConfigByDay(self.Day);
if (self.Season == self.SeasonConfig.Id)
{
return;
}
self.Season = self.SeasonConfig.Id;
UnitHelper.NofityUpdateValley(self, new List<int> { NumericType.Season }, new List<long> { self.Season });
//如果是春天,长大一岁
}
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 AddFarmland(this Unit self,Farmland farmland)
{
self.FarmlandDic[farmland.Id] = farmland;
}
public static void RemoveFarmland(this Unit self, Farmland farmland)
{
self.FarmlandDic.Remove(farmland.Id);
}
public static void UpdateGameTime(this Unit self)
{
foreach (var v in self.FarmlandDic.Values)
{
((Farmland)v).Update();
}
}
}
}