예제 #1
0
        public bool SpawnEntity(IEntity entity)
        {
            var player = _client.Player;

            if (player.Id == entity.Id)
            {
                return(false);
            }

            var chunkManager  = _world.ChunkManager;
            var chunkPosition = ChunkPosition.FromWorld(entity.Position);

            if (!chunkManager.IsLoaded(chunkPosition))
            {
                Logger.Warn("Trying to spawn {0} in an unloaded chunk", entity);
                return(false);
            }

            if (!_loadedEntities.Add(entity))
            {
                return(false);
            }

            Logger.Info("Spawning entity #{0} on {1}", entity.Id, _client);
            var connection = _client.Connection;

            connection.SpawnEntity(entity);
            return(true);
        }
예제 #2
0
        public IBlockState GetBlock(Vector3Int position)
        {
            var chunkPos = ChunkPosition.FromWorld(position);

            if (!ChunkManager.IsLoaded(chunkPos))
            {
                return(null);
            }

            var chunk = ChunkManager.GetChunk(chunkPos);
            var index = Chunk.SectionIndex(position.Y);

            if (!chunk.HasSection(index))
            {
                return(null);
            }

            var section = chunk[index];

            return(section.BlockStorage.GetBlock(
                       position.X % Minecraft.Units.Chunk.Size,
                       position.Y % Minecraft.Units.Chunk.SectionHeight,
                       position.Z % Minecraft.Units.Chunk.Size
                       ));
        }
예제 #3
0
        private void OnEntitySpawned(object sender, EntityEventArgs e)
        {
            var entity       = e.Entity;
            var position     = ChunkPosition.FromWorld(entity.Position);
            var chunkManager = _world.ChunkManager;

            if (chunkManager.IsLoaded(position))
            {
                SpawnEntity(entity);
            }
        }
예제 #4
0
        private void OnWorldContextRegistered(object sender, WorldContextEventArgs e)
        {
            var chunkManager = _world.ChunkManager;

            var worldContext  = e.WorldContext;
            var entityManager = worldContext.EntityManager;

            foreach (var entity in entityManager)
            {
                var chunkPosition = ChunkPosition.FromWorld(entity.Position);
                if (chunkManager.IsLoaded(chunkPosition))
                {
                    SpawnEntity(entity);
                }
            }

            entityManager.EntitySpawned += OnEntitySpawned;
            entityManager.EntityRemoved += OnEntityRemoved;
        }
예제 #5
0
        public void SetBlock(Vector3Int position, IBlockState blockState)
        {
            var chunkPos = ChunkPosition.FromWorld(position);

            if (!ChunkManager.IsLoaded(chunkPos))
            {
                return;
            }

            var chunk = ChunkManager.GetChunk(chunkPos);
            var index = Chunk.SectionIndex(position.Y);

            var section = chunk.CreateSection(index);

            section.BlockStorage.SetBlock(
                position.X % Minecraft.Units.Chunk.Size,
                position.Y % Minecraft.Units.Chunk.SectionHeight,
                position.Z % Minecraft.Units.Chunk.Size,
                blockState
                );
        }
예제 #6
0
        private void OnChunkUnloaded(object sender, ChunkEventArgs e)
        {
            var toRemove = new HashSet <IEntity>();

            var unloadedChunk = e.Position;
            var worldContexts = _world.WorldContexts;

            foreach (var worldContext in worldContexts)
            {
                var entityManager = worldContext.EntityManager;
                foreach (var entity in entityManager)
                {
                    var chunkPosition = ChunkPosition.FromWorld(entity.Position);
                    if (unloadedChunk != chunkPosition)
                    {
                        continue;
                    }

                    toRemove.Add(entity);
                }
            }

            RemoveEntities(toRemove);
        }
예제 #7
0
 public IEnumerable <IEntity> GetEntities(ChunkPosition position)
 {
     return(_entities.Where(e => ChunkPosition.FromWorld(e.Position) == position));
 }