Esempio n. 1
0
        public virtual bool EnterScene(Scene _this, ObjBase obj)
        {
            if (_this.mTicking)
            {
                _this.mNeedToAddObjDict.Add(obj.ObjId, obj);
                return(true);
            }

            _this.OnObjBeforeEnterScene(obj);

            var zoneId = _this.Pos2ZoneId(obj.GetPosition().X, obj.GetPosition().Y);

            if (zoneId < 0 || zoneId >= _this.mZoneList.Count)
            {
                //这里保证一下Character进入的位置必须得是个有效的位置
                if (obj.IsCharacter())
                {
                    var character = obj as ObjCharacter;
                    var validPos  = _this.FindNearestValidPosition(character.GetPosition());
                    if (null != validPos)
                    {
                        character.SetPosition(validPos.Value);
                        Logger.Warn("[{0}] set valid pos[{1},{2}]", character.GetName(), character.GetPosition().X,
                                    character.GetPosition().Y);
                    }
                    else
                    {
                        character.SetPosition((float)_this.TableSceneData.Entry_x, (float)_this.TableSceneData.Entry_z);
                        Logger.Warn("[{0}] set entry pos[{1},{2}]", character.GetName(), character.GetPosition().X,
                                    character.GetPosition().Y);
                    }
                }

                zoneId = _this.Pos2ZoneId(obj.GetPosition().X, obj.GetPosition().Y);
                if (zoneId < 0 || zoneId >= _this.mZoneList.Count)
                {
                    Logger.Fatal("zone = null[x={0},y={1}]", obj.GetPosition().X, obj.GetPosition().Y);
                    return(false);
                }
            }

            if (_this.mObjDict.ContainsKey(obj.ObjId))
            {
                if (_this.mObjDict[obj.ObjId].Equals(obj))
                {
                    Logger.Fatal("!ObjDic.ContainsKey({0})  mObjDict[obj.ObjId].Equals(obj)", obj.ObjId);
                    return(false);
                }
                _this.RemoveObj(obj);
                Logger.Fatal("!ObjDic.ContainsKey({0})", obj.ObjId);
            }

            _this.AddObj(obj);

            obj.EnterScene(_this);

            var zone = _this.mZoneList[zoneId];

            zone.AddObj(obj);
            obj.SetZone(zone);

            //player 一上来时不时Active状态,不会走这里,只有player掉了EnterSceneOver之后才是active状态,
            //EnterSceneOver里自己会处理可见和飞可见的玩家
            if (obj.Active)
            {
                var canSee = true;
                if (obj.IsCharacter())
                {
                    var character = obj as ObjCharacter;
                    if (character.IsDead())
                    {
                        canSee = false;
                    }
                }
                if (canSee)
                {
                    var bornType = _this.GetBornVisibleType();
                    obj.BroadcastCreateMe(bornType);
                }
            }

            if (obj.GetObjType() == ObjType.PLAYER)
            {
                var player = obj as ObjPlayer;
                foreach (var pair in _this.mAreaDict)
                {
                    pair.Value.AdjustPlayer(player);
                }
            }


            return(true);
        }