using System; using System.Collections.Generic; using System.Linq; using ET.EventType; using UnityEngine; namespace ET { [FriendClass(typeof(Unit))] [FriendClass(typeof(People))] public static class UnitOperate { public static void InitProsperity(Unit unit) { var prosperity = 0; BuildingComponent bc = unit.GetComponent(); foreach (var v in bc.Children) { var building = (Building) v.Value; if (building.Config.Boom > 0) { prosperity += building.Config.Boom; } } // TODO 村民繁荣值 unit.Prosperity = prosperity; } public static void UpgradeScale(Unit unit) { var nextScale = (unit.Scale > 0? unit.Scale : 1) + 1; ScaleConfig sc = ScaleConfigCategory.Instance.Get(nextScale); if (unit.Prosperity >= sc.Prosperity) { unit.Scale = nextScale; RewardGroupConfig rewardGroupConfig = RewardGroupConfigCategory.Instance.Get(sc.RewardGroup); for (var i = 0; i < rewardGroupConfig.ItemId.Length; i++) { StoreOperate.AddItem(unit, rewardGroupConfig.ItemId[i], rewardGroupConfig.Number[i]); } } } public static int PeopleNum(Unit unit) { var peopleNum = unit.GetComponent().Children.Count; return peopleNum; } public static int TotalMinutesPerDay(Unit unit) { // TODO 一天的分钟数 var total = 5*12; return total; } public static long rectify(long origin = 0, long delta = 0,float min = float.NegativeInfinity, float max = float.PositiveInfinity) { var result = origin + delta; if (result < min) { result = (long)min; } if (result > max) { result = (long)max; } return result; } public static int FoodConsumptionPerMinute(Unit unit) { var peopleCount = PeopleNum(unit); WorldParametersConfig wc = WorldParametersConfigCategory.Instance.Get(WorldParam.FoodConsumption); return peopleCount * wc.Value[0]; } public static void UpdateFood(Unit unit) { var consume = FoodConsumptionPerMinute(unit); var previous = unit.Food ; unit.Food = rectify(unit.Food, -consume, -consume*TotalMinutesPerDay(unit)); if (unit.Food <= 0) { if (previous > 0) { // todo debuff } } else if (previous <= 0) { // todo buff } #if SERVER UnitHelper.NofityUpdateValley(unit, new List { NumericType.Food }, new List { unit.Food }); #endif } public static int WaterConsumptionPerMinute(Unit unit) { var peopleCount = PeopleNum(unit); WorldParametersConfig wc = WorldParametersConfigCategory.Instance.Get(WorldParam.WaterConsumption); return peopleCount * wc.Value[0]; } public static void UpdateWater(Unit unit) { var consume = WaterConsumptionPerMinute(unit); var previous = unit.Water ; unit.Water = rectify(unit.Water, -consume, -consume*TotalMinutesPerDay(unit)); if (unit.Water <= 0) { unit.Water = 0; if (previous > 0) { // todo debuff } } else if (previous <= 0) { // todo buff } #if SERVER UnitHelper.NofityUpdateValley(unit, new List { NumericType.Water }, new List { unit.Water }); #endif } public static EventGroupConfig GetNightEventConfig() { EventGroupConfig eventGroupConfig= null; List list = EventGroupConfigCategory.Instance.GetList(); foreach (var v in list) { if (v.Type == 2) { eventGroupConfig = v; break; } } return eventGroupConfig; } public static int[] AddArray(int[] probs, int[] overlay, int index) { if (probs == null || overlay == null) { return probs; } for (var i=0; i NightEvent(Unit unit, int nightTime, int time) { EventGroupConfig egc = GetNightEventConfig(); var nightEvents = new List(); Log.Info($"EventGroupConfig: {egc.ToString()}"); int[] eventWeight = CopyArray(egc.EventWeight); int[] overlayWeight = CopyArray(egc.OverlayWeight); int[] eventList = CopyArray(egc.Event); Log.Info($"eventWeight: {string.Join(",", eventWeight)} :{egc.EventWeight.Clone()}"); Log.Info($"overlayWeight: {string.Join(",", overlayWeight)}"); Log.Info($"eventList: {string.Join(",", eventList)}"); int eventCount = 0; int h = 23 - nightTime / 100; int m = 60 - (nightTime % 100); h += time / 100; m += time % 100; int topM = m + h * 60; int curM = 0; List timeList = new List(); var rand = new ValleyRandom(); rand.SetSeed(unit.EventSeed); while (topM > curM) { var range = rand.RandomNumber(80, 120); curM += range; if (curM > topM) { break; } eventCount++; timeList.Add(TimePass(nightTime, curM)); } for (var i = 0; i < eventCount; i++) { int index =RandomHelper.RandomByWeight(eventWeight); Log.Info($"eventWeight: {string.Join(",",eventWeight)}, eventList: {string.Join(",",eventList)}, index: {string.Join(",",index)}"); if (index == -1) { continue; } int id = eventList[index]; NightEvent nightEvent = new NightEvent(); nightEvent.EventId = id; nightEvent.Time = timeList[i]; nightEvents.Add(nightEvent); eventWeight = AddArray(eventWeight, overlayWeight, index); } foreach (var ne in nightEvents) { NightEvent nightEvent = ne; EventConfig eventConfig = EventConfigCategory.Instance.Get(nightEvent.EventId); // todo 掉落组数量 if (eventConfig.DropGroup[0] > 0) { nightEvent.Drops = new Dictionary(); foreach (var v in eventConfig.DropGroup) { (int dropId,int num) = DropOperate.DropItem(unit, v); if (!nightEvent.Drops.ContainsKey(dropId)) { nightEvent.Drops[dropId] = 0; } nightEvent.Drops[dropId] += num; } } } #if SERVER List names = new List(); foreach (var v in unit.GetComponent().Children.Values) { names.Add(((People)v).Name); } unit.EventNames = new List(); foreach (var ne in nightEvents) { var index = RandomHelper.RandomNumber(0, names.Count - 1); unit.EventNames.Add(names[index]); ne.Name = names[index]; } MessageHelper.SendToClient(unit,new M2C_NotifyNightEvent(){EventSeed = unit.EventSeed, NightTime = nightTime, Time = time, EventNames = unit.EventNames}); #else for (var i=0; i day) { stage = v.Id; } } List listConfig = SeasonConfigCategory.Instance.GetList(); foreach (var v in listConfig) { if (v.Tapy == season && v.Stage == stage) { return v; } } return null; } } }