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.

384 lines
12 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;
3 years ago
//更新桃谷时间
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<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);
}
var dt = now - self.UpdateTimeSec;
dt = dt / 1000;
if (dt > 0)
{
self.GetChild<Weather>(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<Weather>(self.WeatherId)?.Produce(self);
}
self.GameTime = time;
self.GetChild<Weather>(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<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.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<int> { NumericType.Season }, new List<long> { 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<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;
}
3 years ago
//耐久度的更新
public static void DurableReduce(this Unit self)
{
self.GetComponent<BuildingComponent>()?.DurableReduce();
}
3 years ago
public static void UpdateFarmland(this Unit self)
3 years ago
{
//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)
3 years ago
{
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);
}
}
}
3 years ago
}
#if SERVER
FarmlandHelper.NotifyFarmlandRipe(self,ripeIds);
FarmlandHelper.M2C_NtfFarmlandRipeProgress(self,farmlandIds,progresses);
#endif
3 years ago
}
public static void AddAge(this Unit self)
{
foreach (var v in self.GetComponent<PeopleComponent>().Children.Values)
{
((People) v).GrowUp();
}
}
public static void InitGrandChildren(this Unit self)
{
//
var bc = self.GetComponent<BuildingComponent>();
if (bc != null)
{
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);
}
public static void OnWeatherEnd(this Unit self, int configId)
{
var config = WeatherConfigCategory.Instance.Get(configId);
foreach (var v in self.GetComponent<PeopleComponent>().Children.Values)
{
((People) v).Disease += config.Disease;
((People) v).Injury += config.Injury;
}
foreach (var v in self.GetComponent<BuildingComponent>().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<Farmland>(id);
if (farmland != null && farmland.FarmlandState == FarmlandState.FARMLAND_STATE_SEED)
{
farmland.FarmlandState = FarmlandState.FARMLAND_STATE_GROW;
}
}
}
}
}
}
public static List<T> GetGrandChildren<T>(this Unit self) where T : Entity
{
List<T> children = new List<T>();
foreach (Entity grandChildrenValue in self.GrandChildren.Values)
{
if (grandChildrenValue is T)
{
children.Add(grandChildrenValue as T);
}
}
return children;
}
}
}