using System.Collections.Generic; namespace ET { [FriendClass(typeof(Weather))] [FriendClass(typeof(Unit))] public static class WeatherSystem { public static void FromMessage(this Weather self, WeatherProto weatherProto) { self.Id = weatherProto.Id; self.ConfigId = weatherProto.ConfigId; self.State = weatherProto.State; } public static WeatherProto ToMessage(this Weather self) { WeatherProto weatherProto = new WeatherProto(); weatherProto.Id = self.Id; weatherProto.State = self.State; return weatherProto; } public static void Produce(this Weather self,Unit unit) { if (self.Times >= 3 || self.ConfigId>0) { return; } var seasonConfig = unit.GetSeasonConfig(); var randomTimes = 0; //随机季节 while (self.ConfigId==0 && randomTimes>=seasonConfig.WeatherGroup.Length) { var index = RandomHelper.RandomByWeight(seasonConfig.Weights); var weatherId = seasonConfig.WeatherGroup[index]; randomTimes++; WeatherInfo weatherInfo; if (self.WeatherHistory.TryGetValue(weatherId,out weatherInfo)) { if (weatherInfo.Cd >= unit.Day - weatherInfo.StartDay) { self.ConfigId = weatherId; } } else { self.ConfigId = weatherId; } } if (self.ConfigId == 0) { foreach (var v in seasonConfig.WeatherGroup) { WeatherInfo weatherInfo; if (self.WeatherHistory.TryGetValue(v,out weatherInfo)) { if (weatherInfo.Cd >= unit.Day - weatherInfo.StartDay) { self.ConfigId = v; break; } } else { self.ConfigId = v; break; } } } if (self.ConfigId > 0) { self.Duration = RandomHelper.RandomNumber(self.Config.Duration[0], self.Config.Duration[1]+1); var cd = RandomHelper.RandomNumber(self.Config.CD[0], self.Config.CD[1]+1); self.StartTime = RandomHelper.RandomNumber(10, 16) * 100 + RandomHelper.RandomNumber(0, 60); self.WeatherHistory[self.ConfigId] = new WeatherInfo() { ConfigId = self.ConfigId, Cd = cd, StartDay = unit.Day }; } } public static void CheckStart(this Weather self,Unit unit,int gameTime) { if (self.ConfigId == 0) { return; } if (self.StartTime < gameTime && self.State == 0) { self.State = 1; #if SERVER UnitHelper.NotifyWeatherStart(unit, self.ConfigId); #endif } } public static void Update(this Weather self, Unit unit, int tick) { if (self.State == 0) { return; } self.Duration -= tick; if (self.Duration <= 0) { self.State = 0; #if SERVER unit.OnWeatherEnd(self.ConfigId); UnitHelper.NotifyWeatherEnd(unit, self.ConfigId); #endif self.ConfigId = 0; } } } }