Example #1
0
        public static void CheckChunks(this OnlinePlayer player)
        {
            var client = player.Tcp;

            if (client.OnlinePlayer != null && client.OnlinePlayer.AssetsReady)
            {
                var chunkX = client.OnlinePlayer.Position.X >> 4;
                var chunkY = client.OnlinePlayer.Position.Y >> 4;

                var shouldBeLoaded = MapHelpers.GetSquared3x3(new Position(chunkX, chunkY));

                foreach (var position in shouldBeLoaded)
                {
                    var chunkKey = $"{position.X}_{position.Y}";
                    if (!client.ChunksLoaded.Contains(chunkKey))
                    {
                        if (Server.Map.Chunks.ContainsKey(chunkKey))
                        {
                            client.ChunksLoaded.Add(chunkKey);
                            var chunk = Server.Map.Chunks[chunkKey];
                            client.Send(new ChunkPacket()
                            {
                                X         = position.X,
                                Y         = position.Y,
                                ChunkData = chunk.GetData()
                            });

                            foreach (var entity in chunk.EntitiesInChunk[EntityType.MONSTER])
                            {
                                var monsterInstance = (Monster)entity;
                                client.Send(new MonsterSpawnPacket()
                                {
                                    MonsterUid     = monsterInstance.UID,
                                    MonsterName    = monsterInstance.Name,
                                    Position       = monsterInstance.Position,
                                    SpriteIndex    = monsterInstance.GetSpriteAsset().SpriteRowIndex,
                                    MoveSpeed      = monsterInstance.MoveSpeed,
                                    SpawnAnimation = false
                                });
                            }
                        }
                    }
                }
            }
        }
Example #2
0
        public List <OnlinePlayer> GetNearbyPlayers()
        {
            List <OnlinePlayer> near = new List <OnlinePlayer>();
            var radius = MapHelpers.GetSquared3x3(new Position(Position.X >> 4, Position.Y >> 4));

            foreach (var position in radius)
            {
                var chunkThere = Server.Map.GetChunk(position.X, position.Y);
                if (chunkThere != null)
                {
                    foreach (var playerInChunk in chunkThere.PlayersInChunk)
                    {
                        near.Add(playerInChunk);
                    }
                }
            }
            return(near);
        }
        public static List <OnlinePlayer> GetPlayersNear(this Entity player)
        {
            List <OnlinePlayer> near = new List <OnlinePlayer>();
            var chunk  = player.GetChunk();
            var radius = MapHelpers.GetSquared3x3(new Position(chunk.x, chunk.y));

            foreach (var position in radius)
            {
                var chunkThere = Server.Map.GetChunk(position.X, position.Y);
                if (chunkThere != null)
                {
                    foreach (var playerInChunk in chunkThere.PlayersInChunk)
                    {
                        if (playerInChunk.UID != player.UID)
                        {
                            near.Add(playerInChunk);
                        }
                    }
                }
            }
            return(near);
        }