/// <summary> /// 初始化(第一次创建Unit走这里,因为服务端穿的属性是加了BUFF后的属性,所以这里创建BUFF时不叠加属性) /// </summary> /// <param name="self"></param> /// <param name="buffIds"></param> /// <param name="buffTimestamps"></param> public static void Init(this BuffComponent self, List <int> buffIds, List <long> buffTimestamps) { self.Groups = DictionaryComponent <int, Buff> .Create(); self.ActionControls = DictionaryComponent <int, int> .Create(); for (int i = 0; i < buffIds.Count; i++) { var id = buffIds[i]; var timestamp = buffTimestamps[i]; BuffConfig conf = BuffConfigCategory.Instance.Get(id); if (self.Groups.ContainsKey(conf.Group)) { var old = self.Groups[conf.Group]; if (old.Config.Priority > conf.Priority) { Log.Info("添加BUFF失败,优先级" + old.Config.Id + " > " + conf.Id); continue; //优先级低 } Log.Info("优先级高或相同,替换旧的"); self.Remove(self.Groups[conf.Group].Id); } Buff buff = self.AddChild <Buff, int, long, bool>(id, timestamp, true);//走这里不叠加属性 self.Groups[conf.Group] = buff; EventSystem.Instance.Publish(new EventType.AfterAddBuff() { Buff = buff }); } }
public override void Awake(SkillStepComponent self) { SkillStepComponent.Instance = self; self.Params = DictionaryComponent <int, List <object[]> > .Create(); self.StepType = DictionaryComponent <int, List <int> > .Create(); self.TimeLine = DictionaryComponent <int, List <int> > .Create(); }
public override void Awake(SceneLoadComponent self) { self.PreLoadTask = ListComponent <ETTask> .Create(); self.Paths = ListComponent <string> .Create(); self.Types = ListComponent <int> .Create(); self.ObjCount = DictionaryComponent <string, int> .Create(); self.Total = 0; self.FinishCount = 0; }
public static bool Raycast(AOISceneComponent scene, Ray ray, out RaycastHit hit, UnitType[] type = null) { hit = default; if (type == null) { return(false); } using (DictionaryComponent <UnitType, bool> typeTemp = DictionaryComponent <UnitType, bool> .Create()) { using (HashSetComponent <AOITriggerComponent> temp = HashSetComponent <AOITriggerComponent> .Create()) { for (int i = 0; i < type.Length; i++) { var item = type[i]; typeTemp.Add(item, true); } int xIndex = (int)Math.Floor(ray.Start.x / scene.gridLen); int yIndex = (int)Math.Floor(ray.Start.z / scene.gridLen); //z = kx+b float k = 0; float k_1 = 0; float b = 0; if (ray.Dir.x != 0 && ray.Dir.z != 0) { k = ray.Dir.z / ray.Dir.x; k_1 = ray.Dir.x / ray.Dir.z; b = ray.Start.z - k * ray.Start.x; } Vector3 inPoint = ray.Start; while (true) { long cellId = AOIHelper.CreateCellId(xIndex, yIndex); AOICell cell = scene.GetChild <AOICell>(cellId); var xMin = xIndex * scene.gridLen; var xMax = xMin + scene.gridLen; var yMin = yIndex * scene.gridLen; var yMax = yMin + scene.gridLen; //Log.Info("Raycast Check "+xIndex+" "+yIndex); if (cell != null) { ListComponent <RaycastHit> hits = ListComponent <RaycastHit> .Create(); RaycastHits(ray, cell, inPoint, hits, temp, typeTemp); if (hits.Count > 0) { hits.KSsort((i1, i2) => i1.Distance >= i2.Distance?1:-1);//从小到大 hit = hits[0]; //Log.Info("hits.Count > 0"+hit.Trigger.Parent.Parent.Id); hits.Dispose(); return(true); } } //一般情况 if (ray.Dir.x != 0 && ray.Dir.z != 0) { if (ray.Dir.x > 0 && ray.Dir.z > 0) { var z1 = xMax * k + b; if (z1 > yMin && z1 < yMax) { xIndex++; inPoint = new Vector3(xMax, inPoint.y + (xMax - inPoint.x) * ray.Dir.y / ray.Dir.x, z1); } else { yIndex++; inPoint = new Vector3((yMax - b) * k_1, inPoint.y + (yMax - inPoint.z) * ray.Dir.y / ray.Dir.z, yMax); } } else if (ray.Dir.x > 0 && ray.Dir.z < 0) { var z1 = xMax * k + b; if (z1 > yMin && z1 < yMax) { xIndex++; inPoint = new Vector3(xMax, inPoint.y + (xMax - inPoint.x) * ray.Dir.y / ray.Dir.x, z1); } else { yIndex--; inPoint = new Vector3((yMin - b) * k_1, inPoint.y + (yMin - inPoint.z) * ray.Dir.y / ray.Dir.z, yMin); } } else if (ray.Dir.x < 0 && ray.Dir.z < 0) { var z1 = xMin * k + b; if (z1 > yMin && z1 < yMax) { xIndex--; inPoint = new Vector3(xMin, inPoint.y + (xMin - inPoint.x) * ray.Dir.y / ray.Dir.x, z1); } else { yIndex--; inPoint = new Vector3((yMin - b) * k_1, inPoint.y + (yMin - inPoint.z) * ray.Dir.y / ray.Dir.z, yMin); } } else if (ray.Dir.x < 0 && ray.Dir.z > 0) { var z1 = xMin * k + b; if (z1 > yMin && z1 < yMax) { xIndex--; inPoint = new Vector3(xMin, inPoint.y + (xMin - inPoint.x) * ray.Dir.y / ray.Dir.x, z1); } else { yIndex++; inPoint = new Vector3((yMax - b) * k_1, inPoint.y + (yMax - inPoint.z) * ray.Dir.y / ray.Dir.z, yMax); } } else { Log.Error("What's f**k???"); } } //平行于轴了 else if (ray.Dir.x == 0 && ray.Dir.z != 0) { if (ray.Dir.z > 0) { yIndex++; inPoint = new Vector3(inPoint.x, inPoint.y + (yMax - inPoint.z) * ray.Dir.y / ray.Dir.z, yMax); } else { yIndex--; inPoint = new Vector3(inPoint.x, inPoint.y + (yMin - inPoint.z) * ray.Dir.y / ray.Dir.z, yMin); } } else if (ray.Dir.z == 0 && ray.Dir.x != 0) { if (ray.Dir.x > 0) { xIndex++; inPoint = new Vector3(xMax, inPoint.y + (xMax - inPoint.x) * ray.Dir.y / ray.Dir.x, inPoint.z); } else { xIndex--; inPoint = new Vector3(xMin, inPoint.y + (xMin - inPoint.x) * ray.Dir.y / ray.Dir.x, inPoint.z); } } //垂直于地图 else { break; } if (Vector3.Distance(inPoint, ray.Start) > ray.Distance) { break; } } } } return(false); }
/// <summary> /// 改变格子 /// </summary> /// <param name="self"></param> /// <param name="newgrid"></param> public static void ChangeTo(this AOIUnitComponent self, AOICell newgrid) { AOICell oldgrid = self.Cell; Log.Info(self.Id + "From: " + " grid x:" + oldgrid.posx + ",y:" + oldgrid.posy + " ChangeTo:grid x:" + newgrid.posx + ",y:" + newgrid.posy); #region 广播给别人 using (DictionaryComponent <AOIUnitComponent, int> dic = DictionaryComponent <AOIUnitComponent, int> .Create()) { //Remove if (oldgrid.idUnits.ContainsKey(self.Type)) { for (int i = 0; i < oldgrid.ListenerUnits.Count; i++) { var item = oldgrid.ListenerUnits[i]; if (item.Type == UnitType.Player && item != self) { dic.Add(item, -1); } } oldgrid.idUnits[self.Type].Remove(self); self.Cell = null; } else { Log.Error("unit.Type=" + self.Type + "未添加就删除"); } //Add self.Cell = newgrid; if (Define.Debug && newgrid.idUnits[self.Type].Contains(self)) { Log.Error("newgrid.idUnits[self.Type].Contains(self)"); } newgrid.idUnits[self.Type].Add(self); for (int i = 0; i < newgrid.ListenerUnits.Count; i++) { var item = newgrid.ListenerUnits[i]; if (item.Type == UnitType.Player && item != self) { if (dic.ContainsKey(item)) { dic[item] += 1; } else { dic.Add(item, 1); } } } foreach (var item in dic) { if (item.Value > 0) { Game.EventSystem.Publish(new EventType.AOIRegisterUnit() { Receive = item.Key, Unit = self, }); } else if (item.Value < 0) { Game.EventSystem.Publish(new EventType.AOIRemoveUnit() { Receive = item.Key, Unit = self }); } } } #endregion #region 广播给自己 && 刷新监听 var older = oldgrid.GetNearbyGrid(self.Range); var newer = newgrid.GetNearbyGrid(self.Range); DictionaryComponent <AOICell, int> temp = DictionaryComponent <AOICell, int> .Create(); for (int i = 0; i < older.Count; i++) { var item = older[i]; temp[item] = -1; } for (int i = 0; i < newer.Count; i++) { var item = newer[i]; if (temp.ContainsKey(item)) { temp[item] = 0; } else { temp[item] = 1; } } ListComponent <AOIUnitComponent> adder = ListComponent <AOIUnitComponent> .Create(); ListComponent <AOIUnitComponent> remover = ListComponent <AOIUnitComponent> .Create(); foreach (var item in temp) { if (item.Value > 0) { item.Key.AddListener(self); adder.AddRange(item.Key.GetAllUnit()); } else if (item.Value < 0) { item.Key.RemoveListener(self); remover.AddRange(item.Key.GetAllUnit()); } } if (self.Type == UnitType.Player) { for (int i = 0; i < adder.Count; i++) { var item = adder[i]; if (item == self) { continue; } Log.Info("AOIRegisterUnit" + item.Id); Game.EventSystem.Publish(new EventType.AOIRegisterUnit { Receive = self, Unit = item }); } for (int i = 0; i < remover.Count; i++) { var item = remover[i]; if (item == self) { continue; } Log.Info("AOIRemoveUnit" + item.Id); Game.EventSystem.Publish(new EventType.AOIRemoveUnit() { Receive = self, Unit = item }); } } temp.Dispose(); newer.Dispose(); older.Dispose(); adder.Dispose(); remover.Dispose(); #endregion }
/// <summary> /// 改变格子 /// </summary> /// <param name="x"></param> /// <param name="y"></param> /// <param name="viewLen"></param> public static async ETTask ChangeGrid(this AOISceneViewComponent self, int x, int y, int viewLen) { CoroutineLock coroutineLock = null; try { coroutineLock = await CoroutineLockComponent.Instance.Wait(CoroutineLockType.AOIView, self.GetHashCode()); while (SceneManagerComponent.Instance.Busing) { await TimerComponent.Instance.WaitAsync(1); } if (self.LastGridX != null) { int count = 0; count += Math.Abs(x - (int)self.LastGridX); count += Math.Abs(y - (int)self.LastGridY); if (count > 4) //太远了走loading { self.ChangeToScene().Coroutine(); } } DictionaryComponent <long, int> temp = DictionaryComponent <long, int> .Create(); for (int i = -viewLen; i <= viewLen; i++) { for (int j = -viewLen; j <= viewLen; j++) { if (self.LastGridY != null) { var oldid = AOIHelper.CreateCellId((int)self.LastGridX + i, (int)self.LastGridY + j); if (!temp.ContainsKey(oldid)) { temp[oldid] = 0; } temp[oldid]--; } var newid = AOIHelper.CreateCellId(x + i, y + j); if (!temp.ContainsKey(newid)) { temp[newid] = 0; } temp[newid]++; } } foreach (var item in temp) { if (item.Value == 0) { continue; } var objs = self.DynamicSceneMap[self.CurMap].GridMapObjects; if (!objs.ContainsKey(item.Key)) { continue; } for (int i = 0; i < objs[item.Key].Count; i++) { var obj = objs[item.Key][i]; if (item.Value > 0) //新增 { if (self.DynamicSceneObjectMapCount.ContainsKey(obj)) { self.DynamicSceneObjectMapCount[obj]++; } else { self.DynamicSceneObjectMapCount[obj] = 1; } //需要显示 if (self.DynamicSceneObjectMapCount[obj] > 0) { AOISceneViewComponent.DynamicSceneViewObj viewObj; //已经有 if (self.DynamicSceneObjectMapObj.ContainsKey(obj)) { viewObj = self.DynamicSceneObjectMapObj[obj]; if (viewObj.Obj == null) //之前有单没加载出来,IsLoading改为true,防止之前已经被改成false了 { viewObj.IsLoading = true; } continue; } Log.Info("AOISceneView Load " + obj.Path); //没有 self.DynamicSceneObjectMapObj[obj] = new AOISceneViewComponent.DynamicSceneViewObj(); viewObj = self.DynamicSceneObjectMapObj[obj]; viewObj.IsLoading = true; GameObjectPoolComponent.Instance.GetGameObjectAsync(obj.Path, (view) => { if (!viewObj.IsLoading) //加载出来后已经不需要的 { GameObjectPoolComponent.Instance.RecycleGameObject(view); self.DynamicSceneObjectMapObj.Remove(obj); } viewObj.Obj = view; viewObj.IsLoading = false; view.transform.position = obj.Position; view.transform.rotation = obj.Rotation; view.transform.localScale = obj.Scale; view.transform.parent = GlobalComponent.Instance.Scene; }).Coroutine(); } } else //移除 { if (self.DynamicSceneObjectMapCount.ContainsKey(obj)) { self.DynamicSceneObjectMapCount[obj]--; } else { self.DynamicSceneObjectMapCount[obj] = -1; } //不需要显示但有 if (self.DynamicSceneObjectMapCount[obj] <= 0 && self.DynamicSceneObjectMapObj.ContainsKey(obj)) { Log.Info("AOISceneView Remove " + obj.Path); var viewObj = self.DynamicSceneObjectMapObj[obj]; if (viewObj.Obj == null) //还在加载 { viewObj.IsLoading = false; } else { viewObj.Obj.SetActive(false); GameObjectPoolComponent.Instance.RecycleGameObject(viewObj.Obj); self.DynamicSceneObjectMapObj.Remove(obj); } } } } } temp.Dispose(); self.LastGridX = x; self.LastGridY = y; } finally { coroutineLock?.Dispose(); } }
public override void Awake(BuffComponent self) { self.Groups = DictionaryComponent <int, Buff> .Create(); self.ActionControls = DictionaryComponent <int, int> .Create(); }