示例#1
0
            public override void Destroy(ResourcesLoaderComponent self)
            {
                async ETTask UnLoadAsync()
                {
                    using (ListComponent <string> list = ListComponent <string> .Create())
                    {
                        list.AddRange(self.LoadedResource);
                        self.LoadedResource = null;

                        if (TimerComponent.Instance == null)
                        {
                            return;
                        }

                        // 延迟5秒卸载包,因为包卸载是引用计数,5秒之内假如重新有逻辑加载了这个包,那么可以避免一次卸载跟加载
                        await TimerComponent.Instance.WaitAsync(5000);

                        foreach (string abName in list)
                        {
                            CoroutineLock coroutineLock = null;
                            try
                            {
                                coroutineLock =
                                    await CoroutineLockComponent.Instance.Wait(CoroutineLockType.ResourcesLoader, abName.GetHashCode(), 0);

                                {
                                    if (ResourcesComponent.Instance == null)
                                    {
                                        return;
                                    }

                                    await ResourcesComponent.Instance.UnloadBundleAsync(abName);
                                }
                            }
                            finally
                            {
                                coroutineLock?.Dispose();
                            }
                        }
                    }
                }

                UnLoadAsync().Coroutine();
            }
示例#2
0
        /// <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
        }