public void Enter(MapUnit mapUnit, double position, bool isPreEnter) { if (mapUnit == null) { Log.Error($"Block.Enter() Failed, mapUnit == null"); return; } //進入該Block if (!isPreEnter) { mapUnit.Block = this; } //判斷是否已有本Block的SendChannel if (mapUnit.SendChannel?.Block == this) { return; } //找尋還有空位的BlockChannel BlockChannel freeChannel = null; for (int i = 0; i < _blockChannels.Count; i++) { if (!_blockChannels[i].IsFullMember()) { freeChannel = _blockChannels[i]; break; } } //新開一個BlockChannel if (freeChannel == null) { freeChannel = new BlockChannel(this); freeChannel.Start(); _blockChannels.Add(freeChannel); } //進入 freeChannel.Enter(mapUnit, position); }
public void ModifyPosition(MapUnit mapUnit, double position) { position %= _roadDistance_m; bool IsInside = mapUnit.Block != null && mapUnit.Block.IsInside(position); if (!IsInside) { BlockChannel oldBlockChannel = null; BlockChannel newBlockChannel = null; //離開舊的Block if (mapUnit.Block != null) { oldBlockChannel = mapUnit.SendChannel; mapUnit.Block.Leave(mapUnit, false); } //加入新的Block for (int i = 0; i < _blocks.Count; i++) { if (_blocks[i].IsInside(position)) { _blocks[i].Enter(mapUnit, position, false); break; } } newBlockChannel = mapUnit.SendChannel; //清空舊的資料 _m2C_MapUnitCreateAndDestroy.CreateMapUnitInfos.Clear(); _m2C_MapUnitCreateAndDestroy.DestroyMapUnitIds.Clear(); //取得觀看的BlockChannel資訊 for (int i = 0; i < newBlockChannel.PositionMambers.Count; i++) { var rev = newBlockChannel.PositionMambers[i]; var info = rev.Info; //info.DebugId = IdGenerater.GenerateId(); //var list = mapUnit.GetLookAtSources(); //BsonDocument doc = new BsonDocument //{ // { "rev.Id" , rev?.Id }, // { "rev.MapAppId" , IdGenerater.GetAppId(rev == null ? 0 : rev.Id) }, // { "rev.Uid" , rev?.Uid }, // { "rev.RoomId" , rev?.RoomId }, // { "rev.Room.Id" , rev?.Room?.Id }, // { "rev.Block.room.Id" , rev?.Block?.room?.Id }, // { "rev.Block.roomId" , rev?.Block?.roomId }, // { "rev.Block.blockId" , rev?.Block?.blockId }, // { "sendToList", list.Select(e => $"MapUnitId:{e.Id}, Uid:{e.Uid}, BlockId:{e.Block?.blockId}").ToJson() } //}; //Log.Trace($"DebugId[{info.DebugId}]> {doc.ToJson()}"); _m2C_MapUnitCreateAndDestroy.CreateMapUnitInfos.Add(info); } //移除取消觀看的BlockChannel資訊 for (int i = 0; i < oldBlockChannel?.PositionMambers?.Count; i++) { if (_m2C_MapUnitCreateAndDestroy.CreateMapUnitInfos.Contains(oldBlockChannel.PositionMambers[i].Info)) { continue; } _m2C_MapUnitCreateAndDestroy.DestroyMapUnitIds.Add(oldBlockChannel.PositionMambers[i].Id); } MapMessageHelper.BroadcastTarget(_m2C_MapUnitCreateAndDestroy, mapUnit.GetLookAtSources()); } //更新MapUnit _m2C_MapUnitUpdate.MapUnitId = mapUnit.Id; _m2C_MapUnitUpdate.DistanceTravelledTarget = mapUnit.Info.DistanceTravelled; _m2C_MapUnitUpdate.SpeedMS = mapUnit.Info.SpeedMS; _m2C_MapUnitUpdate.DistanceTravelledUpdateUTCTick = System.DateTime.UtcNow.Ticks; SendMessage(mapUnit, _m2C_MapUnitUpdate); }
public void LookMapUnitBlockChannel(MapUnit target) { if (target == null) { Log.Error($"觀看對象不存在, SelfUid:{Uid}"); return; } if (target.SendChannel == null) { return; } if (target == LookAtTarget) { return; } if (RoomId == 0 || RoomId != target.RoomId) { return; } BlockChannel oldBlockChannel = null; BlockChannel newBlockChannel = null; _m2C_MapUnitCreateAndDestroy.CreateMapUnitInfos.Clear(); _m2C_MapUnitCreateAndDestroy.DestroyMapUnitIds.Clear(); //取消觀看舊的BlockChannel if (LookAtTarget != null && LookAtTarget.SendChannel != null) { LookAtTarget.SendChannel.Unlook(this); if (LookAtTarget.LookAtSources.Contains(this)) { LookAtTarget.LookAtSources.Remove(this); } oldBlockChannel = LookAtTarget.SendChannel; } //觀看新的BlockChannel target.SendChannel.Look(this); LookAtTarget = target; if (!LookAtTarget.LookAtSources.Contains(this)) { LookAtTarget.LookAtSources.Add(this); } newBlockChannel = target.SendChannel; //清空舊的資料 _m2C_MapUnitCreateAndDestroy.CreateMapUnitInfos.Clear(); _m2C_MapUnitCreateAndDestroy.DestroyMapUnitIds.Clear(); //取得觀看的BlockChannel資訊 for (int i = 0; i < newBlockChannel.PositionMambers.Count; i++) { var rev = newBlockChannel.PositionMambers[i]; var info = rev.Info; //info.DebugId = IdGenerater.GenerateId(); //BsonDocument doc = new BsonDocument //{ // { "Block.room.Id" , Block?.room?.Id }, // { "Block.roomId" , Block?.roomId }, // { "Block.blockId" , Block?.blockId }, // { "rev.Id" , rev?.Id }, // { "rev.MapAppId" , IdGenerater.GetAppId(rev == null ? 0 : rev.Id) }, // { "rev.Uid" , rev?.Uid }, // { "rev.RoomId" , rev?.RoomId }, // { "rev.Room.Id" , rev?.Room?.Id }, // { "rev.Block.room.Id" , rev?.Block?.room?.Id }, // { "rev.Block.roomId" , rev?.Block?.roomId }, // { "rev.Block.blockId" , rev?.Block?.blockId }, // { "sendToList", $"MapUnitId:{this.Id}, Uid:{this.Uid}, BlockId:{this.Block?.blockId}" } //}; //Log.Trace($"DebugId[{info.DebugId}]> {doc.ToJson()}"); _m2C_MapUnitCreateAndDestroy.CreateMapUnitInfos.Add(info); } // 移除取消觀看的BlockChannel資訊 for (int i = 0; i < oldBlockChannel?.PositionMambers?.Count; i++) { if (_m2C_MapUnitCreateAndDestroy.CreateMapUnitInfos.Contains(oldBlockChannel.PositionMambers[i].Info)) { continue; } _m2C_MapUnitCreateAndDestroy.DestroyMapUnitIds.Add(oldBlockChannel.PositionMambers[i].Id); } MapMessageHelper.BroadcastTarget(_m2C_MapUnitCreateAndDestroy, this); }