public static async ETTask Init(this TargetSelectComponent self) { string path = "GameAssets/SkillPreview/Prefabs/TargetSelectManager.prefab"; string targetPath = "GameAssets/SkillPreview/Prefabs/TargetIcon.prefab"; using (ListComponent <ETTask <GameObject> > tasks = ListComponent <ETTask <GameObject> > .Create()) { tasks.Add(GameObjectPoolComponent.Instance.GetGameObjectAsync(targetPath, (obj) => { self.CursorImage = obj.GetComponent <Image>(); self.CursorImage.transform.parent = UIManagerComponent.Instance.GetLayer(UILayerNames.TipLayer).transform; self.CursorImage.transform.localPosition = Vector3.zero; self.CursorImage.rectTransform.anchoredPosition = Input.mousePosition; })); tasks.Add(GameObjectPoolComponent.Instance.GetGameObjectAsync(path, (obj) => { self.RangeCircleObj = obj.transform.Find("RangeCircle").gameObject; self.gameObject = obj; })); await ETTaskHelper.WaitAll(tasks); self.waiter.SetResult(self.gameObject); self.waiter = null; } }
public static bool ChangeSpeed(this MoveComponent self, float speed) { if (self.IsArrived()) { return(false); } if (speed < 0.0001) { return(false); } Unit unit = self.GetParent <Unit>(); using (ListComponent <Vector3> path = ListComponent <Vector3> .Create()) { self.MoveForward(true); path.Add(unit.Position); // 第一个是Unit的pos for (int i = self.N; i < self.Targets.Count; ++i) { path.Add(self.Targets[i]); } self.MoveToAsync(path, speed).Coroutine(); } return(true); }
/// <summary> /// 异步加载assetbundle, 加载ab包分两部分,第一部分是从硬盘加载,第二部分加载all assets。两者不能同时并发 /// </summary> public static async ETTask LoadBundleAsync(this ResourcesComponent self, string assetBundleName) { assetBundleName = assetBundleName.BundleNameToLower(); string[] dependencies = self.GetSortedDependencies(assetBundleName); //Log.Debug($"-----------dep load async start {assetBundleName} dep: {dependencies.ToList().ListToString()}"); using (ListComponent <ABInfo> abInfos = ListComponent <ABInfo> .Create()) { async ETTask LoadDependency(string dependency, List <ABInfo> abInfosList) { CoroutineLock coroutineLock = null; try { coroutineLock = await CoroutineLockComponent.Instance.Wait(CoroutineLockType.Resources, dependency.GetHashCode()); ABInfo abInfo = await self.LoadOneBundleAsync(dependency); if (abInfo == null || abInfo.RefCount > 1) { return; } abInfosList.Add(abInfo); } finally { coroutineLock?.Dispose(); } } // LoadFromFileAsync部分可以并发加载 using (ListComponent <ETTask> tasks = ListComponent <ETTask> .Create()) { foreach (string dependency in dependencies) { tasks.Add(LoadDependency(dependency, abInfos)); } await ETTaskHelper.WaitAll(tasks); // ab包从硬盘加载完成,可以再并发加载all assets tasks.Clear(); foreach (ABInfo abInfo in abInfos) { tasks.Add(self.LoadOneBundleAllAssets(abInfo)); } await ETTaskHelper.WaitAll(tasks); } } }
public static async ETTask CloseWindowByLayer(this UIManagerComponent self, UILayerNames layer, params string[] except_ui_names) { Dictionary <string, bool> dict_ui_names = null; if (except_ui_names != null && except_ui_names.Length > 0) { dict_ui_names = new Dictionary <string, bool>(); for (int i = 0; i < except_ui_names.Length; i++) { dict_ui_names[except_ui_names[i]] = true; } } using (ListComponent <ETTask> TaskScheduler = ListComponent <ETTask> .Create()) { foreach (var item in self.windows) { if (item.Value.Layer == layer && (dict_ui_names == null || !dict_ui_names.ContainsKey(item.Key))) { TaskScheduler.Add(self.CloseWindow(item.Key)); } } await ETTaskHelper.WaitAll(TaskScheduler); } }
//销毁指定窗口所有窗口 public static async ETTask DestroyWindowExceptNames(this UIManagerComponent self, string[] type_names = null) { Dictionary <string, bool> dict_ui_names = new Dictionary <string, bool>(); if (type_names != null) { for (int i = 0; i < type_names.Length; i++) { dict_ui_names[type_names[i]] = true; } } var keys = self.windows.Keys.ToArray(); using (ListComponent <ETTask> TaskScheduler = ListComponent <ETTask> .Create()) { for (int i = self.windows.Count - 1; i >= 0; i--) { if (!dict_ui_names.ContainsKey(keys[i])) { TaskScheduler.Add(self.DestroyWindow(keys[i])); } } await ETTaskHelper.WaitAll(TaskScheduler); } }
public async ETTask PublishAsync <T>(T a) where T : struct { List <object> iEvents; if (!this.allEvents.TryGetValue(typeof(T), out iEvents)) { return; } using (ListComponent <ETTask> list = ListComponent <ETTask> .Create()) { foreach (object obj in iEvents) { if (!(obj is AEvent <T> aEvent)) { Log.Error($"event error: {obj.GetType().Name}"); continue; } list.Add(aEvent.Handle(a)); } try { await ETTaskHelper.WaitAll(list); } catch (Exception e) { Log.Error(e); } } }
public static Unit Create(Scene currentScene, UnitInfo unitInfo) { UnitComponent unitComponent = currentScene.GetComponent <UnitComponent>(); Unit unit = unitComponent.AddChildWithId <Unit, int>(unitInfo.UnitId, unitInfo.ConfigId); unitComponent.Add(unit); unit.Position = new Vector3(unitInfo.X, unitInfo.Y, unitInfo.Z); unit.Forward = new Vector3(unitInfo.ForwardX, unitInfo.ForwardY, unitInfo.ForwardZ); NumericComponent numericComponent = unit.AddComponent <NumericComponent>(); for (int i = 0; i < unitInfo.Ks.Count; ++i) { numericComponent.Set(unitInfo.Ks[i], unitInfo.Vs[i]); } unit.AddComponent <MoveComponent>(); if (unitInfo.MoveInfo != null) { if (unitInfo.MoveInfo.X.Count > 0) { using (ListComponent <Vector3> list = ListComponent <Vector3> .Create()) { list.Add(unit.Position); for (int i = 0; i < unitInfo.MoveInfo.X.Count; ++i) { list.Add(new Vector3(unitInfo.MoveInfo.X[i], unitInfo.MoveInfo.Y[i], unitInfo.MoveInfo.Z[i])); } unit.MoveToAsync(list).Coroutine(); } } } unit.AddComponent <ObjectWait>(); unit.AddComponent <XunLuoPathComponent>(); Game.EventSystem.Publish(new EventType.AfterUnitCreate() { Unit = unit }); return(unit); }
public static async ETTask DestroyAllWindow(this UIManagerComponent self) { var keys = self.windows.Keys.ToArray(); using (ListComponent <ETTask> TaskScheduler = ListComponent <ETTask> .Create()) { for (int i = self.windows.Count - 1; i >= 0; i--) { TaskScheduler.Add(self.DestroyWindow(self.windows[keys[i]].Name)); } await ETTaskHelper.WaitAll(TaskScheduler); } }
private static void RaycastHits(Ray ray, AOICell cell, Vector3 inPoint, ListComponent <RaycastHit> hits, HashSetComponent <AOITriggerComponent> triggers, DictionaryComponent <UnitType, bool> type) { for (int i = cell.Triggers.Count - 1; i >= 0; i--) { var item = cell.Triggers[i]; if (item.IsDisposed) { cell.Triggers.RemoveAt(i); Log.Warning("自动移除不成功"); continue; } if (item.IsCollider && !triggers.Contains(item) && type.ContainsKey(UnitType.ALL) || type.ContainsKey(item.GetParent <AOIUnitComponent>().Type)) { if (item.IsPointInTrigger(inPoint, item.GetRealPos(), item.GetRealRot())) { triggers.Add(item); hits.Add(new RaycastHit { Hit = inPoint, Trigger = item, Distance = Vector3.Distance(inPoint, ray.Start) }); } else if (item.IsRayInTrigger(ray, item.GetRealPos(), item.GetRealRot(), out var hit)) { triggers.Add(item); hits.Add(new RaycastHit { Hit = hit, Trigger = item, Distance = Vector3.Distance(hit, ray.Start) }); } } } }
/// <summary> /// 销毁隐藏状态的窗口 /// </summary> /// <param name="self"></param> public static async ETTask DestroyUnShowWindow(this UIManagerComponent self) { using (ListComponent <ETTask> TaskScheduler = ListComponent <ETTask> .Create()) { foreach (var key in self.windows.Keys.ToList()) { if (!self.windows[key].Active) { TaskScheduler.Add(self.DestroyWindow(key)); } } await ETTaskHelper.WaitAll(TaskScheduler); } }
//预加载一系列资源 public static async ETTask LoadDependency(this GameObjectPoolComponent self, List <string> res) { if (res.Count <= 0) { return; } using (ListComponent <ETTask> TaskScheduler = ListComponent <ETTask> .Create()) { for (int i = 0; i < res.Count; i++) { TaskScheduler.Add(self.PreLoadGameObjectAsync(res[i], 1)); } await ETTaskHelper.WaitAll(TaskScheduler); } }
/// <summary> /// 获取指定位置为中心指定圈数的所有格子 /// </summary> /// <param name="self"></param> /// <param name="turnNum"></param> /// <param name="posx"></param> /// <param name="posy"></param> /// <returns></returns> public static ListComponent <AOICell> GetNearbyGrid(this AOISceneComponent self, int turnNum, int posx, int posy) { ListComponent <AOICell> res = ListComponent <AOICell> .Create(); for (int i = 0; i <= turnNum * 2 + 1; i++) { var x = posx - turnNum + i; for (int j = 0; j <= turnNum * 2 + 1; j++) { var y = posy - turnNum + j; res.Add(self.GetCell(x, y)); } } return(res); }
//销毁指定层级外层级所有窗口 public static async ETTask DestroyWindowExceptLayer(this UIManagerComponent self, UILayerNames layer) { var keys = self.windows.Keys.ToArray(); using (ListComponent <ETTask> TaskScheduler = ListComponent <ETTask> .Create()) { for (int i = self.windows.Count - 1; i >= 0; i--) { if (self.windows[keys[i]].Layer != layer) { TaskScheduler.Add(self.DestroyWindow(keys[i])); } } await ETTaskHelper.WaitAll(TaskScheduler); } }
protected override async ETTask Run(Session session, M2C_PathfindingResult message) { Unit unit = session.DomainScene().CurrentScene().GetComponent <UnitComponent>().Get(message.Id); float speed = unit.GetComponent <NumericComponent>().GetAsFloat(NumericType.Speed); using (ListComponent <Vector3> list = ListComponent <Vector3> .Create()) { for (int i = 0; i < message.Xs.Count; ++i) { list.Add(new Vector3(message.Xs[i], message.Ys[i], message.Zs[i])); } await unit.GetComponent <MoveComponent>().MoveToAsync(list, speed); } }
public async ETTask Run(ModeContex contex, string content) { switch (content) { case ConsoleMode.CreateRobot: Log.Console("CreateRobot args error!"); break; default: CreateRobotArgs options = null; Parser.Default.ParseArguments <CreateRobotArgs>(content.Split(' ')) .WithNotParsed(error => throw new Exception($"CreateRobotArgs error!")) .WithParsed(o => { options = o; }); // 获取当前进程的RobotScene using (ListComponent <StartSceneConfig> thisProcessRobotScenes = ListComponent <StartSceneConfig> .Create()) { List <StartSceneConfig> robotSceneConfigs = StartSceneConfigCategory.Instance.Robots; foreach (StartSceneConfig robotSceneConfig in robotSceneConfigs) { if (robotSceneConfig.Process != Game.Options.Process) { continue; } thisProcessRobotScenes.Add(robotSceneConfig); } // 创建机器人 for (int i = 0; i < options.Num; ++i) { int index = i % thisProcessRobotScenes.Count; StartSceneConfig robotSceneConfig = thisProcessRobotScenes[index]; Scene robotScene = Game.Scene.Get(robotSceneConfig.Id); RobotManagerComponent robotManagerComponent = robotScene.GetComponent <RobotManagerComponent>(); Scene robot = await robotManagerComponent.NewRobot(Game.Options.Process * 10000 + i); robot.AddComponent <AIComponent, int>(1); Log.Console($"create robot {robot.Zone}"); await TimerComponent.Instance.WaitAsync(2000); } } break; } contex.Parent.RemoveComponent <ModeContex>(); await ETTask.CompletedTask; }
/// <summary> /// 获取8个顶点,注意用完dispose /// </summary> /// <param name="self"></param> /// <param name="realPos"></param> /// <param name="realRot"></param> /// <returns></returns> public static ListComponent <Vector3> GetAllVertex(this OBBComponent self, Vector3 realPos, Quaternion realRot) { ListComponent <Vector3> res = ListComponent <Vector3> .Create(); for (float i = -0.5f; i < 0.5f; i++) { for (float j = -0.5f; j < 0.5f; j++) { for (float k = -0.5f; k < 0.5f; k++) { Vector3 temp = new Vector3(self.Scale.x * i, self.Scale.y * j, self.Scale.z * k); temp = realPos + realRot * temp; res.Add(temp); } } } return(res); }
public static async ETTask LoadAsync(this ConfigComponent self) { self.AllConfig.Clear(); List <Type> types = Game.EventSystem.GetTypes(typeof(ConfigAttribute)); Dictionary <string, byte[]> configBytes = new Dictionary <string, byte[]>(); self.ConfigLoader.GetAllConfigBytes(configBytes); using (ListComponent <Task> listTasks = ListComponent <Task> .Create()) { foreach (Type type in types) { Task task = Task.Run(() => self.LoadOneInThread(type, configBytes)); listTasks.Add(task); } await Task.WhenAll(listTasks.ToArray()); } }
public static void Check(this ActorLocationSenderComponent self) { using (ListComponent <long> list = ListComponent <long> .Create()) { long timeNow = TimeHelper.ServerNow(); foreach ((long key, Entity value) in self.Children) { ActorLocationSender actorLocationMessageSender = (ActorLocationSender)value; if (timeNow > actorLocationMessageSender.LastSendOrRecvTime + ActorLocationSenderComponent.TIMEOUT_TIME) { list.Add(key); } } foreach (long id in list) { self.Remove(id); } } }
public static Unit Create(Scene currentScene, UnitInfo unitInfo) { UnitComponent unitComponent = currentScene.GetComponent <UnitComponent>(); Unit unit = unitComponent.AddChildWithId <Unit, int>(unitInfo.UnitId, unitInfo.ConfigId); unitComponent.Add(unit); unit.Position = new Vector3(unitInfo.X, unitInfo.Y, unitInfo.Z); unit.Forward = new Vector3(unitInfo.ForwardX, unitInfo.ForwardY, unitInfo.ForwardZ); switch (unit.Type) { case UnitType.Monster: case UnitType.Player: { NumericComponent numericComponent = unit.AddComponent <NumericComponent>(); for (int i = 0; i < unitInfo.Ks.Count; ++i) { if (unitInfo.Ks[i] > NumericType.Max) //不需要同步最终值 { numericComponent.Set(unitInfo.Ks[i], unitInfo.Vs[i], true); } } unit.AddComponent <MoveComponent>(); if (unitInfo.MoveInfo != null) { if (unitInfo.MoveInfo.X.Count > 0) { using (ListComponent <Vector3> list = ListComponent <Vector3> .Create()) { list.Add(unit.Position); for (int i = 0; i < unitInfo.MoveInfo.X.Count; ++i) { list.Add(new Vector3(unitInfo.MoveInfo.X[i], unitInfo.MoveInfo.Y[i], unitInfo.MoveInfo.Z[i])); } unit.MoveToAsync(list).Coroutine(); } } } unit.AddComponent <AOIUnitComponent, Vector3, Quaternion, UnitType>(unit.Position, unit.Rotation, unit.Type); CombatUnitComponent combatU; if (unitInfo.SkillIds != null) { combatU = unit.AddComponent <CombatUnitComponent, List <int> >(unitInfo.SkillIds); } else { combatU = unit.AddComponent <CombatUnitComponent>(); } if (unitInfo.BuffIds != null && unitInfo.BuffIds.Count > 0) { var buffC = combatU.GetComponent <BuffComponent>(); buffC.Init(unitInfo.BuffIds, unitInfo.BuffTimestamp); } unit.AddComponent <ObjectWait>(); unit.AddComponent <XunLuoPathComponent>(); break; } case UnitType.Skill: { NumericComponent numericComponent = unit.AddComponent <NumericComponent>(); if (unitInfo.Ks != null && unitInfo.Ks.Count > 0) { for (int i = 0; i < unitInfo.Ks.Count; ++i) { if (unitInfo.Ks[i] > NumericType.Max) //不需要同步最终值 { numericComponent.Set(unitInfo.Ks[i], unitInfo.Vs[i], true); } } } unit.AddComponent <MoveComponent>(); if (unitInfo.MoveInfo != null && unitInfo.MoveInfo.X.Count > 0) { using (ListComponent <Vector3> list = ListComponent <Vector3> .Create()) { list.Add(unit.Position); for (int i = 0; i < unitInfo.MoveInfo.X.Count; ++i) { list.Add(new Vector3(unitInfo.MoveInfo.X[i], unitInfo.MoveInfo.Y[i], unitInfo.MoveInfo.Z[i])); } unit.MoveToAsync(list).Coroutine(); } } unit.AddComponent <AOIUnitComponent, Vector3, Quaternion, UnitType>(unit.Position, unit.Rotation, unit.Type); unit.AddComponent <ObjectWait>(); break; } default: { Log.Error("没有处理 " + unit.Type); break; } } Game.EventSystem.PublishAsync(new EventType.AfterUnitCreate() { Unit = unit }).Coroutine(); return(unit); }
/// <summary> /// 获取12条边,注意用完dispose /// </summary> /// <param name="self"></param> /// <param name="realPos"></param> /// <param name="realRot"></param> /// <returns></returns> public static ListComponent <Ray> GetAllSide(this OBBComponent self, Vector3 realPos, Quaternion realRot) { ListComponent <Ray> res = ListComponent <Ray> .Create(); Vector3 temp = realPos + realRot * new Vector3(self.Scale.x, self.Scale.y, self.Scale.z); Ray ray = new Ray() { Start = temp, Dir = realRot * Vector3.left, Distance = self.Scale.x }; res.Add(ray); ray = new Ray() { Start = temp, Dir = realRot * Vector3.down, Distance = self.Scale.y }; res.Add(ray); ray = new Ray() { Start = temp, Dir = realRot * Vector3.back, Distance = self.Scale.z }; res.Add(ray); temp = realPos + realRot * new Vector3(-self.Scale.x, -self.Scale.y, -self.Scale.z); ray = new Ray() { Start = temp, Dir = realRot * Vector3.right, Distance = self.Scale.x }; res.Add(ray); ray = new Ray() { Start = temp, Dir = realRot * Vector3.up, Distance = self.Scale.y }; res.Add(ray); ray = new Ray() { Start = temp, Dir = realRot * Vector3.forward, Distance = self.Scale.z }; res.Add(ray); temp = realPos + realRot * new Vector3(-self.Scale.x, self.Scale.y, self.Scale.z); ray = new Ray() { Start = temp, Dir = realRot * Vector3.up, Distance = self.Scale.y }; res.Add(ray); ray = new Ray() { Start = temp, Dir = realRot * Vector3.forward, Distance = self.Scale.z }; res.Add(ray); temp = realPos + realRot * new Vector3(self.Scale.x, self.Scale.y, -self.Scale.z); ray = new Ray() { Start = temp, Dir = realRot * Vector3.right, Distance = self.Scale.x }; res.Add(ray); ray = new Ray() { Start = temp, Dir = realRot * Vector3.up, Distance = self.Scale.y }; res.Add(ray); temp = realPos + realRot * new Vector3(self.Scale.x, -self.Scale.y, self.Scale.z); ray = new Ray() { Start = temp, Dir = realRot * Vector3.right, Distance = self.Scale.x }; res.Add(ray); ray = new Ray() { Start = temp, Dir = realRot * Vector3.forward, Distance = self.Scale.z }; res.Add(ray); return(res); }