示例#1
0
        /// <summary>
        /// Adds character to map cell.
        /// </summary>
        public void AddPlayer(Character character)
        {
            Players.TryAdd(character.Id, character);
            AddListeners(character);
            AssignCellIndex(character);

            // Send update players.
            var oldPlayers = character.OldCellId != -1 ? Map.Cells[character.OldCellId].GetAllPlayers(true) : new List <Character>();
            var newPlayers = GetAllPlayers(true);

            var sendPlayerLeave = oldPlayers.Where(p => !newPlayers.Contains(p) && p != character);
            var sendPlayerEnter = newPlayers.Where(p => !oldPlayers.Contains(p));

            foreach (var player in sendPlayerLeave)
            {
                _packetsHelper.SendCharacterLeave(player.Client, character);
                _packetsHelper.SendCharacterLeave(character.Client, player);
            }

            foreach (var player in sendPlayerEnter)
            {
                if (player.Id != character.Id)
                {
                    // Notify players in this map, that new player arrived.
                    _packetsHelper.SendCharacterEnter(player.Client, character);

                    // Notify new player, about already loaded player.
                    _packetsHelper.SendCharacterEnter(character.Client, player);
                }
            }

            // Send update npcs.
            var oldCellNPCs = character.OldCellId != -1 ? Map.Cells[character.OldCellId].GetAllNPCs(true) : new List <Npc>();
            var newCellNPCs = GetAllNPCs(true);

            var npcToLeave = oldCellNPCs.Where(npc => !newCellNPCs.Contains(npc));
            var npcToEnter = newCellNPCs.Where(npc => !oldCellNPCs.Contains(npc));

            foreach (var npc in npcToLeave)
            {
                _packetsHelper.SendNpcLeave(character.Client, npc);
            }
            foreach (var npc in npcToEnter)
            {
                _packetsHelper.SendNpcEnter(character.Client, npc);
            }

            // Send update mobs.
            var oldCellMobs = character.OldCellId != -1 ? Map.Cells[character.OldCellId].GetAllMobs(true) : new List <Mob>();
            var newCellMobs = GetAllMobs(true);

            var mobToLeave = oldCellMobs.Where(m => !newCellMobs.Contains(m));
            var mobToEnter = newCellMobs.Where(m => !oldCellMobs.Contains(m));

            foreach (var mob in mobToLeave)
            {
                _packetsHelper.SendMobLeave(character.Client, mob);
            }

            foreach (var mob in mobToEnter)
            {
                _packetsHelper.SendMobEnter(character.Client, mob, false);
            }
        }